installnode.sh 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/usr/bin/env bash
  2. # Cause the script to fail if any subcommand fails
  3. set -e
  4. if type -P "node" &>/dev/null; then
  5. echo "node is in \$PATH"
  6. exit
  7. fi
  8. node_version=$1
  9. arch=$2
  10. osname=`uname -s`
  11. if [ "$osname" = "Darwin" ]; then
  12. platformarch="darwin-$arch"
  13. else
  14. platformarch="linux-$arch"
  15. fi
  16. echo "PlatformArch: $platformarch"
  17. DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  18. output_dir="$DIR/node"
  19. url="http://nodejs.org/dist/v$node_version/node-v$node_version-$platformarch.tar.gz"
  20. echo "Downloading from: $url"
  21. tmp="$(mktemp -d -t install-node.XXXXXX)"
  22. cleanup() {
  23. exitcode=$?
  24. if [ $exitcode -ne 0 ]; then
  25. echo "Failed to install node with exit code: $exitcode"
  26. fi
  27. rm -rf "$tmp"
  28. exit $exitcode
  29. }
  30. trap "cleanup" EXIT
  31. cd "$tmp"
  32. curl -Lsfo $(basename $url) "$url" --retry 5
  33. echo "Installing node from $(basename $url) $url"
  34. mkdir $output_dir
  35. echo "Unpacking to $output_dir"
  36. tar --strip-components 1 -xzf "node-v$node_version-$platformarch.tar.gz" --no-same-owner --directory "$output_dir"