-
Notifications
You must be signed in to change notification settings - Fork 0
/
n
executable file
·361 lines (291 loc) · 7.59 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
#!/usr/bin/env sh
# Library version
VERSION="0.7.3"
N_PREFIX=${N_PREFIX-/usr/local}
VERSIONS_DIR=$N_PREFIX/n/versions
#
# Log the given <msg ...>
#
log() {
printf "\033[90m...\033[0m $@\n"
}
#
# Exit with the given <msg ...>
#
abort() {
printf "\033[31mError: $@\033[0m\n" && exit 1
}
# setup
test -d $VERSIONS_DIR || mkdir -p $VERSIONS_DIR
if ! test -d $VERSIONS_DIR; then
abort "Failed to create versions directory ($VERSIONS_DIR), do you have permissions to do this?"
fi
# curl / wget support
GET=
# wget support (Added --no-check-certificate for Github downloads)
which wget > /dev/null && GET="wget --no-check-certificate -q -O-"
# curl support
which curl > /dev/null && GET="curl -# -L"
# Ensure we have curl or wget
test -z "$GET" && abort "curl or wget required"
#
# 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 custom <version> <tarball> [config ...] Install custom node <tarball> with [args ...]
n use <version> [args ...] Execute node <version> with [args ...]
n npm <version> [args ...] Execute npm <version> with [args ...]
n bin <version> Output bin path for <version>
n rm <version ...> Remove the given version(s)
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:
- rm
which bin
use as
list ls
custom c
help
exit 0
}
#
# 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 current node --version
# and others installed.
#
display_versions() {
check_current_version
for dir in $VERSIONS_DIR/*; do
local version=${dir##*/}
local config=`test -f $dir/.config && cat $dir/.config`
if test "$version" = "$active"; then
printf " \033[32mο\033[0m $version \033[90m$config\033[0m\n"
else
printf " $version \033[90m$config\033[0m\n"
fi
done
}
#
# Install node <version> [config ...]
#
install_node() {
local version=$1; shift
local config=$@
check_current_version
# remove "v"
version=${version#v}
# activate
local dir=$VERSIONS_DIR/$version
if test -d $dir; then
# TODO: refactor, this is lame
cd $dir \
&& mkdir -p $N_PREFIX/lib/node \
&& cp -fR $dir/include/node $N_PREFIX/include \
&& cp -fR $dir/bin/* $N_PREFIX/bin \
&& cp -fR $dir/lib/node/* $N_PREFIX/lib/node/ .
# install
else
local tarball="node-v$version.tar.gz"
local url="http://nodejs.org/dist/$tarball"
# >= 0.5.x
local minor=$(echo $version | cut -d '.' -f 2)
test $minor -ge "5" && url="http://nodejs.org/dist/v$version/$tarball"
install_tarball $version $url $config
fi
}
#
# Install node <version> <tarball> [config ...]
#
install_tarball() {
local version=$1
local url=$2; shift 2
local config=$@
# remove "v"
version=${version#v}
local dir=$VERSIONS_DIR/$version
local tarball="node-v$version.tar.gz"
local logpath="/tmp/n.log"
# create build directory
mkdir -p $N_PREFIX/n/node-v$version
# fetch and unpack
cd $N_PREFIX/n/node-v$version \
&& $GET $url | tar xz --strip-components=1 > $logpath 2>&1
# see if things are alright
if test $? -gt 0; then
rm $tarball
echo "\033[31mError: installation failed\033[0m"
echo " node version $version does not exist,"
echo " n failed to fetch the tarball,"
echo " or tar failed. Try a different"
echo " version or view $logpath to view"
echo " error details."
exit 1
fi
cd "$N_PREFIX/n/node-v$version" \
&& ./configure --prefix $VERSIONS_DIR/$version $config\
&& JOBS=4 make install \
&& cd .. \
&& cleanup $version \
&& mkdir -p $dir \
&& echo $config > "$dir/.config" \
&& n $version \
&& ln -s "$N_PREFIX/n/versions/$version" "$N_PREFIX/n/current"
}
#
# Cleanup after the given <version>
#
cleanup() {
local version=$1
local dir="node-v$version"
if test -d $dir; then
log "removing source"
rm -fr $dir
fi
if test -f "$dir.tar.gz"; then
log "removing tarball"
rm -fr "$dir.tar.gz"
fi
}
#
# 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 "$1 is not installed"
fi
}
#
# Execute the given <version> of npm
# with [args ...]
#
execute_with_npm_version() {
test -z $1 && abort "version required"
local version=${1#v}
local bin=$VERSIONS_DIR/$version/bin
shift # remove version
if test -f $bin/npm; then
$bin/node $bin/npm $@
else
abort "npm is not installed, node.js version must be greater than or equal to 0.6.3"
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]+\.\d*[02468]\.[0-9]+' \
| sort -u -k 1,1n -k 2,2n -k 3,3n -t . \
| tail -n1
}
#
# Display the versions of node available.
#
list_versions() {
check_current_version
local versions=""
versions=`$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 . \
| awk '{ print " " $1 }'`
for v in $versions; do
if test "$active" = "$v"; then
printf " \033[32mο\033[0m $v \033[0m\n"
else
if test -d $VERSIONS_DIR/$v; then
printf " * $v \033[0m\n"
else
printf " $v\n"
fi
fi
done
}
# 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 ;;
npm) shift; execute_with_npm_version $@; exit ;;
rm|-) remove_version $2; exit ;;
latest) install_node `n --latest`; exit ;;
stable) install_node `n --stable`; exit ;;
ls|list) list_versions $2; exit ;;
c|custom) shift; install_tarball $@; exit ;;
*) install_node $@; exit ;;
esac
shift
done
fi