Skip to content

Commit

Permalink
[Perf] use/nvm_die_on_prefix: replicate npm config algorithm and …
Browse files Browse the repository at this point in the history
…remove `npm config` call
  • Loading branch information
ljharb committed Aug 25, 2020
1 parent 4436638 commit 93cc2ab
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 31 deletions.
87 changes: 69 additions & 18 deletions nvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2175,6 +2175,15 @@ nvm_npm_global_modules() {
nvm_echo "${INSTALLS} //// ${LINKS}"
}

nvm_npmrc_bad_news_bears() {
local NVM_NPMRC
NVM_NPMRC="${1-}"
if [ -n "${NVM_NPMRC}" ] && [ -f "${NVM_NPMRC}" ] && nvm_grep -Ee '^(prefix|globalconfig) *=' <"${NVM_NPMRC}" >/dev/null; then
return 0
fi
return 1
}

nvm_die_on_prefix() {
local NVM_DELETE_PREFIX
NVM_DELETE_PREFIX="${1-}"
Expand Down Expand Up @@ -2222,29 +2231,70 @@ nvm_die_on_prefix() {
fi
fi

if ! nvm_has 'npm'; then
return
# here, npm config checks npmrc files.
# the stack is: cli, env, project, user, global, builtin, defaults
# cli does not apply; env is covered above, defaults don't exist for prefix
# there are 4 npmrc locations to check: project, global, user, and builtin
# project: find the closest node_modules or package.json-containing dir, `.npmrc`
# global: default prefix + `/etc/npmrc`
# user: $HOME/.npmrc
# builtin: npm install location, `npmrc`
#
# if any of them have a `prefix`, fail.
# if any have `globalconfig`, fail also, just in case, to avoid spidering configs.

local NVM_NPM_BUILTIN_NPMRC
NVM_NPM_BUILTIN_NPMRC="${NVM_VERSION_DIR}/lib/node_modules/npm/npmrc"
if nvm_npmrc_bad_news_bears "${NVM_NPM_BUILTIN_NPMRC}"; then
if [ "_${NVM_DELETE_PREFIX}" = "_1" ]; then
npm config --loglevel=warn delete prefix --userconfig="${NVM_NPM_BUILTIN_NPMRC}"
npm config --loglevel=warn delete globalconfig --userconfig="${NVM_NPM_BUILTIN_NPMRC}"
else
nvm_err "Your builtin npmrc file ($(nvm_sanitize_path "${NVM_NPM_BUILTIN_NPMRC}"))"
nvm_err 'has a `globalconfig` and/or a `prefix` setting, which are incompatible with nvm.'
nvm_err "Run \`${NVM_COMMAND}\` to unset it."
return 10
fi
fi

local NVM_NPM_PREFIX
local NVM_OS
NVM_OS="$(nvm_get_os)"
NVM_NPM_PREFIX="$(npm config --loglevel=warn get prefix)"
if [ "${NVM_VERSION_DIR}" != "${NVM_NPM_PREFIX}" ] && ! (nvm_tree_contains_path "${NVM_VERSION_DIR}" "${NVM_NPM_PREFIX}" >/dev/null 2>&1); then
local NVM_NPM_GLOBAL_NPMRC
NVM_NPM_GLOBAL_NPMRC="${NVM_VERSION_DIR}/etc/npmrc"
if nvm_npmrc_bad_news_bears "${NVM_NPM_GLOBAL_NPMRC}"; then
if [ "_${NVM_DELETE_PREFIX}" = "_1" ]; then
npm config --global --loglevel=warn delete prefix
npm config --global --loglevel=warn delete globalconfig
else
nvm_err "Your global npmrc file ($(nvm_sanitize_path "${NVM_NPM_GLOBAL_NPMRC}"))"
nvm_err 'has a `globalconfig` and/or a `prefix` setting, which are incompatible with nvm.'
nvm_err "Run \`${NVM_COMMAND}\` to unset it."
return 10
fi
fi

local NVM_NPM_USER_NPMRC
NVM_NPM_USER_NPMRC="${HOME}/.npmrc"
if nvm_npmrc_bad_news_bears "${NVM_NPM_USER_NPMRC}"; then
if [ "_${NVM_DELETE_PREFIX}" = "_1" ]; then
npm config --loglevel=warn delete prefix --userconfig="${NVM_NPM_USER_NPMRC}"
npm config --loglevel=warn delete globalconfig --userconfig="${NVM_NPM_USER_NPMRC}"
else
nvm_err "Your user’s .npmrc file ($(nvm_sanitize_path "${NVM_NPM_USER_NPMRC}"))"
nvm_err 'has a `globalconfig` and/or a `prefix` setting, which are incompatible with nvm.'
nvm_err "Run \`${NVM_COMMAND}\` to unset it."
return 10
fi
fi

local NVM_NPM_PROJECT_NPMRC
NVM_NPM_PROJECT_NPMRC="$(nvm_find_project_dir)/.npmrc"
if nvm_npmrc_bad_news_bears "${NVM_NPM_PROJECT_NPMRC}"; then
if [ "_${NVM_DELETE_PREFIX}" = "_1" ]; then
npm config --loglevel=warn delete prefix
npm config --loglevel=warn delete globalconfig
else
nvm deactivate >/dev/null 2>&1
nvm_err "nvm is not compatible with the npm config \"prefix\" option: currently set to \"${NVM_NPM_PREFIX}\""
if nvm_has 'npm'; then
nvm_err "Run \`npm config delete prefix\` or \`${NVM_COMMAND}\` to unset it."
else
nvm_err "Run \`${NVM_COMMAND}\` to unset it."
fi
if [ "${NVM_OS}" = 'darwin' ]; then
nvm_err "Make sure your username ($(whoami)) matches the one in your \$HOME path."
nvm_err "See the \"macOS Troubleshooting\" section in the docs for more information."
fi
nvm_err "Your project npmrc file ($(nvm_sanitize_path "${NVM_NPM_PROJECT_NPMRC}"))"
nvm_err 'has a `globalconfig` and/or a `prefix` setting, which are incompatible with nvm.'
nvm_err "Run \`${NVM_COMMAND}\` to unset it."
return 10
fi
fi
Expand Down Expand Up @@ -3698,6 +3748,7 @@ nvm() {
nvm_sanitize_path nvm_has_colors nvm_process_parameters \
nvm_node_version_has_solaris_binary nvm_iojs_version_has_solaris_binary \
nvm_curl_libz_support nvm_command_info nvm_is_zsh nvm_stdout_is_terminal \
nvm_npmrc_bad_news_bears \
>/dev/null 2>&1
unset NVM_RC_VERSION NVM_NODEJS_ORG_MIRROR NVM_IOJS_ORG_MIRROR NVM_DIR \
NVM_CD_FLAGS NVM_BIN NVM_INC NVM_MAKE_JOBS \
Expand Down
18 changes: 5 additions & 13 deletions test/fast/Unit tests/nvm_die_on_prefix
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,10 @@ EXIT_CODE="$(export npm_CONFIG_PREFIX=bar ; nvm_die_on_prefix 0 foo "$(nvm_versi
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'npm_CONFIG_PREFIX=bar nvm_die_on_prefix 0 foo' did not error with '$EXPECTED_OUTPUT'; got '$OUTPUT'"
[ "_$EXIT_CODE" = "_4" ] || die "'npm_CONFIG_PREFIX=bar nvm_die_on_prefix 0 foo' did not exit with 4; got '$EXIT_CODE'"

npm() {
local args
args="$@"
if [ "_$args" = "_config --loglevel=warn get prefix" ]; then
echo "./bad prefix"
fi
}
OUTPUT="$(nvm_die_on_prefix 0 foo "$(nvm_version_dir new)" 2>&1)"
EXPECTED_OUTPUT="nvm is not compatible with the npm config \"prefix\" option: currently set to \"./bad prefix\"
Run \`npm config delete prefix\` or \`foo\` to unset it."
EXIT_CODE="$(nvm_die_on_prefix 0 foo "$(nvm_version_dir new)" >/dev/null 2>&1; echo $?)"
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'nvm_die_on_prefix 0 foo' did not error with '$EXPECTED_OUTPUT' with bad prefix set; got '$OUTPUT'"
[ "_$EXIT_CODE" = "_10" ] || die "'nvm_die_on_prefix 0 foo' did not exit with 10 with bad prefix set; got '$EXIT_CODE'"
# TODO: add test cases
# project
# global
# builtin
# user

cleanup

0 comments on commit 93cc2ab

Please sign in to comment.