Skip to content

Commit

Permalink
Add support for $NPM_CONFIG_PREFIX to nvm_die_on_prefix.
Browse files Browse the repository at this point in the history
Fixes #606.
  • Loading branch information
ljharb committed Sep 23, 2015
1 parent 1458de7 commit 8ee6f30
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 8 additions & 1 deletion nvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,13 @@ nvm_die_on_prefix() {
return 3
fi

if [ -n "$NPM_CONFIG_PREFIX" ] && ! (nvm_tree_contains_path "$NVM_DIR" "$NPM_CONFIG_PREFIX" >/dev/null 2>&1); then
nvm deactivate >/dev/null 2>&1
echo >&2 "nvm is not compatible with the \"NPM_CONFIG_PREFIX\" environment variable: currently set to \"$NPM_CONFIG_PREFIX\""
echo >&2 "Run \`unset NPM_CONFIG_PREFIX\` to unset it."
return 4
fi

if ! nvm_has 'npm'; then
return
fi
Expand All @@ -1291,7 +1298,7 @@ nvm_die_on_prefix() {
else
echo >&2 "Run \`$NVM_COMMAND\` to unset it."
fi
return 4
return 10
fi
fi
}
Expand Down
9 changes: 8 additions & 1 deletion test/fast/Unit tests/nvm_die_on_prefix
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ EXIT_CODE="$(PREFIX=bar nvm_die_on_prefix 0 foo >/dev/null 2>&1; echo $?)"
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'PREFIX=bar nvm_die_on_prefix 0 foo' did not error with '$EXPECTED_OUTPUT'; got '$OUTPUT'"
[ "_$EXIT_CODE" = "_3" ] || die "'PREFIX=bar nvm_die_on_prefix 0 foo' did not exit with 3; got '$EXIT_CODE'"

OUTPUT="$(NPM_CONFIG_PREFIX=bar nvm_die_on_prefix 0 foo 2>&1)"
EXPECTED_OUTPUT='nvm is not compatible with the "NPM_CONFIG_PREFIX" environment variable: currently set to "bar"
Run `unset NPM_CONFIG_PREFIX` to unset it.'
EXIT_CODE="$(NPM_CONFIG_PREFIX=bar nvm_die_on_prefix 0 foo >/dev/null 2>&1; echo $?)"
[ "_$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="$@"
Expand All @@ -62,6 +69,6 @@ EXPECTED_OUTPUT="nvm is not compatible with the npm config \"prefix\" option: cu
Run \`npm config delete prefix\` or \`foo\` to unset it."
EXIT_CODE="$(nvm_die_on_prefix 0 foo >/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" = "_4" ] || die "'nvm_die_on_prefix 0 foo' did not exit with 4 with bad prefix set; got '$EXIT_CODE'"
[ "_$EXIT_CODE" = "_10" ] || die "'nvm_die_on_prefix 0 foo' did not exit with 10 with bad prefix set; got '$EXIT_CODE'"

cleanup

0 comments on commit 8ee6f30

Please sign in to comment.