You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.5 KiB
Bash

#!/usr/bin/env bash
NODE_VERSION=${NODE_VERSION:-v14.15.3}
PREFIX_DIR=${PREFIX_DIR:-/usr/local}
BIN_DIR=${PREFIX_DIR}/bin
NODE_PREFIX_DIR=${PREFIX_DIR}/apps/node
NODE_DEST_DIR=${NODE_PREFIX_DIR}/${NODE_VERSION}
NODE_DEST_BIN_DIR=${NODE_DEST_DIR}/bin
NODE_CURRENT=${NODE_PREFIX_DIR}/current
OS=
ARCH=
case $(uname -s) in
Linux*) OS=linux;;
Darwin*) OS=darwin;;
*) >&2 echo "unsupported os"; return;
esac
case $(uname -m) in
x86_64*) ARCH=x64;;
*) >&2 echo "unsupported architecture: $(uname -m)"; exit 1;;
esac
[[ -d "${NODE_DEST_DIR}" ]] || sudo mkdir -p "${NODE_DEST_DIR}"
URL=https://nodejs.org/dist/${NODE_VERSION}/node-${NODE_VERSION}-${OS}-${ARCH}.tar.xz
TMP_DIR=
if [[ ! -d ${NODE_DEST_BIN_DIR} ]]; then
TMP_DIR=$(mktemp --directory)
DEST_FILE=$TMP_DIR/node.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}" || exit 1
[[ -s current ]] && sudo unlink current
sudo ln --symbolic "${NODE_VERSION}" current
echo current version set to "${NODE_VERSION}"
cd "${BIN_DIR}" || exit 1
for bin in "${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