Last active
February 6, 2024 07:05
-
-
Save heycqing/5042797154f652523b3227f4985eb20e to your computer and use it in GitHub Desktop.
用于切换不同项目下window的node版本
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| TARGET_VERSION="14" | |
| # 检查 nvm 是否已安装 | |
| if ! command -v nvm &> /dev/null; then | |
| echo "nvm is not installed. Installing nvm..." | |
| # 安装 nvm (这里仅提供了基本的安装命令,实际安装可能更复杂) | |
| curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash | |
| export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")" | |
| [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm | |
| fi | |
| # 检查当前版本 | |
| CURRENT_VERSION=$(nvm current) | |
| if [ "$CURRENT_VERSION" = "v$TARGET_VERSION" ]; then | |
| echo "You are already using Node.js version $TARGET_VERSION." | |
| exit 0 | |
| fi | |
| # 安装并使用目标版本 | |
| nvm install $TARGET_VERSION | |
| nvm use $TARGET_VERSION |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @echo off | |
| SET TARGET_VERSION=18.12.0 | |
| REM 检查 nvm 是否已安装 | |
| nvm version > nul | |
| if %errorlevel% neq 0 ( | |
| echo nvm is not installed. Installing nvm-windows... | |
| REM 安装 nvm-windows (这里需要用户手动操作,因为 nvm-windows 的安装更复杂) | |
| REM 通常需要从 https://github.com/coreybutler/nvm-windows/releases 下载安装包 | |
| REM 并且可能涉及到安全权限和环境变量设置 | |
| exit /b 1 | |
| ) | |
| REM 检查当前版本 | |
| FOR /F "tokens=*" %%i IN ('nvm list ^| findstr /B /C:"* Currently using"') DO SET CURRENT_VERSION=%%i | |
| SET CURRENT_VERSION=%CURRENT_VERSION:* Currently using %=% | |
| IF "%CURRENT_VERSION%" == "%TARGET_VERSION%" ( | |
| echo You are already using Node.js version %TARGET_VERSION%. | |
| exit /b 0 | |
| ) | |
| REM 安装并使用目标版本 | |
| nvm install %TARGET_VERSION% | |
| nvm use %TARGET_VERSION% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment