-
Notifications
You must be signed in to change notification settings - Fork 2
/
install-pm2.sh
64 lines (52 loc) · 1.78 KB
/
install-pm2.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/bash
# 检查是否已安装 Node.js 和 npm
if ! command -v node &> /dev/null || ! command -v npm &> /dev/null
then
echo "Node.js or npm is not installed. Please install Node.js first."
exit 1
fi
# 设置 npm 全局安装路径为用户目录
mkdir -p ~/.npm-global
npm config set prefix '~/.npm-global'
# 将 npm 全局 bin 添加到 PATH
if ! grep -q "PATH=~/.npm-global/bin:\$PATH" ~/.profile; then
echo "export PATH=~/.npm-global/bin:\$PATH" >> ~/.profile
echo "PATH has been updated. Please run 'source ~/.profile' or log out and log back in for the changes to take effect."
# 手动更新当前 shell 的 PATH
export PATH=~/.npm-global/bin:$PATH
fi
# 保存原始镜像地址
original_registry=$(npm config get registry)
# 设置淘宝镜像
echo "Setting npm registry to CNPM mirror for faster installation..."
npm config set registry https://registry.npmmirror.com
# 安装 pm2
echo "Installing pm2. This may take a few moments..."
npm install -g pm2 &
# 获取后台进程的 PID
bg_pid=$!
# 显示进度动画
spin='-\|/'
i=0
while kill -0 $bg_pid 2>/dev/null
do
i=$(( (i+1) %4 ))
printf "\r${spin:$i:1} Installing... (Press Ctrl+C to cancel)"
sleep .1
done
# 手动更新当前 shell 的 PATH,确保 pm2 可执行
export PATH=~/.npm-global/bin:$PATH
# 检查 pm2 是否成功安装
if command -v pm2 &> /dev/null
then
echo -e "\npm2 has been successfully installed."
pm2 --version
else
echo -e "\nFailed to install pm2. Please check your permissions and try again."
exit 1
fi
# 切换回原来的镜像
echo "Switching back to the original npm registry..."
npm config set registry "$original_registry"
echo "Npm registry has been set back to the original: $original_registry"
echo "安装成功。如有问题请联系Q365172043。"