Browse Source

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

master
Jordan Harband 10 years ago
parent
commit
cbf0f12aac
  1. 22
      nvm.sh
  2. 8
      test/fast/Running "nvm use system" should work as expected
  3. 28
      test/fast/Unit tests/nvm_print_npm_version

22
nvm.sh

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

8
test/fast/Running "nvm use system" should work as expected

@ -5,9 +5,13 @@ die () { echo $@ ; exit 1; }
. ../../nvm.sh . ../../nvm.sh
nvm_has_system_node() { return 0; } 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_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" nvm use system 2>&1 > /dev/null || [ $? -eq 127 ] || die "Did not return error code, system node not found"

28
test/fast/Unit tests/nvm_print_npm_version

@ -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
Loading…
Cancel
Save