UNPKG

8.01 kBapplication/x-shView Raw
1#!/bin/sh
2
3# A word about this shell script:
4#
5# It must work everywhere, including on systems that lack
6# a /bin/bash, map 'sh' to ksh, ksh97, bash, ash, or zsh,
7# and potentially have either a posix shell or bourne
8# shell living at /bin/sh.
9#
10# See this helpful document on writing portable shell scripts:
11# http://www.gnu.org/s/hello/manual/autoconf/Portable-Shell.html
12#
13# The only shell it won't ever work on is cmd.exe.
14
15if [ "x$0" = "xsh" ]; then
16 # run as curl | sh
17 # on some systems, you can just do cat>npm-install.sh
18 # which is a bit cuter. But on others, &1 is already closed,
19 # so catting to another script file won't do anything.
20 # Follow Location: headers, and fail on errors
21 curl -f -L -s https://www.npmjs.org/install.sh > npm-install-$$.sh
22 ret=$?
23 if [ $ret -eq 0 ]; then
24 (exit 0)
25 else
26 rm npm-install-$$.sh
27 echo "Failed to download script" >&2
28 exit $ret
29 fi
30 sh npm-install-$$.sh
31 ret=$?
32 rm npm-install-$$.sh
33 exit $ret
34fi
35
36# See what "npm_config_*" things there are in the env,
37# and make them permanent.
38# If this fails, it's not such a big deal.
39configures="`env | grep 'npm_config_' | sed -e 's|^npm_config_||g'`"
40
41npm_config_loglevel="error"
42if [ "x$npm_debug" = "x" ]; then
43 (exit 0)
44else
45 echo "Running in debug mode."
46 echo "Note that this requires bash or zsh."
47 set -o xtrace
48 set -o pipefail
49 npm_config_loglevel="verbose"
50fi
51export npm_config_loglevel
52
53# make sure that node exists
54node=`which node 2>&1`
55ret=$?
56if [ $ret -eq 0 ] && [ -x "$node" ]; then
57 (exit 0)
58else
59 echo "npm cannot be installed without nodejs." >&2
60 echo "Install node first, and then try again." >&2
61 echo "" >&2
62 echo "Maybe node is installed, but not in the PATH?" >&2
63 echo "Note that running as sudo can change envs." >&2
64 echo ""
65 echo "PATH=$PATH" >&2
66 exit $ret
67fi
68
69# set the temp dir
70TMP="${TMPDIR}"
71if [ "x$TMP" = "x" ]; then
72 TMP="/tmp"
73fi
74TMP="${TMP}/npm.$$"
75rm -rf "$TMP" || true
76mkdir "$TMP"
77if [ $? -ne 0 ]; then
78 echo "failed to mkdir $TMP" >&2
79 exit 1
80fi
81
82BACK="$PWD"
83
84ret=0
85tar="${TAR}"
86if [ -z "$tar" ]; then
87 tar="${npm_config_tar}"
88fi
89if [ -z "$tar" ]; then
90 tar=`which tar 2>&1`
91 ret=$?
92fi
93
94if [ $ret -eq 0 ] && [ -x "$tar" ]; then
95 echo "tar=$tar"
96 echo "version:"
97 $tar --version
98 ret=$?
99fi
100
101if [ $ret -eq 0 ]; then
102 (exit 0)
103else
104 echo "No suitable tar program found."
105 exit 1
106fi
107
108
109
110# Try to find a suitable make
111# If the MAKE environment var is set, use that.
112# otherwise, try to find gmake, and then make.
113# If no make is found, then just execute the necessary commands.
114
115# XXX For some reason, make is building all the docs every time. This
116# is an annoying source of bugs. Figure out why this happens.
117MAKE=NOMAKE
118
119if [ "x$MAKE" = "x" ]; then
120 make=`which gmake 2>&1`
121 if [ $? -eq 0 ] && [ -x $make ]; then
122 (exit 0)
123 else
124 make=`which make 2>&1`
125 if [ $? -eq 0 ] && [ -x $make ]; then
126 (exit 0)
127 else
128 make=NOMAKE
129 fi
130 fi
131else
132 make="$MAKE"
133fi
134
135if [ -x "$make" ]; then
136 (exit 0)
137else
138 # echo "Installing without make. This may fail." >&2
139 make=NOMAKE
140fi
141
142# If there's no bash, then don't even try to clean
143if [ -x "/bin/bash" ]; then
144 (exit 0)
145else
146 clean="no"
147fi
148
149node_version=`"$node" --version 2>&1`
150ret=$?
151if [ $ret -ne 0 ]; then
152 echo "You need node to run this program." >&2
153 echo "node --version reports: $node_version" >&2
154 echo "with exit code = $ret" >&2
155 echo "Please install node before continuing." >&2
156 exit $ret
157fi
158
159t="${npm_install}"
160if [ -z "$t" ]; then
161 # switch based on node version.
162 # note that we can only use strict sh-compatible patterns here.
163 case $node_version in
164 0.[012345].* | v0.[012345].*)
165 echo "You are using an outdated and unsupported version of" >&2
166 echo "node ($node_version). Please update node and try again." >&2
167 exit 99
168 ;;
169 v0.[678].* | 0.[678].*)
170 echo "install npm@1.1"
171 t=1.1
172 ;;
173 *)
174 echo "install npm@latest"
175 t="latest"
176 ;;
177 esac
178fi
179
180# the npmca cert
181cacert='
182-----BEGIN CERTIFICATE-----
183MIIChzCCAfACCQDauvz/KHp8ejANBgkqhkiG9w0BAQUFADCBhzELMAkGA1UEBhMC
184VVMxCzAJBgNVBAgTAkNBMRAwDgYDVQQHEwdPYWtsYW5kMQwwCgYDVQQKEwNucG0x
185IjAgBgNVBAsTGW5wbSBDZXJ0aWZpY2F0ZSBBdXRob3JpdHkxDjAMBgNVBAMTBW5w
186bUNBMRcwFQYJKoZIhvcNAQkBFghpQGl6cy5tZTAeFw0xMTA5MDUwMTQ3MTdaFw0y
187MTA5MDIwMTQ3MTdaMIGHMQswCQYDVQQGEwJVUzELMAkGA1UECBMCQ0ExEDAOBgNV
188BAcTB09ha2xhbmQxDDAKBgNVBAoTA25wbTEiMCAGA1UECxMZbnBtIENlcnRpZmlj
189YXRlIEF1dGhvcml0eTEOMAwGA1UEAxMFbnBtQ0ExFzAVBgkqhkiG9w0BCQEWCGlA
190aXpzLm1lMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDLI4tIqPpRW+ACw9GE
191OgBlJZwK5f8nnKCLK629Pv5yJpQKs3DENExAyOgDcyaF0HD0zk8zTp+ZsLaNdKOz
192Gn2U181KGprGKAXP6DU6ByOJDWmTlY6+Ad1laYT0m64fERSpHw/hjD3D+iX4aMOl
193y0HdbT5m1ZGh6SJz3ZqxavhHLQIDAQABMA0GCSqGSIb3DQEBBQUAA4GBAC4ySDbC
194l7W1WpLmtLGEQ/yuMLUf6Jy/vr+CRp4h+UzL+IQpCv8FfxsYE7dhf/bmWTEupBkv
195yNL18lipt2jSvR3v6oAHAReotvdjqhxddpe5Holns6EQd1/xEZ7sB1YhQKJtvUrl
196ZNufy1Jf1r0ldEGeA+0ISck7s+xSh9rQD2Op
197-----END CERTIFICATE-----
198'
199
200echo "$cacert" > "$TMP/cafile.crt"
201cacert="$TMP/cafile.crt"
202
203# need to echo "" after, because Posix sed doesn't treat EOF
204# as an implied end of line.
205url=`(curl -SsL --cacert "$cacert" https://registry.npmjs.org/npm/$t; echo "") \
206 | sed -e 's/^.*tarball":"//' \
207 | sed -e 's/".*$//'`
208
209ret=$?
210if [ "x$url" = "x" ]; then
211 ret=125
212 # try without the -e arg to sed.
213 url=`(curl -SsL --cacert "$cacert" https://registry.npmjs.org/npm/$t; echo "") \
214 | sed 's/^.*tarball":"//' \
215 | sed 's/".*$//'`
216 ret=$?
217 if [ "x$url" = "x" ]; then
218 ret=125
219 fi
220fi
221if [ $ret -ne 0 ]; then
222 echo "Failed to get tarball url for npm/$t" >&2
223 exit $ret
224fi
225
226
227echo "fetching: $url" >&2
228
229cd "$TMP" \
230 && curl -SsL --cacert "$cacert" "$url" \
231 | $tar -xzf - \
232 && rm "$cacert" \
233 && cd "$TMP"/* \
234 && (req=`"$node" bin/read-package-json.js package.json engines.node`
235 if [ -d node_modules ]; then
236 "$node" node_modules/semver/bin/semver -v "$node_version" -r "$req"
237 ret=$?
238 else
239 "$node" bin/semver.js -v "$node_version" -r "$req"
240 ret=$?
241 fi
242 if [ $ret -ne 0 ]; then
243 echo "You need node $req to run this program." >&2
244 echo "node --version reports: $node_version" >&2
245 echo "Please upgrade node before continuing." >&2
246 exit $ret
247 fi) \
248 && (ver=`"$node" bin/read-package-json.js package.json version`
249 isnpm10=0
250 if [ $ret -eq 0 ]; then
251 req=`"$node" bin/read-package-json.js package.json engines.node`
252 if [ -d node_modules ]; then
253 if "$node" node_modules/semver/bin/semver -v "$ver" -r "1"
254 then
255 isnpm10=1
256 fi
257 else
258 if "$node" bin/semver -v "$ver" -r ">=1.0"; then
259 isnpm10=1
260 fi
261 fi
262 fi
263
264 ret=0
265 if [ $isnpm10 -eq 1 ] && [ -f "scripts/clean-old.sh" ]; then
266 if [ "x$skipclean" = "x" ]; then
267 (exit 0)
268 else
269 clean=no
270 fi
271 if [ "x$clean" = "xno" ] \
272 || [ "x$clean" = "xn" ]; then
273 echo "Skipping 0.x cruft clean" >&2
274 ret=0
275 elif [ "x$clean" = "xy" ] || [ "x$clean" = "xyes" ]; then
276 NODE="$node" /bin/bash "scripts/clean-old.sh" "-y"
277 ret=$?
278 else
279 NODE="$node" /bin/bash "scripts/clean-old.sh" </dev/tty
280 ret=$?
281 fi
282 fi
283
284 if [ $ret -ne 0 ]; then
285 echo "Aborted 0.x cleanup. Exiting." >&2
286 exit $ret
287 fi) \
288 && (if [ "x$configures" = "x" ]; then
289 (exit 0)
290 else
291 echo "./configure "$configures
292 echo "$configures" > npmrc
293 fi) \
294 && (if [ "$make" = "NOMAKE" ]; then
295 (exit 0)
296 elif "$make" uninstall install; then
297 (exit 0)
298 else
299 make="NOMAKE"
300 fi
301 if [ "$make" = "NOMAKE" ]; then
302 "$node" cli.js rm npm -gf
303 "$node" cli.js install -gf
304 fi) \
305 && cd "$BACK" \
306 && rm -rf "$TMP" \
307 && echo "It worked"
308
309ret=$?
310if [ $ret -ne 0 ]; then
311 echo "It failed" >&2
312fi
313exit $ret