Skip to content

Commit 16c8e22

Browse files
maartin0ljharb
andcommitted
[New] nvm use/nvm install: add --save option
Fixes #2849. Co-authored-by: Martin <[email protected]> Co-authored-by: Jordan Harband <[email protected]>
1 parent 29dce5e commit 16c8e22

8 files changed

+327
-2
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ test/bak
88
.urchin.log
99
.urchin_stdout
1010
test/**/test_output
11+
test/**/.nvmrc
1112

1213
node_modules/
1314
npm-debug.log

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ test/bak
99
.urchin.log
1010
.urchin_stdout
1111
test/**/test_output
12+
test/**/.nvmrc
1213

1314
node_modules/
1415
npm-debug.log

nvm.sh

100644100755
Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2894,6 +2894,23 @@ nvm_is_natural_num() {
28942894
esac
28952895
}
28962896

2897+
nvm_write_nvmrc() {
2898+
local VERSION_STRING
2899+
VERSION_STRING=$(nvm_version "${1-$VERSION_STRING}")
2900+
if [ "$VERSION_STRING" = '' ] || [ "$VERSION_STRING" = 'N/A' ]; then
2901+
return 1
2902+
fi
2903+
echo "$VERSION_STRING" | tee "$PWD"/.nvmrc > /dev/null || {
2904+
if [ "${NVM_SILENT:-0}" -ne 1 ]; then
2905+
nvm_err "Warning: Unable to write version number ($VERSION_STRING) to .nvmrc"
2906+
fi
2907+
return 3
2908+
}
2909+
if [ "${NVM_SILENT:-0}" -ne 1 ]; then
2910+
nvm_echo "Wrote version number ($VERSION_STRING) to .nvmrc"
2911+
fi
2912+
}
2913+
28972914
# Check version dir permissions
28982915
nvm_check_file_permissions() {
28992916
nvm_is_zsh && setopt local_options nonomatch
@@ -2999,6 +3016,7 @@ nvm() {
29993016
nvm_echo ' --no-progress Disable the progress bar on any downloads'
30003017
nvm_echo ' --alias=<name> After installing, set the alias specified to the version specified. (same as: nvm alias <name> <version>)'
30013018
nvm_echo ' --default After installing, set default alias to the version specified. (same as: nvm alias default <version>)'
3019+
nvm_echo ' --save After installing, write the specified version to .nvmrc'
30023020
nvm_echo ' nvm uninstall <version> Uninstall a version'
30033021
nvm_echo ' nvm uninstall --lts Uninstall using automatic LTS (long-term support) alias `lts/*`, if available.'
30043022
nvm_echo ' nvm uninstall --lts=<LTS name> Uninstall using automatic alias for provided LTS line, if available.'
@@ -3007,6 +3025,7 @@ nvm() {
30073025
nvm_echo ' --silent Silences stdout/stderr output'
30083026
nvm_echo ' --lts Uses automatic LTS (long-term support) alias `lts/*`, if available.'
30093027
nvm_echo ' --lts=<LTS name> Uses automatic alias for provided LTS line, if available.'
3028+
nvm_echo ' --save Writes the specified version to .nvmrc.'
30103029
nvm_echo ' nvm exec [<version>] [<command>] Run <command> on <version>. Uses .nvmrc if available and version is omitted.'
30113030
nvm_echo ' The following optional arguments, if provided, must appear directly after `nvm exec`:'
30123031
nvm_echo ' --silent Silences stdout/stderr output'
@@ -3219,6 +3238,8 @@ nvm() {
32193238
local ALIAS
32203239
local NVM_UPGRADE_NPM
32213240
NVM_UPGRADE_NPM=0
3241+
local NVM_WRITE_TO_NVMRC
3242+
NVM_WRITE_TO_NVMRC=0
32223243

32233244
local PROVIDED_REINSTALL_PACKAGES_FROM
32243245
local REINSTALL_PACKAGES_FROM
@@ -3317,6 +3338,10 @@ nvm() {
33173338
SKIP_DEFAULT_PACKAGES=true
33183339
shift
33193340
;;
3341+
--save | -w)
3342+
NVM_WRITE_TO_NVMRC=1
3343+
shift
3344+
;;
33203345
*)
33213346
break # stop parsing args
33223347
;;
@@ -3553,6 +3578,7 @@ nvm() {
35533578
else
35543579
EXIT_CODE=$?
35553580
fi
3581+
35563582
return $EXIT_CODE
35573583
;;
35583584
"uninstall")
@@ -3704,6 +3730,7 @@ nvm() {
37043730
--) ;;
37053731
--lts) NVM_LTS='*' ;;
37063732
--lts=*) NVM_LTS="${1##--lts=}" ;;
3733+
--save | -w) NVM_WRITE_TO_NVMRC=1 ;;
37073734
--*) ;;
37083735
*)
37093736
if [ -n "${1-}" ]; then
@@ -3737,6 +3764,10 @@ nvm() {
37373764
return 127
37383765
fi
37393766

3767+
if [ "${NVM_WRITE_TO_NVMRC:-0}" -eq 1 ]; then
3768+
nvm_write_nvmrc "$VERSION"
3769+
fi
3770+
37403771
if [ "_${VERSION}" = '_system' ]; then
37413772
if nvm_has_system_node && nvm deactivate "${NVM_SILENT_ARG-}" >/dev/null 2>&1; then
37423773
if [ "${NVM_SILENT:-0}" -ne 1 ]; then
@@ -4362,6 +4393,7 @@ nvm() {
43624393
nvm_echo_with_colors nvm_err_with_colors \
43634394
nvm_get_artifact_compression nvm_install_binary_extract nvm_extract_tarball \
43644395
nvm_process_nvmrc nvm_nvmrc_invalid_msg \
4396+
nvm_write_nvmrc \
43654397
>/dev/null 2>&1
43664398
unset NVM_RC_VERSION NVM_NODEJS_ORG_MIRROR NVM_IOJS_ORG_MIRROR NVM_DIR \
43674399
NVM_CD_FLAGS NVM_BIN NVM_INC NVM_MAKE_JOBS \
@@ -4498,7 +4530,7 @@ nvm_auto() {
44984530
local NVM_CURRENT
44994531
if [ "_${NVM_MODE}" = '_install' ]; then
45004532
VERSION="$(nvm_alias default 2>/dev/null || nvm_echo)"
4501-
if [ -n "${VERSION}" ]; then
4533+
if [ -n "${VERSION}" ] && ! [ "_${VERSION}" = '_N/A' ] && nvm_is_valid_version "${VERSION}"; then
45024534
nvm install "${VERSION}" >/dev/null
45034535
elif nvm_rc_version >/dev/null 2>&1; then
45044536
nvm install >/dev/null
@@ -4507,7 +4539,7 @@ nvm_auto() {
45074539
NVM_CURRENT="$(nvm_ls_current)"
45084540
if [ "_${NVM_CURRENT}" = '_none' ] || [ "_${NVM_CURRENT}" = '_system' ]; then
45094541
VERSION="$(nvm_resolve_local_alias default 2>/dev/null || nvm_echo)"
4510-
if [ -n "${VERSION}" ]; then
4542+
if [ -n "${VERSION}" ] && ! [ "_${VERSION}" = '_N/A' ] && nvm_is_valid_version "${VERSION}"; then
45114543
nvm use --silent "${VERSION}" >/dev/null
45124544
elif nvm_rc_version >/dev/null 2>&1; then
45134545
nvm use --silent >/dev/null
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/sh
2+
\. ../../../nvm.sh
3+
\. ../../common.sh
4+
5+
set -e
6+
7+
TEST_VERSION="v0.2.4"
8+
9+
if [ -f .nvmrc ]; then mv .nvmrc .nvmrc.orig; fi
10+
11+
cleanup () {
12+
nvm cache clear
13+
nvm deactivate
14+
nvm unalias default
15+
rm -rf ${NVM_DIR}/v* .nvmrc
16+
if [ -f .nvmrc.orig ]; then mv .nvmrc.orig .nvmrc; fi
17+
unset -f nvm_ls_remote nvm_ls_remote_iojs
18+
}
19+
20+
die () {
21+
echo "$@"
22+
cleanup
23+
exit 1
24+
}
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+
cleanup
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/sh
2+
\. ../../../nvm.sh
3+
\. ../../common.sh
4+
5+
set -e
6+
7+
TEST_VERSION="v0.2.4"
8+
9+
if [ -f .nvmrc ]; then mv .nvmrc .nvmrc.orig; fi
10+
if [ -f ../.nvmrc ]; then mv ../.nvmrc ../.nvmrc.orig; fi
11+
12+
del_nvmrc () {
13+
rm -f .nvmrc ../.nvmrc
14+
}
15+
16+
cleanup () {
17+
del_nvmrc
18+
nvm cache clear
19+
nvm deactivate
20+
nvm unalias default
21+
rm -rf ${NVM_DIR}/v*
22+
if [ -f .nvmrc.orig ]; then mv .nvmrc.orig .nvmrc; fi
23+
if [ -f ../.nvmrc.orig ]; then mv ../.nvmrc.orig ../.nvmrc; fi
24+
unset -f nvm_ls_remote nvm_ls_remote_iojs
25+
}
26+
27+
die () {
28+
echo "$@"
29+
cleanup
30+
exit 1
31+
}
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_nvmrc
43+
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+
cleanup
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/sh
2+
\. ../../../nvm.sh
3+
\. ../../common.sh
4+
5+
set -e
6+
7+
TEST_VERSION="v0.2.4"
8+
9+
if [ -f .nvmrc ]; then mv .nvmrc .nvmrc.orig; fi
10+
11+
cleanup () {
12+
nvm cache clear
13+
nvm deactivate
14+
nvm unalias default
15+
rm -rf ${NVM_DIR}/v* .nvmrc
16+
if [ -f .nvmrc.orig ]; then mv .nvmrc.orig .nvmrc; fi
17+
unset -f nvm_ls_remote nvm_ls_remote_iojs
18+
}
19+
20+
die () {
21+
echo "$@"
22+
cleanup
23+
exit 1
24+
}
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+
cleanup
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/bin/sh
2+
\. ../../../nvm.sh
3+
4+
set -e
5+
6+
if [ -f .nvmrc ]; then mv .nvmrc .nvmrc.orig; fi
7+
8+
TEST_VERSION="v0.2.4"
9+
10+
cleanup () {
11+
nvm cache clear
12+
nvm deactivate
13+
nvm unalias default
14+
rm -rf ${NVM_DIR}/v* .nvmrc
15+
if [ -f .nvmrc.orig ]; then mv .nvmrc.orig .nvmrc; fi
16+
unset -f nvm_ls_remote nvm_ls_remote_iojs
17+
}
18+
19+
die () {
20+
echo "$@"
21+
cleanup
22+
exit 1
23+
}
24+
25+
nvm deactivate 2>/dev/null || die 'unable to deactivate'
26+
27+
\. ../../common.sh
28+
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. install
41+
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 OUTPUT
51+
52+
# 2. use
53+
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+
cleanup

0 commit comments

Comments
 (0)