feat(env): 添加 Node.js 自动安装脚本
- 新增 init_node.sh 脚本,实现 Node.js 一键安装和配置- 支持 CentOS/RHEL、Ubuntu/Debian 等主流 Linux 发行版 - 自动检测操作系统类型并安装必要依赖- 配置阿里云 npm镜像源,提高包下载速度 - 可选安装并配置 Yarn - 完成安装后进行版本测试,确保安装成功
This commit is contained in:
parent
be03bea588
commit
14d9d684f9
|
|
@ -0,0 +1,162 @@
|
|||
#!/bin/bash
|
||||
|
||||
# ==============================================
|
||||
# Node.js 安装脚本
|
||||
# 功能:自动安装 Node.js 并配置阿里云镜像源
|
||||
# 适用系统:CentOS/RHEL、Ubuntu/Debian 等主流 Linux 发行版
|
||||
# ==============================================
|
||||
|
||||
# 启用严格模式
|
||||
set -euo pipefail
|
||||
|
||||
# --------------------------
|
||||
# 颜色定义
|
||||
# --------------------------
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[0;33m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
# --------------------------
|
||||
# 日志函数
|
||||
# --------------------------
|
||||
log_info() {
|
||||
echo -e "${GREEN}[INFO]${NC} $1"
|
||||
}
|
||||
|
||||
log_warn() {
|
||||
echo -e "${YELLOW}[WARN]${NC} $1"
|
||||
}
|
||||
|
||||
log_error() {
|
||||
echo -e "${RED}[ERROR]${NC} $1" >&2
|
||||
}
|
||||
|
||||
# --------------------------
|
||||
# 检查 root 权限
|
||||
# --------------------------
|
||||
check_root() {
|
||||
if [ "$(id -u)" -ne 0 ]; then
|
||||
log_error "请使用 root 用户或通过 sudo 运行此脚本"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
# --------------------------
|
||||
# 检测系统类型
|
||||
# --------------------------
|
||||
detect_os() {
|
||||
if [ -f /etc/os-release ]; then
|
||||
. /etc/os-release
|
||||
OS_NAME="${ID:-unknown}"
|
||||
OS_VERSION="${VERSION_ID:-unknown}"
|
||||
else
|
||||
log_error "无法检测操作系统类型,请手动安装 Node.js"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
# --------------------------
|
||||
# 安装依赖工具
|
||||
# --------------------------
|
||||
install_dependencies() {
|
||||
log_info "正在安装必要的依赖工具..."
|
||||
|
||||
if [[ "$OS_NAME" == "centos" || "$OS_NAME" == "rhel" || "$OS_NAME" == "rocky" || "$OS_NAME" == "almalinux" ]]; then
|
||||
dnf install -y curl wget
|
||||
elif [[ "$OS_NAME" == "ubuntu" || "$OS_NAME" == "debian" ]]; then
|
||||
apt update
|
||||
apt install -y curl wget
|
||||
else
|
||||
log_error "不支持的操作系统: ${OS_NAME}"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
# --------------------------
|
||||
# 安装 Node.js
|
||||
# --------------------------
|
||||
install_nodejs() {
|
||||
log_info "正在安装 Node.js..."
|
||||
|
||||
# 添加 NodeSource 仓库
|
||||
if [[ "$OS_NAME" == "centos" || "$OS_NAME" == "rhel" || "$OS_NAME" == "rocky" || "$OS_NAME" == "almalinux" ]]; then
|
||||
curl -sL https://rpm.nodesource.com/setup_18.x | bash -
|
||||
dnf install -y nodejs
|
||||
elif [[ "$OS_NAME" == "ubuntu" || "$OS_NAME" == "debian" ]]; then
|
||||
curl -sL https://deb.nodesource.com/setup_18.x | bash -
|
||||
apt install -y nodejs
|
||||
else
|
||||
log_error "不支持的操作系统: ${OS_NAME}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
log_info "Node.js 安装完成"
|
||||
}
|
||||
|
||||
# --------------------------
|
||||
# 配置 npm 阿里云镜像源
|
||||
# --------------------------
|
||||
configure_npm_mirror() {
|
||||
log_info "正在配置 npm 阿里云镜像源..."
|
||||
|
||||
npm config set registry https://registry.npmmirror.com
|
||||
|
||||
log_info "npm 阿里云镜像源已配置完成"
|
||||
}
|
||||
|
||||
# --------------------------
|
||||
# 安装并配置 Yarn(可选)
|
||||
# --------------------------
|
||||
install_and_configure_yarn() {
|
||||
log_info "正在安装并配置 Yarn..."
|
||||
|
||||
# 安装 Yarn
|
||||
if [[ "$OS_NAME" == "centos" || "$OS_NAME" == "rhel" || "$OS_NAME" == "rocky" || "$OS_NAME" == "almalinux" ]]; then
|
||||
npm install -g yarn
|
||||
elif [[ "$OS_NAME" == "ubuntu" || "$OS_NAME" == "debian" ]]; then
|
||||
npm install -g yarn
|
||||
fi
|
||||
|
||||
# 配置 Yarn 阿里云镜像源
|
||||
yarn config set registry https://registry.npmmirror.com
|
||||
|
||||
log_info "Yarn 安装并配置完成"
|
||||
}
|
||||
|
||||
# --------------------------
|
||||
# 测试 Node.js 和 npm 安装
|
||||
# --------------------------
|
||||
test_installation() {
|
||||
log_info "正在测试 Node.js 和 npm 安装..."
|
||||
|
||||
NODE_VERSION=$(node --version)
|
||||
NPM_VERSION=$(npm --version)
|
||||
|
||||
log_info "Node.js 安装成功,版本为: ${NODE_VERSION}"
|
||||
log_info "npm 安装成功,版本为: ${NPM_VERSION}"
|
||||
|
||||
if command -v yarn &> /dev/null; then
|
||||
YARN_VERSION=$(yarn --version)
|
||||
log_info "Yarn 安装成功,版本为: ${YARN_VERSION}"
|
||||
fi
|
||||
}
|
||||
|
||||
# --------------------------
|
||||
# 主函数
|
||||
# --------------------------
|
||||
main() {
|
||||
check_root
|
||||
detect_os
|
||||
install_dependencies
|
||||
install_nodejs
|
||||
configure_npm_mirror
|
||||
install_and_configure_yarn
|
||||
test_installation
|
||||
}
|
||||
|
||||
# --------------------------
|
||||
# 执行主函数
|
||||
# --------------------------
|
||||
main
|
||||
exit 0
|
||||
Loading…
Reference in New Issue