[New] `nvm use`/`nvm install`: add `--save` option
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fixes #2849.
authored by Martin · committed by Jordan Harband · parent dd89af5 · Browse files · 8 files·+327 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Martin <maartin00000@gmail.com>
- Co-authored-by: Jordan Harband <ljharb@gmail.com>
Diff stats
8 files+327 added−2 removed
- tests:+291 added−0 removed
- other:+36 added−2 removed
Provenance
- Commit
f439acd- Tree
d900cbc- Author
Martin <16228305+maartin0@users.noreply.github.com>- Committer
Jordan Harband <ljharb@gmail.com>- Parent
dd89af5
8 files changed+327 added−2 removed
added:test/fast/Unit tests/nvm_write_nvmrc+86 added
1+#!/bin/sh2+\. ../../../nvm.sh3+\. ../../common.sh4+5+set -e6+7+TEST_VERSION="v0.2.4"8+9+if [ -f .nvmrc ]; then mv .nvmrc .nvmrc.orig; fi10+11+del_nvmrc () {12+ rm -f .nvmrc13+}14+15+del_alias () {16+ nvm unalias test >/dev/null 2>&117+}18+19+cleanup () {20+ del_nvmrc21+ del_alias22+ nvm cache clear23+ nvm deactivate24+ nvm unalias default25+ rm -rf ${NVM_DIR}/v*26+ if [ -f .nvmrc.orig ]; then mv .nvmrc.orig .nvmrc; fi27+ unset -f nvm_ls_remote nvm_ls_remote_iojs28+}29+30+die () {31+ echo "$@"32+ cleanup33+ exit 134+}35+36+REMOTE="$PWD/mocks/nvm_ls_remote.txt"37+nvm_ls_remote() {38+ cat "$REMOTE"39+}40+REMOTE_IOJS="$PWD/mocks/nvm_ls_remote_iojs.txt"41+nvm_ls_remote_iojs() {42+ cat "$REMOTE_IOJS"43+}44+45+make_fake_node "$TEST_VERSION"46+47+test_version () {48+ del_nvmrc49+ VERSION_STRING=${1-}50+ make_fake_node "$VERSION_STRING"51+52+ nvm_write_nvmrc $VERSION_STRING || die "\`nvm_write_nvmrc ${VERSION_STRING}\` failed"53+ OUTPUT="$(cat .nvmrc)"54+55+ nvm_is_valid_version "$(cat .nvmrc)" \56+ || die "\`nvm install --save ${VERSION_STRING}\`+ \`cat .nvmrc\` outputted invalid version: got '${OUTPUT}'"57+}58+59+# 1.60+61+test_version "$TEST_VERSION" || die62+63+# 2. with an alias64+del_alias65+nvm alias test "$TEST_VERSION"66+test_version test || die67+68+# 3. fails with invalid permissions69+del_nvmrc70+touch .nvmrc71+chmod 0 .nvmrc72+nvm_write_nvmrc $TEST_VERSION 2>/dev/null && die "\`nvm_write_nvmrc $TEST_VERSION\` did not fail with invalid permissions"73+del_nvmrc74+75+# 4. respects NVM_SILENT=176+export NVM_SILENT=177+[ "$(nvm_write_nvmrc $TEST_VERSION)" = "" ] || die "\`nvm_write_nvmrc $TEST_VERSION\` was not silenced by NVM_SILENT=1"78+unset NVM_SILENT79+80+# 5. fails with an invalid version number81+TEST_VERSION="not_a_node_version"82+nvm_write_nvmrc $TEST_VERSION 2>/dev/null && die "\`nvm_write_nvmrc $TEST_VERSION\` did not fail"83+84+#85+86+cleanupadded:test/fast/Unit tests/Running 'nvm install --save' works as expected'+43 added
1+#!/bin/sh2+\. ../../../nvm.sh3+\. ../../common.sh4+5+set -e6+7+TEST_VERSION="v0.2.4"8+9+if [ -f .nvmrc ]; then mv .nvmrc .nvmrc.orig; fi10+11+cleanup () {12+ nvm cache clear13+ nvm deactivate14+ nvm unalias default15+ rm -rf ${NVM_DIR}/v* .nvmrc16+ if [ -f .nvmrc.orig ]; then mv .nvmrc.orig .nvmrc; fi17+ unset -f nvm_ls_remote nvm_ls_remote_iojs18+}19+20+die () {21+ echo "$@"22+ cleanup23+ exit 124+}25+26+REMOTE="$PWD/mocks/nvm_ls_remote.txt"27+nvm_ls_remote() {28+ cat "$REMOTE"29+}30+REMOTE_IOJS="$PWD/mocks/nvm_ls_remote_iojs.txt"31+nvm_ls_remote_iojs() {32+ cat "$REMOTE_IOJS"33+}34+35+make_fake_node "$TEST_VERSION"36+37+nvm install --save "$TEST_VERSION" || die "\`nvm install --save $TEST_VERSION\` failed"38+OUTPUT="$(cat .nvmrc)"39+40+nvm_is_valid_version "$(cat .nvmrc)" \41+ || die "\`nvm install --save $TEST_VERSION\`+ \`cat .nvmrc\` outputted invalid version: got '${OUTPUT}'"42+43+cleanupadded:test/fast/Unit tests/Running 'nvm use --save' works with a .nvmrc in the parent dir+57 added
1+#!/bin/sh2+\. ../../../nvm.sh3+\. ../../common.sh4+5+set -e6+7+TEST_VERSION="v0.2.4"8+9+if [ -f .nvmrc ]; then mv .nvmrc .nvmrc.orig; fi10+if [ -f ../.nvmrc ]; then mv ../.nvmrc ../.nvmrc.orig; fi11+12+del_nvmrc () {13+ rm -f .nvmrc ../.nvmrc14+}15+16+cleanup () {17+ del_nvmrc18+ nvm cache clear19+ nvm deactivate20+ nvm unalias default21+ rm -rf ${NVM_DIR}/v*22+ if [ -f .nvmrc.orig ]; then mv .nvmrc.orig .nvmrc; fi23+ if [ -f ../.nvmrc.orig ]; then mv ../.nvmrc.orig ../.nvmrc; fi24+ unset -f nvm_ls_remote nvm_ls_remote_iojs25+}26+27+die () {28+ echo "$@"29+ cleanup30+ exit 131+}32+33+REMOTE="$PWD/mocks/nvm_ls_remote.txt"34+nvm_ls_remote() {35+ cat "$REMOTE"36+}37+REMOTE_IOJS="$PWD/mocks/nvm_ls_remote_iojs.txt"38+nvm_ls_remote_iojs() {39+ cat "$REMOTE_IOJS"40+}41+42+del_nvmrc43+make_fake_node "$TEST_VERSION"44+45+(cd ..46+nvm use --save "$TEST_VERSION" || die "\`nvm use --save $TEST_VERSION\` failed in the parent dir")47+nvm use --save || die "\`nvm use --save\` failed"48+49+[ -f ../.nvmrc ] && [ -f .nvmrc ] || die "expected two .nvmrc files to be generated"50+51+OUTPUT=$(cat .nvmrc)52+EXPECTED_OUTPUT="$(cat ../.nvmrc)"53+54+[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] \55+ || die "invalid \`nvm use --save \` output: expected '$EXPECTED_OUTPUT'; got '$OUTPUT'"56+57+cleanupadded:test/fast/Unit tests/Running 'nvm use --silent --save' doesn't output anything+43 added
1+#!/bin/sh2+\. ../../../nvm.sh3+\. ../../common.sh4+5+set -e6+7+TEST_VERSION="v0.2.4"8+9+if [ -f .nvmrc ]; then mv .nvmrc .nvmrc.orig; fi10+11+cleanup () {12+ nvm cache clear13+ nvm deactivate14+ nvm unalias default15+ rm -rf ${NVM_DIR}/v* .nvmrc16+ if [ -f .nvmrc.orig ]; then mv .nvmrc.orig .nvmrc; fi17+ unset -f nvm_ls_remote nvm_ls_remote_iojs18+}19+20+die () {21+ echo "$@"22+ cleanup23+ exit 124+}25+26+REMOTE="$PWD/mocks/nvm_ls_remote.txt"27+nvm_ls_remote() {28+ cat "$REMOTE"29+}30+REMOTE_IOJS="$PWD/mocks/nvm_ls_remote_iojs.txt"31+nvm_ls_remote_iojs() {32+ cat "$REMOTE_IOJS"33+}34+35+make_fake_node "$TEST_VERSION"36+37+OUTPUT=$(nvm use --save --silent "$TEST_VERSION" || die "\`nvm use --save --silent $TEST_VERSION\` failed")38+EXPECTED_OUTPUT=""39+40+[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] \41+ || die "\`nvm use --save --silent $TEST_VERSION\` output was not silenced to '$EXPECTED_OUTPUT'; got '$OUTPUT'"42+43+cleanupadded:test/fast/Unit tests/Running 'nvm use -w' works as expected'+62 added
1+#!/bin/sh2+\. ../../../nvm.sh3+4+set -e5+6+if [ -f .nvmrc ]; then mv .nvmrc .nvmrc.orig; fi7+8+TEST_VERSION="v0.2.4"9+10+cleanup () {11+ nvm cache clear12+ nvm deactivate13+ nvm unalias default14+ rm -rf ${NVM_DIR}/v* .nvmrc15+ if [ -f .nvmrc.orig ]; then mv .nvmrc.orig .nvmrc; fi16+ unset -f nvm_ls_remote nvm_ls_remote_iojs17+}18+19+die () {20+ echo "$@"21+ cleanup22+ exit 123+}24+25+nvm deactivate 2>/dev/null || die 'unable to deactivate'26+27+\. ../../common.sh28+29+REMOTE="$PWD/mocks/nvm_ls_remote.txt"30+nvm_ls_remote() {31+ cat "$REMOTE"32+}33+REMOTE_IOJS="$PWD/mocks/nvm_ls_remote_iojs.txt"34+nvm_ls_remote_iojs() {35+ cat "$REMOTE_IOJS"36+}37+38+make_fake_node "$TEST_VERSION"39+40+# 1. install41+42+nvm install -w "$TEST_VERSION" || die "\`nvm install -w $TEST_VERSION\` failed"43+OUTPUT="$(cat .nvmrc)"44+45+nvm_is_valid_version "$(cat .nvmrc)" \46+ || die "\`nvm install -w $TEST_VERSION\`+ \`cat .nvmrc\` outputted invalid version: got '${OUTPUT}'"47+48+#49+50+unset OUTPUT51+52+# 2. use53+54+nvm use -w "$TEST_VERSION" || die "\`nvm use -w $TEST_VERSION\` failed"55+OUTPUT="$(cat .nvmrc)"56+57+nvm_is_valid_version "$(cat .nvmrc)" \58+ || die "\`nvm use -w $TEST_VERSION\`+ \`cat .nvmrc\` outputted invalid version: got '${OUTPUT}'"59+60+#61+62+cleanupmodified:.dockerignore+1 added
6 unmodified lines7 test/bak8 .urchin.log9 .urchin_stdout10 test/**/test_output11+test/**/.nvmrc12 13 node_modules/14 npm-debug.log15 2 unmodified linesmodified:.gitignore+1 added
7 unmodified lines8 test/bak9 .urchin.log10 .urchin_stdout11 test/**/test_output12+test/**/.nvmrc13 14 node_modules/15 npm-debug.log16 8 unmodified linesmodified:nvm.sh+34 added-2 removed
2909 unmodified lines2910 ;;2911 esac2912 }2913 2914+nvm_write_nvmrc() {2915+ local VERSION_STRING2916+ VERSION_STRING=$(nvm_version "${1-$VERSION_STRING}")2917+ if [ "$VERSION_STRING" = '∞' ] || [ "$VERSION_STRING" = 'N/A' ]; then2918+ return 12919+ fi2920+ echo "$VERSION_STRING" | tee "$PWD"/.nvmrc > /dev/null || {2921+ if [ "${NVM_SILENT:-0}" -ne 1 ]; then2922+ nvm_err "Warning: Unable to write version number ($VERSION_STRING) to .nvmrc"2923+ fi2924+ return 32925+ }2926+ if [ "${NVM_SILENT:-0}" -ne 1 ]; then2927+ nvm_echo "Wrote version number ($VERSION_STRING) to .nvmrc"2928+ fi2929+}2930+2931 # Check version dir permissions2932 nvm_check_file_permissions() {2933 nvm_is_zsh && setopt local_options nonomatch2934 for FILE in "$1"/* "$1"/.[!.]* "$1"/..?* ; do97 unmodified lines3032 nvm_echo ' --latest-npm After installing, attempt to upgrade to the latest working npm on the given node version'3033 nvm_echo ' --no-progress Disable the progress bar on any downloads'3034 nvm_echo ' --alias=<name> After installing, set the alias specified to the version specified. (same as: nvm alias <name> <version>)'3035 nvm_echo ' --default After installing, set default alias to the version specified. (same as: nvm alias default <version>)'3036+ nvm_echo ' --save After installing, write the specified version to .nvmrc'3037 nvm_echo ' nvm uninstall <version> Uninstall a version'3038 nvm_echo ' nvm uninstall --lts Uninstall using automatic LTS (long-term support) alias `lts/*`, if available.'3039 nvm_echo ' nvm uninstall --lts=<LTS name> Uninstall using automatic alias for provided LTS line, if available.'3040 nvm_echo ' nvm use [<version>] Modify PATH to use <version>. Uses .nvmrc if available and version is omitted.'3041 nvm_echo ' The following optional arguments, if provided, must appear directly after `nvm use`:'3042 nvm_echo ' --silent Silences stdout/stderr output'3043 nvm_echo ' --lts Uses automatic LTS (long-term support) alias `lts/*`, if available.'3044 nvm_echo ' --lts=<LTS name> Uses automatic alias for provided LTS line, if available.'3045+ nvm_echo ' --save Writes the specified version to .nvmrc.'3046 nvm_echo ' nvm exec [<version>] [<command>] Run <command> on <version>. Uses .nvmrc if available and version is omitted.'3047 nvm_echo ' The following optional arguments, if provided, must appear directly after `nvm exec`:'3048 nvm_echo ' --silent Silences stdout/stderr output'3049 nvm_echo ' --lts Uses automatic LTS (long-term support) alias `lts/*`, if available.'204 unmodified lines3254 local LTS3255 local ALIAS3256 local NVM_UPGRADE_NPM3257 NVM_UPGRADE_NPM=03258+ local NVM_WRITE_TO_NVMRC3259+ NVM_WRITE_TO_NVMRC=03260 3261 local PROVIDED_REINSTALL_PACKAGES_FROM3262 local REINSTALL_PACKAGES_FROM3263 local SKIP_DEFAULT_PACKAGES90 unmodified lines3354 --skip-default-packages)3355 SKIP_DEFAULT_PACKAGES=true3356 shift3357 ;;3358+ --save | -w)3359+ NVM_WRITE_TO_NVMRC=13360+ shift3361+ ;;3362 *)3363 break # stop parsing args3364 ;;3365 esac228 unmodified lines3594 fi3595 else3596 EXIT_CODE=$?3597 fi3598+3599 return $EXIT_CODE3600 ;;3601 "uninstall")3602 if [ $# -ne 1 ]; then143 unmodified lines3746 --delete-prefix) NVM_DELETE_PREFIX=1 ;;3747 --) ;;3748 --lts) NVM_LTS='*' ;;3749 --lts=*) NVM_LTS="${1##--lts=}" ;;3750+ --save | -w) NVM_WRITE_TO_NVMRC=1 ;;3751 --*) ;;3752 *)3753 if [ -n "${1-}" ]; then3754 PROVIDED_VERSION="$1"25 unmodified lines3780 >&2 nvm --help3781 return 1273782 fi3783 3784+ if [ "${NVM_WRITE_TO_NVMRC:-0}" -eq 1 ]; then3785+ nvm_write_nvmrc "$VERSION"3786+ fi3787+3788 if [ "_${VERSION}" = '_system' ]; then3789 if nvm_has_system_node && nvm deactivate "${NVM_SILENT_ARG-}" >/dev/null 2>&1; then3790 if [ "${NVM_SILENT:-0}" -ne 1 ]; then3791 nvm_echo "Now using system version of node: $(node -v 2>/dev/null)$(nvm_print_npm_version)"617 unmodified lines4409 nvm_get_colors nvm_set_colors nvm_print_color_code nvm_wrap_with_color_code nvm_format_help_message_colors \4410 nvm_echo_with_colors nvm_err_with_colors \4411 nvm_get_artifact_compression nvm_install_binary_extract nvm_extract_tarball \4412 nvm_process_nvmrc nvm_nvmrc_invalid_msg \4413+ nvm_write_nvmrc \4414 >/dev/null 2>&14415 unset NVM_RC_VERSION NVM_NODEJS_ORG_MIRROR NVM_IOJS_ORG_MIRROR NVM_DIR \4416 NVM_CD_FLAGS NVM_BIN NVM_INC NVM_MAKE_JOBS \4417 NVM_COLORS INSTALLED_COLOR SYSTEM_COLOR \128 unmodified lines4546 local VERSION4547 local NVM_CURRENT4548 if [ "_${NVM_MODE}" = '_install' ]; then4549 VERSION="$(nvm_alias default 2>/dev/null || nvm_echo)"4518- if [ -n "${VERSION}" ]; then4550+ if [ -n "${VERSION}" ] && ! [ "_${VERSION}" = '_N/A' ] && nvm_is_valid_version "${VERSION}"; then4551 nvm install "${VERSION}" >/dev/null4552 elif nvm_rc_version >/dev/null 2>&1; then4553 nvm install >/dev/null4554 fi4555 elif [ "_$NVM_MODE" = '_use' ]; then4556 NVM_CURRENT="$(nvm_ls_current)"4557 if [ "_${NVM_CURRENT}" = '_none' ] || [ "_${NVM_CURRENT}" = '_system' ]; then4558 VERSION="$(nvm_resolve_local_alias default 2>/dev/null || nvm_echo)"4527- if [ -n "${VERSION}" ]; then4559+ if [ -n "${VERSION}" ] && ! [ "_${VERSION}" = '_N/A' ] && nvm_is_valid_version "${VERSION}"; then4560 nvm use --silent "${VERSION}" >/dev/null4561 elif nvm_rc_version >/dev/null 2>&1; then4562 nvm use --silent >/dev/null4563 fi25 unmodified lines