diff --git a/README.markdown b/README.markdown index 3da592b3b4..4eeddae346 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 diff --git a/nvm.sh b/nvm.sh index accb54faef..9709243d3a 100644 --- a/nvm.sh +++ b/nvm.sh @@ -547,6 +547,7 @@ nvm() { echo " nvm unalias Deletes the alias named " echo " nvm reinstall-packages Reinstall 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.32 Install a specific version number" @@ -946,6 +947,43 @@ nvm() { "current" ) nvm_version current ;; + "which" ) + 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="$2" + fi + if [ -z "$VERSION" ]; then + nvm help + return 127 + fi + + if [ "_$VERSION" = '_system' ]; then + if nvm_has_system_node >/dev/null 2>&1; then + echo $(nvm use system && 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/bin + ;; "alias" ) mkdir -p "$NVM_DIR/alias" if [ $# -le 2 ]; then 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 0000000000..53ddf30883 --- /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/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 0000000000..43b2bae341 --- /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" ] diff --git a/test/fast/Listing paths/teardown b/test/fast/Listing paths/teardown new file mode 100755 index 0000000000..2bc7383bb3 --- /dev/null +++ b/test/fast/Listing paths/teardown @@ -0,0 +1,2 @@ +rmdir ../../../v0.0.2 >/dev/null 2>&1 +rmdir ../../../v0.0.20 >/dev/null 2>&1