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.

62 lines
1.5 KiB
Bash

#!/usr/bin/env bash
set -e
NODE_VERSION=${NODE_VERSION:-v12.16.2}
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}
[[ -s current ]] && sudo unlink current
sudo ln --symbolic "${NODE_VERSION}" current
echo current version set to "${NODE_VERSION}"
cd ${BIN_DIR}
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