-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnv.sh
executable file
·266 lines (239 loc) · 6.27 KB
/
nv.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/bin/bash
CONFIG_FOLDER=${NV_CONFIG:-$HOME/.config/nv}
BIN_LINK=$CONFIG_FOLDER/current
NODE_INSTALLATION=$CONFIG_FOLDER/dist
NODE_VERSION_REGEX='s/\([0-9]*\.[0-9]*\.[0-9]\).*/\1/p'
NODE_LATEST_DIST_URL="https://nodejs.org/dist/latest/"
NODE_CARBON_DIST_URL="https://nodejs.org/dist/latest-carbon/"
NODE_BORON_DIST_URL="https://nodejs.org/dist/latest-boron/"
NODE_ARGON_DIST_URL="https://nodejs.org/dist/latest-argon/"
REMOTE_URLS=($NODE_LATEST_DIST_URL $NODE_CARBON_DIST_URL $NODE_BORON_DIST_URL $NODE_ARGON_DIST_URL)
REMOTE_NAME=("latest" "carbon" "boron" "argon")
NV_VERSION="v1.0"
wget=$(which wget)
net_get_latest_version () {
$wget -qO- $1 | grep node-v | sed 's/<a href="node-v//' | head -1 | sed -n $NODE_VERSION_REGEX
}
_list_remote () {
echo "Getting remote versions"
echo "This might take a while..."
echo
v=0
for url in ${REMOTE_URLS[@]}; do
version=$(net_get_latest_version $url)
if [ -d "$NODE_INSTALLATION/$version" ]; then
echo " [x] $version (${REMOTE_NAME[$v]})"
else
echo " [ ] $version (${REMOTE_NAME[$v]})"
fi
v=$((v + 1))
done
echo
echo "[x] -> Downloaded"
echo "[ ] -> Not downloaded"
}
_list_local () {
echo "Getting installed versions"
node_path=$(which node 2>&-)
if [ ! $? = 0 ]; then
echo "No installations of node is being used"
elif [ "$BIN_LINK/node" != "$node_path" ]; then
echo "Node installation is not under nv"
echo "Its possible that current installation of node is installed directly on tthe system"
echo
echo "Node path found -> $node_path"
else
selected=$(node --version 2>&- | sed 's/v//')
fi
echo
echo "Downloaded versions:"
for i in $(ls $NODE_INSTALLATION); do
if [ "$selected" = "$i" ]; then
echo " *$i"
else
echo " $i"
fi
done
echo
echo "* -> selected"
}
list_cmd () {
if [ ! -z $1 ]; then
case $1 in
-r|--remote)
_list_remote
;;
*)
echo "Unknown parameter $1 for list command"
exit 1
esac
else
_list_local
fi
}
get_cmd () {
version=$1
shift
# Get OS and Arch
OS=$(uname -s | awk '{print tolower($0)}')
ARCH=$(uname -m)
if [ $ARCH == "x86_64" ]; then
ARCH="x64"
fi
# Get latest version if no version is passed
if [ -z $version ]; then
echo "Geting latest version from remote"
version=$(net_get_latest_version $NODE_LATEST_DIST_URL)
echo "Downloading version $version"
else
# Check if version name was passed
case $version in
latest|LATEST)
version=$(net_get_latest_version $NODE_LATEST_DIST_URL)
echo "Current latest is $version"
;;
carbon|CARBON)
version=$(net_get_latest_version $NODE_CARBON_DIST_URL)
echo "Current carbon is $version"
;;
boron|BORON)
version=$(net_get_latest_version $NODE_BORON_DIST_URL)
echo "Current boron is $version"
;;
argon|ARGON)
version=$(net_get_latest_version $NODE_ARGON_DIST_URL)
echo "Current argon is $version"
;;
esac
fi
filename="node-v$version-$OS-$ARCH.tar.xz"
url="https://nodejs.org/dist/v$version/$filename"
if [ -d "$NODE_INSTALLATION/$version" ]; then
echo "Version $version already installed"
echo
echo "To use this version, you can type \`nv use $version\`"
echo " \`nv use $version\`"
echo "Or simply:"
echo " \`nv use\`"
echo "if you'd like to use the latest vesrsion"
exit 1
fi
if [ ! -f /tmp/$filename ]; then
mkdir -p $CONFIG_FOLDER/logs
$wget -O /tmp/$filename $url 2> $CONFIG_FOLDER/logs/wget.log
if [ $? != 0 ]; then
cat $CONFIG_FOLDER/logs/wget.log
echo "\nwget exited with error."
exit 1
fi
fi
mkdir -p $NODE_INSTALLATION
tar -xvf /tmp/$filename -C $NODE_INSTALLATION
mv $NODE_INSTALLATION/node-v$version* $NODE_INSTALLATION/$version
if [ $? != 0 ]; then
echo "\nFailed to extract /tmp/$filename into $target_dir"
exit 1
fi
}
use_cmd () {
version=$1
if [ -z $version ]; then
echo "Please specify a version"
echo "usage: nv use <version>"
echo
exit 1
fi
echo "Using node $version"
folder="$NODE_INSTALLATION/$version/bin"
if [ ! -d $folder ]; then
echo "Could not find folder $folder"
echo "Invalid version $version of nodejs"
echo "Try installing with \`nv get $version\`"
exit 1
else
echo "Using installation in folder $folder"
fi
if [ -d $BIN_LINK ]; then
rm $BIN_LINK
fi
ln -s $folder $BIN_LINK
}
update_cmd () {
echo "Updating Node Versioning"
repo="https://github.com/fredrb/nv.git"
git=$(which git)
if [ $? != 0 ]; then
echo "Could not found git.\nPlease install git before installing Node Versioning"
exit 1
fi
if [ ! -d $CONFIG_FOLDER/.git ]; then
echo "\`nv update\` only works if nv config folder was setup as a git repository."
echo "If you installed manually, you can pull the newest changes to update nv command"
exit 1
fi
pushd $CONFIG_FOLDER
$git pull origin master
popd
echo "Node Versioning updated $(nv version)"
}
version_cmd () {
echo "$NV_VERSION"
}
print_help () {
echo "usage: nv command [options]"
echo
echo "command:"
echo
echo " list [options]:"
echo " prints all installed versions"
echo " options:"
echo " --remote|-r: print available release versions"
echo
echo " get <version>:"
echo " downloads and install version number"
echo " (e.g. nv get 10.4.0)"
echo " version can be either version number or release names (latest|carbon|boron|argon)"
echo " (e.g. nv get latest)"
echo
echo " use <version>:"
echo " selects version as current node version"
echo " (e.g. nv use 10.4.0)"
echo
echo " update:"
echo " self update nv command"
echo
echo " help:"
echo " prints this help text"
}
command=$1
shift
if [ ! -d $NV_CONFIG ]; then
echo "Cannot find nv folder: ($NV_CONFIG)"
echo "Installation corrupted"
exit 1
fi
if [ ! -d $NODE_INSTALLATION ]; then
echo "Cannot find nv dist folder ($NODE_INSTALLATION)"
echo "Installation corrupted"
exit 1
fi
case $command in
list)
list_cmd "$@"
;;
get)
get_cmd "$@"
;;
use)
use_cmd "$@"
;;
update)
update_cmd "$@"
;;
version|-v|--version)
version_cmd "$@"
;;
help|*)
print_help
;;
esac