Compare commits

...

8 Commits

Author SHA1 Message Date
Matt Saladna
6cfed63b5a
Merge b4ee18be6a into 759f70f196 2025-02-13 11:53:04 -08:00
Jordan Harband
759f70f196
[Refactor] prefer case over if/else chains 2025-02-04 22:45:35 -08:00
Jordan Harband
06a9179309
[Refactor] combine sed -e invocations/arguments 2025-02-04 15:55:10 -08:00
Matt Saladna
b4ee18be6a
Partial reversion of nvm-sh/nvm#1517. Each alias lookup adds 8 to the pids.current bean counter resulting in 64+ concurrent tids (tasks, w/ threads) thus making it susceptible to hit pids.max. Once reached, subshell behavior is unexpected yielding "retry: Resource temporarily unavailable" errors until cgroup capacity is restored. 2024-03-16 12:26:55 -04:00
Matt Saladna
3b811237cb
Drop same-owner on extraction 2023-12-09 19:30:19 -05:00
Matt Saladna
ad4fa37deb
Add test for NVM_DIR outside nvm.sh
Fix symlink traversal
2023-12-09 19:26:17 -05:00
Matt Saladna
d956279e5a
shellcheck warnings 2023-12-09 19:26:17 -05:00
Matt Saladna
c18cbf4d25
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.
2023-12-09 19:25:43 -05:00
3 changed files with 56 additions and 29 deletions

View File

@ -1,6 +1,9 @@
#!/usr/bin/env bash #!/usr/bin/env bash
DIR="$(command cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" SOURCE=${BASH_SOURCE[0]}
test -L "$SOURCE" && command cd "$( dirname "$SOURCE" )" && \
SOURCE=$(readlink "$SOURCE")
DIR="$(command cd "$( dirname "$SOURCE" )" && pwd )"
unset NVM_CD_FLAGS unset NVM_CD_FLAGS

62
nvm.sh
View File

@ -136,15 +136,17 @@ nvm_download() {
eval "curl -q --fail ${CURL_COMPRESSED_FLAG:-} ${CURL_HEADER_FLAG:-} ${NVM_DOWNLOAD_ARGS}" eval "curl -q --fail ${CURL_COMPRESSED_FLAG:-} ${CURL_HEADER_FLAG:-} ${NVM_DOWNLOAD_ARGS}"
elif nvm_has "wget"; then elif nvm_has "wget"; then
# Emulate curl with wget # Emulate curl with wget
ARGS=$(nvm_echo "$@" | command sed -e 's/--progress-bar /--progress=bar /' \ ARGS=$(nvm_echo "$@" | command sed "
-e 's/--compressed //' \ s/--progress-bar /--progress=bar /
-e 's/--fail //' \ s/--compressed //
-e 's/-L //' \ s/--fail //
-e 's/-I /--server-response /' \ s/-L //
-e 's/-s /-q /' \ s/-I /--server-response /
-e 's/-sS /-nv /' \ s/-s /-q /
-e 's/-o /-O /' \ s/-sS /-nv /
-e 's/-C - /-c /') s/-o /-O /
s/-C - /-c /
")
if [ -n "${NVM_AUTH_HEADER:-}" ]; then if [ -n "${NVM_AUTH_HEADER:-}" ]; then
ARGS="${ARGS} --header \"${NVM_AUTH_HEADER}\"" ARGS="${ARGS} --header \"${NVM_AUTH_HEADER}\""
@ -1263,10 +1265,9 @@ nvm_list_aliases() {
if [ -n "${LTS_ALIAS}" ]; then if [ -n "${LTS_ALIAS}" ]; then
nvm_echo "${LTS_ALIAS}" nvm_echo "${LTS_ALIAS}"
fi fi
} & }
done done
wait )
) | command sort
return return
} }
@ -1413,11 +1414,11 @@ nvm_add_iojs_prefix() {
nvm_strip_iojs_prefix() { nvm_strip_iojs_prefix() {
local NVM_IOJS_PREFIX local NVM_IOJS_PREFIX
NVM_IOJS_PREFIX="$(nvm_iojs_prefix)" NVM_IOJS_PREFIX="$(nvm_iojs_prefix)"
if [ "${1-}" = "${NVM_IOJS_PREFIX}" ]; then
nvm_echo case "${1-}" in
else "${NVM_IOJS_PREFIX}") nvm_echo ;;
nvm_echo "${1#"${NVM_IOJS_PREFIX}"-}" *) nvm_echo "${1#"${NVM_IOJS_PREFIX}"-}" ;;
fi esac
} }
nvm_ls() { nvm_ls() {
@ -1549,12 +1550,15 @@ nvm_ls() {
fi fi
if [ "${NVM_ADD_SYSTEM-}" = true ]; then if [ "${NVM_ADD_SYSTEM-}" = true ]; then
if [ -z "${PATTERN}" ] || [ "${PATTERN}" = 'v' ]; then case "${PATTERN}" in
VERSIONS="${VERSIONS} '' | v)
VERSIONS="${VERSIONS}
system" system"
elif [ "${PATTERN}" = 'system' ]; then ;;
VERSIONS="system" system)
fi VERSIONS="system"
;;
esac
fi fi
if [ -z "${VERSIONS}" ]; then if [ -z "${VERSIONS}" ]; then
@ -1688,7 +1692,7 @@ EOF
LTS="${LTS#lts/}" LTS="${LTS#lts/}"
fi fi
VERSIONS="$({ command awk -v lts="${LTS-}" '{ VERSIONS="$( { command awk -v lts="${LTS-}" '{
if (!$1) { next } if (!$1) { next }
if (lts && $10 ~ /^\-?$/) { next } if (lts && $10 ~ /^\-?$/) { next }
if (lts && lts != "*" && tolower($10) !~ tolower(lts)) { next } if (lts && lts != "*" && tolower($10) !~ tolower(lts)) { next }
@ -2529,12 +2533,12 @@ nvm_extract_tarball() {
if [ "${NVM_OS}" = 'openbsd' ]; then if [ "${NVM_OS}" = 'openbsd' ]; then
if [ "${tar_compression_flag}" = 'J' ]; 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 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 fi
else 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 fi
} }
@ -2732,7 +2736,7 @@ nvm_npm_global_modules() {
local NPMLIST local NPMLIST
local VERSION local VERSION
VERSION="$1" VERSION="$1"
NPMLIST=$(nvm use "${VERSION}" >/dev/null && npm list -g --depth=0 2>/dev/null | command sed 1,1d | nvm_grep -v 'UNMET PEER DEPENDENCY') NPMLIST=$(nvm use "${VERSION}" >/dev/null && npm list -g --depth=0 2>/dev/null | command sed -e '1d' -e '/UNMET PEER DEPENDENCY/d')
local INSTALLS local INSTALLS
INSTALLS=$(nvm_echo "${NPMLIST}" | command sed -e '/ -> / d' -e '/\(empty\)/ d' -e 's/^.* \(.*@[^ ]*\).*/\1/' -e '/^npm@[^ ]*.*$/ d' | command xargs) INSTALLS=$(nvm_echo "${NPMLIST}" | command sed -e '/ -> / d' -e '/\(empty\)/ d' -e 's/^.* \(.*@[^ ]*\).*/\1/' -e '/^npm@[^ ]*.*$/ d' | command xargs)
@ -4075,7 +4079,9 @@ nvm() {
nvm_echo "Running node ${VERSION}$(nvm use --silent "${VERSION}" && nvm_print_npm_version)" nvm_echo "Running node ${VERSION}$(nvm use --silent "${VERSION}" && nvm_print_npm_version)"
fi fi
fi fi
NODE_VERSION="${VERSION}" "${NVM_DIR}/nvm-exec" "$@" NVM_EXEC="$NVM_DIR/nvm-exec"
test ! -f "$NVM_EXEC" && NVM_EXEC="$(dirname "${BASH_SOURCE[0]-}")/nvm-exec"
NODE_VERSION="$VERSION" "$NVM_EXEC" "$@"
;; ;;
"ls" | "list") "ls" | "list")
local PATTERN local PATTERN

View File

@ -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"