qwen-code/scripts/installation/install-qwen-with-source.sh
2026-02-05 03:36:19 -08:00

158 lines
No EOL
4.6 KiB
Bash
Executable file

#!/bin/bash
# Script to install Node.js and Qwen Code
# This script checks and installs Node.js and Qwen Code if not already installed
echo "==========================================="
echo "Qwen Code Installation Script"
echo "==========================================="
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to check and install Node.js
install_nodejs() {
if command_exists node; then
NODE_VERSION=$(node --version)
echo "✓ Node.js is already installed: $NODE_VERSION"
return 0
else
echo "Installing Node.js 20+..."
install_nodejs_via_nvm
fi
}
# Function to install Node.js via nvm
install_nodejs_via_nvm() {
# Install NVM if not already installed
if [ ! -d "$HOME/.nvm" ]; then
echo "Downloading NVM..."
# Download NVM install script to a temporary file first
TMP_INSTALL_SCRIPT="/tmp/nvm_install_$.sh"
if curl -f -s -S -o "$TMP_INSTALL_SCRIPT" "https://qwen-code-assets.oss-cn-hangzhou.aliyuncs.com/installation/install_nvm.sh"; then
# Run the installation script
if bash "$TMP_INSTALL_SCRIPT"; then
rm -f "$TMP_INSTALL_SCRIPT"
else
echo "✗ NVM installation failed"
rm -f "$TMP_INSTALL_SCRIPT"
echo "Please install Node.js manually from: https://nodejs.org/"
exit 1
fi
else
echo "✗ Failed to download NVM"
rm -f "$TMP_INSTALL_SCRIPT"
echo "Please check your internet connection or install Node.js manually from https://nodejs.org/"
exit 1
fi
fi
# Load NVM
export NVM_DIR="$HOME/.nvm"
if [ -s "$NVM_DIR/nvm.sh" ]; then
\. "$NVM_DIR/nvm.sh"
else
echo "✗ NVM installation failed"
echo "Please install Node.js manually from https://nodejs.org/"
exit 1
fi
# Load bash completion if available
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
# Verify NVM is loaded
if ! command_exists nvm; then
echo "✗ Failed to load NVM"
echo "Please manually load NVM or install Node.js from https://nodejs.org/"
exit 1
fi
# Install and use Node.js 20+
echo "Installing Node.js 20..."
if nvm install 20 >/dev/null 2>&1; then
nvm use 20 >/dev/null 2>&1
else
echo "✗ Failed to install Node.js 20"
exit 1
fi
# Verify Node.js installation
if command_exists node; then
NODE_VERSION=$(node --version)
echo "✓ Node.js $NODE_VERSION installed"
else
echo "✗ Node.js installation verification failed"
exit 1
fi
}
# Function to check and install Qwen Code
install_qwen_code() {
if command_exists qwen; then
QWEN_VERSION=$(qwen --version 2>/dev/null || echo "unknown")
echo "✓ Qwen Code is already installed: $QWEN_VERSION"
return 0
fi
echo "Installing Qwen Code..."
# Check if running as root
if [ "$(id -u)" -eq 0 ]; then
# Running as root, no need for sudo
NPM_INSTALL_CMD="npm install -g @qwen-code/qwen-code"
else
# Not root, use sudo
NPM_INSTALL_CMD="sudo npm install -g @qwen-code/qwen-code"
fi
# Install Qwen Code globally
if $NPM_INSTALL_CMD >/dev/null 2>&1; then
# Verify installation
if command_exists qwen; then
QWEN_VERSION=$(qwen --version 2>/dev/null || echo "unknown")
echo "✓ Qwen Code $QWEN_VERSION installed"
else
echo "⚠ Qwen Code installed but not in PATH"
echo " You may need to restart your terminal"
fi
else
echo "✗ Failed to install Qwen Code"
exit 1
fi
}
# Main execution
main() {
# Step 1: Check and install Node.js
install_nodejs
echo ""
# Step 2: Check and install Qwen Code
install_qwen_code
echo ""
echo "==========================================="
echo "✓ Installation completed!"
echo "==========================================="
echo ""
# Try to source the shell configuration file
if [ -f "$HOME/.zshrc" ]; then
echo "Loading zsh configuration..."
source "$HOME/.zshrc" 2>/dev/null || true
elif [ -f "$HOME/.bashrc" ]; then
echo "Loading bash configuration..."
source "$HOME/.bashrc" 2>/dev/null || true
fi
echo "To use Qwen Code in new terminals, run: qwen"
echo "If 'qwen' command is not found, please restart your terminal."
}
# Run main function
main