Merge pull request #408 from creationix/nvm_system_awareness

Add `system` awareness to `nvm use`, `nvm ls`, and `nvm current`.
master
Jordan Harband 2014-07-21 22:55:27 -07:00
commit 8cbf149558
4 changed files with 64 additions and 3 deletions

23
nvm.sh
View File

@ -160,7 +160,15 @@ nvm_binary_available() {
} }
nvm_ls_current() { nvm_ls_current() {
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` echo `node -v 2>/dev/null`
else
echo 'system'
fi
} }
nvm_ls() { nvm_ls() {
@ -194,6 +202,9 @@ nvm_ls() {
echo "N/A" echo "N/A"
return 3 return 3
fi fi
if [ -z "$PATTERN" ] && nvm_has_system_node; then
VERSIONS="$VERSIONS$(printf '\n%s' 'system')"
fi
echo "$VERSIONS" echo "$VERSIONS"
return return
} }
@ -251,6 +262,8 @@ nvm_print_versions() {
FORMAT='\033[0;32m-> %9s\033[0m' FORMAT='\033[0;32m-> %9s\033[0m'
elif [ -d "$NVM_DIR/$VERSION" ]; then elif [ -d "$NVM_DIR/$VERSION" ]; then
FORMAT='\033[0;34m%12s\033[0m' FORMAT='\033[0;34m%12s\033[0m'
elif [ "$VERSION" = "system" ]; then
FORMAT='\033[0;33m%12s\033[0m'
else else
FORMAT='%12s' FORMAT='%12s'
fi fi
@ -536,9 +549,19 @@ nvm() {
if [ -n "$NVM_RC_VERSION" ]; then if [ -n "$NVM_RC_VERSION" ]; then
VERSION=`nvm_version $NVM_RC_VERSION` VERSION=`nvm_version $NVM_RC_VERSION`
fi fi
else
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 else
VERSION=`nvm_version $2` VERSION=`nvm_version $2`
fi fi
fi
if [ -z "$VERSION" ]; then if [ -z "$VERSION" ]; then
nvm help nvm help
return 127 return 127

View File

@ -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'

View File

@ -3,4 +3,8 @@
die () { echo $@ ; exit 1; } die () { echo $@ ; exit 1; }
. ../../nvm.sh . ../../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'

View File

@ -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"