forked from tj/n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
n
executable file
·416 lines (338 loc) · 7.36 KB
/
n
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#!/usr/bin/env sh
# Library version
VERSION="1.0.0"
N_PREFIX=${N_PREFIX-/usr/local}
VERSIONS_DIR=$N_PREFIX/n/versions
UP=$'\033[A'
DOWN=$'\033[B'
# setup
test -d $VERSIONS_DIR || mkdir -p $VERSIONS_DIR
#
# Log <type> <msg>
#
log() {
printf " \033[36m%10s\033[0m : \033[90m%s\033[0m\n" $1 $2
}
#
# Exit with the given <msg ...>
#
abort() {
printf "\n \033[31mError: $@\033[0m\n\n" && exit 1
}
# curl / wget support
GET=
GET_OK=
if command -v curl > /dev/null; then
GET="curl -L#"
GET_OK="curl -Is"
elif command -v wget > /dev/null; then
GET="wget -O-"
GET_OK="wget --spider -SqO-"
else
abort "curl or wget required"
fi
#
# Output usage information.
#
display_help() {
cat <<-help
Usage: n [options] [COMMAND] [config]
Commands:
n Output versions installed
n latest [config ...] Install or activate the latest node release
n stable [config ...] Install or activate the latest stable node release
n <version> [config ...] Install and/or use node <version>
n use <version> [args ...] Execute node <version> with [args ...]
n bin <version> Output bin path for <version>
n rm <version ...> Remove the given version(s)
n prev Revert to the previously activated version
n --latest Output the latest node version available
n --stable Output the latest stable node version available
n ls Output the versions of node available
Options:
-V, --version Output current version of n
-h, --help Display help information
Aliases:
which bin
use as
list ls
- rm
help
exit 0
}
#
# Hide cursor.
#
hide_cursor() {
printf "\e[?25l"
}
#
# Show cursor.
#
show_cursor() {
printf "\e[?25h"
}
#
# Output version after selected.
#
next_version_installed() {
list_versions_installed | grep $selected -A 1 | tail -n 1
}
#
# Output version before selected.
#
prev_version_installed() {
list_versions_installed | grep $selected -B 1 | head -n 1
}
#
# Output n version.
#
display_n_version() {
echo $VERSION && exit 0
}
#
# Check for installed version, and populate $active
#
check_current_version() {
which node &> /dev/null
if test $? -eq 0; then
active=`node --version`
active=${active#v}
fi
}
#
# Display installed versions with <selected>.
#
display_versions_with_selected() {
selected=$1
echo
for dir in $VERSIONS_DIR/*; do
local version=${dir##*/}
local config=`test -f $dir/.config && cat $dir/.config`
if test "$version" = "$selected"; then
printf " \033[36mο\033[0m $version \033[90m$config\033[0m\n"
else
printf " \033[90m$version $config\033[0m\n"
fi
done
echo
}
#
# List installed versions.
#
list_versions_installed() {
for dir in $VERSIONS_DIR/*; do
local version=${dir##*/}
echo $version
done
}
#
# Display current node --version
# and others installed.
#
display_versions() {
clear
check_current_version
display_versions_with_selected $active
while true; do
read -s -n 3 c
case "$c" in
$UP)
clear
display_versions_with_selected $(prev_version_installed)
;;
$DOWN)
clear
display_versions_with_selected $(next_version_installed)
;;
*)
clear
activate $selected
exit
;;
esac
done
}
#
# Move up a line and erase.
#
erase_line() {
printf "\033[1A\033[2K"
}
#
# Check if the HEAD response of <url> is 200.
#
is_ok() {
$GET_OK $1 2>&1 | egrep "HTTP.*?200" > /dev/null
}
#
# Determine tarball url for <version>.
#
tarball_url() {
local version=$1
local uname="$(uname -a)"
local arch=x86
local os=
# from nave(1)
case "$uname" in
Linux*) os=linux ;;
Darwin*) os=darwin ;;
SunOS*) os=sunos ;;
esac
case "$uname" in
*x86_64*) arch=x64 ;;
*armv6l*) arch=arm-pi ;;
esac
echo "http://nodejs.org/dist/v${version}/node-v${version}-${os}-${arch}.tar.gz"
}
#
# Activate <version>
#
activate() {
local version=$1
local dir=$VERSIONS_DIR/$version
check_current_version
echo $active > $VERSIONS_DIR/.prev
cp -fR $dir/* $N_PREFIX
}
#
# Activate previous node.
#
activate_previous() {
test -f $VERSIONS_DIR/.prev || abort "no previous versions activated"
local prev=$(cat $VERSIONS_DIR/.prev)
activate $prev
echo
log activate $prev
echo
}
#
# Install <version>
#
install_node() {
local version=${1#v}
local config=$@
local dir=$VERSIONS_DIR/$version
local url=$(tarball_url $version)
if test -d $dir; then
activate $version
exit
fi
echo
log install $version
is_ok $url || abort "invalid version $version"
log mkdir $dir
mkdir -p $dir
cd $dir
log fetch $url
$GET $url | tar -zx --strip 1
erase_line
activate $version
log installed $(node --version)
echo
}
#
# Remove <version ...>
#
remove_version() {
test -z $1 && abort "version(s) required"
local version=${1#v}
while test $# -ne 0; do
rm -rf $VERSIONS_DIR/$version
shift
done
}
#
# Output bin path for <version>
#
display_bin_path_for_version() {
test -z $1 && abort "version required"
local version=${1#v}
local bin=$VERSIONS_DIR/$version/bin/node
if test -f $bin; then
printf $bin
else
abort "$1 is not installed"
fi
}
#
# Execute the given <version> of node
# with [args ...]
#
execute_with_version() {
test -z $1 && abort "version required"
local version=${1#v}
local bin=$VERSIONS_DIR/$version/bin/node
shift # remove version
if test -f $bin; then
$bin $@
else
abort "$version is not installed"
fi
}
#
# Display the latest node release version.
#
display_latest_version() {
$GET 2> /dev/null http://nodejs.org/dist/ \
| egrep -o '[0-9]+\.[0-9]+\.[0-9]+' \
| sort -u -k 1,1n -k 2,2n -k 3,3n -t . \
| tail -n1
}
#
# Display the latest stable node release version.
#
display_latest_stable_version() {
$GET 2> /dev/null http://nodejs.org/dist/ \
| egrep -o '[0-9]+\.[0-9]*[02468]\.[0-9]+' \
| sort -u -k 1,1n -k 2,2n -k 3,3n -t . \
| tail -n1
}
#
# Display the versions of node available.
#
display_remote_versions() {
check_current_version
local versions=""
versions=`$GET 2> /dev/null http://nodejs.org/dist/ \
| egrep -o '[0-9]+\.[0-9]+\.[0-9]+' \
| egrep -v '^0\.[0-7]\.' \
| egrep -v '^0\.8\.[0-5]$' \
| sort -u -k 1,1n -k 2,2n -k 3,3n -t . \
| awk '{ print " " $1 }'`
echo
for v in $versions; do
if test "$active" = "$v"; then
printf " \033[36mο\033[0m $v \033[0m\n"
else
if test -d $VERSIONS_DIR/$v; then
printf " $v \033[0m\n"
else
printf " \033[90m$v\033[0m\n"
fi
fi
done
echo
}
# Handle arguments
if test $# -eq 0; then
display_versions
else
while test $# -ne 0; do
case $1 in
-V|--version) display_n_version ;;
-h|--help|help) display_help ;;
--latest) display_latest_version $2; exit ;;
--stable) display_latest_stable_version $2; exit ;;
bin|which) display_bin_path_for_version $2; exit ;;
as|use) shift; execute_with_version $@; exit ;;
rm|-) remove_version $2; exit ;;
latest) install_node `n --latest`; exit ;;
stable) install_node `n --stable`; exit ;;
ls|list) display_remote_versions $2; exit ;;
prev) activate_previous; exit ;;
*) install_node $@; exit ;;
esac
shift
done
fi