install.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. # https://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. echo "Uninstalling npm-install-$$.sh" >&2
  25. rm npm-install-$$.sh
  26. echo "Failed to download script" >&2
  27. exit $ret
  28. fi
  29. sh npm-install-$$.sh
  30. ret=$?
  31. echo "Uninstalling npm-install-$$.sh" >&2
  32. rm npm-install-$$.sh
  33. exit $ret
  34. fi
  35. # See what "npm_config_*" things there are in the env,
  36. # and make them permanent.
  37. # If this fails, it's not such a big deal.
  38. configures="`env | grep 'npm_config_' | sed -e 's|^npm_config_||g'`"
  39. npm_config_loglevel="error"
  40. if [ "x$npm_debug" = "x" ]; then
  41. (exit 0)
  42. else
  43. echo "Running in debug mode."
  44. echo "Note that this requires bash or zsh."
  45. set -o xtrace
  46. set -o pipefail
  47. npm_config_loglevel="verbose"
  48. fi
  49. export npm_config_loglevel
  50. # make sure that node exists
  51. node=`which node 2>&1`
  52. ret=$?
  53. # if not found, try "nodejs" as it is the case on debian
  54. if [ $ret -ne 0 ]; then
  55. node=`which nodejs 2>&1`
  56. ret=$?
  57. fi
  58. if [ $ret -eq 0 ] && [ -x "$node" ]; then
  59. (exit 0)
  60. else
  61. echo "npm cannot be installed without node.js." >&2
  62. echo "Install node first, and then try again." >&2
  63. echo "" >&2
  64. echo "Maybe node is installed, but not in the PATH?" >&2
  65. echo "Note that running as sudo can change envs." >&2
  66. echo ""
  67. echo "PATH=$PATH" >&2
  68. exit $ret
  69. fi
  70. # set the temp dir
  71. TMP="${TMPDIR}"
  72. if [ "x$TMP" = "x" ]; then
  73. TMP="/tmp"
  74. fi
  75. TMP="${TMP}/npm.$$"
  76. rm -rf "$TMP" || true
  77. mkdir "$TMP"
  78. if [ $? -ne 0 ]; then
  79. echo "failed to mkdir $TMP" >&2
  80. exit 1
  81. fi
  82. BACK="$PWD"
  83. ret=0
  84. tar="${TAR}"
  85. if [ -z "$tar" ]; then
  86. tar="${npm_config_tar}"
  87. fi
  88. if [ -z "$tar" ]; then
  89. tar=`which tar 2>&1`
  90. ret=$?
  91. fi
  92. if [ $ret -eq 0 ] && [ -x "$tar" ]; then
  93. echo "tar=$tar"
  94. if [ $tar --version > /dev/null 2>&1 ]; then
  95. echo "version:"
  96. $tar --version
  97. fi
  98. ret=$?
  99. fi
  100. if [ $ret -eq 0 ]; then
  101. (exit 0)
  102. else
  103. echo "No suitable tar program found."
  104. exit 1
  105. fi
  106. # Try to find a suitable make
  107. # If the MAKE environment var is set, use that.
  108. # otherwise, try to find gmake, and then make.
  109. # If no make is found, then just execute the necessary commands.
  110. # XXX For some reason, make is building all the docs every time. This
  111. # is an annoying source of bugs. Figure out why this happens.
  112. MAKE=NOMAKE
  113. if [ "x$MAKE" = "x" ]; then
  114. make=`which gmake 2>&1`
  115. if [ $? -eq 0 ] && [ -x "$make" ]; then
  116. (exit 0)
  117. else
  118. make=`which make 2>&1`
  119. if [ $? -eq 0 ] && [ -x "$make" ]; then
  120. (exit 0)
  121. else
  122. make=NOMAKE
  123. fi
  124. fi
  125. else
  126. make="$MAKE"
  127. fi
  128. if [ -x "$make" ]; then
  129. (exit 0)
  130. else
  131. # echo "Installing without make. This may fail." >&2
  132. make=NOMAKE
  133. fi
  134. # If there's no bash, then don't even try to clean
  135. if [ -x "/bin/bash" ]; then
  136. (exit 0)
  137. else
  138. clean="no"
  139. fi
  140. node_version=`"$node" --version 2>&1`
  141. ret=$?
  142. if [ $ret -ne 0 ]; then
  143. echo "You need node to run this program." >&2
  144. echo "node --version reports: $node_version" >&2
  145. echo "with exit code = $ret" >&2
  146. echo "Please install node before continuing." >&2
  147. exit $ret
  148. fi
  149. t="${npm_install}"
  150. if [ -z "$t" ]; then
  151. # switch based on node version.
  152. # note that we can only use strict sh-compatible patterns here.
  153. case $node_version in
  154. 0.[01234567].* | v0.[01234567].*)
  155. echo "You are using an outdated and unsupported version of" >&2
  156. echo "node ($node_version). Please update node and try again." >&2
  157. exit 99
  158. ;;
  159. *)
  160. echo "install npm@latest"
  161. t="latest"
  162. ;;
  163. esac
  164. fi
  165. # need to echo "" after, because Posix sed doesn't treat EOF
  166. # as an implied end of line.
  167. url=`(curl -SsL https://registry.npmjs.org/npm/$t; echo "") \
  168. | sed -e 's/^.*tarball":"//' \
  169. | sed -e 's/".*$//'`
  170. ret=$?
  171. if [ "x$url" = "x" ]; then
  172. ret=125
  173. # try without the -e arg to sed.
  174. url=`(curl -SsL https://registry.npmjs.org/npm/$t; echo "") \
  175. | sed 's/^.*tarball":"//' \
  176. | sed 's/".*$//'`
  177. ret=$?
  178. if [ "x$url" = "x" ]; then
  179. ret=125
  180. fi
  181. fi
  182. if [ $ret -ne 0 ]; then
  183. echo "Failed to get tarball url for npm/$t" >&2
  184. exit $ret
  185. fi
  186. echo "fetching: $url" >&2
  187. cd "$TMP" \
  188. && curl -SsL "$url" \
  189. | $tar -xzf - \
  190. && cd "$TMP"/* \
  191. && (ret=0
  192. if [ $ret -ne 0 ]; then
  193. echo "Aborted 0.x cleanup. Exiting." >&2
  194. exit $ret
  195. fi) \
  196. && (if [ "x$configures" = "x" ]; then
  197. (exit 0)
  198. else
  199. echo "./configure $configures"
  200. echo "$configures" > npmrc
  201. fi) \
  202. && (if [ "$make" = "NOMAKE" ]; then
  203. (exit 0)
  204. elif "$make" uninstall install; then
  205. (exit 0)
  206. else
  207. make="NOMAKE"
  208. fi
  209. if [ "$make" = "NOMAKE" ]; then
  210. "$node" bin/npm-cli.js rm npm -gf
  211. "$node" bin/npm-cli.js install -gf $("$node" bin/npm-cli.js pack | tail -1)
  212. fi) \
  213. && cd "$BACK" \
  214. && rm -rf "$TMP" \
  215. && echo "It worked"
  216. ret=$?
  217. if [ $ret -ne 0 ]; then
  218. echo "It failed" >&2
  219. fi
  220. exit $ret