Create `nvm_print_npm_version` and use that when printing the `npm` version.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Jordan Harband · committed by Jordan Harband · parent de93dc4 · Browse files · 3 files·+44 added−14 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+44 added−14 removed
- tests:+34 added−2 removed
- other:+10 added−12 removed
Provenance
- Commit
cbf0f12- Tree
2c7c84f- Author
Jordan Harband <ljharb@gmail.com>- Committer
Jordan Harband <ljharb@gmail.com>- Parent
de93dc4
3 files changed+44 added−14 removed
added:test/fast/Unit tests/nvm_print_npm_version+28 added
1+#!/bin/sh2+3+cleanup () {4+ alias nvm_has='\nvm_has'5+ alias npm='\npm'6+ unset -f nvm_has npm7+}8+die () { echo $@ ; exit 1; }9+10+. ../../../nvm.sh11+12+nvm_has() { return 1; }13+OUTPUT="$(nvm_print_npm_version)"14+[ -z "$OUTPUT" ] || die "nvm_print_npm_version did not return empty when nvm_has returns 1, got '$OUTPUT'"15+16+nvm_has() { return 0; }17+npm() {18+ if [ "_$@" = "_--version" ]; then19+ echo "1.2.3"20+ else21+ echo "error"22+ fi23+}24+OUTPUT="$(nvm_print_npm_version)"25+EXPECTED_OUTPUT=" (npm v1.2.3)"26+[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "nvm_print_npm_version did not provided '$EXPECTED_OUTPUT', got '$OUTPUT'"27+28+cleanupmodified:test/fast/Running "nvm use system" should work as expected+6 added-2 removed
3 unmodified lines4 5 . ../../nvm.sh6 7 nvm_has_system_node() { return 0; }8+nvm_print_npm_version() { return ' (npm v1.2.3)'; }9+EXPECTED_OUTPUT="Now using system version of node: $(node -v)$(nvm_print_npm_version)"8-[ "$(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"10+[ "$(nvm use system 2>&1 | tail -n1)" = "$EXPECTED_OUTPUT" ] || die "Could not use system version of node"11 12 nvm_has_system_node() { return 1; }13+nvm_print_npm_version() { return ''; }14+EXPECTED_OUTPUT="System version of node not found."11-[ "$(nvm use system 2>&1 | tail -n1)" = "System version of node not found." ] || die "Did not report error, system node not found"15+[ "$(nvm use system 2>&1 | tail -n1)" = "$EXPECTED_OUTPUT" ] || die "Did not report error, system node not found"16 nvm use system 2>&1 > /dev/null || [ $? -eq 127 ] || die "Did not return error code, system node not found"17 modified:nvm.sh+10 added-12 removed
56 unmodified lines57 nvm_has_system_iojs() {58 [ "$(nvm deactivate >/dev/null 2>&1 && command -v iojs)" != '' ]59 }60 61+nvm_print_npm_version() {62+ if nvm_has "npm"; then63+ npm --version 2>/dev/null | command xargs printf " (npm v%s)"64+ fi65+}66+67 # Make zsh glob matching behave same as bash68 # This fixes the "zsh: no matches found" errors69 if nvm_has "unsetopt"; then70 unsetopt nomatch 2>/dev/null1293 unmodified lines1364 return 1271365 fi1366 1367 if [ "_$VERSION" = '_system' ]; then1362- local NPM_VERSION1363- if nvm_has "npm"; then1364- NPM_VERSION="(npm v$(npm --version 2>/dev/null))"1365- fi1368 if nvm_has_system_node && nvm deactivate >/dev/null 2>&1; then1367- echo "Now using system version of node: $(node -v 2>/dev/null) $NPM_VERSION"1369+ echo "Now using system version of node: $(node -v 2>/dev/null)$(nvm_print_npm_version)"1370 return1371 elif nvm_has_system_iojs && nvm deactivate >/dev/null 2>&1; then1370- echo "Now using system version of io.js: $(iojs --version 2>/dev/null) $NPM_VERSION"1372+ echo "Now using system version of io.js: $(iojs --version 2>/dev/null)$(nvm_print_npm_version)"1373 return1374 else1375 echo "System version of node not found." >&21376 return 12734 unmodified lines1411 export NVM_BIN="$NVM_VERSION_DIR/bin"1412 if [ "$NVM_SYMLINK_CURRENT" = true ]; then1413 command rm -f "$NVM_DIR/current" && ln -s "$NVM_VERSION_DIR" "$NVM_DIR/current"1414 fi1413- local NPM_VERSION1414- if nvm_has "npm"; then1415- NPM_VERSION="(npm v$(npm --version 2>/dev/null))"1416- fi1415 if nvm_is_iojs_version "$VERSION"; then1418- echo "Now using io.js $(nvm_strip_iojs_prefix "$VERSION") $NPM_VERSION"1416+ echo "Now using io.js $(nvm_strip_iojs_prefix "$VERSION")$(nvm_print_npm_version)"1417 else1420- echo "Now using node $VERSION $NPM_VERSION"1418+ echo "Now using node $VERSION$(nvm_print_npm_version)"1419 fi1420 ;;1421 "run" )1422 local provided_version345 unmodified lines