Add `nvm_die_on_prefix`
parent
eb81fba8f7
commit
a1def71062
38
nvm.sh
38
nvm.sh
|
@ -1250,6 +1250,44 @@ nvm_npm_global_modules() {
|
|||
echo "$INSTALLS //// $LINKS"
|
||||
}
|
||||
|
||||
nvm_die_on_prefix() {
|
||||
local NVM_DELETE_PREFIX
|
||||
NVM_DELETE_PREFIX="$1"
|
||||
case "$NVM_DELETE_PREFIX" in
|
||||
0|1) ;;
|
||||
*)
|
||||
echo >&2 'First argument "delete the prefix" must be zero or one'
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
local NVM_COMMAND
|
||||
NVM_COMMAND="$2"
|
||||
if [ -z "$NVM_COMMAND" ]; then
|
||||
echo >&2 'Second argument "nvm command" must be nonempty'
|
||||
return 2
|
||||
fi
|
||||
|
||||
if ! nvm_has 'npm'; then
|
||||
return
|
||||
fi
|
||||
local NVM_NPM_PREFIX
|
||||
NVM_NPM_PREFIX="$(npm config get prefix)"
|
||||
if ! (nvm_tree_contains_path "$NVM_DIR" "$NVM_NPM_PREFIX" >/dev/null 2>&1); then
|
||||
if [ "_$NVM_DELETE_PREFIX" = "_1" ]; then
|
||||
npm config delete prefix
|
||||
else
|
||||
nvm deactivate >/dev/null 2>&1
|
||||
echo >&2 "nvm is not compatible with the npm config \"prefix\" option: currently set to \"$NVM_NPM_PREFIX\""
|
||||
if nvm_has 'npm'; then
|
||||
echo >&2 "Run \`npm config delete prefix\` or \`$NVM_COMMAND\` to unset it."
|
||||
else
|
||||
echo >&2 "Run \`$NVM_COMMAND\` to unset it."
|
||||
fi
|
||||
return 3
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
nvm() {
|
||||
if [ $# -lt 1 ]; then
|
||||
nvm help
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
#!/bin/sh
|
||||
|
||||
cleanup () {
|
||||
alias nvm_has='\nvm_has'
|
||||
alias npm='\npm'
|
||||
unset -f nvm_has npm
|
||||
}
|
||||
die () { echo $@ ; exit 1; }
|
||||
|
||||
. ../../../nvm.sh
|
||||
|
||||
OUTPUT="$(nvm_die_on_prefix 2>&1)"
|
||||
EXPECTED_OUTPUT="First argument \"delete the prefix\" must be zero or one"
|
||||
EXIT_CODE="$(nvm_die_on_prefix >/dev/null 2>&1; echo $?)"
|
||||
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'nvm_die_on_prefix' did not error with '$EXPECTED_OUTPUT'; got '$OUTPUT'"
|
||||
[ "_$EXIT_CODE" = "_1" ] || die "'nvm_die_on_prefix' did not exit with 1; got "$EXIT_CODE""
|
||||
|
||||
OUTPUT="$(nvm_die_on_prefix 2 2>&1)"
|
||||
EXPECTED_OUTPUT="First argument \"delete the prefix\" must be zero or one"
|
||||
EXIT_CODE="$(nvm_die_on_prefix 2 >/dev/null 2>&1; echo $?)"
|
||||
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'nvm_die_on_prefix 2' did not error with '$EXPECTED_OUTPUT'; got '$OUTPUT'"
|
||||
[ "_$EXIT_CODE" = "_1" ] || die "'nvm_die_on_prefix' did not exit with 1; got "$EXIT_CODE""
|
||||
|
||||
OUTPUT="$(nvm_die_on_prefix 0 2>&1)"
|
||||
EXPECTED_OUTPUT="Second argument \"nvm command\" must be nonempty"
|
||||
EXIT_CODE="$(nvm_die_on_prefix 0 >/dev/null 2>&1; echo $?)"
|
||||
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'nvm_die_on_prefix 0' did not error with '$EXPECTED_OUTPUT'; got '$OUTPUT'"
|
||||
[ "_$EXIT_CODE" = "_2" ] || die "'nvm_die_on_prefix 0' did not exit with 2; got '$EXIT_CODE'"
|
||||
|
||||
nvm_has() { return 1; } # ie, npm is not installed
|
||||
OUTPUT="$(nvm_die_on_prefix 0 foo 2>&1)"
|
||||
[ -z "$OUTPUT" ] || die "nvm_die_on_prefix was not a noop when nvm_has returns 1, got '$OUTPUT'"
|
||||
|
||||
nvm_has() { return 0; }
|
||||
|
||||
npm() {
|
||||
local args
|
||||
args="$@"
|
||||
if [ "_$args" = "_config get prefix" ]; then
|
||||
echo "$(nvm_version_dir new)/good prefix"
|
||||
fi
|
||||
}
|
||||
OUTPUT="$(nvm_die_on_prefix 0 foo 2>&1)"
|
||||
[ -z "$OUTPUT" ] || die "'nvm_die_on_prefix' was not a noop when prefix is good; got '$OUTPUT'"
|
||||
|
||||
npm() {
|
||||
local args
|
||||
args="$@"
|
||||
if [ "_$args" = "_config get prefix" ]; then
|
||||
echo "./bad prefix"
|
||||
fi
|
||||
}
|
||||
OUTPUT="$(nvm_die_on_prefix 0 foo 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 >/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" = "_3" ] || die "'nvm_die_on_prefix 0 foo' did not exit with 3 with bad prefix set; got '$EXIT_CODE'"
|
||||
|
||||
cleanup
|
Loading…
Reference in New Issue