diff --git a/nvm.sh b/nvm.sh index 609474d..d0335e1 100644 --- a/nvm.sh +++ b/nvm.sh @@ -160,7 +160,15 @@ nvm_binary_available() { } nvm_ls_current() { - echo `node -v 2>/dev/null` + local NODE_PATH + NODE_PATH="$(which node)" + if [ $? -ne 0 ]; then + echo 'none' + elif nvm_tree_contains_path "$NVM_DIR" "$NODE_PATH"; then + echo `node -v 2>/dev/null` + else + echo 'system' + fi } nvm_ls() { @@ -194,6 +202,9 @@ nvm_ls() { echo "N/A" return 3 fi + if [ -z "$PATTERN" ] && nvm_has_system_node; then + VERSIONS="$VERSIONS$(printf '\n%s' 'system')" + fi echo "$VERSIONS" return } @@ -251,6 +262,8 @@ nvm_print_versions() { FORMAT='\033[0;32m-> %9s\033[0m' elif [ -d "$NVM_DIR/$VERSION" ]; then FORMAT='\033[0;34m%12s\033[0m' + elif [ "$VERSION" = "system" ]; then + FORMAT='\033[0;33m%12s\033[0m' else FORMAT='%12s' fi @@ -537,7 +550,17 @@ nvm() { VERSION=`nvm_version $NVM_RC_VERSION` fi else - VERSION=`nvm_version $2` + if [ $2 = 'system' ]; then + if nvm_has_system_node && nvm deactivate; then + echo "Now using system version of node: $(node -v 2>/dev/null)." + return + else + echo "System version of node not found." >&2 + return 127 + fi + else + VERSION=`nvm_version $2` + fi fi if [ -z "$VERSION" ]; then nvm help diff --git "a/test/fast/Listing versions/Running \"nvm ls\" should include \"system\" when appropriate" "b/test/fast/Listing versions/Running \"nvm ls\" should include \"system\" when appropriate" new file mode 100755 index 0000000..9afd40a --- /dev/null +++ "b/test/fast/Listing versions/Running \"nvm ls\" should include \"system\" when appropriate" @@ -0,0 +1,21 @@ +#!/bin/sh + +die () { echo $@ ; exit 1; } + +. ../../../nvm.sh + +mkdir -p ../../../v0.0.1 +mkdir -p ../../../v0.0.3 +mkdir -p ../../../v0.0.9 +mkdir -p ../../../v0.3.1 +mkdir -p ../../../v0.3.3 +mkdir -p ../../../v0.3.9 + +nvm_has_system_node() { return 0; } +nvm ls | grep system 2>&1 > /dev/null +[ $? -eq 0 ] || die '"nvm ls" did not contain "system" when system node is present' + +nvm_has_system_node() { return 1; } +nvm ls | grep system 2>&1 > /dev/null +[ $? -ne 0 ] || die '"nvm ls" contained "system" when system node is not present' + diff --git "a/test/fast/Running \"nvm current\" should display current nvm environment." "b/test/fast/Running \"nvm current\" should display current nvm environment." index d9293ed..5748fd6 100755 --- "a/test/fast/Running \"nvm current\" should display current nvm environment." +++ "b/test/fast/Running \"nvm current\" should display current nvm environment." @@ -3,4 +3,8 @@ die () { echo $@ ; exit 1; } . ../../nvm.sh -[ "$(nvm current)" = "$(node -v)" ] || die "Failed to find current version" + +nvm deactivate 2>&1 + +[ "$(nvm current)" = "system" ] || [ "$(nvm current)" = "none" ] || die '"nvm current" did not report "system" or "none" when deactivated' + diff --git "a/test/fast/Running \"nvm use system\" should work as expected" "b/test/fast/Running \"nvm use system\" should work as expected" new file mode 100755 index 0000000..9b4e850 --- /dev/null +++ "b/test/fast/Running \"nvm use system\" should work as expected" @@ -0,0 +1,13 @@ +#!/bin/sh + +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)." ] || 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 use system 2>&1 > /dev/null || [ $? -eq 127 ] || die "Did not return error code, system node not found" +