From 382c3aa85ec9e07a26438e6e4e3d7be5d6abcd55 Mon Sep 17 00:00:00 2001 From: Daniel Bretoi Date: Fri, 21 Nov 2014 16:54:31 -0800 Subject: [PATCH 1/8] implements nvm which --- nvm.sh | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/nvm.sh b/nvm.sh index 969af37..d8dcfae 100644 --- a/nvm.sh +++ b/nvm.sh @@ -547,6 +547,7 @@ nvm() { echo " nvm unalias Deletes the alias named " echo " nvm copy-packages Install global NPM packages contained in to current version" echo " nvm unload Unload NVM from shell" + echo " nvm which [] Display path to installed node version" echo echo "Example:" echo " nvm install v0.10.24 Install a specific version number" @@ -949,6 +950,40 @@ nvm() { "current" ) nvm_version current ;; + "which" ) + INPUT=$2 + + if [ "_$2" != '_system' ]; then + VERSION="$(nvm_version "$INPUT")" + else + VERSION="$INPUT" + fi + if [ -z "$VERSION" ]; then + nvm help + return 127 + fi + + if [ "_$VERSION" = '_system' ]; then + if nvm_has_system_node && nvm deactivate >/dev/null 2>&1; then + echo $(dirname `which node`) + return + else + echo "System version of node not found." >&2 + return 127 + fi + elif [ "_$VERSION" = "_∞" ]; then + echo "The alias \"$2\" leads to an infinite loop. Aborting." >&2 + return 8 + fi + + local NVM_VERSION_DIR + NVM_VERSION_DIR="$(nvm_version_path "$VERSION")" + if [ ! -d "$NVM_VERSION_DIR" ]; then + echo "$VERSION version is not installed yet" >&2 + return 1 + fi + echo $NVM_DIR/$VERSION + ;; "alias" ) mkdir -p "$NVM_DIR/alias" if [ $# -le 2 ]; then From b63042bd9f5d2e081d1d1c748d75ff242443186c Mon Sep 17 00:00:00 2001 From: Daniel Bretoi Date: Fri, 21 Nov 2014 16:59:15 -0800 Subject: [PATCH 2/8] forgot to append bin --- nvm.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nvm.sh b/nvm.sh index d8dcfae..e25a148 100644 --- a/nvm.sh +++ b/nvm.sh @@ -982,7 +982,7 @@ nvm() { echo "$VERSION version is not installed yet" >&2 return 1 fi - echo $NVM_DIR/$VERSION + echo $NVM_DIR/$VERSION/bin ;; "alias" ) mkdir -p "$NVM_DIR/alias" From ee520abf78add0afcd9f52311c7467dab26c1e11 Mon Sep 17 00:00:00 2001 From: Daniel Bretoi Date: Fri, 21 Nov 2014 17:51:49 -0800 Subject: [PATCH 3/8] adding some tests for nvm which --- ....0.2\" should display only version 0.0.2." | 20 +++++++++++++++++++ test/fast/Listing paths/teardown | 17 ++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100755 "test/fast/Listing paths/Running \"nvm which 0.0.2\" should display only version 0.0.2." create mode 100755 test/fast/Listing paths/teardown diff --git "a/test/fast/Listing paths/Running \"nvm which 0.0.2\" should display only version 0.0.2." "b/test/fast/Listing paths/Running \"nvm which 0.0.2\" should display only version 0.0.2." new file mode 100755 index 0000000..53ddf30 --- /dev/null +++ "b/test/fast/Listing paths/Running \"nvm which 0.0.2\" should display only version 0.0.2." @@ -0,0 +1,20 @@ +#!/bin/sh + +mkdir ../../../v0.0.2 +mkdir ../../../v0.0.20 + +. ../../../nvm.sh + +die () { echo $@ ; exit 1; } + +# The result should contain only the appropriate version numbers. + +nvm which 0.0.2 | grep "$NVM_DIR/v0.0.2/bin" > /dev/null +if [ $? -ne 0 ]; then + die '"nvm which 0.0.2" did not contain the correct path' +fi + +nvm which 0.0.20 | grep "$NVM_DIR/v0.0.20/bin" > /dev/null +if [ $? -ne 0 ]; then + die '"nvm which 0.0.2" did not contain the correct path' +fi diff --git a/test/fast/Listing paths/teardown b/test/fast/Listing paths/teardown new file mode 100755 index 0000000..25669ea --- /dev/null +++ b/test/fast/Listing paths/teardown @@ -0,0 +1,17 @@ +rmdir ../../../v0.0.1 >/dev/null 2>&1 +rmdir ../../../v0.0.2 >/dev/null 2>&1 +rmdir ../../../v0.0.3 >/dev/null 2>&1 +rmdir ../../../v0.0.9 >/dev/null 2>&1 +rmdir ../../../v0.0.20 >/dev/null 2>&1 +rmdir ../../../v0.1.2 >/dev/null 2>&1 +rmdir ../../../v0.1.3 >/dev/null 2>&1 +rmdir ../../../v0.2.3 >/dev/null 2>&1 +rmdir ../../../v0.3.1 >/dev/null 2>&1 +rmdir ../../../v0.3.3 >/dev/null 2>&1 +rmdir ../../../v0.3.9 >/dev/null 2>&1 +rmdir ../../../v0.20.3 >/dev/null 2>&1 +rmdir ../../../versions >/dev/null 2>&1 +unalias nvm_has_system_node >/dev/null 2>&1 +rm -f ../../../alias/stable >/dev/null 2>&1 +rm -f ../../../alias/unstable >/dev/null 2>&1 + From 8b25457b74c52745be865aba8f9134d6ccd1197c Mon Sep 17 00:00:00 2001 From: Daniel Bretoi Date: Fri, 21 Nov 2014 17:53:08 -0800 Subject: [PATCH 4/8] remove redundant lines from test file --- test/fast/Listing paths/teardown | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/test/fast/Listing paths/teardown b/test/fast/Listing paths/teardown index 25669ea..2bc7383 100755 --- a/test/fast/Listing paths/teardown +++ b/test/fast/Listing paths/teardown @@ -1,17 +1,2 @@ -rmdir ../../../v0.0.1 >/dev/null 2>&1 rmdir ../../../v0.0.2 >/dev/null 2>&1 -rmdir ../../../v0.0.3 >/dev/null 2>&1 -rmdir ../../../v0.0.9 >/dev/null 2>&1 rmdir ../../../v0.0.20 >/dev/null 2>&1 -rmdir ../../../v0.1.2 >/dev/null 2>&1 -rmdir ../../../v0.1.3 >/dev/null 2>&1 -rmdir ../../../v0.2.3 >/dev/null 2>&1 -rmdir ../../../v0.3.1 >/dev/null 2>&1 -rmdir ../../../v0.3.3 >/dev/null 2>&1 -rmdir ../../../v0.3.9 >/dev/null 2>&1 -rmdir ../../../v0.20.3 >/dev/null 2>&1 -rmdir ../../../versions >/dev/null 2>&1 -unalias nvm_has_system_node >/dev/null 2>&1 -rm -f ../../../alias/stable >/dev/null 2>&1 -rm -f ../../../alias/unstable >/dev/null 2>&1 - From c9a53fe0c751e001dd697ba5705de93d8f6e22f6 Mon Sep 17 00:00:00 2001 From: Daniel Bretoi Date: Fri, 21 Nov 2014 17:55:46 -0800 Subject: [PATCH 5/8] include nvm which in documentaiton --- README.markdown | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.markdown b/README.markdown index bdf9007..8ae1af7 100644 --- a/README.markdown +++ b/README.markdown @@ -61,6 +61,10 @@ Or, you can run any arbitrary command in a subshell with the desired version of nvm exec 0.10 node --version +You can also get the path to the executable to where it was installed: + + nvm which 0.10 + In place of a version pointer like "0.10", you can use the special default aliases "stable" and "unstable": nvm install stable From 0c33fd2598be65979edcba2029c83a5cc284a8b0 Mon Sep 17 00:00:00 2001 From: Daniel Bretoi Date: Sat, 22 Nov 2014 08:17:15 -0800 Subject: [PATCH 6/8] change to use default behavior for which --- nvm.sh | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/nvm.sh b/nvm.sh index e25a148..d3aeae7 100644 --- a/nvm.sh +++ b/nvm.sh @@ -951,12 +951,15 @@ nvm() { nvm_version current ;; "which" ) - INPUT=$2 - - if [ "_$2" != '_system' ]; then - VERSION="$(nvm_version "$INPUT")" + if [ $# -eq 1 ]; then + nvm_rc_version + if [ -n "$NVM_RC_VERSION" ]; then + VERSION=`nvm_version $NVM_RC_VERSION` + fi + elif [ "_$2" != '_system' ]; then + VERSION="$(nvm_version "$2")" else - VERSION="$INPUT" + VERSION="$2" fi if [ -z "$VERSION" ]; then nvm help From 234b925c7a67f7975682b7bc9761b5b90b889f36 Mon Sep 17 00:00:00 2001 From: Daniel Bretoi Date: Tue, 2 Dec 2014 12:40:23 -0800 Subject: [PATCH 7/8] style and remove nvm deactivate in nvm which --- nvm.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nvm.sh b/nvm.sh index d3aeae7..e3f4b3b 100644 --- a/nvm.sh +++ b/nvm.sh @@ -954,7 +954,7 @@ nvm() { if [ $# -eq 1 ]; then nvm_rc_version if [ -n "$NVM_RC_VERSION" ]; then - VERSION=`nvm_version $NVM_RC_VERSION` + VERSION=$(nvm_version $NVM_RC_VERSION) fi elif [ "_$2" != '_system' ]; then VERSION="$(nvm_version "$2")" @@ -967,7 +967,7 @@ nvm() { fi if [ "_$VERSION" = '_system' ]; then - if nvm_has_system_node && nvm deactivate >/dev/null 2>&1; then + if nvm_has_system_node >/dev/null 2>&1; then echo $(dirname `which node`) return else From a1c0c34088f67ff0884134ceded8a00148d674b2 Mon Sep 17 00:00:00 2001 From: Daniel Bretoi Date: Tue, 2 Dec 2014 12:58:07 -0800 Subject: [PATCH 8/8] fix to proper echo of nvm which. Checkin test which had been left out --- nvm.sh | 2 +- ... foo\" should return a nonzero exit code when not found" | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100755 "test/fast/Listing paths/Running \"nvm which foo\" should return a nonzero exit code when not found" diff --git a/nvm.sh b/nvm.sh index e3f4b3b..9a42199 100644 --- a/nvm.sh +++ b/nvm.sh @@ -968,7 +968,7 @@ nvm() { if [ "_$VERSION" = '_system' ]; then if nvm_has_system_node >/dev/null 2>&1; then - echo $(dirname `which node`) + echo $(nvm use system && echo dirname $(which node)) return else echo "System version of node not found." >&2 diff --git "a/test/fast/Listing paths/Running \"nvm which foo\" should return a nonzero exit code when not found" "b/test/fast/Listing paths/Running \"nvm which foo\" should return a nonzero exit code when not found" new file mode 100755 index 0000000..43b2bae --- /dev/null +++ "b/test/fast/Listing paths/Running \"nvm which foo\" should return a nonzero exit code when not found" @@ -0,0 +1,6 @@ + #!/bin/sh + +. ../../../nvm.sh + +nvm which nonexistent_version +[ "$?" = "1" ]