Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 163 lines (123 sloc) 2.41 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`
f1be3e1 @tj Displaying installed versions
authored
81 echo " * ${version:1:${#version}}"
0594bb0 @tj Added display_current_version()
authored
82 fi
83 }
84
85 #
f1be3e1 @tj Displaying installed versions
authored
86 # Display installed versions
87 #
88
89 display_installed_versions() {
90 for file in $VERSIONS_DIR/*; do
91 local version=${file##*/}
92 echo " - $version"
93 done
94 }
95
96 #
0594bb0 @tj Added display_current_version()
authored
97 # Display current node --version
98 # and others installed.
99 #
100
101 display_versions() {
102 display_current_version
f1be3e1 @tj Displaying installed versions
authored
103 display_installed_versions
0594bb0 @tj Added display_current_version()
authored
104 }
105
106 #
3017360 @tj installing
authored
107 # Install node <version>
108 #
109
110 install_node() {
111 local version=$1
112
113 # remove "v"
114 if test "${version:0:1}" = "v"; then
115 version=${version:1:${#version}}
116 fi
117
1abb735 @tj Added cleanup()
authored
118 # install
119 local dir="node-v$version"
3d10d31 @tj Added N_PREFIX support
authored
120 cd $PREFIX/n \
3017360 @tj installing
authored
121 && $GET "http://nodejs.org/dist/node-v$version.tar.gz" \
1abb735 @tj Added cleanup()
authored
122 > "$dir.tar.gz" \
123 && tar -zxf "$dir.tar.gz" \
124 && cd $dir \
3d10d31 @tj Added N_PREFIX support
authored
125 && ./configure --prefix $VERSIONS_DIR/$version\
ed8f539 @tj typo
authored
126 && make install \
1abb735 @tj Added cleanup()
authored
127 && cd .. \
128 && cleanup $version
129 }
130
131 #
132 # Cleanup after the given <version>
133 #
134
135 cleanup() {
136 local version=$1
137 local dir="node-v$version"
138
139 if test -d $dir; then
140 log "removing source"
141 rm -fr $dir
142 fi
143
cb5e54b @tj typos
authored
144 if test -f "$dir.tar.gz"; then
1abb735 @tj Added cleanup()
authored
145 log "removing tarball"
146 rm -fr "$dir.tar.gz"
147 fi
3017360 @tj installing
authored
148 }
149
1667a31 @tj Started n(1)
authored
150 # Handle arguments
151
152 if test $# -eq 0; then
153 display_versions
154 else
155 while test $# -ne 0; do
156 case $1 in
157 -V|--version) display_n_version ;;
158 -h|--help) display_help ;;
3017360 @tj installing
authored
159 *) install_node $1 ;;
1667a31 @tj Started n(1)
authored
160 esac
161 shift
162 done
163 fi
Something went wrong with that request. Please try again.