Skip to content

Commit 44039c1

Browse files
msaladnaljharb
authored andcommitted
[New] nvm-exec: 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.
1 parent 4e2a71b commit 44039c1

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

nvm-exec

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#!/usr/bin/env bash
22

3-
DIR="$(command cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
3+
SOURCE=${BASH_SOURCE[0]}
4+
if [ -L "${SOURCE}" ]; then
5+
command cd "$(dirname "${SOURCE}")" || exit 127
6+
SOURCE="$(readlink "${SOURCE}")"
7+
fi
8+
DIR="$(command cd "$(dirname "${SOURCE}")" && pwd)"
49

510
unset NVM_CD_FLAGS
611

nvm.sh

+9-4
Original file line numberDiff line numberDiff line change
@@ -2384,12 +2384,12 @@ nvm_extract_tarball() {
23842384

23852385
if [ "${NVM_OS}" = 'openbsd' ]; then
23862386
if [ "${tar_compression_flag}" = 'J' ]; then
2387-
command xzcat "${TARBALL}" | "${tar}" -xf - -C "${TMPDIR}" -s '/[^\/]*\///' || return 1
2387+
command xzcat "${TARBALL}" | "${tar}" --no-same-owner -xf - -C "${TMPDIR}" -s '/[^\/]*\///' || return 1
23882388
else
2389-
command "${tar}" -x${tar_compression_flag}f "${TARBALL}" -C "${TMPDIR}" -s '/[^\/]*\///' || return 1
2389+
command "${tar}" --no-same-owner -x${tar_compression_flag}f "${TARBALL}" -C "${TMPDIR}" -s '/[^\/]*\///' || return 1
23902390
fi
23912391
else
2392-
command "${tar}" -x${tar_compression_flag}f "${TARBALL}" -C "${TMPDIR}" --strip-components 1 || return 1
2392+
command "${tar}" --no-same-owner -x${tar_compression_flag}f "${TARBALL}" -C "${TMPDIR}" --strip-components 1 || return 1
23932393
fi
23942394
}
23952395

@@ -3871,7 +3871,12 @@ nvm() {
38713871
nvm_echo "Running node ${VERSION}$(nvm use --silent "${VERSION}" && nvm_print_npm_version)"
38723872
fi
38733873
fi
3874-
NODE_VERSION="${VERSION}" "${NVM_DIR}/nvm-exec" "$@"
3874+
local NVM_EXEC
3875+
NVM_EXEC="${NVM_DIR}/nvm-exec"
3876+
if [ ! -f "${NVM_EXEC}" ]; then
3877+
NVM_EXEC="$(dirname "${BASH_SOURCE[0]-}")/nvm-exec"
3878+
fi
3879+
NODE_VERSION="${VERSION}" "${NVM_EXEC}" "$@"
38753880
;;
38763881
"ls" | "list")
38773882
local PATTERN
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/sh
2+
3+
set -ex
4+
5+
die () { echo "$@" ; exit 1; }
6+
7+
INSTPATH="$(mktemp -p "$(pwd)" -d)"
8+
trap 'test ! -z "${INSTPATH-}" && test -d "$INSTPATH" && rm -rf "$INSTPATH"' EXIT
9+
declare -x NVM_DIR=$INSTPATH
10+
\. ../../../nvm.sh
11+
12+
nvm install --lts || die 'nvm install --lts failed'
13+
nvm exec --lts npm --version || die "`nvm exec` failed to run"
14+
declare -x NODE_VERSION="$(nvm exec --lts --silent node --version)"
15+
16+
ln -s ../../../../nvm-exec "$INSTPATH/nvm-exec" || die "failed to create a symlink to $INSTPATH/"
17+
"$INSTPATH/nvm-exec" npm ls > /dev/null || die "`nvm exec` failed to run using nvm-exec helper"
18+

0 commit comments

Comments
 (0)