flnxi-Wu/scripts/tools/upgrade-nodejs.sh
WuYongJie f646b71813
Some checks failed
CI Check / check (push) Has been cancelled
chore: 初始化本地仓库并同步最近改动
2025-11-18 17:50:16 +08:00

134 lines
3.1 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# Node.js 升级脚本(适用于 CentOS/RHEL)
echo "=========================================="
echo " Node.js 版本升级工具"
echo "=========================================="
echo ""
# 检查当前版本
echo "[1/6] 检查当前 Node.js 版本..."
CURRENT_VERSION=$(node -v 2>/dev/null || echo "未安装")
echo "当前版本: $CURRENT_VERSION"
echo ""
# 备份现有项目
echo "[2/6] 备份当前项目..."
if [ -d "node_modules" ]; then
echo "⏳ 备份 node_modules..."
mv node_modules node_modules.backup.$(date +%Y%m%d_%H%M%S)
echo "✅ 备份完成"
fi
if [ -f "package-lock.json" ]; then
echo "⏳ 备份 package-lock.json..."
cp package-lock.json package-lock.json.backup.$(date +%Y%m%d_%H%M%S)
echo "✅ 备份完成"
fi
echo ""
echo "[3/6] 检测系统..."
if [ -f /etc/centos-release ]; then
OS="centos"
echo "✅ 检测到 CentOS/RHEL"
elif [ -f /etc/lsb-release ]; then
OS="ubuntu"
echo "✅ 检测到 Ubuntu/Debian"
else
OS="unknown"
echo "⚠️ 未知系统"
fi
echo ""
echo "[4/6] 安装 Node.js 18..."
if [ "$OS" = "centos" ]; then
# CentOS/RHEL
echo "📦 使用 NodeSource 仓库..."
# 清理旧的 NodeSource 仓库
rm -f /etc/yum.repos.d/nodesource*.repo
# 添加 Node.js 18.x 仓库
curl -fsSL https://rpm.nodesource.com/setup_18.x | bash -
# 安装 Node.js
dnf install -y nodejs || yum install -y nodejs
elif [ "$OS" = "ubuntu" ]; then
# Ubuntu/Debian
echo "📦 使用 NodeSource 仓库..."
# 安装依赖
apt-get update
apt-get install -y ca-certificates curl gnupg
# 添加 Node.js 18.x 仓库
curl -fsSL https://deb.nodesource.com/setup_18.x | bash -
# 安装 Node.js
apt-get install -y nodejs
else
echo "❌ 无法自动安装,请手动安装 Node.js 18"
echo ""
echo "手动安装步骤:"
echo "1. 访问: https://nodejs.org/en/download/"
echo "2. 下载 Node.js 18.x LTS 版本"
echo "3. 安装后重新运行此脚本"
exit 1
fi
echo ""
echo "[5/6] 验证新版本..."
NEW_VERSION=$(node -v)
NPM_VERSION=$(npm -v)
echo "✅ Node.js: $NEW_VERSION"
echo "✅ npm: $NPM_VERSION"
# 检查版本是否满足要求
MAJOR_VERSION=$(echo $NEW_VERSION | cut -d'.' -f1 | sed 's/v//')
if [ "$MAJOR_VERSION" -ge 18 ]; then
echo "✅ 版本满足要求 (需要 ≥18.x)"
else
echo "❌ 版本不满足要求 (需要 ≥18.x)"
exit 1
fi
echo ""
echo "[6/6] 重新安装项目依赖..."
echo "⏳ 这可能需要几分钟..."
# 清理缓存
npm cache clean --force
# 重新安装
npm install
if [ $? -eq 0 ]; then
echo "✅ 依赖安装成功"
else
echo "❌ 依赖安装失败"
echo ""
echo "请手动执行:"
echo " npm install"
exit 1
fi
echo ""
echo "=========================================="
echo " ✅ 升级完成!"
echo "=========================================="
echo ""
echo "📊 版本信息:"
echo " Node.js: $(node -v)"
echo " npm: $(npm -v)"
echo ""
echo "🎯 下一步:"
echo " npm run dev # 启动开发服务器"
echo " npm run build # 构建生产版本"
echo ""