From b4d20626f98107c12407b5ad24e25fce04597bd2 Mon Sep 17 00:00:00 2001 From: Buddy Sandidge Date: Sat, 14 Mar 2020 17:11:17 -0700 Subject: [PATCH] Add node install script --- bin/install-node-system | 61 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100755 bin/install-node-system diff --git a/bin/install-node-system b/bin/install-node-system new file mode 100755 index 0000000..7ef78c5 --- /dev/null +++ b/bin/install-node-system @@ -0,0 +1,61 @@ +#!/usr/bin/env bash + +set -e + +NODE_VERSION=${NODE_VERSION:-v12.16.1} + +PREFIX_DIR=/usr/local +BIN_DIR=${PREFIX_DIR}/bin + +NODE_PREFIX_DIR=${PREFIX_DIR}/node +NODE_DEST_DIR=${NODE_PREFIX_DIR}/${NODE_VERSION} +NODE_DEST_BIN_DIR=${NODE_DEST_DIR}/bin +NODE_CURRENT=${NODE_PREFIX_DIR}/current + +OS= +if [[ $OSTYPE =~ linux ]]; then + OS=linux +elif [[ $OSTYPE =~ darwin ]]; then + OS=darwin +else + echo unknown os: $OSTYPE + exit 1 +fi + +ARCH= +if [[ $(uname -m) == x86_64 ]]; then + ARCH=x64 +else + echo arch not yet supported: $(uname -m) + exit 1 +fi + +[[ -d ${NODE_DEST_DIR} ]] || sudo mkdir -p ${NODE_DEST_DIR} + +TMP_DIR= +if [[ ! -d ${NODE_DEST_BIN_DIR} ]]; then + TMP_DIR=$(mktemp --directory) + DEST_FILE=$TMP_DIR/node.tar.xz + URL=https://nodejs.org/dist/${NODE_VERSION}/node-${NODE_VERSION}-${OS}-${ARCH}.tar.xz + echo downloading node $NODE_VERSION to temp directory ${TMP_DIR} + curl --silent --output ${DEST_FILE} ${URL} + + sudo tar xf ${DEST_FILE} --directory ${NODE_DEST_DIR} --strip-components 1 + sudo chown --recursive root:root ${NODE_DEST_DIR} +else + echo node $NODE_VERSION already installed +fi + +cd ${NODE_PREFIX_DIR} +sudo unlink current +sudo ln --symbolic ${NODE_VERSION} current +echo current version set to ${NODE_VERSION} +cd ${BIN_DIR} +for bin in $(ls ${NODE_CURRENT}/bin); do + [[ -s $bin ]] || sudo ln --symbolic ${NODE_CURRENT}/bin/$bin +done + +if [[ $TMP_DIR != "" ]]; then + rm -rf ${TMP_DIR} + echo removed up temp directory ${TMP_DIR} +fi