Skip to content

Commit 2519281

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 294ff9e commit 2519281

7 files changed

+240
-0
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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2807,6 +2807,23 @@ nvm_is_natural_num() {
28072807
esac
28082808
}
28092809

2810+
nvm_write_nvmrc() {
2811+
local VERSION_STRING
2812+
VERSION_STRING=$(nvm_version "${1-$VERSION_STRING}")
2813+
if [ "$VERSION_STRING" = '' ] || [ "$VERSION_STRING" = 'N/A' ]; then
2814+
return 1
2815+
fi
2816+
echo "$VERSION_STRING" | tee "$PWD"/.nvmrc > /dev/null || {
2817+
if [ "${NVM_SILENT:-0}" -ne 1 ]; then
2818+
nvm_err "Warning: Unable to write version number ($VERSION_STRING) to .nvmrc"
2819+
fi
2820+
return 3
2821+
}
2822+
if [ "${NVM_SILENT:-0}" -ne 1 ]; then
2823+
nvm_echo "Wrote version number ($VERSION_STRING) to .nvmrc"
2824+
fi
2825+
}
2826+
28102827
# Check version dir permissions
28112828
nvm_check_file_permissions() {
28122829
nvm_is_zsh && setopt local_options nonomatch
@@ -2912,6 +2929,7 @@ nvm() {
29122929
nvm_echo ' --no-progress Disable the progress bar on any downloads'
29132930
nvm_echo ' --alias=<name> After installing, set the alias specified to the version specified. (same as: nvm alias <name> <version>)'
29142931
nvm_echo ' --default After installing, set default alias to the version specified. (same as: nvm alias default <version>)'
2932+
nvm_echo ' --save After installing, write the specified version to .nvmrc'
29152933
nvm_echo ' nvm uninstall <version> Uninstall a version'
29162934
nvm_echo ' nvm uninstall --lts Uninstall using automatic LTS (long-term support) alias `lts/*`, if available.'
29172935
nvm_echo ' nvm uninstall --lts=<LTS name> Uninstall using automatic alias for provided LTS line, if available.'
@@ -2920,6 +2938,7 @@ nvm() {
29202938
nvm_echo ' --silent Silences stdout/stderr output'
29212939
nvm_echo ' --lts Uses automatic LTS (long-term support) alias `lts/*`, if available.'
29222940
nvm_echo ' --lts=<LTS name> Uses automatic alias for provided LTS line, if available.'
2941+
nvm_echo ' --save Writes the specified version to .nvmrc.'
29232942
nvm_echo ' nvm exec [<version>] [<command>] Run <command> on <version>. Uses .nvmrc if available and version is omitted.'
29242943
nvm_echo ' The following optional arguments, if provided, must appear directly after `nvm exec`:'
29252944
nvm_echo ' --silent Silences stdout/stderr output'
@@ -3132,6 +3151,8 @@ nvm() {
31323151
local ALIAS
31333152
local NVM_UPGRADE_NPM
31343153
NVM_UPGRADE_NPM=0
3154+
local NVM_WRITE_TO_NVMRC
3155+
NVM_WRITE_TO_NVMRC=0
31353156

31363157
local PROVIDED_REINSTALL_PACKAGES_FROM
31373158
local REINSTALL_PACKAGES_FROM
@@ -3230,6 +3251,10 @@ nvm() {
32303251
SKIP_DEFAULT_PACKAGES=true
32313252
shift
32323253
;;
3254+
--save)
3255+
NVM_WRITE_TO_NVMRC=1
3256+
shift
3257+
;;
32333258
*)
32343259
break # stop parsing args
32353260
;;
@@ -3466,6 +3491,7 @@ nvm() {
34663491
else
34673492
EXIT_CODE=$?
34683493
fi
3494+
34693495
return $EXIT_CODE
34703496
;;
34713497
"uninstall")
@@ -3617,6 +3643,7 @@ nvm() {
36173643
--) ;;
36183644
--lts) NVM_LTS='*' ;;
36193645
--lts=*) NVM_LTS="${1##--lts=}" ;;
3646+
--save) NVM_WRITE_TO_NVMRC=1 ;;
36203647
--*) ;;
36213648
*)
36223649
if [ -n "${1-}" ]; then
@@ -3650,6 +3677,10 @@ nvm() {
36503677
return 127
36513678
fi
36523679

3680+
if [ "${NVM_WRITE_TO_NVMRC:-0}" -eq 1 ]; then
3681+
nvm_write_nvmrc "$VERSION"
3682+
fi
3683+
36533684
if [ "_${VERSION}" = '_system' ]; then
36543685
if nvm_has_system_node && nvm deactivate "${NVM_SILENT_ARG-}" >/dev/null 2>&1; then
36553686
if [ "${NVM_SILENT:-0}" -ne 1 ]; then
@@ -4271,6 +4302,7 @@ nvm() {
42714302
nvm_get_colors nvm_set_colors nvm_print_color_code nvm_wrap_with_color_code nvm_format_help_message_colors \
42724303
nvm_echo_with_colors nvm_err_with_colors \
42734304
nvm_get_artifact_compression nvm_install_binary_extract nvm_extract_tarball \
4305+
nvm_write_nvmrc \
42744306
>/dev/null 2>&1
42754307
unset NVM_RC_VERSION NVM_NODEJS_ORG_MIRROR NVM_IOJS_ORG_MIRROR NVM_DIR \
42764308
NVM_CD_FLAGS NVM_BIN NVM_INC NVM_MAKE_JOBS \
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/sh
2+
\. ../../../nvm.sh
3+
\. ../../common.sh
4+
5+
if [ -f .nvmrc ]; then mv .nvmrc .nvmrc.orig; fi
6+
7+
cleanup () {
8+
rm -f .nvmrc
9+
if [ -f .nvmrc.orig ]; then mv .nvmrc.orig .nvmrc; fi
10+
unset -f nvm_ls_remote nvm_ls_remote_iojs
11+
}
12+
13+
die () {
14+
echo "$@"
15+
cleanup
16+
exit 1
17+
}
18+
19+
REMOTE="$PWD/mocks/nvm_ls_remote.txt"
20+
nvm_ls_remote() {
21+
cat "$REMOTE"
22+
}
23+
REMOTE_IOJS="$PWD/mocks/nvm_ls_remote_iojs.txt"
24+
nvm_ls_remote_iojs() {
25+
cat "$REMOTE_IOJS"
26+
}
27+
28+
make_fake_node "lts/*"
29+
30+
nvm install --save --lts || die "\`nvm install --save --lts\` failed"
31+
OUTPUT="$(cat .nvmrc)"
32+
33+
nvm_is_valid_version "$(cat .nvmrc)" \
34+
|| die "\`nvm install --save --lts\`+ \`cat .nvmrc\` outputted invalid version: got '${OUTPUT}'"
35+
36+
cleanup
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/bin/sh
2+
\. ../../../nvm.sh
3+
\. ../../common.sh
4+
5+
TEST_VERSION=$(nvm_version node)
6+
echo "Using test version '$TEST_VERSION'"
7+
8+
if [ -f .nvmrc ]; then mv .nvmrc .nvmrc.orig; fi
9+
if [ -f ../.nvmrc ]; then mv ../.nvmrc ../.nvmrc.orig; fi
10+
11+
del_nvmrc () {
12+
rm -f .nvmrc ../.nvmrc
13+
}
14+
15+
cleanup () {
16+
del_nvmrc
17+
if [ -f .nvmrc.orig ]; then mv .nvmrc.orig .nvmrc; fi
18+
if [ -f ../.nvmrc.orig ]; then mv ../.nvmrc.orig ../.nvmrc; fi
19+
unset -f nvm_ls_remote nvm_ls_remote_iojs
20+
}
21+
22+
die () {
23+
echo "$@"
24+
cleanup
25+
exit 1
26+
}
27+
28+
REMOTE="$PWD/mocks/nvm_ls_remote.txt"
29+
nvm_ls_remote() {
30+
cat "$REMOTE"
31+
}
32+
REMOTE_IOJS="$PWD/mocks/nvm_ls_remote_iojs.txt"
33+
nvm_ls_remote_iojs() {
34+
cat "$REMOTE_IOJS"
35+
}
36+
37+
del_nvmrc
38+
make_fake_node "$TEST_VERSION"
39+
40+
(cd ..
41+
nvm use --save "$TEST_VERSION" || die "\`nvm use --save $TEST_VERSION\` failed in the parent dir")
42+
nvm use --save || die "\`nvm use --save\` failed"
43+
44+
[ -f ../.nvmrc ] && [ -f .nvmrc ] || die "expected two .nvmrc files to be generated"
45+
46+
OUTPUT=$(cat .nvmrc)
47+
EXPECTED_OUTPUT="$(cat ../.nvmrc)"
48+
49+
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] \
50+
|| die "invalid \`nvm use --save \` output: expected '$EXPECTED_OUTPUT'; got '$OUTPUT'"
51+
52+
cleanup
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/sh
2+
\. ../../../nvm.sh
3+
\. ../../common.sh
4+
5+
TEST_VERSION=$(nvm_version node)
6+
echo "Using test version '$TEST_VERSION'"
7+
8+
if [ -f .nvmrc ]; then mv .nvmrc .nvmrc.orig; fi
9+
10+
cleanup () {
11+
rm -f .nvmrc
12+
if [ -f .nvmrc.orig ]; then mv .nvmrc.orig .nvmrc; fi
13+
unset -f nvm_ls_remote nvm_ls_remote_iojs
14+
}
15+
16+
die () {
17+
echo "$@"
18+
cleanup
19+
exit 1
20+
}
21+
22+
REMOTE="$PWD/mocks/nvm_ls_remote.txt"
23+
nvm_ls_remote() {
24+
cat "$REMOTE"
25+
}
26+
REMOTE_IOJS="$PWD/mocks/nvm_ls_remote_iojs.txt"
27+
nvm_ls_remote_iojs() {
28+
cat "$REMOTE_IOJS"
29+
}
30+
31+
make_fake_node "$TEST_VERSION"
32+
33+
OUTPUT=$(nvm use --save --silent "$TEST_VERSION" || die "\`nvm use --save --silent $TEST_VERSION\` failed")
34+
EXPECTED_OUTPUT=""
35+
36+
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] \
37+
|| die "\`nvm use --save --silent $TEST_VERSION\` output was not silenced to '$EXPECTED_OUTPUT'; got '$OUTPUT'"
38+
39+
cleanup

test/fast/Unit tests/nvm_write_nvmrc

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/bin/sh
2+
\. ../../../nvm.sh
3+
\. ../../common.sh
4+
5+
if [ -f .nvmrc ]; then mv .nvmrc .nvmrc.orig; fi
6+
7+
del_nvmrc () {
8+
rm -f .nvmrc
9+
}
10+
11+
del_alias () {
12+
nvm unalias test >/dev/null 2>&1
13+
}
14+
15+
cleanup () {
16+
del_nvmrc
17+
del_alias
18+
if [ -f .nvmrc.orig ]; then mv .nvmrc.orig .nvmrc; fi
19+
unset -f nvm_ls_remote nvm_ls_remote_iojs
20+
}
21+
22+
die () {
23+
echo "$@"
24+
cleanup
25+
exit 1
26+
}
27+
28+
REMOTE="$PWD/mocks/nvm_ls_remote.txt"
29+
nvm_ls_remote() {
30+
cat "$REMOTE"
31+
}
32+
REMOTE_IOJS="$PWD/mocks/nvm_ls_remote_iojs.txt"
33+
nvm_ls_remote_iojs() {
34+
cat "$REMOTE_IOJS"
35+
}
36+
37+
TEST_VERSION="v12.22.12"
38+
make_fake_node "$TEST_VERSION"
39+
40+
test_version () {
41+
del_nvmrc
42+
VERSION_STRING=${1-}
43+
make_fake_node "$VERSION_STRING"
44+
45+
nvm_write_nvmrc $VERSION_STRING || die "\`nvm_write_nvmrc ${VERSION_STRING}\` failed"
46+
OUTPUT="$(cat .nvmrc)"
47+
48+
nvm_is_valid_version "$(cat .nvmrc)" \
49+
|| die "\`nvm install --save ${VERSION_STRING}\`+ \`cat .nvmrc\` outputted invalid version: got '${OUTPUT}'"
50+
}
51+
52+
# 1.
53+
54+
test_version "$TEST_VERSION" || die
55+
56+
# 2. with an alias
57+
del_alias
58+
nvm alias test "$TEST_VERSION"
59+
test_version test || die
60+
61+
# 3. fails with invalid permissions
62+
del_nvmrc
63+
touch .nvmrc
64+
chmod 0 .nvmrc
65+
nvm_write_nvmrc $TEST_VERSION 2>/dev/null && die "\`nvm_write_nvmrc $TEST_VERSION\` did not fail with invalid permissions"
66+
del_nvmrc
67+
68+
# 4. respects NVM_SILENT=1
69+
export NVM_SILENT=1
70+
[ "$(nvm_write_nvmrc $TEST_VERSION)" = "" ] || die "\`nvm_write_nvmrc $TEST_VERSION\` was not silenced by NVM_SILENT=1"
71+
unset NVM_SILENT
72+
73+
# 5. fails with an invalid version number
74+
TEST_VERSION="not_a_node_version"
75+
nvm_write_nvmrc $TEST_VERSION 2>/dev/null && die "\`nvm_write_nvmrc $TEST_VERSION\` did not fail"
76+
77+
#
78+
79+
cleanup

0 commit comments

Comments
 (0)