Skip to content

HTTPS clone URL

Subversion checkout URL

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