zulip/scripts/lib/install-node

56 lines
1.8 KiB
Bash
Executable File

#!/usr/bin/env bash
set -eo pipefail
node_version=14.17.5
nvm_version=0.38.0
# This is a fix for the fact that nvm uses $HOME to determine which
# user account's home directory to ~/.config to. Ideally, we'd have a
# more systematic fix, like using `sudo -H` everywhere.
export HOME=/root
current_node_version="none"
if node_wrapper_path="$(command -v node)"; then
current_node_version="$(node --version)"
fi
if [ "$current_node_version" = "v$node_version" ] && [ -L "$node_wrapper_path" ]; then
echo "Node version $node_version is already installed."
exit 0
fi
if [ "$current_node_version" != "v$node_version" ] || ! [ -L "$node_wrapper_path" ]; then
export NVM_DIR=/usr/local/nvm
# shellcheck source=/dev/null
if ! [ -e "$NVM_DIR/nvm.sh" ] || {
. "$NVM_DIR/nvm.sh"
[ "$(nvm --version)" != "$nvm_version" ]
}; then
mkdir -p "$NVM_DIR"
curl_opts=(-fL)
if [ -n "${CUSTOM_CA_CERTIFICATES:-}" ]; then
curl_opts+=(--cacert "${CUSTOM_CA_CERTIFICATES}")
fi
curl "${curl_opts[@]}" "https://raw.githubusercontent.com/nvm-sh/nvm/v$nvm_version/install.sh" | bash
# shellcheck source=/dev/null
. "$NVM_DIR/nvm.sh"
fi
# Tell NVM that we don't want it messing around with $PATH; we'll
# adjust which npm to use by symlinks below.
nvm alias default system
nvm install "$node_version"
NODE_BIN="$(nvm which $node_version)"
# Fix messed-up uid=500 and group write bits produced by nvm
n=${NODE_BIN%/bin/node}
chown -R root:root "$n"
chmod -R go-w "$n"
# Install node symlink to /usr/local/bin
ln -nsf "$NODE_BIN" /usr/local/bin/node
ln -nsf "$(dirname "$NODE_BIN")/npm" /usr/local/bin/npm
ln -nsf "$(dirname "$NODE_BIN")/npx" /usr/local/bin/npx
fi