add permission check for mac

This commit is contained in:
DennisYu07 2026-02-05 01:47:09 -08:00
parent 879d91fb4d
commit fff66a0221

View file

@ -1,153 +1,137 @@
#!/bin/bash
# Script to install Node.js and Qwen Code with source information
# This script handles the installation process and sets the installation source
set -e # Exit on any error
# 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 with Source Tracking"
echo "Qwen Code Installation Script"
echo "==========================================="
# Function to display usage
usage() {
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " -s, --source SOURCE Specify the installation source (e.g., github, npm, internal)"
echo " -h, --help Show this help message"
echo ""
exit 1
}
# Parse command line arguments
SOURCE="unknown"
while [[ $# -gt 0 ]]; do
case $1 in
-s|--source)
SOURCE="$2"
shift 2
;;
-h|--help)
usage
;;
*)
usage
;;
esac
done
echo "INFO: Installation source: $SOURCE"
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to install Node.js
# Function to check and install Node.js
install_nodejs() {
echo "INFO: Checking for Node.js installation..."
if command_exists node; then
NODE_VERSION=$(node --version)
echo "INFO: Node.js is already installed: $NODE_VERSION"
# Check if version is sufficient (>= 20.0.0)
if [[ $(node -pe "require('semver').gte(require('semver').coerce('$NODE_VERSION'), '20.0.0')") == "true" ]]; then
echo "INFO: Node.js version is sufficient. Skipping installation."
return 0
else
echo "INFO: Node.js version is too low. Installing Node.js 20+..."
install_nodejs_via_nvm
fi
echo "✓ Node.js is already installed: $NODE_VERSION"
return 0
else
echo "INFO: Node.js not found. Installing Node.js 20+..."
echo "Installing Node.js 20+..."
install_nodejs_via_nvm
fi
}
# Function to install Node.js via nvm
install_nodejs_via_nvm() {
echo "INFO: Installing Node Version Manager (NVM)..."
# Install NVM if not already installed
if [ ! -d "$HOME/.nvm" ]; then
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
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://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.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
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
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+
nvm install 20
nvm use 20
echo "INFO: Node.js $(node --version) installed and activated via NVM."
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 install Qwen Code with source information
# Function to check and install Qwen Code
install_qwen_code() {
echo "INFO: Checking for Qwen Code installation..."
if command_exists qwen; then
QWEN_VERSION=$(qwen --version)
echo "INFO: Qwen Code is already installed: $QWEN_VERSION"
echo "INFO: Skipping Qwen Code installation."
# Update source.json even if Qwen Code is already installed
echo "INFO: Updating source.json in ~/.qwen/"
if [ ! -d "$HOME/.qwen" ]; then
mkdir -p "$HOME/.qwen"
fi
echo "{\"source\": \"$SOURCE\"}" > "$HOME/.qwen/source.json"
echo "SUCCESS: Installation source updated in ~/.qwen/source.json"
QWEN_VERSION=$(qwen --version 2>/dev/null || echo "unknown")
echo "✓ Qwen Code is already installed: $QWEN_VERSION"
return 0
fi
echo "INFO: Qwen Code not found. Installing Qwen Code with source: $SOURCE"
echo "Installing Qwen Code..."
# Install Qwen Code globally
echo "INFO: Running: npm install -g @qwen-code/qwen-code"
npm install -g @qwen-code/qwen-code
echo "SUCCESS: Qwen Code installed successfully!"
# After installation, create source.json in the .qwen directory
echo "INFO: Creating source.json in ~/.qwen/"
if [ ! -d "$HOME/.qwen" ]; then
mkdir -p "$HOME/.qwen"
fi
# Create the source.json file with the installation source
echo "{\"source\": \"$SOURCE\"}" > "$HOME/.qwen/source.json"
echo "SUCCESS: Installation source saved to ~/.qwen/source.json"
# Verify installation
if command_exists qwen; then
echo "SUCCESS: Qwen Code is available as 'qwen' command."
qwen --version
# Install Qwen Code globally (may require sudo)
if sudo npm install -g @qwen-code/qwen-code >/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 "WARNING: Qwen Code may not be in PATH. Please check your npm global bin directory."
echo "✗ Failed to install Qwen Code"
exit 1
fi
}
# Main execution
main() {
echo "INFO: Starting installation process..."
# Install Node.js
# Step 1: Check and install Node.js
install_nodejs
# Install Qwen Code with source information
install_qwen_code
echo ""
# Step 2: Check and install Qwen Code
install_qwen_code
echo ""
echo "==========================================="
echo "SUCCESS: Installation completed!"
echo " Installation completed!"
echo "==========================================="
echo ""
echo "Run 'qwen' to start using Qwen Code"
}
# Run main function