Skip to content

Commit 4fcf37f

Browse files
committed
[New] support --no-use on sourcing, in supported shells.
Fixes #972.
1 parent e76fe61 commit 4fcf37f

File tree

3 files changed

+68
-12
lines changed

3 files changed

+68
-12
lines changed

nvm-exec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
DIR="$(command cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
44

5-
source "$DIR/nvm.sh"
5+
. "$DIR/nvm.sh" --no-use
66

77
if [ -n "$NODE_VERSION" ]; then
88
nvm use $NODE_VERSION > /dev/null || (echo "NODE_VERSION not set" >&2 && exit 127)

nvm.sh

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2325,7 +2325,7 @@ $NVM_LS_REMOTE_POST_MERGED_OUTPUT" | command grep -v "N/A" | command sed '/^$/d'
23252325
nvm_print_npm_version nvm_npm_global_modules \
23262326
nvm_has_system_node nvm_has_system_iojs \
23272327
nvm_download nvm_get_latest nvm_has nvm_get_latest \
2328-
nvm_supports_source_options nvm_supports_xz > /dev/null 2>&1
2328+
nvm_supports_source_options nvm_auto nvm_supports_xz > /dev/null 2>&1
23292329
unset RC_VERSION NVM_NODEJS_ORG_MIRROR NVM_DIR NVM_CD_FLAGS > /dev/null 2>&1
23302330
;;
23312331
* )
@@ -2343,17 +2343,41 @@ nvm_supports_xz() {
23432343
command which xz >/dev/null 2>&1 && nvm_version_greater_than_or_equal_to "$1" "2.3.2"
23442344
}
23452345

2346-
NVM_VERSION="$(nvm_alias default 2>/dev/null || echo)"
2347-
if nvm_supports_source_options && [ "$#" -gt 0 ] && [ "_$1" = "_--install" ]; then
2348-
if [ -n "$NVM_VERSION" ]; then
2349-
nvm install "$NVM_VERSION" >/dev/null
2350-
elif nvm_rc_version >/dev/null 2>&1; then
2351-
nvm install >/dev/null
2346+
nvm_auto() {
2347+
local NVM_MODE
2348+
NVM_MODE="${1-}"
2349+
local VERSION
2350+
if [ "_$NVM_MODE" = '_install' ]; then
2351+
VERSION="$(nvm_alias default 2>/dev/null || echo)"
2352+
if [ -n "$VERSION" ]; then
2353+
nvm install "$VERSION" >/dev/null
2354+
elif nvm_rc_version >/dev/null 2>&1; then
2355+
nvm install >/dev/null
2356+
fi
2357+
elif [ "_$NVM_MODE" = '_use' ]; then
2358+
VERSION="$(nvm_alias default 2>/dev/null || echo)"
2359+
if [ -n "$VERSION" ]; then
2360+
nvm use --silent "$VERSION" >/dev/null
2361+
elif nvm_rc_version >/dev/null 2>&1; then
2362+
nvm use --silent >/dev/null
2363+
fi
2364+
elif [ "_$NVM_MODE" != '_none' ]; then
2365+
echo >&2 'Invalid auto mode supplied.'
2366+
return 1
23522367
fi
2353-
elif [ -n "$NVM_VERSION" ]; then
2354-
nvm use --silent "$NVM_VERSION" >/dev/null
2355-
elif nvm_rc_version >/dev/null 2>&1; then
2356-
nvm use --silent >/dev/null
2368+
}
2369+
2370+
NVM_AUTO_MODE='use'
2371+
if nvm_supports_source_options; then
2372+
while [ $# -ne 0 ]
2373+
do
2374+
case "$1" in
2375+
--install) NVM_AUTO_MODE='install' ;;
2376+
--no-use) NVM_AUTO_MODE='none' ;;
2377+
esac
2378+
shift
2379+
done
23572380
fi
2381+
nvm_auto "$NVM_AUTO_MODE"
23582382

23592383
} # this ensures the entire script is downloaded #
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/sh
2+
3+
die () { echo $@ ; exit 1; }
4+
supports_source_options () {
5+
[ "_$(echo 'echo $1' | . /dev/stdin yes)" = "_yes" ]
6+
}
7+
8+
if ! supports_source_options; then
9+
echo 'this shell does not support passing options on sourcing'
10+
exit 0;
11+
fi
12+
13+
. ../../nvm.sh
14+
nvm install 4.1.0 || die 'install of v4.1.0 failed'
15+
nvm_version 4.1.0 >/dev/null 2>&1 || die "v4.1.0 not installed: $(nvm ls)"
16+
nvm deactivate || die 'nvm deactivate failed'
17+
18+
NVM_CURRENT="$(nvm current)"
19+
[ "_$NVM_CURRENT" = '_none' ] || [ "_$NVM_CURRENT" = '_system' ] || die "'nvm current' did not return 'none' or 'system', got '$NVM_CURRENT' `nvm ls`"
20+
21+
nvm unload || die 'nvm unload failed'
22+
23+
. ../../nvm.sh --no-use
24+
EXIT_CODE="$(echo $?)"
25+
26+
echo 'sourcing complete.'
27+
28+
[ "_$EXIT_CODE" = "_0" ] || die "sourcing returned nonzero exit code: $EXIT_CODE"
29+
30+
NVM_CURRENT="$(nvm current)"
31+
[ "_$NVM_CURRENT" = '_none' ] || [ "_$NVM_CURRENT" = '_system' ] || die "'nvm current' did not return 'none' or 'system', got '$NVM_CURRENT' `nvm ls`"
32+

0 commit comments

Comments
 (0)