use new bootstrap + webinstall approach
[webi-installers/.git] / node / node.bash
index 89cb7c17d760f6be66036ae0556df0e582153980..0d2a91715a2b35e898962ffd177bc7c695bd933d 100644 (file)
@@ -12,7 +12,7 @@
 #   ```
 #   <br/>
 #   <br/>
-#   
+#
 #   <table>
 #   <tr>
 #   <td>Run a webserver</td>
 #   <pre><code class="language-javascript">'use strict'
 #   var express = require('express');
 #   var app = express();
-#   
+#
 #   app.use('/', function (req, res, next) {
 #     res.end("Hello, World!");
 #   });
-#   
+#
 #   module.exports = app;</code></pre>
 #   <br/>
 #   <code>server.js:</code>
 set -e
 set -u
 
-my_tmp=${WEBI_TMP:-$(mktemp -d node-install.XXXXXX)}
-sudo_cmd=${WEBI_SUDO:-}
+##################
+#  Install node  #
+##################
 
-http_get() {
-  if [ -n "$(command -v curl)" ]; then
-    curl -fsSL $1 -o $2 || echo 'error downloading node'
-  elif [ -n "$(command -v wget)" ]; then
-    wget --quiet $1 -O $2 || echo 'error downloading node'
-  else
-    echo "'wget' and 'curl' are missing. Please run the following command and try again"
-    echo ""
-    echo "    sudo apt-get install --yes curl wget"
-    exit 1
-  fi
-}
+new_node_home="${HOME}/.local/opt/node-v${WEBI_VERSION}"
+new_node="${HOME}/.local/opt/node-v${WEBI_VERSION}/bin/node"
 
-WEBI_CSV=$(curl -fsSL "https://webinstall.dev/api/releases/node@${WEBI_VERSION:-}.csv?os=$(uname -s)&arch=$(uname -m)&ext=tar&limit=1" -H "User-Agent: $(uname -a)")
-NODEJS_VER=$(echo $WEBI_CSV | cut -d ',' -f 1)
-NODEJS_REMOTE=$(echo $WEBI_CSV | cut -d ',' -f 9)
-NODEJS_LOCAL="$my_tmp/$(echo $NODEJS_REMOTE | sed s:.*/::)"
-NODE_OS="$(echo $WEBI_CSV | cut -d ',' -f 5)"
-
-#########
-# BEGIN #
-#########
-
-# WEBI_ARCH uses only slightly different names from NODE_ARCH
-NODE_OS="$(echo $WEBI_CSV | cut -d ',' -f 5)"
-if [ "macos" == "$NODE_OS" ]; then
-  NODE_OS="darwin"
+# Test for existing version
+set +e
+cur_node="$(command -v node)"
+set -e
+if [ -e "$new_node_home/bin/node" ]; then
+    # node of some version is already installed
+    if [ "v${WEBI_VERSION}" == "$("$new_node_home/bin/node" -v 2>/dev/null)" ]; then
+        echo node v${WEBI_VERSION} already installed at $new_node_home
+        exit 0
+    fi
 fi
-NODE_ARCH="$(echo $WEBI_CSV | cut -d ',' -f 6)"
-if [ "amd64" == "$NODE_ARCH" ]; then
-  NODE_ARCH="x64"
+if [ "$cur_node" != "$new_node" ]; then
+    echo "WARN: possible conflict with node v$WEBI_VERSION at $cur_node"
 fi
 
-node_install_path=$HOME/.local/opt/node-v${NODEJS_VER}
-mkdir -p $node_install_path
+# Note: this file is `source`d by the true installer and hence will have the webi functions
 
-if [ -e "$node_install_path/bin/node" ]; then
-  # node of some version is already installed
-  if [ "v${NODEJS_VER}" == "$($node_install_path/bin/node -v 2>/dev/null)" ]; then
-    echo node ${NODEJS_VER} already installed at $node_install_path
-    exit 0
-  fi
-fi
+# because we created releases.js we can use webi_download()
+# downloads node to ~/Downloads
+webi_download
 
-# TODO warn if existing node in path my take precedence
+# because this is tar or zip, we can webi_extract()
+# extracts to the WEBI_TMP directory, raw (no --strip-prefix)
+webi_extract
 
-echo "downloading node v${NODEJS_VER}..."
-http_get ${NODEJS_REMOTE} ${NODEJS_LOCAL} || echo 'error downloading node'
+pushd "$WEBI_TMP" 2>&1 >/dev/null
+    echo Installing node v${WEBI_VERSION} as "$new_node"
 
-echo "installing node v${NODEJS_VER}..."
-tar xf ${NODEJS_LOCAL} -C $my_tmp/
-# we know how it'll unpack
-NODEJS_UNTAR=$my_tmp/node-v${NODEJS_VER}-${NODE_OS}-${NODE_ARCH}
+    # simpler for single-binary commands
+    #mv ./example*/bin/example "$HOME/.local/bin"
 
-# this funny business is to allow something a non-/opt directory
-# ( such as /usr/local ) to be an install target
-rm ${NODEJS_UNTAR}/{LICENSE,CHANGELOG.md,README.md}
-if [ -n "$(command -v rsync 2>/dev/null | grep rsync)" ]; then
-  echo $sudo_cmd rsync -Krl "${NODEJS_UNTAR}/" "$node_install_path/"
-  rsync -Krl "${NODEJS_UNTAR}/" "$node_install_path/" 2>/dev/null || $sudo_cmd rsync -Krl "${NODEJS_UNTAR}/" "$node_install_path/"
-else
-  # due to symlink issues on Arch Linux, don't copy the share directory
-  rm -rf ${NODEJS_UNTAR}/share
-  echo $sudo_cmd cp -Hr "${NODEJS_UNTAR}/*" "$node_install_path/"
-  cp -Hr "${NODEJS_UNTAR}"/* "$node_install_path/" 2>/dev/null || $sudo_cmd cp -Hr "${NODEJS_UNTAR}"/* "$node_install_path/"
-fi
-rm -rf "${NODEJS_UNTAR}"
-rm -rf "${my_tmp}"
+    # best for packages and toolchains
+    rm -rf "$new_node_home"
+    if [ -n "$(command -v rsync 2>/dev/null | grep rsync)" ]; then
+      rsync -Krl ./node*/ "$new_node_home/" 2>/dev/null
+    else
+      cp -Hr ./node*/* "$new_node_home/" 2>/dev/null
+      cp -Hr ./node*/.* "$new_node_home/" 2>/dev/null
+    fi
+popd 2>&1 >/dev/null
 
-# By default, npm is stupid and uses any version of node in any path. Stop that.
-# npm config set scripts-prepend-node-path true
-"$node_install_path"/bin/node "$node_install_path"/bin/npm --scripts-prepend-node-path=true config set scripts-prepend-node-path true
+###################
+#   Update PATH   #
+###################
 
-#######
-# END #
-#######
+# TODO get better output from pathman / output the path to add as return to webi bootstrap
+webi_path_add "$new_node_home/bin"
 
-pathman add $node_install_path/bin
+echo "Installed 'node' and 'npm'"
+echo ""