Skill: piaolingwuyu/nvm-auto-switch (version 1.0.0)

--- name: nvm-auto-switch description: 根据 package.json 中的 engines 字段自动检测 Node.js 版本要求，如未安装 nvm 则自动安装，并切换到正确的 Node 版本。支持 Windows、macOS 和 Linux。在开发 Node.js 项目、切换 Node 版本或设置开发环境时使用。 --- # NVM 自动切换 根据 package.json 的 engines 字段自动切换到正确的 Node.js 版本。 ## 使用场景 - 进入 Node.js 项目目录 - 设置新的开发环境 - Node 版本不匹配错误 - CI/CD 流水线设置 ## 快速开始 ### Windows ```powershell # 自动获取当前目录 .\scripts\nvm-auto-switch.ps1 # 或指定项目路径 .\scripts\nvm-auto-switch.ps1 [项目路径] ``` ### macOS / Linux ```bash # 自动获取当前目录 ./scripts/nvm-auto-switch.sh # 或指定项目路径 ./scripts/nvm-auto-switch.sh [项目路径] ``` ## 工作原理 1. **检测操作系统** - 识别 Windows、macOS 或 Linux 2. **自动获取项目路径** - 如未提供路径，自动使用当前工作目录 3. **检查当前 Node 版本** - 获取当前激活的 Node.js 版本 4. **检查/安装 NVM** - 如未安装则自动安装 5. **读取 package.json** - 解析 engines.node 字段 6. **对比版本** - 如果当前版本已满足要求，跳过切换 7. **查找/安装 Node** - 使用现有版本或下载匹配版本 8. **切换版本** - 激活正确的 Node 版本 ## 工作流程 ``` 获取项目路径（参数或当前目录） ↓ 检查当前 Node 版本 ↓ 读取 package.json engines.node 解析版本要求 (例如 >=14.0.0) ↓ 当前版本已满足要求？ ├── 是 → 跳过切换，提示当前版本兼容 │ └── 否 → 继续 ↓ 检查 NVM 是否已安装？ ├── 否 → 自动安装 NVM │ ├── Windows: 下载 nvm-setup.exe │ └── macOS/Linux: 运行安装脚本 │ └── 是 → 继续 ↓ 查找匹配的已安装版本？ ├── 是 → nvm use <版本号> │ └── 否 → nvm install <最新匹配版本> nvm use <版本号> ``` ## 脚本 `scripts/` 目录下的工具脚本： | 脚本 | 平台 | 用途 | |------|------|------| | `nvm-auto-switch.ps1` | Windows | PowerShell 实现 | | `nvm-auto-switch.sh` | macOS/Linux | Bash 实现 | ## 示例 **当前目录（自动获取）：** ```bash ./scripts/nvm-auto-switch.sh ``` ```powershell .\scripts\nvm-auto-switch.ps1 ``` **指定项目：** ```bash ./scripts/nvm-auto-switch.sh /path/to/project ``` ```powershell .\scripts\nvm-auto-switch.ps1 "C:\projects\my-app" ``` ## 注意事项 - Windows 需要管理员权限来安装 nvm - macOS/Linux 需要 curl 来安装 nvm - 支持版本前缀：`^`, `~`, `>=`, `<=` 等 - 安装 nvm 后可能需要重启终端 - 不提供路径参数时，自动使用当前工作目录

path: scripts/nvm-auto-switch.sh
content: #!/bin/bash

# Get script directory (where this script is located)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Get project path from argument or use current directory
PROJECT_PATH="${1:-$(pwd)}"
PACKAGE_JSON_PATH="$PROJECT_PATH/package.json"

echo "========================================"
echo "  NVM Auto Switch Node Version"
echo "========================================"
echo ""
echo "Script location: $SCRIPT_DIR"
echo "Project path: $PROJECT_PATH"

# Check if package.json exists
if [ ! -f "$PACKAGE_JSON_PATH" ]; then
    echo "Error: package.json not found: $PACKAGE_JSON_PATH"
    exit 1
fi

# Read engines.node from package.json
NODE_VERSION=$(cat "$PACKAGE_JSON_PATH" | grep -o '"node"[[:space:]]*:[[:space:]]*"[^"]*"' | cut -d'"' -f4)

if [ -z "$NODE_VERSION" ]; then
    echo "Warning: engines.node not specified in package.json, skipping"
    exit 0
fi

echo "Project path: $PROJECT_PATH"
echo "Required Node version: $NODE_VERSION"

# Parse version number
TARGET_VERSION=$(echo "$NODE_VERSION" | sed 's/[^0-9.]//g')
MAJOR_VERSION=$(echo "$TARGET_VERSION" | cut -d'.' -f1)

echo "Target major version: $MAJOR_VERSION.x"
echo ""

# Function to check if nvm is available
check_nvm() {
    type nvm &> /dev/null
    return $?
}

# Check if nvm is installed
if ! check_nvm; then
    echo "nvm command not found, starting auto installation..."
    echo ""
    
    # Download and install nvm
    echo "Downloading nvm..."
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
    
    # Load nvm
    export NVM_DIR="$HOME/.nvm"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
    
    # Check if installation succeeded
    if ! check_nvm; then
        echo "Error: nvm installation failed, please install manually"
        echo "Install command: curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash"
        exit 1
    fi
    
    echo "nvm installed successfully"
    echo ""
fi

# Ensure nvm is loaded
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

echo "Installed Node versions:"
nvm list
echo ""

# Get list of installed versions
INSTALLED_VERSIONS=$(nvm list | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | sort -V -r)

# Find matching version
MATCHED_VERSION=$(echo "$INSTALLED_VERSIONS" | grep "^$MAJOR_VERSION\." | head -n 1)

if [ -n "$MATCHED_VERSION" ]; then
    echo "Found matching installed version: $MATCHED_VERSION"
    nvm use "$MATCHED_VERSION"
    echo ""
    echo "Successfully switched to Node $MATCHED_VERSION"
else
    echo "No matching version found, need to download Node $MAJOR_VERSION.x latest version"
    echo ""
    
    # Get available latest version
    LATEST_VERSION=$(nvm list-remote | grep "^v$MAJOR_VERSION\." | tail -n 1 | sed 's/^v//')
    
    if [ -z "$LATEST_VERSION" ]; then
        echo "Error: No available version found for Node $MAJOR_VERSION.x"
        exit 1
    fi
    
    echo "Preparing to download Node $LATEST_VERSION..."
    nvm install "$LATEST_VERSION"
    
    echo "Switching to newly installed version..."
    nvm use "$LATEST_VERSION"
    
    echo ""
    echo "Installed and switched to Node $LATEST_VERSION"
fi

echo ""
echo "Verifying current version:"
echo "   Node: $(node -v)"
echo "   npm:  v$(npm -v)"


Security scan verdicts — ClawScan: clean | static analysis: clean | VirusTotal: clean | SkillSpector: suspicious (HIGH)