improve output when re-installing or switching node versions
[webi-installers/.git] / node / install.bash
index 251022dff592e49d5d3f3794b504474b15e91b44..0302650da7dd6dc4a7ebd04fff62265066b3b67f 100644 (file)
@@ -14,6 +14,8 @@
 #   > Hello, World!
 #   ```
 #
+#   <br/>
+#
 #   ### A Simple Web Server
 #
 #   `server.js`:
@@ -28,6 +30,8 @@
 #   });
 #   ```
 #
+#   <br/>
+#
 #   ```bash
 #   node server.js
 #   ```
@@ -41,6 +45,8 @@
 #   npm install --save express
 #   ```
 #
+#   <br/>
+#
 #   `app.js`:
 #
 #   ```js
@@ -56,6 +62,8 @@
 #   module.exports = app;</code></pre>
 #   ```
 #
+#   <br/>
+#
 #   `server.js`:
 #
 #   ```js
@@ -69,6 +77,8 @@
 #   });
 #   ```
 #
+#   <br/>
+#
 #   ```bash
 #   npm start
 #   ```
@@ -81,23 +91,46 @@ set -u
 #  Install node  #
 ##################
 
+common_node_home="${HOME}/.local/opt/node"
 new_node_home="${HOME}/.local/opt/node-v${WEBI_VERSION}"
 new_node="${HOME}/.local/opt/node-v${WEBI_VERSION}/bin/node"
 
+update_node_home() {
+    rm -rf "$common_node_home"
+    ln -s "$new_node_home" "$common_node_home"
+
+    # TODO get better output from pathman / output the path to add as return to webi bootstrap
+    webi_path_add "$common_node_home/bin"
+}
+
+if [ -x "$new_node" ]; then
+  update_node_home
+  echo "switched to node v${WEBI_VERSION} at $new_node_home"
+  exit 0
+fi
+
 # 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
+cur_node_version=""
+if [ -n "$cur_node" ]; then
+  cur_node_version="$("$cur_node" -v 2>/dev/null)"
+  if [ "$cur_node_version" == "v${WEBI_VERSION}" ]; then
+    echo "node v${WEBI_VERSION} already installed at $cur_node"
+    exit 0
+  else
+    if [ "$cur_node" != "$common_node_home/bin/node" ]; then
+      echo "WARN: possible conflict between node v${WEBI_VERSION} and v${cur_node_version} at ${cur_node}"
     fi
+    if [ -x "$new_node_home/bin/node" ]; then
+      update_node_home
+      echo "switched to node${WEBI_VERSION} at $new_node_home"
+      exit 0
+    fi
+  fi
 fi
-if [ -n "$cur_node" ] && [ "$cur_node" != "$new_node" ]; then
-    echo "WARN: possible conflict with node v$WEBI_VERSION at $cur_node"
-fi
+
 
 # Note: this file is `source`d by the true installer and hence will have the webi functions
 
@@ -130,8 +163,7 @@ popd 2>&1 >/dev/null
 #   Update PATH   #
 ###################
 
-# TODO get better output from pathman / output the path to add as return to webi bootstrap
-webi_path_add "$new_node_home/bin"
+update_node_home
 
 echo "Installed 'node' and 'npm'"
 echo ""