mirror of
https://github.com/nvm-sh/nvm.git
synced 2026-10-11 00:00:13 +08:00
[Perf] nvm_die_on_prefix: detect the OS only when it is needed
This commit is contained in:
@@ -3365,9 +3365,6 @@ nvm_die_on_prefix() {
|
||||
return 3
|
||||
fi
|
||||
|
||||
local NVM_OS
|
||||
NVM_OS="$(nvm_get_os)"
|
||||
|
||||
# npm normalizes NPM_CONFIG_-prefixed env vars
|
||||
# https://github.com/npm/npmconf/blob/22827e4038d6eebaafeb5c13ed2b92cf97b8fb82/npmconf.js#L331-L348
|
||||
# https://github.com/npm/npm/blob/5e426a78ca02d0044f8dd26e0c5f881217081cbd/lib/config/core.js#L343-L359
|
||||
@@ -3377,6 +3374,10 @@ nvm_die_on_prefix() {
|
||||
local NVM_NPM_CONFIG_x_PREFIX_ENV
|
||||
NVM_NPM_CONFIG_x_PREFIX_ENV="$(command awk 'BEGIN { for (name in ENVIRON) if (toupper(name) == "NPM_CONFIG_PREFIX") { print name; break } }')"
|
||||
if [ -n "${NVM_NPM_CONFIG_x_PREFIX_ENV-}" ]; then
|
||||
# `$NVM_OS` is only used to translate Windows paths, but detecting the OS
|
||||
# shells out to `uname`, so it is deferred until it is actually needed
|
||||
local NVM_OS
|
||||
NVM_OS="$(nvm_get_os)"
|
||||
local NVM_CONFIG_VALUE
|
||||
eval "NVM_CONFIG_VALUE=\"\$${NVM_NPM_CONFIG_x_PREFIX_ENV}\""
|
||||
if [ -n "${NVM_CONFIG_VALUE-}" ] && [ "_${NVM_OS}" = "_win" ]; then
|
||||
|
||||
@@ -49,6 +49,20 @@ nvm_has() { return 0; }
|
||||
OUTPUT="$(nvm_die_on_prefix 0 foo "$(nvm_version_dir new)" 2>&1)"
|
||||
[ -z "$OUTPUT" ] || die "'nvm_die_on_prefix' was not a noop when prefix is good; got '$OUTPUT'"
|
||||
|
||||
# `nvm_get_os` runs `uname`; it is only needed when an `NPM_CONFIG_PREFIX`
|
||||
# variable is set, so with none set it must not run at all
|
||||
MOCK_BIN="${TEST_DIR}/bin"
|
||||
mkdir -p "${MOCK_BIN}"
|
||||
{
|
||||
echo '#!/bin/sh'
|
||||
echo 'echo "uname was called" >&2'
|
||||
echo 'exit 1'
|
||||
} > "${MOCK_BIN}/uname"
|
||||
chmod +x "${MOCK_BIN}/uname"
|
||||
unset NPM_CONFIG_PREFIX npm_config_prefix npm_CONFIG_PREFIX
|
||||
OUTPUT="$(PATH="${MOCK_BIN}:${PATH}" nvm_die_on_prefix 0 foo "$(nvm_version_dir new)" 2>&1)"
|
||||
[ -z "$OUTPUT" ] || die "'nvm_die_on_prefix' detected the OS (ran uname) when no NPM_CONFIG_PREFIX was set; got '$OUTPUT'"
|
||||
|
||||
mkdir -p "$(nvm_version_dir new)"
|
||||
ln -s "$(nvm_version_dir new)" "$TEST_DIR/node"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user