Skip to content

Commit

Permalink
[New] nvm-exec: allow nvm-exec to be linked into individual .nvm di…
Browse files Browse the repository at this point in the history
…rectories 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.
  • Loading branch information
msaladna authored and ljharb committed Dec 10, 2023
1 parent bab86d5 commit 14c863a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
7 changes: 6 additions & 1 deletion nvm-exec
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#!/usr/bin/env bash

DIR="$(command cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
SOURCE=${BASH_SOURCE[0]}
if [ -L "${SOURCE}" ]; then
command cd "$(dirname "${SOURCE}")"
SOURCE="$(readlink "${SOURCE}")"
fi
DIR="$(command cd "$(dirname "${SOURCE}")" && pwd)"

unset NVM_CD_FLAGS

Expand Down
13 changes: 9 additions & 4 deletions nvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2384,12 +2384,12 @@ nvm_extract_tarball() {

if [ "${NVM_OS}" = 'openbsd' ]; then
if [ "${tar_compression_flag}" = 'J' ]; then
command xzcat "${TARBALL}" | "${tar}" -xf - -C "${TMPDIR}" -s '/[^\/]*\///' || return 1
command xzcat "${TARBALL}" | "${tar}" --no-same-owner -xf - -C "${TMPDIR}" -s '/[^\/]*\///' || return 1
else
command "${tar}" -x${tar_compression_flag}f "${TARBALL}" -C "${TMPDIR}" -s '/[^\/]*\///' || return 1
command "${tar}" --no-same-owner -x${tar_compression_flag}f "${TARBALL}" -C "${TMPDIR}" -s '/[^\/]*\///' || return 1
fi
else
command "${tar}" -x${tar_compression_flag}f "${TARBALL}" -C "${TMPDIR}" --strip-components 1 || return 1
command "${tar}" --no-same-owner -x${tar_compression_flag}f "${TARBALL}" -C "${TMPDIR}" --strip-components 1 || return 1
fi
}

Expand Down Expand Up @@ -3871,7 +3871,12 @@ nvm() {
nvm_echo "Running node ${VERSION}$(nvm use --silent "${VERSION}" && nvm_print_npm_version)"
fi
fi
NODE_VERSION="${VERSION}" "${NVM_DIR}/nvm-exec" "$@"
local 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 14c863a

Please sign in to comment.