Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 153 lines (115 sloc) 2.243 kb
73896ff @tj Initial commit
authored
1 #!/usr/bin/env sh
2
1667a31 @tj Started n(1)
authored
3 # Library version
4
5 VERSION="0.0.1"
3d10d31 @tj Added N_PREFIX support
authored
6 PREFIX=${N_PREFIX-/usr/local}
7 VERSIONS_DIR=$PREFIX/n/versions
1abb735 @tj Added cleanup()
authored
8
9 # setup
10
3d10d31 @tj Added N_PREFIX support
authored
11 test -d $VERSIONS_DIR || mkdir -p $VERSIONS_DIR
1667a31 @tj Started n(1)
authored
12
13 # curl / wget support
14
15 GET=
16
17 # wget support
18 which wget > /dev/null && GET="wget -q -O-"
19
20 # curl support
21 which curl > /dev/null && GET="curl -# -L"
22
d3ac35d @tj abort
authored
23 # Ensure we have curl or wget
24
25 test -z "$GET" && abort "curl or wget required"
26
1667a31 @tj Started n(1)
authored
27 #
28 # Log the given <msg ...>
29 #
30
31 log() {
32 echo "... $@"
33 }
34
35 #
d3ac35d @tj abort
authored
36 # Exit with the given <msg ...>
37 #
38
39 abort() {
40 echo "Error: $@" && exit 1
41 }
42
43 #
1667a31 @tj Started n(1)
authored
44 # Output usage information.
45 #
46
47 display_help() {
48 cat <<-help
49
50 Usage: n [options] <version>
51
1abb735 @tj Added cleanup()
authored
52 Commands:
53
54 n Output versions installed
3d10d31 @tj Added N_PREFIX support
authored
55 n <version> Install and/or use node <version>
1abb735 @tj Added cleanup()
authored
56
1667a31 @tj Started n(1)
authored
57 Options:
58
59 -V, --version Output current version of n
60 -h, --help Display help information
61
62 help
63 exit 0
64 }
65
66 #
67 # Output n version.
68 #
69
70 display_n_version() {
71 echo $VERSION && exit 0
72 }
73
3017360 @tj installing
authored
74 #
0594bb0 @tj Added display_current_version()
authored
75 # Display current node --version
76 #
77
78 display_current_version() {
79 if test `which node`; then
80 local version=`node --version`
81 echo " current: $version"
82 else
83 echo " current: none"
84 fi
85 }
86
87 #
88 # Display current node --version
89 # and others installed.
90 #
91
92 display_versions() {
93 display_current_version
94 }
95
96 #
3017360 @tj installing
authored
97 # Install node <version>
98 #
99
100 install_node() {
101 local version=$1
102
103 # remove "v"
104 if test "${version:0:1}" = "v"; then
105 version=${version:1:${#version}}
106 fi
107
1abb735 @tj Added cleanup()
authored
108 # install
109 local dir="node-v$version"
3d10d31 @tj Added N_PREFIX support
authored
110 cd $PREFIX/n \
3017360 @tj installing
authored
111 && $GET "http://nodejs.org/dist/node-v$version.tar.gz" \
1abb735 @tj Added cleanup()
authored
112 > "$dir.tar.gz" \
113 && tar -zxf "$dir.tar.gz" \
114 && cd $dir \
3d10d31 @tj Added N_PREFIX support
authored
115 && ./configure --prefix $VERSIONS_DIR/$version\
ed8f539 @tj typo
authored
116 && make install \
1abb735 @tj Added cleanup()
authored
117 && cd .. \
118 && cleanup $version
119 }
120
121 #
122 # Cleanup after the given <version>
123 #
124
125 cleanup() {
126 local version=$1
127 local dir="node-v$version"
128
129 if test -d $dir; then
130 log "removing source"
131 rm -fr $dir
132 fi
133
cb5e54b @tj typos
authored
134 if test -f "$dir.tar.gz"; then
1abb735 @tj Added cleanup()
authored
135 log "removing tarball"
136 rm -fr "$dir.tar.gz"
137 fi
3017360 @tj installing
authored
138 }
139
1667a31 @tj Started n(1)
authored
140 # Handle arguments
141
142 if test $# -eq 0; then
143 display_versions
144 else
145 while test $# -ne 0; do
146 case $1 in
147 -V|--version) display_n_version ;;
148 -h|--help) display_help ;;
3017360 @tj installing
authored
149 *) install_node $1 ;;
1667a31 @tj Started n(1)
authored
150 esac
151 shift
152 done
153 fi
Something went wrong with that request. Please try again.