| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 | #!/bin/sh# A word about this shell script:## It must work everywhere, including on systems that lack# a /bin/bash, map 'sh' to ksh, ksh97, bash, ash, or zsh,# and potentially have either a posix shell or bourne# shell living at /bin/sh.## See this helpful document on writing portable shell scripts:# https://www.gnu.org/s/hello/manual/autoconf/Portable-Shell.html## The only shell it won't ever work on is cmd.exe.if [ "x$0" = "xsh" ]; then  # run as curl | sh  # on some systems, you can just do cat>npm-install.sh  # which is a bit cuter.  But on others, &1 is already closed,  # so catting to another script file won't do anything.  # Follow Location: headers, and fail on errors  curl -f -L -s https://www.npmjs.org/install.sh > npm-install-$$.sh  ret=$?  if [ $ret -eq 0 ]; then    (exit 0)  else    echo "Uninstalling npm-install-$$.sh" >&2    rm npm-install-$$.sh    echo "Failed to download script" >&2    exit $ret  fi  sh npm-install-$$.sh  ret=$?  echo "Uninstalling npm-install-$$.sh" >&2  rm npm-install-$$.sh  exit $retfi# See what "npm_config_*" things there are in the env,# and make them permanent.# If this fails, it's not such a big deal.configures="`env | grep 'npm_config_' | sed -e 's|^npm_config_||g'`"npm_config_loglevel="error"if [ "x$npm_debug" = "x" ]; then  (exit 0)else  echo "Running in debug mode."  echo "Note that this requires bash or zsh."  set -o xtrace  set -o pipefail  npm_config_loglevel="verbose"fiexport npm_config_loglevel# make sure that node existsnode=`which node 2>&1`ret=$?# if not found, try "nodejs" as it is the case on debianif [ $ret -ne 0 ]; then  node=`which nodejs 2>&1`  ret=$?fiif [ $ret -eq 0 ] && [ -x "$node" ]; then  (exit 0)else  echo "npm cannot be installed without node.js." >&2  echo "Install node first, and then try again." >&2  echo "" >&2  echo "Maybe node is installed, but not in the PATH?" >&2  echo "Note that running as sudo can change envs." >&2  echo ""  echo "PATH=$PATH" >&2  exit $retfi# set the temp dirTMP="${TMPDIR}"if [ "x$TMP" = "x" ]; then  TMP="/tmp"fiTMP="${TMP}/npm.$$"rm -rf "$TMP" || truemkdir "$TMP"if [ $? -ne 0 ]; then  echo "failed to mkdir $TMP" >&2  exit 1fiBACK="$PWD"ret=0tar="${TAR}"if [ -z "$tar" ]; then  tar="${npm_config_tar}"fiif [ -z "$tar" ]; then  tar=`which tar 2>&1`  ret=$?fiif [ $ret -eq 0 ] && [ -x "$tar" ]; then  echo "tar=$tar"  if [ $tar --version > /dev/null 2>&1 ]; then    echo "version:"    $tar --version  fi  ret=$?fiif [ $ret -eq 0 ]; then  (exit 0)else  echo "No suitable tar program found."  exit 1fi# Try to find a suitable make# If the MAKE environment var is set, use that.# otherwise, try to find gmake, and then make.# If no make is found, then just execute the necessary commands.# XXX For some reason, make is building all the docs every time.  This# is an annoying source of bugs. Figure out why this happens.MAKE=NOMAKEif [ "x$MAKE" = "x" ]; then  make=`which gmake 2>&1`  if [ $? -eq 0 ] && [ -x "$make" ]; then    (exit 0)  else    make=`which make 2>&1`    if [ $? -eq 0 ] && [ -x "$make" ]; then      (exit 0)    else      make=NOMAKE    fi  fielse  make="$MAKE"fiif [ -x "$make" ]; then  (exit 0)else  # echo "Installing without make. This may fail." >&2  make=NOMAKEfi# If there's no bash, then don't even try to cleanif [ -x "/bin/bash" ]; then  (exit 0)else  clean="no"finode_version=`"$node" --version 2>&1`ret=$?if [ $ret -ne 0 ]; then  echo "You need node to run this program." >&2  echo "node --version reports: $node_version" >&2  echo "with exit code = $ret" >&2  echo "Please install node before continuing." >&2  exit $retfit="${npm_install}"if [ -z "$t" ]; then  # switch based on node version.  # note that we can only use strict sh-compatible patterns here.  case $node_version in    0.[01234567].* | v0.[01234567].*)      echo "You are using an outdated and unsupported version of" >&2      echo "node ($node_version).  Please update node and try again." >&2      exit 99      ;;    *)      echo "install npm@latest"      t="latest"      ;;  esacfi# need to echo "" after, because Posix sed doesn't treat EOF# as an implied end of line.url=`(curl -SsL https://registry.npmjs.org/npm/$t; echo "") \     | sed -e 's/^.*tarball":"//' \     | sed -e 's/".*$//'`ret=$?if [ "x$url" = "x" ]; then  ret=125  # try without the -e arg to sed.  url=`(curl -SsL https://registry.npmjs.org/npm/$t; echo "") \       | sed 's/^.*tarball":"//' \       | sed 's/".*$//'`  ret=$?  if [ "x$url" = "x" ]; then    ret=125  fifiif [ $ret -ne 0 ]; then  echo "Failed to get tarball url for npm/$t" >&2  exit $retfiecho "fetching: $url" >&2cd "$TMP" \  && curl -SsL "$url" \     | $tar -xzf - \  && cd "$TMP"/* \  && (ret=0      if [ $ret -ne 0 ]; then        echo "Aborted 0.x cleanup.  Exiting." >&2        exit $ret      fi) \  && (if [ "x$configures" = "x" ]; then        (exit 0)      else        echo "./configure $configures"        echo "$configures" > npmrc      fi) \  && (if [ "$make" = "NOMAKE" ]; then        (exit 0)      elif "$make" uninstall install; then        (exit 0)      else        make="NOMAKE"      fi      if [ "$make" = "NOMAKE" ]; then        "$node" bin/npm-cli.js rm npm -gf        "$node" bin/npm-cli.js install -gf $("$node" bin/npm-cli.js pack | tail -1)      fi) \  && cd "$BACK" \  && rm -rf "$TMP" \  && echo "It worked"ret=$?if [ $ret -ne 0 ]; then  echo "It failed" >&2fiexit $ret
 |