From 6686b65a0f3cf9c904908c294b6ea31b7a83f023 Mon Sep 17 00:00:00 2001 From: Thomas Fritz Date: Thu, 23 Aug 2012 17:47:57 +0200 Subject: [PATCH 1/2] Added nvm standalone shell script. Therefore added check if current node executable is wihtin the auto detected NVM_DIR. If not it will return 'N/A'. So it will not display any 'current version' as long as there is no installed node version within this current NVM_DIR. There is also a 'which', 'root' and 'bin' command. 'which' returns the absolute path to the currently used node version. 'root' prints the absolute path to root path of the currently used node version - the path where the bin and lib dir is. And 'bin' prints the absolute path to the 'bin' directory of the currently used node version. --- nvm | 4 ++++ nvm.sh | 60 +++++++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 51 insertions(+), 13 deletions(-) create mode 100755 nvm mode change 100755 => 100644 nvm.sh diff --git a/nvm b/nvm new file mode 100755 index 0000000..b310b02 --- /dev/null +++ b/nvm @@ -0,0 +1,4 @@ +#!/bin/bash +source "$(cd $(dirname $0);pwd -P)/nvm.sh" +nvm "$@" +exit $? \ No newline at end of file diff --git a/nvm.sh b/nvm.sh old mode 100755 new mode 100644 index f88486e..b8488f5 --- a/nvm.sh +++ b/nvm.sh @@ -4,7 +4,6 @@ # # Implemented by Tim Caswell # with much bash help from Matthew Ranney - # Auto detect the NVM_DIR if [ ! -d "$NVM_DIR" ]; then export NVM_DIR=$(cd $(dirname ${BASH_SOURCE[0]:-$0}) && pwd) @@ -37,7 +36,13 @@ nvm_ls() { PATTERN=$1 VERSIONS='' + CURRENT_NODE="$(which node)" + if [ "$PATTERN" = 'current' ]; then + if [ "${CURRENT_NODE##$NVM_DIR}" = "${CURRENT_NODE}" ]; then + echo "N/A" + return + fi echo `node -v 2>/dev/null` return fi @@ -85,18 +90,21 @@ nvm() echo "Node Version Manager" echo echo "Usage:" - echo " nvm help Show this message" - echo " nvm install Download and install a " - echo " nvm uninstall Uninstall a version" - echo " nvm use Modify PATH to use " - echo " nvm run [] Run with as arguments" - echo " nvm ls List installed versions" - echo " nvm ls List versions matching a given description" - echo " nvm deactivate Undo effects of NVM on current shell" - echo " nvm alias [] Show all aliases beginning with " - echo " nvm alias Set an alias named pointing to " - echo " nvm unalias Deletes the alias named " - echo " nvm copy-packages Install global NPM packages contained in to current version" + echo " nvm help Show this message" + echo " nvm install Download and install a " + echo " nvm uninstall Uninstall a version" + echo " nvm use Modify PATH to use " + echo " nvm run [] [] Run with as arguments" + echo " nvm which [] Prints the path of the currently used node executable" + echo " nvm root [] Prints the root path of the currently used node version" + echo " nvm bin [] Prints the path to the 'bin' directory of the currently used node version" + echo " nvm ls List installed versions" + echo " nvm ls List versions matching a given description" + echo " nvm deactivate Undo effects of NVM on current shell" + echo " nvm alias [] Show all aliases beginning with " + echo " nvm alias Set an alias named pointing to " + echo " nvm unalias Deletes the alias named " + echo " nvm copy-packages Install global NPM packages contained in to current version" echo echo "Example:" echo " nvm install v0.4.12 Install a specific version number" @@ -249,6 +257,32 @@ nvm() echo "Running node $VERSION" $NVM_DIR/$VERSION/bin/node "${@:3}" ;; + "which" ) + # prints out the current node binary + VERSION=`nvm_version $2` + if [ ! -d $NVM_DIR/$VERSION ]; then + echo "$VERSION version is not installed yet" + return; + fi + echo $NVM_DIR/$VERSION/bin/node + ;; + "bin" ) + # prints out the current node binary + VERSION=`nvm_version $2` + if [ ! -d $NVM_DIR/$VERSION ]; then + echo "$VERSION version is not installed yet" + return; + fi + echo $NVM_DIR/$VERSION/bin + ;; + "root" ) + VERSION=`nvm_version $2` + if [ ! -d $NVM_DIR/$VERSION ]; then + echo "$VERSION version is not installed yet" + return; + fi + echo $NVM_DIR/$VERSION + ;; "ls" | "list" ) print_versions "`nvm_ls $2`" if [ $# -eq 1 ]; then From 103b30d03c5ac5cb2db550ce07701f79de6d6c2a Mon Sep 17 00:00:00 2001 From: Thomas Fritz Date: Fri, 24 Aug 2012 16:50:52 +0200 Subject: [PATCH 2/2] Added return / exit codes. 1 if unexpected. 0 when everything ok. --- nvm.sh | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/nvm.sh b/nvm.sh index b8488f5..074f502 100644 --- a/nvm.sh +++ b/nvm.sh @@ -262,26 +262,29 @@ nvm() VERSION=`nvm_version $2` if [ ! -d $NVM_DIR/$VERSION ]; then echo "$VERSION version is not installed yet" - return; + return 1; fi echo $NVM_DIR/$VERSION/bin/node + return 0; ;; "bin" ) # prints out the current node binary VERSION=`nvm_version $2` if [ ! -d $NVM_DIR/$VERSION ]; then echo "$VERSION version is not installed yet" - return; + return 1; fi echo $NVM_DIR/$VERSION/bin + return 0; ;; "root" ) VERSION=`nvm_version $2` if [ ! -d $NVM_DIR/$VERSION ]; then echo "$VERSION version is not installed yet" - return; + return 1; fi echo $NVM_DIR/$VERSION + return 0; ;; "ls" | "list" ) print_versions "`nvm_ls $2`"