Files
zulip/scripts/lib/install-node
Anders Kaseorg a5407e1c7d scripts: Replace node-wrapper with a symlink.
Commit 00e057bf44 (#4727) simplified
node-wrapper to a one-line wrapper script for performance.  Copying
the binary was proposed and rejected because node finds some of its
modules relative to its own path.  But a symlink doesn’t have that
issue, as you can verify with

    node -e 'console.log(require.resolve.paths("foo"))'

(To find its own path, node uses `process.execPath`, which resolves
symlinks, and there’s no plausible reason for that behavior to change.
https://github.com/nodejs/node/blob/v8.11.1/lib/module.js#L708-L717
https://github.com/nodejs/node/blob/v10.7.0/lib/internal/modules/cjs/loader.js#L761-L770)

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
2018-07-30 11:48:10 -07:00

56 lines
1.8 KiB
Bash
Executable File

#!/usr/bin/env bash
set -e
ZULIP_PATH="$(dirname "$0")/../.."
ZULIP_SRV="/srv"
if [ "$TRAVIS" ] ; then
ZULIP_SRV="/home/travis"
fi
YARN_BIN="$ZULIP_SRV/zulip-yarn/bin/yarn"
node_version=8.11.1
yarn_version=1.7.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="$(type -p node)"; then
current_node_version="$(node --version)"
fi
if [ "$($YARN_BIN --version 2>/dev/null)" = "$yarn_version" ] && [ "$current_node_version" = "v$node_version" ] && [ -L "$node_wrapper_path" ]; then
echo "Node version $node_version and yarn version $yarn_version are already installed."
exit 0
fi
if [ "$current_node_version" != "v$node_version" ] || ! [ -L "$node_wrapper_path" ]; then
export NVM_DIR=/usr/local/nvm
if ! [ -e "$NVM_DIR/nvm.sh" ]; then
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
fi
source "$NVM_DIR/nvm.sh"
nvm install "$node_version" && nvm alias default "$node_version"
export NODE_BIN="$(nvm which default)"
# Fix messed-up uid=500 and group write bits produced by nvm
n=$(which node)
n=${n%/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
fi
# Work around the fact that apparently sudo doesn't clear the HOME
# environment variable in some cases; we don't want root
# accessing/storing yarn configuration in the non-root user's home
# directory.
export HOME=/root
# Install yarn if not installed
bash "$ZULIP_PATH/scripts/lib/third/install-yarn.sh" "$ZULIP_SRV" --version "$yarn_version"