#!/bin/bash # Parse command line arguments PRESELECTED_PM="" PRESELECTED_VERSION="" while [[ $# -gt 0 ]]; do case $1 in --package-manager|--pm) PRESELECTED_PM="$2" shift 2 ;; --version|-v) PRESELECTED_VERSION="$2" shift 2 ;; *) echo "Unknown option: $1" echo "Usage: $0 [--package-manager|--pm ] [--version|-v ]" exit 1 ;; esac done # Color definitions RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' CYAN='\033[0;36m' BOLD='\033[1m' NC='\033[0m' # No Color # Unicode symbols CHECK_MARK="✓" CROSS_MARK="✗" ARROW="→" PACKAGE="📦" DOWNLOAD="⬇️" INSTALL="🔧" # Function to print colored messages print_info() { echo -e "${BLUE}${ARROW}${NC} $1" } print_success() { echo -e "${GREEN}${CHECK_MARK}${NC} $1" } print_error() { echo -e "${RED}${CROSS_MARK}${NC} $1" } print_warning() { echo -e "${YELLOW}⚠${NC} $1" } print_header() { echo -e "\n${BOLD}${CYAN}$1${NC}\n" } # Spinner function for long-running operations spinner() { local pid=$1 local delay if [[ "$(uname -s)" == "OS/390" ]]; then delay=1 else delay=0.1 fi local spinstr='⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏' local temp while ps -p "$pid" > /dev/null 2>&1; do temp=${spinstr#?} printf " ${CYAN}%c${NC} " "$spinstr" spinstr=$temp${spinstr%"$temp"} sleep $delay printf "\b\b\b\b\b" done printf " \b\b\b\b" } # Function to check if a command exists command_exists() { command -v "$1" >/dev/null 2>&1 } # Function to check Node.js version check_node_version() { print_header "Checking Node.js Installation" if ! command_exists node; then print_error "Node.js is not installed." echo -e " ${YELLOW}Please install Node.js version 22.15 or higher and try again.${NC}" exit 1 fi local node_version local required_version local min_version node_version=$(node -v | sed 's/v//') required_version="22.15.0" # Compare versions using sort -V min_version=$(printf '%s\n%s' "${required_version}" "${node_version}" | sort -V | head -n1) if [[ ${min_version} != "${required_version}" ]]; then print_error "Node.js version ${node_version} is installed, but version 22.15 or higher is required." echo -e " ${YELLOW}Please upgrade Node.js and try again.${NC}" exit 1 fi print_success "Node.js version ${BOLD}${node_version}${NC} detected (minimum 22.15 required)" } # Check Node.js version first check_node_version # Script to select a package manager (npm, pnpm, or yarn) print_header "Package Manager Selection" echo "" if [[ -z "$PRESELECTED_PM" ]]; then print_info "Tip: You can skip this selection by using: ${BOLD}--package-manager${NC} or ${BOLD}--pm${NC} (e.g., --pm npm)" fi # Check which package managers are available available_managers=() options_text="" option_num=1 if command_exists npm; then available_managers+=("npm") options_text+=" ${option_num}) ${BOLD}npm${NC}\n" option_num=$((option_num + 1)) fi if command_exists pnpm; then available_managers+=("pnpm") options_text+=" ${option_num}) ${BOLD}pnpm${NC}\n" option_num=$((option_num + 1)) fi if command_exists yarn; then available_managers+=("yarn") options_text+=" ${option_num}) ${BOLD}yarn${NC}\n" option_num=$((option_num + 1)) fi # Check if no package managers are available if [[ ${#available_managers[@]} -eq 0 ]]; then print_error "No supported package managers (npm, pnpm, or yarn) found on your system." echo -e " ${YELLOW}Please install one of these package managers and try again.${NC}" exit 1 fi # Handle preselected package manager selected="" if [[ -n "$PRESELECTED_PM" ]]; then # Validate that the preselected package manager is available found=false for manager in "${available_managers[@]}"; do if [[ "$manager" == "$PRESELECTED_PM" ]]; then found=true break fi done if [[ "$found" == "true" ]]; then selected="$PRESELECTED_PM" print_info "Using preselected package manager: ${BOLD}${selected}${NC}" else print_error "Preselected package manager '${PRESELECTED_PM}' is not available." echo -e " ${YELLOW}Available options: ${available_managers[*]}${NC}" exit 1 fi # If only one package manager is available, select it automatically elif [[ ${#available_managers[@]} -eq 1 ]]; then selected="${available_managers[0]}" print_info "Only ${BOLD}${selected}${NC} is available. Using it automatically." else # Display available options echo -e "${CYAN}Available package managers:${NC}" echo -e "${options_text}" # Read user input while true; do read -r -p "$(echo -e "${BOLD}Enter your choice (1-${#available_managers[@]}):${NC} ")" choice < /dev/tty # Check if choice is a number and within range if [[ ${choice} =~ ^[0-9]+$ ]] && [[ ${choice} -ge 1 ]] && [[ ${choice} -le ${#available_managers[@]} ]]; then selected="${available_managers[$((choice-1))]}" print_success "Selected ${BOLD}${selected}${NC}" break else print_error "Invalid selection. Please enter a number between 1 and ${#available_managers[@]}." fi done fi echo "" print_header "Resolving Bob Shell Version" # Resolve version: use provided value or fetch the latest @ibm/bob release from the public npm registry if [[ -n "$PRESELECTED_VERSION" ]]; then if [[ ! "$PRESELECTED_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+(\-[A-Za-z]+\.[0-9]+)?$ ]]; then print_error "Invalid version format '${PRESELECTED_VERSION}'. Expected format: X.Y.Z or X.Y.Z-tag (e.g. 1.0.5 or 2.0.0-rc.1)" exit 1 fi version="$PRESELECTED_VERSION" print_info "Using specified version: ${BOLD}${version}${NC}" else print_info "Fetching latest version..." version=$(curl -fsSL https://registry.npmjs.org/@ibm/bob/latest 2>/dev/null | node -e 'let s="";process.stdin.on("data",(c)=>{s+=c;}).on("end",()=>{try{process.stdout.write(String(JSON.parse(s).version||""));}catch{}});') if [[ -z "$version" ]]; then print_error "Failed to fetch Bob Shell version" exit 1 fi print_success "Latest version: ${BOLD}${version}${NC}" fi # The package manager verifies the package integrity reported by the npm registry package_spec="@ibm/bob@${version}" echo "" print_header "Installing Bob Shell" print_info "Installing Bob Shell ${BOLD}${version}${NC} with ${BOLD}${selected}${NC}..." echo "" # Install with the selected package manager install_success=false # Function to show spinner during installation show_spinner() { local pid=$1 local delay if [[ "$(uname -s)" == "OS/390" ]]; then delay=1 else delay=0.1 fi local spinstr='⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏' while kill -0 "$pid" 2>/dev/null; do local temp=${spinstr#?} # Use echo -e to properly interpret color codes, then printf for positioning printf "\r ${CYAN}%s${NC} Installing... " "${spinstr:0:1}" spinstr=$temp${spinstr%"$temp"} sleep $delay done printf "\r \r" } # pnpm 11+ installs each global package into its own directory below `pnpm root -g`, so ask pnpm itself pnpm_has_bobshell() { pnpm list -g --depth=0 --json 2>/dev/null | node -e 'let s="";process.stdin.on("data",(c)=>{s+=c;}).on("end",()=>{try{process.exit([].concat(JSON.parse(s)).some((p)=>p&&p.dependencies&&p.dependencies.bobshell)?0:1);}catch{process.exit(1);}});' } # Remove the old "bobshell" package, which owns the same "bob" binary and would cause EEXIST # (npm, yarn) or ERR_PNPM_GLOBAL_BIN_CONFLICT (pnpm 11+) has_bobshell=false case ${selected} in npm) [[ -f "$(npm root -g 2>/dev/null)/bobshell/package.json" ]] && has_bobshell=true ;; pnpm) pnpm_has_bobshell && has_bobshell=true ;; yarn) [[ -f "$(yarn global dir 2>/dev/null)/node_modules/bobshell/package.json" ]] && has_bobshell=true ;; esac if [[ "$has_bobshell" == true ]]; then print_info "Old version of Bob Shell (${BOLD}bobshell${NC}) detected. Removing it..." case ${selected} in npm) npm uninstall -g bobshell >/dev/null 2>&1 ;; pnpm) pnpm remove -g bobshell >/dev/null 2>&1 ;; yarn) yarn global remove bobshell >/dev/null 2>&1 ;; esac fi # Global bin directory where the selected package manager links the "bob" binary bin_dir="" case ${selected} in npm) bin_dir=$(npm prefix -g 2>/dev/null) && bin_dir="${bin_dir}/bin" ;; pnpm) bin_dir=$(pnpm bin -g 2>/dev/null) ;; yarn) bin_dir=$(yarn global bin 2>/dev/null) ;; esac bob_cmd="bob" [[ -n "$bin_dir" ]] && bob_cmd="${bin_dir}/bob" # Remove a leftover "bob" link that is dangling or points to the old "bobshell" package. # pnpm writes shell-script shims instead of links and removes them itself. if [[ -L "$bob_cmd" ]]; then link_target=$(readlink "$bob_cmd") if [[ ! -e "$bob_cmd" || "$link_target" == *node_modules/bobshell/* ]]; then print_info "Removing leftover ${BOLD}bob${NC} link at ${BOLD}${bob_cmd}${NC}..." rm -f "$bob_cmd" fi fi install_exit=0 if [[ ${selected} == "pnpm" ]]; then temp_npmrc=$(mktemp) echo "registry=https://registry.npmjs.org/" > "${temp_npmrc}" NPM_CONFIG_USERCONFIG="${temp_npmrc}" PNPM_CONFIG_NPMRC_AUTH_FILE="${temp_npmrc}" pnpm add --registry=https://registry.npmjs.org/ --allow-build=@officecli/officecli --no-lockfile -g "${package_spec}" > /tmp/bobshell-install.log 2>&1 & show_spinner $! wait $! install_exit=$? rm -f "${temp_npmrc}" fi if [[ ${selected} == "npm" ]]; then npm install --registry=https://registry.npmjs.org/ --allow-scripts=@officecli/officecli --progress=false --loglevel=error -g "${package_spec}" > /tmp/bobshell-install.log 2>&1 & show_spinner $! wait $! install_exit=$? fi if [[ ${selected} == "yarn" ]]; then YARN_REGISTRY="https://registry.npmjs.org/" yarn global add --silent "${package_spec}" > /tmp/bobshell-install.log 2>&1 & show_spinner $! wait $! install_exit=$? fi # Verify the binary installed by the selected package manager, not whichever "bob" comes first in PATH if [[ $install_exit -ne 0 ]]; then print_warning "${selected} exited with code ${install_exit}" elif command_exists "$bob_cmd"; then installed_version=$("$bob_cmd" --version 2>/dev/null | head -n 1 | tr -d '[:space:]') if [[ "$installed_version" == "$version" ]]; then install_success=true else print_warning "Version mismatch: ${bob_cmd} reports ${installed_version}, expected ${version}" fi else print_warning "bob command not found at ${bob_cmd} after installation" fi echo "" if [[ "$install_success" == true ]]; then print_header "Installation Complete! ${CHECK_MARK}" print_success "Bob Shell ${BOLD}${version}${NC} has been successfully installed" echo "" print_info "You can now run: ${BOLD}bob${NC}" echo "" path_bob=$(command -v bob 2>/dev/null) if [[ -n "$path_bob" && "$path_bob" != "$bob_cmd" ]]; then print_warning "Another ${BOLD}bob${NC} at ${BOLD}${path_bob}${NC} comes first in your PATH and shadows ${BOLD}${bob_cmd}${NC}" echo "" fi # Telemetry Notice for External Users echo -e "${CYAN}Usage data is collected by default.${NC}" echo "" echo -e "${BLUE}${ARROW}${NC} To opt out, type ${BOLD}/settings${NC}, navigate to ${BOLD}Enable Usage Metrics${NC} and set the flag to ${BOLD}false${NC}" echo "" # Clean up log file rm -f /tmp/bobshell-install.log else print_error "Installation failed" echo "" # Show error logs if they exist if [[ -f /tmp/bobshell-install.log ]]; then error_content=$(cat /tmp/bobshell-install.log 2>/dev/null) if [[ -n "$error_content" ]]; then echo -e "${RED}Error details:${NC}" echo "$error_content" else echo -e "${YELLOW}No error details available in log file${NC}" fi else echo -e "${YELLOW}Log file not found${NC}" fi if grep -q "EEXIST" /tmp/bobshell-install.log 2>/dev/null; then echo "" print_info "A ${BOLD}bob${NC} file that does not belong to Bob Shell already exists at ${BOLD}${bob_cmd}${NC}." print_info "Inspect it with ${BOLD}ls -la ${bob_cmd}${NC}, remove it if you no longer need it, and run this script again." fi exit 1 fi exit 0