# > Hello, World!
# ```
#
-# <br/>
-#
# ### A Simple Web Server
#
# `server.js`:
# });
# ```
#
-# <br/>
-#
# ```bash
# node server.js
# ```
# npm install --save express
# ```
#
-# <br/>
-#
# `app.js`:
#
# ```js
# module.exports = app;</code></pre>
# ```
#
-# <br/>
-#
# `server.js`:
#
# ```js
# });
# ```
#
-# <br/>
-#
# ```bash
# npm start
# ```
set -e
set -u
-##################
-# Install node #
-##################
+pkg_cmd_name="node"
-new_node_home="${HOME}/.local/opt/node-v${WEBI_VERSION}"
-new_node="${HOME}/.local/opt/node-v${WEBI_VERSION}/bin/node"
+pkg_get_current_version() {
+ # 'node --version' has output in this format:
+ # v12.8.0
+ # This trims it down to just the version number:
+ # 12.8.0
+ echo "$(node --version 2>/dev/null | head -n 1 | cut -d' ' -f1 | sed 's:^v::')"
+}
-# 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
-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
-
-# because we created releases.js we can use webi_download()
-# downloads node to ~/Downloads
-webi_download
-
-# because this is tar or zip, we can webi_extract()
-# extracts to the WEBI_TMP directory, raw (no --strip-prefix)
-webi_extract
-
-pushd "$WEBI_TMP" 2>&1 >/dev/null
- echo Installing node v${WEBI_VERSION} as "$new_node"
-
- # simpler for single-binary commands
- #mv ./example*/bin/example "$HOME/.local/bin"
-
- # 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
- rm -rf ./node*
-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"
-
-echo "Installed 'node' and 'npm'"
-echo ""
+pkg_format_cmd_version() {
+ # 'node v12.8.0' is the canonical version format for node
+ my_version="$1"
+ echo "$pkg_cmd_name v$my_version"
+}
+
+pkg_link_new_version() {
+ # 'pkg_common_opt' will default to $HOME/.local/opt/node
+ # 'pkg_new_opt' will be the installed version, such as to $HOME/.local/opt/node-v12.8.0
+ rm -rf "$pkg_common_opt"
+ ln -s "$pkg_new_opt" "$pkg_common_opt"
+}
+
+pkg_pre_install() {
+ # web_* are defined in webi/template.bash at https://github.com/webinstall/packages
+
+ # if selected version is installed, re-link it and quit
+ webi_check
+
+ # will save to ~/Downloads/$WEBI_PKG_FILE by default
+ webi_download
+
+ # supported formats (.xz, .tar.*, .zip) will be extracted to $WEBI_TMP
+ webi_extract
+}
+
+pkg_install() {
+ pushd "$WEBI_TMP" 2>&1 >/dev/null
+
+ # remove the versioned folder, just in case it's there with junk
+ rm -rf "$pkg_new_opt"
+
+ # rename the entire extracted folder to the new location
+ # (this will be "$HOME/.local/opt/node-v$WEBI_VERSION" by default)
+
+ # ex (full directory): ./node-v13-linux-amd64/bin/node.exe
+ mv ./"$pkg_cmd_name"* "$pkg_new_opt"
+
+ # ex (single file): ./caddy-v13-linux-amd64.exe
+ #mv ./"$pkg_cmd_name"* "$pkg_common_cmd"
+
+ # ex (single file, nested in directory): ./rg/rg-v13-linux-amd64
+ #mv ./"$pkg_cmd_name"*/"$pkg_cmd_name"* "$pkg_commend_cmd"
+
+ popd 2>&1 >/dev/null
+}
+
+pkg_post_install() {
+ pkg_link_new_version
+
+ # web_path_add is defined in webi/template.bash at https://github.com/webinstall/packages
+ # Adds "$HOME/.local/opt/node/bin" to PATH
+ webi_path_add "$pkg_common_bin"
+}
+
+pkg_post_install_message() {
+ echo "Installed 'node' and 'npm'"
+}