Merge pull request #583 from danielb2/which

Adds `nvm which`
master
Jordan Harband 2014-12-17 01:26:50 -08:00
commit 74b36b09ff
5 changed files with 70 additions and 0 deletions

View File

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

38
nvm.sh
View File

@ -547,6 +547,7 @@ nvm() {
echo " nvm unalias <name> Deletes the alias named <name>"
echo " nvm reinstall-packages <version> Reinstall global \`npm\` packages contained in <version> to current version"
echo " nvm unload Unload \`nvm\` from shell"
echo " nvm which [<version>] 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

View File

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

View File

@ -0,0 +1,6 @@
#!/bin/sh
. ../../../nvm.sh
nvm which nonexistent_version
[ "$?" = "1" ]

View File

@ -0,0 +1,2 @@
rmdir ../../../v0.0.2 >/dev/null 2>&1
rmdir ../../../v0.0.20 >/dev/null 2>&1