[New] support `--no-use` on sourcing, in supported shells.
Fixes #972.
diff --git a/nvm-exec b/nvm-exec
index 29633a3..57067af 100755
--- a/nvm-exec
+++ b/nvm-exec
@@ -2,7 +2,7 @@
DIR="$(command cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
-source "$DIR/nvm.sh"
+. "$DIR/nvm.sh" --no-use
if [ -n "$NODE_VERSION" ]; then
nvm use $NODE_VERSION > /dev/null || (echo "NODE_VERSION not set" >&2 && exit 127)
diff --git a/nvm.sh b/nvm.sh
index 5a83d15..5ec26aa 100755
--- a/nvm.sh
+++ b/nvm.sh
@@ -2325,7 +2325,7 @@
nvm_print_npm_version nvm_npm_global_modules \
nvm_has_system_node nvm_has_system_iojs \
nvm_download nvm_get_latest nvm_has nvm_get_latest \
- nvm_supports_source_options nvm_supports_xz > /dev/null 2>&1
+ nvm_supports_source_options nvm_auto nvm_supports_xz > /dev/null 2>&1
unset RC_VERSION NVM_NODEJS_ORG_MIRROR NVM_DIR NVM_CD_FLAGS > /dev/null 2>&1
;;
* )
@@ -2343,17 +2343,41 @@
command which xz >/dev/null 2>&1 && nvm_version_greater_than_or_equal_to "$1" "2.3.2"
}
-NVM_VERSION="$(nvm_alias default 2>/dev/null || echo)"
-if nvm_supports_source_options && [ "$#" -gt 0 ] && [ "_$1" = "_--install" ]; then
- if [ -n "$NVM_VERSION" ]; then
- nvm install "$NVM_VERSION" >/dev/null
- elif nvm_rc_version >/dev/null 2>&1; then
- nvm install >/dev/null
+nvm_auto() {
+ local NVM_MODE
+ NVM_MODE="${1-}"
+ local VERSION
+ if [ "_$NVM_MODE" = '_install' ]; then
+ VERSION="$(nvm_alias default 2>/dev/null || echo)"
+ if [ -n "$VERSION" ]; then
+ nvm install "$VERSION" >/dev/null
+ elif nvm_rc_version >/dev/null 2>&1; then
+ nvm install >/dev/null
+ fi
+ elif [ "_$NVM_MODE" = '_use' ]; then
+ VERSION="$(nvm_alias default 2>/dev/null || echo)"
+ if [ -n "$VERSION" ]; then
+ nvm use --silent "$VERSION" >/dev/null
+ elif nvm_rc_version >/dev/null 2>&1; then
+ nvm use --silent >/dev/null
+ fi
+ elif [ "_$NVM_MODE" != '_none' ]; then
+ echo >&2 'Invalid auto mode supplied.'
+ return 1
fi
-elif [ -n "$NVM_VERSION" ]; then
- nvm use --silent "$NVM_VERSION" >/dev/null
-elif nvm_rc_version >/dev/null 2>&1; then
- nvm use --silent >/dev/null
+}
+
+NVM_AUTO_MODE='use'
+if nvm_supports_source_options; then
+ while [ $# -ne 0 ]
+ do
+ case "$1" in
+ --install) NVM_AUTO_MODE='install' ;;
+ --no-use) NVM_AUTO_MODE='none' ;;
+ esac
+ shift
+ done
fi
+nvm_auto "$NVM_AUTO_MODE"
} # this ensures the entire script is downloaded #
diff --git a/test/sourcing/Sourcing nvm.sh with --no-use should not use anything b/test/sourcing/Sourcing nvm.sh with --no-use should not use anything
new file mode 100755
index 0000000..c0b6533
--- /dev/null
+++ b/test/sourcing/Sourcing nvm.sh with --no-use should not use anything
@@ -0,0 +1,32 @@
+#!/bin/sh
+
+die () { echo $@ ; exit 1; }
+supports_source_options () {
+ [ "_$(echo 'echo $1' | . /dev/stdin yes)" = "_yes" ]
+}
+
+if ! supports_source_options; then
+ echo 'this shell does not support passing options on sourcing'
+ exit 0;
+fi
+
+. ../../nvm.sh
+nvm install 4.1.0 || die 'install of v4.1.0 failed'
+nvm_version 4.1.0 >/dev/null 2>&1 || die "v4.1.0 not installed: $(nvm ls)"
+nvm deactivate || die 'nvm deactivate failed'
+
+NVM_CURRENT="$(nvm current)"
+[ "_$NVM_CURRENT" = '_none' ] || [ "_$NVM_CURRENT" = '_system' ] || die "'nvm current' did not return 'none' or 'system', got '$NVM_CURRENT' `nvm ls`"
+
+nvm unload || die 'nvm unload failed'
+
+. ../../nvm.sh --no-use
+EXIT_CODE="$(echo $?)"
+
+echo 'sourcing complete.'
+
+[ "_$EXIT_CODE" = "_0" ] || die "sourcing returned nonzero exit code: $EXIT_CODE"
+
+NVM_CURRENT="$(nvm current)"
+[ "_$NVM_CURRENT" = '_none' ] || [ "_$NVM_CURRENT" = '_system' ] || die "'nvm current' did not return 'none' or 'system', got '$NVM_CURRENT' `nvm ls`"
+