Create `nvm_print_npm_version` and use that when printing the `npm` version.

master
Jordan Harband 2015-04-12 10:59:17 -07:00
parent de93dc46d0
commit cbf0f12aac
3 changed files with 44 additions and 14 deletions

22
nvm.sh
View File

@ -58,6 +58,12 @@ nvm_has_system_iojs() {
[ "$(nvm deactivate >/dev/null 2>&1 && command -v iojs)" != '' ]
}
nvm_print_npm_version() {
if nvm_has "npm"; then
npm --version 2>/dev/null | command xargs printf " (npm v%s)"
fi
}
# Make zsh glob matching behave same as bash
# This fixes the "zsh: no matches found" errors
if nvm_has "unsetopt"; then
@ -1359,15 +1365,11 @@ nvm() {
fi
if [ "_$VERSION" = '_system' ]; then
local NPM_VERSION
if nvm_has "npm"; then
NPM_VERSION="(npm v$(npm --version 2>/dev/null))"
fi
if nvm_has_system_node && nvm deactivate >/dev/null 2>&1; then
echo "Now using system version of node: $(node -v 2>/dev/null) $NPM_VERSION"
echo "Now using system version of node: $(node -v 2>/dev/null)$(nvm_print_npm_version)"
return
elif nvm_has_system_iojs && nvm deactivate >/dev/null 2>&1; then
echo "Now using system version of io.js: $(iojs --version 2>/dev/null) $NPM_VERSION"
echo "Now using system version of io.js: $(iojs --version 2>/dev/null)$(nvm_print_npm_version)"
return
else
echo "System version of node not found." >&2
@ -1410,14 +1412,10 @@ nvm() {
if [ "$NVM_SYMLINK_CURRENT" = true ]; then
command rm -f "$NVM_DIR/current" && ln -s "$NVM_VERSION_DIR" "$NVM_DIR/current"
fi
local NPM_VERSION
if nvm_has "npm"; then
NPM_VERSION="(npm v$(npm --version 2>/dev/null))"
fi
if nvm_is_iojs_version "$VERSION"; then
echo "Now using io.js $(nvm_strip_iojs_prefix "$VERSION") $NPM_VERSION"
echo "Now using io.js $(nvm_strip_iojs_prefix "$VERSION")$(nvm_print_npm_version)"
else
echo "Now using node $VERSION $NPM_VERSION"
echo "Now using node $VERSION$(nvm_print_npm_version)"
fi
;;
"run" )

View File

@ -5,9 +5,13 @@ die () { echo $@ ; exit 1; }
. ../../nvm.sh
nvm_has_system_node() { return 0; }
[ "$(nvm use system 2>&1 | tail -n1)" = "Now using system version of node: $(node -v) (npm v$(npm -v))" ] || die "Could not use system version of node"
nvm_print_npm_version() { return ' (npm v1.2.3)'; }
EXPECTED_OUTPUT="Now using system version of node: $(node -v)$(nvm_print_npm_version)"
[ "$(nvm use system 2>&1 | tail -n1)" = "$EXPECTED_OUTPUT" ] || die "Could not use system version of node"
nvm_has_system_node() { return 1; }
[ "$(nvm use system 2>&1 | tail -n1)" = "System version of node not found." ] || die "Did not report error, system node not found"
nvm_print_npm_version() { return ''; }
EXPECTED_OUTPUT="System version of node not found."
[ "$(nvm use system 2>&1 | tail -n1)" = "$EXPECTED_OUTPUT" ] || die "Did not report error, system node not found"
nvm use system 2>&1 > /dev/null || [ $? -eq 127 ] || die "Did not return error code, system node not found"

View File

@ -0,0 +1,28 @@
#!/bin/sh
cleanup () {
alias nvm_has='\nvm_has'
alias npm='\npm'
unset -f nvm_has npm
}
die () { echo $@ ; exit 1; }
. ../../../nvm.sh
nvm_has() { return 1; }
OUTPUT="$(nvm_print_npm_version)"
[ -z "$OUTPUT" ] || die "nvm_print_npm_version did not return empty when nvm_has returns 1, got '$OUTPUT'"
nvm_has() { return 0; }
npm() {
if [ "_$@" = "_--version" ]; then
echo "1.2.3"
else
echo "error"
fi
}
OUTPUT="$(nvm_print_npm_version)"
EXPECTED_OUTPUT=" (npm v1.2.3)"
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "nvm_print_npm_version did not provided '$EXPECTED_OUTPUT', got '$OUTPUT'"
cleanup