[New] support `--no-use` on sourcing, in supported shells.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fixes #972.
authored by Jordan Harband · committed by Jordan Harband · parent e76fe61 · Browse files · 3 files·+68 added−12 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+68 added−12 removed
- tests:+32 added−0 removed
- other:+36 added−12 removed
Provenance
- Commit
4fcf37f- Tree
b9fde34- Author
Jordan Harband <ljharb@gmail.com>- Committer
Jordan Harband <ljharb@gmail.com>- Parent
e76fe61
3 files changed+68 added−12 removed
added:test/sourcing/Sourcing nvm.sh with --no-use should not use anything+32 added
1+#!/bin/sh2+3+die () { echo $@ ; exit 1; }4+supports_source_options () {5+ [ "_$(echo 'echo $1' | . /dev/stdin yes)" = "_yes" ]6+}7+8+if ! supports_source_options; then9+ echo 'this shell does not support passing options on sourcing'10+ exit 0;11+fi12+13+. ../../nvm.sh14+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-use24+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+modified:nvm-exec+1 added-1 removed
1 #!/bin/bash2 3 DIR="$(command cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"4 5-source "$DIR/nvm.sh"5+. "$DIR/nvm.sh" --no-use6 7 if [ -n "$NODE_VERSION" ]; then8 nvm use $NODE_VERSION > /dev/null || (echo "NODE_VERSION not set" >&2 && exit 127)9 else4 unmodified linesmodified:nvm.sh+35 added-11 removed
2323 unmodified lines2324 nvm_version_greater nvm_version_greater_than_or_equal_to \2325 nvm_print_npm_version nvm_npm_global_modules \2326 nvm_has_system_node nvm_has_system_iojs \2327 nvm_download nvm_get_latest nvm_has nvm_get_latest \2328- nvm_supports_source_options nvm_supports_xz > /dev/null 2>&12328+ nvm_supports_source_options nvm_auto nvm_supports_xz > /dev/null 2>&12329 unset RC_VERSION NVM_NODEJS_ORG_MIRROR NVM_DIR NVM_CD_FLAGS > /dev/null 2>&12330 ;;2331 * )2332 >&2 nvm help9 unmodified lines2342 nvm_supports_xz() {2343 command which xz >/dev/null 2>&1 && nvm_version_greater_than_or_equal_to "$1" "2.3.2"2344 }2345 2346+nvm_auto() {2347+ local NVM_MODE2348+ NVM_MODE="${1-}"2349+ local VERSION2346-NVM_VERSION="$(nvm_alias default 2>/dev/null || echo)"2347-if nvm_supports_source_options && [ "$#" -gt 0 ] && [ "_$1" = "_--install" ]; then2348- if [ -n "$NVM_VERSION" ]; then2349- nvm install "$NVM_VERSION" >/dev/null2350- elif nvm_rc_version >/dev/null 2>&1; then2351- nvm install >/dev/null2350+ if [ "_$NVM_MODE" = '_install' ]; then2351+ VERSION="$(nvm_alias default 2>/dev/null || echo)"2352+ if [ -n "$VERSION" ]; then2353+ nvm install "$VERSION" >/dev/null2354+ elif nvm_rc_version >/dev/null 2>&1; then2355+ nvm install >/dev/null2356+ fi2357+ elif [ "_$NVM_MODE" = '_use' ]; then2358+ VERSION="$(nvm_alias default 2>/dev/null || echo)"2359+ if [ -n "$VERSION" ]; then2360+ nvm use --silent "$VERSION" >/dev/null2361+ elif nvm_rc_version >/dev/null 2>&1; then2362+ nvm use --silent >/dev/null2363+ fi2364+ elif [ "_$NVM_MODE" != '_none' ]; then2365+ echo >&2 'Invalid auto mode supplied.'2366+ return 12367 fi2353-elif [ -n "$NVM_VERSION" ]; then2354- nvm use --silent "$NVM_VERSION" >/dev/null2355-elif nvm_rc_version >/dev/null 2>&1; then2356- nvm use --silent >/dev/null2368+}2369+2370+NVM_AUTO_MODE='use'2371+if nvm_supports_source_options; then2372+ while [ $# -ne 0 ]2373+ do2374+ case "$1" in2375+ --install) NVM_AUTO_MODE='install' ;;2376+ --no-use) NVM_AUTO_MODE='none' ;;2377+ esac2378+ shift2379+ done2380 fi2381+nvm_auto "$NVM_AUTO_MODE"2382 2383 } # this ensures the entire script is downloaded #