From d7058a4952949660ca795dcbe0db0bcc2c501b13 Mon Sep 17 00:00:00 2001 From: Matt Saladna Date: Thu, 21 Jun 2018 14:33:11 -0400 Subject: [PATCH 1/3] Allow nvm-exec to be linked into individual .nvm directories for system-wide installs with a localized Nodes. Let's say we have nvm installed in a separate mount, /.socket. NVM_DIR is $HOME/.nvm in /etc/profile.d/nvm.sh. With this setup, users can install Node versions to their home directories without each installing nvm. nvm install --lts This works fine as does nvm use --lts. When nvm exec is used though, it fails because it looks for nvm-exec in $NVM_DIR. First fix is to look for nvm-exec in $NVM_DIR. If NVM_DIR does not contain nvm-exec, check $BASH_SOURCE[0]. The second fix is to follow nvm-exec if a symbolic link to determine the proper location of nvm's home. Alternatively we could use a second environment variable, NVM_HOME in exec instead of relying on the directory name of nvm-exec. --- nvm-exec | 2 +- nvm.sh | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/nvm-exec b/nvm-exec index d4f37df357..83eb885ed0 100755 --- a/nvm-exec +++ b/nvm-exec @@ -1,6 +1,6 @@ #!/usr/bin/env bash -DIR="$(command cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +DIR="$(dirname $(realpath "${BASH_SOURCE[0]}"))" # shellcheck disable=SC1090,SC1091 \. "$DIR/nvm.sh" --no-use diff --git a/nvm.sh b/nvm.sh index 6ea962a239..f094a2018e 100644 --- a/nvm.sh +++ b/nvm.sh @@ -3810,8 +3810,14 @@ nvm() { nvm_echo "Running node ${VERSION}$(nvm use --silent "${VERSION}" && nvm_print_npm_version)" fi fi - NODE_VERSION="${VERSION}" "${NVM_DIR}/nvm-exec" "$@" + + NVM_EXEC="${NVM_DIR}/nvm-exec" + if [ ! -f "${NVM_EXEC}" ]; then + NVM_EXEC=`dirname ${BASH_SOURCE[0]-}`/nvm-exec + fi + NODE_VERSION="${VERSION}" "${NVM_EXEC}" "$@" ;; + "ls" | "list") local PATTERN local NVM_NO_COLORS From 9dc29fd8ebd53dbaa95be7c3e3ff4b21f3dc010c Mon Sep 17 00:00:00 2001 From: Matt Saladna Date: Thu, 21 Jun 2018 14:38:55 -0400 Subject: [PATCH 2/3] shellcheck warnings --- nvm-exec | 2 +- nvm.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/nvm-exec b/nvm-exec index 83eb885ed0..0838f38281 100755 --- a/nvm-exec +++ b/nvm-exec @@ -1,6 +1,6 @@ #!/usr/bin/env bash -DIR="$(dirname $(realpath "${BASH_SOURCE[0]}"))" +DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" # shellcheck disable=SC1090,SC1091 \. "$DIR/nvm.sh" --no-use diff --git a/nvm.sh b/nvm.sh index f094a2018e..39a78055ef 100644 --- a/nvm.sh +++ b/nvm.sh @@ -3813,7 +3813,7 @@ nvm() { NVM_EXEC="${NVM_DIR}/nvm-exec" if [ ! -f "${NVM_EXEC}" ]; then - NVM_EXEC=`dirname ${BASH_SOURCE[0]-}`/nvm-exec + NVM_EXEC="$(dirname "${BASH_SOURCE[0]-}")/nvm-exec" fi NODE_VERSION="${VERSION}" "${NVM_EXEC}" "$@" ;; From c74c74eb0a9f2a11015a00ce1551a290797b11dc Mon Sep 17 00:00:00 2001 From: Matt Saladna Date: Thu, 21 Jun 2018 16:38:15 -0400 Subject: [PATCH 3/3] Add test for NVM_DIR outside nvm.sh --- ...m exec --lts' when NVM_DIR differs from nvm | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 test/slow/nvm exec/Running 'nvm exec --lts' when NVM_DIR differs from nvm diff --git a/test/slow/nvm exec/Running 'nvm exec --lts' when NVM_DIR differs from nvm b/test/slow/nvm exec/Running 'nvm exec --lts' when NVM_DIR differs from nvm new file mode 100644 index 0000000000..ee314be76b --- /dev/null +++ b/test/slow/nvm exec/Running 'nvm exec --lts' when NVM_DIR differs from nvm @@ -0,0 +1,18 @@ +#!/bin/sh + +set -ex + +die () { echo "$@" ; exit 1; } + +INSTPATH="$(mktemp -p "$(pwd)" -d)" +trap 'test ! -z "${INSTPATH-}" && test -d "$INSTPATH" && rm -rf "$INSTPATH"' EXIT +declare -x NVM_DIR=$INSTPATH +\. ../../../nvm.sh + +nvm install --lts || die 'nvm install --lts failed' +nvm exec --lts npm --version || die "`nvm exec` failed to run" +declare -x NODE_VERSION="$(nvm exec --lts --silent node --version)" + +ln -s ../../../../nvm-exec "$INSTPATH/nvm-exec" || die "failed to create a symlink to $INSTPATH/" +"$INSTPATH/nvm-exec" npm ls > /dev/null || die "`nvm exec` failed to run using nvm-exec helper" +