From f6ba08253b1cac724c21350e35ecc9ec8dbfcd43 Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Thu, 8 May 2014 10:20:11 +0200 Subject: [PATCH] utility method for append path, fixes #414 --- nvm.sh | 16 ++++++++++++---- test/fast/Unit tests/nvm_prepend_path | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) create mode 100755 test/fast/Unit tests/nvm_prepend_path diff --git a/nvm.sh b/nvm.sh index 5724da7..fa4ec6b 100644 --- a/nvm.sh +++ b/nvm.sh @@ -101,6 +101,14 @@ nvm_strip_path() { echo "$1" | sed -e "s#$NVM_DIR/[^/]*$2[^:]*:##g" -e "s#:$NVM_DIR/[^/]*$2[^:]*##g" -e "s#$NVM_DIR/[^/]*$2[^:]*##g" } +nvm_prepend_path() { + if [ -z "$1" ]; then + echo "$2" + else + echo "$2:$1" + fi +} + nvm_binary_available() { # binaries started with node 0.8.6 local MINIMAL="0.8.6" @@ -478,18 +486,18 @@ nvm() { # Strip other version from PATH PATH=`nvm_strip_path "$PATH" "/bin"` # Prepend current version - PATH="$NVM_DIR/$VERSION/bin:$PATH" + PATH=`nvm_prepend_path "$PATH" "$NVM_DIR/$VERSION/bin"` if [ -z "$MANPATH" ]; then MANPATH=$(manpath) fi # Strip other version from MANPATH MANPATH=`nvm_strip_path "$MANPATH" "/share/man"` # Prepend current version - MANPATH="$NVM_DIR/$VERSION/share/man:$MANPATH" + MANPATH=`nvm_prepend_path "$MANPATH" "$NVM_DIR/$VERSION/share/man"` # Strip other version from NODE_PATH NODE_PATH=`nvm_strip_path "$NODE_PATH" "/lib/node_modules"` # Prepend current version - NODE_PATH="$NVM_DIR/$VERSION/lib/node_modules:$NODE_PATH" + NODE_PATH=`nvm_prepend_path "$NODE_PATH" "$NVM_DIR/$VERSION/lib/node_modules"` export PATH hash -r export MANPATH @@ -535,7 +543,7 @@ nvm() { return; fi RUN_NODE_PATH=`nvm_strip_path "$NODE_PATH" "/lib/node_modules"` - RUN_NODE_PATH="$NVM_DIR/$VERSION/lib/node_modules:$NODE_PATH" + RUN_NODE_PATH=`nvm_prepend_path "$NODE_PATH" "$NVM_DIR/$VERSION/lib/node_modules"` echo "Running node $VERSION" NODE_PATH=$RUN_NODE_PATH $NVM_DIR/$VERSION/bin/node "$@" ;; diff --git a/test/fast/Unit tests/nvm_prepend_path b/test/fast/Unit tests/nvm_prepend_path new file mode 100755 index 0000000..7bc6637 --- /dev/null +++ b/test/fast/Unit tests/nvm_prepend_path @@ -0,0 +1,18 @@ +#!/bin/sh + +die () { echo $@ ; exit 1; } + +. ../../../nvm.sh + +TEST_PATH=/usr/bin:/usr/local/bin + +NEW_PATH=`nvm_prepend_path "$TEST_PATH" "$NVM_DIR/v0.2.5/bin"` + +[ "$NEW_PATH" = "$NVM_DIR/v0.2.5/bin:/usr/bin:/usr/local/bin" ] || die "Not correctly prepended: $STRIPPED_PATH " + + +EMPTY_PATH= + +NEW_PATH=`nvm_prepend_path "$EMPTY_PATH" "$NVM_DIR/v0.2.5/bin"` + +[ "$NEW_PATH" = "$NVM_DIR/v0.2.5/bin" ] || die "Not correctly prepended: $STRIPPED_PATH "