npm-install.sh 6.1 KB

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