33a9abb7e0e9061ef32828c8c8ace1f77e48318f
[webi-installers/.git] / node / node.bash
1 #!/bin/bash
2
3 # title: Node.js
4 # homepage: https://nodejs.org
5 # tagline: JavaScript V8 runtime
6 # description: |
7 #   Node.jsĀ® is a JavaScript runtime built on Chrome's V8 JavaScript engine
8
9 set -e
10 set -u
11
12 NODEJS_VER=${WEBI_VERSION:-}
13 NODEJS_VER="${NODEJS_VER:-v}" # Search for 'v' at the least
14
15 # WEBI_ARCH uses only slightly different names from NODE_ARCH
16 NODE_OS="${WEBI_OS}" # linux or darwin
17 NODE_ARCH="${WEBI_ARCH}"
18 if [ "amd64" == "$NODE_ARCH" ]; then
19   NODE_ARCH="x64"
20 fi
21
22 my_tmp="$WEBI_TMP"
23 sudo_cmd="$WEBI_SUDO"
24
25 #########
26 # BEGIN #
27 #########
28
29 get_node_version() {
30   # sort -rV    # will sort by version number, but it appears these are already sorted
31   # tail -n +2  # starts at line two (1-indexed) and all after (omits the csv header with 'version' and such)
32   # cut -f 1    # gets only the first column
33   # head -n 1   # gets only the most recent version
34   my_char="."
35   my_count=$(awk -F"${my_char}" '{print NF-1}' <<< "${NODEJS_VER}")
36   # get the latest version if partial
37   if [ $my_count -ne 2 ]; then
38     if [ "$NODEJS_VER" != "v" ]; then
39       NODEJS_VER="$NODEJS_VER\\."
40     fi
41     if [ -n "$(type -p curl)" ]; then
42       NODEJS_VER=$(curl -fsL "https://nodejs.org/dist/index.tab" | tail -n +2 | cut -f 1 | grep "^$NODEJS_VER" | head -n 1) \
43         || echo 'error automatically determining current node.js version'
44     elif [ -n "$(type -p wget)" ]; then
45       NODEJS_VER=$(wget --quiet "https://nodejs.org/dist/index.tab" -O - | tail -n +2 | cut -f 1 | grep "^$NODEJS_VER" | head -n 1) \
46         || echo 'error automatically determining current node.js version'
47     else
48       echo "Found neither 'curl' nor 'wget'. Can't Continue."
49       exit 1
50     fi
51   fi
52 }
53
54 get_node_version
55
56 #
57 # node
58 #
59 node_install_path=$HOME/.local/opt/node-${NODEJS_VER}
60 mkdir -p $node_install_path
61
62 # TODO warn if existing node in path my take precedence
63 if [ -e "$node_install_path/bin/node" ]; then
64   # node of some version is already installed
65   if [ "${NODEJS_VER}" == "$($node_install_path/bin/node -v 2>/dev/null)" ]; then
66     echo node ${NODEJS_VER} already installed at $node_install_path
67     exit 0
68   fi
69 fi
70
71 NODEJS_REMOTE="https://nodejs.org/dist/${NODEJS_VER}/node-${NODEJS_VER}-${NODE_OS}-${NODE_ARCH}.tar.gz"
72 NODEJS_LOCAL="$my_tmp/node-${NODEJS_VER}-${NODE_OS}-${NODE_ARCH}.tar.gz"
73 NODEJS_UNTAR="$my_tmp/node-${NODEJS_VER}-${NODE_OS}-${NODE_ARCH}"
74
75 echo "installing node as node ${NODEJS_VER}..."
76
77 if [ -n "$(command -v curl 2>/dev/null | grep curl)" ]; then
78   curl -fsSL ${NODEJS_REMOTE} -o ${NODEJS_LOCAL} || echo 'error downloading node'
79 elif [ -n "$(command -v wget 2>/dev/null | grep wget)" ]; then
80   wget --quiet ${NODEJS_REMOTE} -O ${NODEJS_LOCAL} || echo 'error downloading node'
81 else
82   echo "'wget' and 'curl' are missing. Please run the following command and try again"
83   echo "    sudo apt-get install --yes curl wget"
84   exit 1
85 fi
86
87 mkdir -p ${NODEJS_UNTAR}/
88 # --strip-components isn't portable, switch to portable version by performing move step after untar
89 tar xf ${NODEJS_LOCAL} -C ${NODEJS_UNTAR}/ #--strip-components=1
90 mv ${NODEJS_UNTAR}/node-${NODEJS_VER}-${NODE_OS}-${NODE_ARCH}/* ${NODEJS_UNTAR}/
91 rm -rf ${NODEJS_UNTAR}/node-${NODEJS_VER}-${NODE_OS}-${NODE_ARCH} # clean up the temporary unzip folder
92 rm ${NODEJS_UNTAR}/{LICENSE,CHANGELOG.md,README.md}
93 if [ -n "$(command -v rsync 2>/dev/null | grep rsync)" ]; then
94   echo $sudo_cmd rsync -Krl "${NODEJS_UNTAR}/" "$node_install_path/"
95   rsync -Krl "${NODEJS_UNTAR}/" "$node_install_path/" 2>/dev/null || $sudo_cmd rsync -Krl "${NODEJS_UNTAR}/" "$node_install_path/"
96 else
97   # due to symlink issues on Arch Linux, don't copy the share directory
98   rm -rf ${NODEJS_UNTAR}/share
99   echo $sudo_cmd cp -Hr "${NODEJS_UNTAR}/*" "$node_install_path/"
100   cp -Hr "${NODEJS_UNTAR}"/* "$node_install_path/" 2>/dev/null || $sudo_cmd cp -Hr "${NODEJS_UNTAR}"/* "$node_install_path/"
101 fi
102 echo ""
103 rm -rf "${NODEJS_UNTAR}"
104
105 chown -R $(whoami) "$node_install_path/lib/node_modules/" 2>/dev/null || $sudo_cmd chown -R $(whoami) "$node_install_path/lib/node_modules/"
106 chown $(whoami) "$node_install_path"/bin/ 2>/dev/null || $sudo_cmd chown $(whoami) "$node_install_path"/bin/
107
108 mkdir -p $node_install_path/lib/node_modules 2> /dev/null || $sudo_cmd mkdir -p $node_install_path/lib/node_modules
109 chown -R $(whoami) $node_install_path/lib/node_modules 2> /dev/null || $sudo_cmd chown -R $(whoami) $node_install_path/lib/node_modules
110
111 # By default, npm is stupid and uses any version of node in any path. Stop that.
112 # npm config set scripts-prepend-node-path true
113 "$node_install_path"/bin/node "$node_install_path"/bin/npm --scripts-prepend-node-path=true config set scripts-prepend-node-path true
114
115 #######
116 # END #
117 #######
118
119 pathman add $node_install_path/bin