4 # homepage: https://nodejs.org
5 # tagline: JavaScript V8 runtime
7 # Node.jsĀ® is a JavaScript runtime built on Chrome's V8 JavaScript engine
12 NODEJS_VER=${WEBI_VERSION:-}
13 NODEJS_VER="${NODEJS_VER:-v}" # Search for 'v' at the least
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
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
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\\."
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'
48 echo "Found neither 'curl' nor 'wget'. Can't Continue."
59 node_install_path=$HOME/.local/opt/node-${NODEJS_VER}
60 mkdir -p $node_install_path
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
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}"
75 echo "installing node as node ${NODEJS_VER}..."
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'
82 echo "'wget' and 'curl' are missing. Please run the following command and try again"
83 echo " sudo apt-get install --yes curl wget"
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/"
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/"
103 rm -rf "${NODEJS_UNTAR}"
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/
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
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
119 pathman add $node_install_path/bin