Preserve PATH locations order when changing versions

Xandor Schiefer 2016-11-14 19:19:39 +02:00 committed by Jordan Harband
parent 7b8af94fc9
commit 334897ae39
No known key found for this signature in database
GPG Key ID: 64A196AEE0916D55
4 changed files with 66 additions and 35 deletions

30
nvm.sh
View File

@ -601,11 +601,21 @@ nvm_strip_path() {
-e "s#${NVM_DIR}/versions/[^/]*/[^/]*${2-}[^:]*##g" -e "s#${NVM_DIR}/versions/[^/]*/[^/]*${2-}[^:]*##g"
} }
nvm_prepend_path() { nvm_change_path() {
# if theres no initial path, just return the supplementary path
if [ -z "${1-}" ]; then if [ -z "${1-}" ]; then
nvm_echo "${2-}" nvm_echo "${3-}${2-}"
# if the initial path doesnt contain an nvm path, prepend the supplementary
# path
elif ! echo "${1-}" | grep -q "${NVM_DIR}/[^/]*${2-}" && \
! echo "${1-}" | grep -q "${NVM_DIR}/versions/[^/]*/[^/]*${2-}"; then
nvm_echo "${3-}${2-}:${1-}"
# use sed to replace the existing nvm path with the supplementary path. This
# preserves the order of the path.
else else
nvm_echo "${2-}:${1-}" nvm_echo "${1-}" | command sed \
-e "s#${NVM_DIR}/[^/]*${2-}[^:]*#${3-}${2-}#g" \
-e "s#${NVM_DIR}/versions/[^/]*/[^/]*${2-}[^:]*#${3-}${2-}#g"
fi fi
} }
@ -2866,19 +2876,15 @@ nvm() {
local NVM_VERSION_DIR local NVM_VERSION_DIR
NVM_VERSION_DIR="$(nvm_version_path "$VERSION")" NVM_VERSION_DIR="$(nvm_version_path "$VERSION")"
# Strip other version from PATH # Change current version
PATH="$(nvm_strip_path "$PATH" "/bin")" PATH="$(nvm_change_path "$PATH" "/bin" "$NVM_VERSION_DIR")"
# Prepend current version
PATH="$(nvm_prepend_path "$PATH" "$NVM_VERSION_DIR/bin")"
if nvm_has manpath; then if nvm_has manpath; then
if [ -z "${MANPATH-}" ]; then if [ -z "${MANPATH-}" ]; then
local MANPATH local MANPATH
MANPATH=$(manpath) MANPATH=$(manpath)
fi fi
# Strip other version from MANPATH # Change current version
MANPATH="$(nvm_strip_path "$MANPATH" "/share/man")" MANPATH="$(nvm_change_path "$MANPATH" "/share/man" "$NVM_VERSION_DIR")"
# Prepend current version
MANPATH="$(nvm_prepend_path "$MANPATH" "$NVM_VERSION_DIR/share/man")"
export MANPATH export MANPATH
fi fi
export PATH export PATH
@ -3387,7 +3393,7 @@ nvm() {
nvm_ensure_default_set nvm_get_arch nvm_get_os \ nvm_ensure_default_set nvm_get_arch nvm_get_os \
nvm_print_implicit_alias nvm_validate_implicit_alias \ nvm_print_implicit_alias nvm_validate_implicit_alias \
nvm_resolve_alias nvm_ls_current nvm_alias \ nvm_resolve_alias nvm_ls_current nvm_alias \
nvm_binary_available nvm_prepend_path nvm_strip_path \ nvm_binary_available nvm_change_path nvm_strip_path \
nvm_num_version_groups nvm_format_version nvm_ensure_version_prefix \ nvm_num_version_groups nvm_format_version nvm_ensure_version_prefix \
nvm_normalize_version nvm_is_valid_version \ nvm_normalize_version nvm_is_valid_version \
nvm_ensure_version_installed nvm_cache_dir \ nvm_ensure_version_installed nvm_cache_dir \

View File

@ -6,17 +6,17 @@ mkdir -p ../../v0.2.3
die () { echo "$@" ; exit 1; } die () { echo "$@" ; exit 1; }
[ `expr $PATH : ".*v0.2.3/.*/bin"` = 0 ] || echo "WARNING: Unexpectedly found v0.2.3 already active" >&2 [ `expr $PATH : ".*v0.2.3/.*/bin.*"` = 0 ] || echo "WARNING: Unexpectedly found v0.2.3 already active" >&2
\. ../../nvm.sh \. ../../nvm.sh
nvm use --delete-prefix v0.2.3 || die "Failed to activate v0.2.3" nvm use --delete-prefix v0.2.3 || die "Failed to activate v0.2.3"
[ `expr "$PATH" : ".*v0.2.3/.*/bin"` != 0 ] || die "PATH not set up properly" [ `expr "$PATH" : ".*v0.2.3/.*/bin.*"` != 0 ] || die "PATH not set up properly"
[ `expr "$NODE_PATH" : ".*v0.2.3/.*/lib/node_modules"` = 0 ] || die "NODE_PATH should not contain (npm root -g)" [ `expr "$NODE_PATH" : ".*v0.2.3/.*/lib/node_modules.*"` = 0 ] || die "NODE_PATH should not contain (npm root -g)"
# ^ note: NODE_PATH should not contain `npm root -g` since globals should not be requireable # ^ note: NODE_PATH should not contain `npm root -g` since globals should not be requireable
[ `expr "$NVM_BIN" : ".*v0.2.3/bin"` != 0 ] || die "NODE_BIN should contain bin directory path" [ `expr "$NVM_BIN" : ".*v0.2.3/bin"` != 0 ] || die "NODE_BIN should contain bin directory path"
nvm deactivate || die "Failed to deactivate v0.2.3" nvm deactivate || die "Failed to deactivate v0.2.3"
[ `expr "$PATH" : ".*v0.2.3/.*/bin"` = 0 ] || die "PATH not cleaned properly" [ `expr "$PATH" : ".*v0.2.3/.*/bin.*"` = 0 ] || die "PATH not cleaned properly"
[ `expr "$NODE_PATH" : ".*v0.2.3/.*/lib/node_modules"` = 0 ] || die "NODE_PATH not cleaned properly" [ `expr "$NODE_PATH" : ".*v0.2.3/.*/lib/node_modules.*"` = 0 ] || die "NODE_PATH not cleaned properly"
[ "_$NVM_BIN" = "_" ] || die "NVM_BIN should be unset: got '$NVM_BIN'" [ "_$NVM_BIN" = "_" ] || die "NVM_BIN should be unset: got '$NVM_BIN'"

View File

@ -0,0 +1,43 @@
#!/bin/sh
die () { echo "$@" ; exit 1; }
\. ../../../nvm.sh
TEST_PATH=/usr/bin:/usr/local/bin
# New version dir
NEW_PATH=`nvm_change_path "$TEST_PATH" "/bin" "$NVM_DIR/versions/node/v7.1.0"`
[ "$NEW_PATH" = "$NVM_DIR/versions/node/v7.1.0/bin:/usr/bin:/usr/local/bin" ] || die "Not correctly changed: $NEW_PATH "
# Old version dir
NEW_PATH=`nvm_change_path "$TEST_PATH" "/bin" "$NVM_DIR/v0.1.2"`
[ "$NEW_PATH" = "$NVM_DIR/v0.1.2/bin:/usr/bin:/usr/local/bin" ] || die "Not correctly changed: $NEW_PATH "
TEST_PATH=/home/user/code/test/node_modules/.bin:$NVM_DIR/versions/node/v4.5.0/bin:/usr/bin:/usr/local/bin
# New version dir
NEW_PATH=`nvm_change_path "$TEST_PATH" "/bin" "$NVM_DIR/versions/node/v7.1.0"`
[ "$NEW_PATH" = "/home/user/code/test/node_modules/.bin:$NVM_DIR/versions/node/v7.1.0/bin:/usr/bin:/usr/local/bin" ] || die "Not correctly changed: $NEW_PATH "
# Old version dir
NEW_PATH=`nvm_change_path "$TEST_PATH" "/bin" "$NVM_DIR/v0.1.2"`
[ "$NEW_PATH" = "/home/user/code/test/node_modules/.bin:$NVM_DIR/v0.1.2/bin:/usr/bin:/usr/local/bin" ] || die "Not correctly changed: $NEW_PATH "
EMPTY_PATH=
# New version dir
NEW_PATH=`nvm_change_path "$EMPTY_PATH" "/bin" "$NVM_DIR/versions/node/v7.1.0"`
[ "$NEW_PATH" = "$NVM_DIR/versions/node/v7.1.0/bin" ] || die "Not correctly prepended: $NEW_PATH "
# Old version dir
NEW_PATH=`nvm_change_path "$EMPTY_PATH" "/bin" "$NVM_DIR/v0.1.2"`
[ "$NEW_PATH" = "$NVM_DIR/v0.1.2/bin" ] || die "Not correctly prepended: $NEW_PATH "

View File

@ -1,18 +0,0 @@
#!/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: $NEW_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: $NEW_PATH "