Skip to content

Commit edfb809

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 811c039 commit edfb809

8 files changed

+306
-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
@@ -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 | -w)
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 | -w) 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 \
@@ -4407,7 +4439,7 @@ nvm_auto() {
44074439
local NVM_CURRENT
44084440
if [ "_${NVM_MODE}" = '_install' ]; then
44094441
VERSION="$(nvm_alias default 2>/dev/null || nvm_echo)"
4410-
if [ -n "${VERSION}" ]; then
4442+
if [ -n "${VERSION}" ] && ! [ "_${VERSION}" = '_N/A' ] && nvm_is_valid_version "${VERSION}"; then
44114443
nvm install "${VERSION}" >/dev/null
44124444
elif nvm_rc_version >/dev/null 2>&1; then
44134445
nvm install >/dev/null
@@ -4416,7 +4448,7 @@ nvm_auto() {
44164448
NVM_CURRENT="$(nvm_ls_current)"
44174449
if [ "_${NVM_CURRENT}" = '_none' ] || [ "_${NVM_CURRENT}" = '_system' ]; then
44184450
VERSION="$(nvm_resolve_local_alias default 2>/dev/null || nvm_echo)"
4419-
if [ -n "${VERSION}" ]; then
4451+
if [ -n "${VERSION}" ] && ! [ "_${VERSION}" = '_N/A' ] && nvm_is_valid_version "${VERSION}"; then
44204452
nvm use --silent "${VERSION}" >/dev/null
44214453
elif nvm_rc_version >/dev/null 2>&1; then
44224454
nvm use --silent >/dev/null
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/bin/sh
2+
\. ../../../nvm.sh
3+
\. ../../common.sh
4+
5+
set -e
6+
7+
if [ -f .nvmrc ]; then mv .nvmrc .nvmrc.orig; fi
8+
9+
cleanup () {
10+
rm -f .nvmrc
11+
if [ -f .nvmrc.orig ]; then mv .nvmrc.orig .nvmrc; fi
12+
unset -f nvm_ls_remote nvm_ls_remote_iojs
13+
}
14+
15+
die () {
16+
echo "$@"
17+
cleanup
18+
exit 1
19+
}
20+
21+
REMOTE="$PWD/mocks/nvm_ls_remote.txt"
22+
nvm_ls_remote() {
23+
cat "$REMOTE"
24+
}
25+
REMOTE_IOJS="$PWD/mocks/nvm_ls_remote_iojs.txt"
26+
nvm_ls_remote_iojs() {
27+
cat "$REMOTE_IOJS"
28+
}
29+
30+
make_fake_node "lts/*"
31+
32+
nvm install --save --lts || die "\`nvm install --save --lts\` failed"
33+
OUTPUT="$(cat .nvmrc)"
34+
35+
nvm_is_valid_version "$(cat .nvmrc)" \
36+
|| die "\`nvm install --save --lts\`+ \`cat .nvmrc\` outputted invalid version: got '${OUTPUT}'"
37+
38+
cleanup
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/bin/sh
2+
\. ../../../nvm.sh
3+
\. ../../common.sh
4+
5+
set -e
6+
7+
TEST_VERSION=$(nvm_version node)
8+
echo "Using test version '$TEST_VERSION'"
9+
10+
if [ -f .nvmrc ]; then mv .nvmrc .nvmrc.orig; fi
11+
if [ -f ../.nvmrc ]; then mv ../.nvmrc ../.nvmrc.orig; fi
12+
13+
del_nvmrc () {
14+
rm -f .nvmrc ../.nvmrc
15+
}
16+
17+
cleanup () {
18+
del_nvmrc
19+
if [ -f .nvmrc.orig ]; then mv .nvmrc.orig .nvmrc; fi
20+
if [ -f ../.nvmrc.orig ]; then mv ../.nvmrc.orig ../.nvmrc; fi
21+
unset -f nvm_ls_remote nvm_ls_remote_iojs
22+
}
23+
24+
die () {
25+
echo "$@"
26+
cleanup
27+
exit 1
28+
}
29+
30+
REMOTE="$PWD/mocks/nvm_ls_remote.txt"
31+
nvm_ls_remote() {
32+
cat "$REMOTE"
33+
}
34+
REMOTE_IOJS="$PWD/mocks/nvm_ls_remote_iojs.txt"
35+
nvm_ls_remote_iojs() {
36+
cat "$REMOTE_IOJS"
37+
}
38+
39+
del_nvmrc
40+
make_fake_node "$TEST_VERSION"
41+
42+
(cd ..
43+
nvm use --save "$TEST_VERSION" || die "\`nvm use --save $TEST_VERSION\` failed in the parent dir")
44+
nvm use --save || die "\`nvm use --save\` failed"
45+
46+
[ -f ../.nvmrc ] && [ -f .nvmrc ] || die "expected two .nvmrc files to be generated"
47+
48+
OUTPUT=$(cat .nvmrc)
49+
EXPECTED_OUTPUT="$(cat ../.nvmrc)"
50+
51+
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] \
52+
|| die "invalid \`nvm use --save \` output: expected '$EXPECTED_OUTPUT'; got '$OUTPUT'"
53+
54+
cleanup
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/sh
2+
\. ../../../nvm.sh
3+
\. ../../common.sh
4+
5+
set -e
6+
7+
TEST_VERSION=$(nvm_version node)
8+
echo "Using test version '$TEST_VERSION'"
9+
10+
if [ -f .nvmrc ]; then mv .nvmrc .nvmrc.orig; fi
11+
12+
cleanup () {
13+
rm -f .nvmrc
14+
if [ -f .nvmrc.orig ]; then mv .nvmrc.orig .nvmrc; fi
15+
unset -f nvm_ls_remote nvm_ls_remote_iojs
16+
}
17+
18+
die () {
19+
echo "$@"
20+
cleanup
21+
exit 1
22+
}
23+
24+
REMOTE="$PWD/mocks/nvm_ls_remote.txt"
25+
nvm_ls_remote() {
26+
cat "$REMOTE"
27+
}
28+
REMOTE_IOJS="$PWD/mocks/nvm_ls_remote_iojs.txt"
29+
nvm_ls_remote_iojs() {
30+
cat "$REMOTE_IOJS"
31+
}
32+
33+
make_fake_node "$TEST_VERSION"
34+
35+
OUTPUT=$(nvm use --save --silent "$TEST_VERSION" || die "\`nvm use --save --silent $TEST_VERSION\` failed")
36+
EXPECTED_OUTPUT=""
37+
38+
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] \
39+
|| die "\`nvm use --save --silent $TEST_VERSION\` output was not silenced to '$EXPECTED_OUTPUT'; got '$OUTPUT'"
40+
41+
cleanup
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/sh
2+
\. ../../../nvm.sh
3+
\. ../../common.sh
4+
5+
set -e
6+
7+
if [ -f .nvmrc ]; then mv .nvmrc .nvmrc.orig; fi
8+
9+
cleanup () {
10+
rm -f .nvmrc
11+
if [ -f .nvmrc.orig ]; then mv .nvmrc.orig .nvmrc; fi
12+
unset -f nvm_ls_remote nvm_ls_remote_iojs
13+
}
14+
15+
die () {
16+
echo "$@"
17+
cleanup
18+
exit 1
19+
}
20+
21+
REMOTE="$PWD/mocks/nvm_ls_remote.txt"
22+
nvm_ls_remote() {
23+
cat "$REMOTE"
24+
}
25+
REMOTE_IOJS="$PWD/mocks/nvm_ls_remote_iojs.txt"
26+
nvm_ls_remote_iojs() {
27+
cat "$REMOTE_IOJS"
28+
}
29+
30+
TEST_VERSION="v0.9.4"
31+
make_fake_node "$TEST_VERSION"
32+
33+
# 1. install
34+
35+
nvm install -w "$TEST_VERSION" || die "\`nvm install -w --lts\` failed"
36+
OUTPUT="$(cat .nvmrc)"
37+
38+
nvm_is_valid_version "$(cat .nvmrc)" \
39+
|| die "\`nvm install -w --lts\`+ \`cat .nvmrc\` outputted invalid version: got '${OUTPUT}'"
40+
41+
#
42+
43+
cleanup
44+
unset OUTPUT
45+
46+
# 2. use
47+
48+
nvm use -w "$TEST_VERSION" || die "\`nvm use -w --lts\` failed"
49+
OUTPUT="$(cat .nvmrc)"
50+
51+
nvm_is_valid_version "$(cat .nvmrc)" \
52+
|| die "\`nvm use -w --lts\`+ \`cat .nvmrc\` outputted invalid version: got '${OUTPUT}'"
53+
54+
#
55+
56+
cleanup

0 commit comments

Comments
 (0)