Skip to content

Commit 67ab6bb

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 7c929f8 commit 67ab6bb

9 files changed

+168
-6
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: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2844,6 +2844,7 @@ nvm() {
28442844
nvm_echo ' --no-progress Disable the progress bar on any downloads'
28452845
nvm_echo ' --alias=<name> After installing, set the alias specified to the version specified. (same as: nvm alias <name> <version>)'
28462846
nvm_echo ' --default After installing, set default alias to the version specified. (same as: nvm alias default <version>)'
2847+
nvm_echo ' --save After installing, write the specified version to .nvmrc'
28472848
nvm_echo ' nvm uninstall <version> Uninstall a version'
28482849
nvm_echo ' nvm uninstall --lts Uninstall using automatic LTS (long-term support) alias `lts/*`, if available.'
28492850
nvm_echo ' nvm uninstall --lts=<LTS name> Uninstall using automatic alias for provided LTS line, if available.'
@@ -2852,6 +2853,7 @@ nvm() {
28522853
nvm_echo ' --silent Silences stdout/stderr output'
28532854
nvm_echo ' --lts Uses automatic LTS (long-term support) alias `lts/*`, if available.'
28542855
nvm_echo ' --lts=<LTS name> Uses automatic alias for provided LTS line, if available.'
2856+
nvm_echo ' --save Writes the specified version to .nvmrc.'
28552857
nvm_echo ' nvm exec [<version>] [<command>] Run <command> on <version>. Uses .nvmrc if available and version is omitted.'
28562858
nvm_echo ' The following optional arguments, if provided, must appear directly after `nvm exec`:'
28572859
nvm_echo ' --silent Silences stdout/stderr output'
@@ -3059,6 +3061,8 @@ nvm() {
30593061
local ALIAS
30603062
local NVM_UPGRADE_NPM
30613063
NVM_UPGRADE_NPM=0
3064+
local NVM_WRITE_TO_NVMRC
3065+
NVM_WRITE_TO_NVMRC=0
30623066

30633067
local PROVIDED_REINSTALL_PACKAGES_FROM
30643068
local REINSTALL_PACKAGES_FROM
@@ -3158,6 +3162,10 @@ nvm() {
31583162
SKIP_DEFAULT_PACKAGES=true
31593163
shift
31603164
;;
3165+
--save)
3166+
NVM_WRITE_TO_NVMRC=1
3167+
shift
3168+
;;
31613169
*)
31623170
break # stop parsing args
31633171
;;
@@ -3533,6 +3541,8 @@ nvm() {
35333541
local NVM_LTS
35343542
local IS_VERSION_FROM_NVMRC
35353543
IS_VERSION_FROM_NVMRC=0
3544+
local NVM_WRITE_TO_NVMRC
3545+
NVM_WRITE_TO_NVMRC=0
35363546

35373547
while [ $# -ne 0 ]; do
35383548
case "$1" in
@@ -3544,6 +3554,7 @@ nvm() {
35443554
--) ;;
35453555
--lts) NVM_LTS='*' ;;
35463556
--lts=*) NVM_LTS="${1##--lts=}" ;;
3557+
--save) NVM_WRITE_TO_NVMRC=1 ;;
35473558
--*) ;;
35483559
*)
35493560
if [ -n "${1-}" ]; then
@@ -3576,16 +3587,24 @@ nvm() {
35763587
>&2 nvm --help
35773588
return 127
35783589
fi
3579-
3590+
local NVMRC_SAVE_TEXT
3591+
NVMRC_SAVE_TEXT=''
3592+
if [ $NVM_WRITE_TO_NVMRC -eq 1 ] && [ "${VERSION}" != '' ] && [ "${VERSION}" != 'N/A' ]; then
3593+
echo "${VERSION}" | tee "${PWD}"/.nvmrc > /dev/null || {
3594+
nvm_err "Warning: Unable to write version \`${VERSION}\` to .nvmrc"
3595+
exit 3
3596+
}
3597+
NVMRC_SAVE_TEXT=' (saved to .nvmrc)'
3598+
fi
35803599
if [ "_${VERSION}" = '_system' ]; then
35813600
if nvm_has_system_node && nvm deactivate "${NVM_SILENT_ARG-}" >/dev/null 2>&1; then
35823601
if [ "${NVM_SILENT:-0}" -ne 1 ]; then
3583-
nvm_echo "Now using system version of node: $(node -v 2>/dev/null)$(nvm_print_npm_version)"
3602+
nvm_echo "Now using system version of node: $(node -v 2>/dev/null)$(nvm_print_npm_version)${NVMRC_SAVE_TEXT}"
35843603
fi
35853604
return
35863605
elif nvm_has_system_iojs && nvm deactivate "${NVM_SILENT_ARG-}" >/dev/null 2>&1; then
35873606
if [ "${NVM_SILENT:-0}" -ne 1 ]; then
3588-
nvm_echo "Now using system version of io.js: $(iojs --version 2>/dev/null)$(nvm_print_npm_version)"
3607+
nvm_echo "Now using system version of io.js: $(iojs --version 2>/dev/null)$(nvm_print_npm_version)${NVMRC_SAVE_TEXT}"
35893608
fi
35903609
return
35913610
elif [ "${NVM_SILENT:-0}" -ne 1 ]; then
@@ -3634,9 +3653,9 @@ nvm() {
36343653
NVM_USE_OUTPUT=''
36353654
if [ "${NVM_SILENT:-0}" -ne 1 ]; then
36363655
if nvm_is_iojs_version "${VERSION}"; then
3637-
NVM_USE_OUTPUT="Now using io.js $(nvm_strip_iojs_prefix "${VERSION}")$(nvm_print_npm_version)"
3656+
NVM_USE_OUTPUT="Now using io.js $(nvm_strip_iojs_prefix "${VERSION}")$(nvm_print_npm_version)${NVMRC_SAVE_TEXT}"
36383657
else
3639-
NVM_USE_OUTPUT="Now using node ${VERSION}$(nvm_print_npm_version)"
3658+
NVM_USE_OUTPUT="Now using node ${VERSION}$(nvm_print_npm_version)${NVMRC_SAVE_TEXT}"
36403659
fi
36413660
fi
36423661
if [ "_${VERSION}" != "_system" ]; then
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
die () {
6+
unset -f nvm_ls_remote nvm_ls_remote_iojs
7+
>&2 echo "$@"
8+
exit 1
9+
}
10+
11+
\. ../../nvm.sh
12+
\. ../common.sh
13+
14+
rm -rf .nvmrc
15+
echo '' > .nvmrc
16+
chmod 0 .nvmrc
17+
18+
REMOTE="$PWD/mocks/nvm_ls_remote.txt"
19+
nvm_ls_remote() {
20+
cat "$REMOTE"
21+
}
22+
REMOTE_IOJS="$PWD/mocks/nvm_ls_remote_iojs.txt"
23+
nvm_ls_remote_iojs() {
24+
cat "$REMOTE_IOJS"
25+
}
26+
27+
make_fake_node lts
28+
29+
{
30+
(nvm install --save --lts) &&
31+
die "\`nvm install --save --lts\` did not fail with invalid permissions"
32+
} || echo "\`nvm install --save --lts\` failed successfully"
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
die () {
6+
unset -f nvm_ls_remote nvm_ls_remote_iojs
7+
>&2 echo "$@"
8+
exit 1
9+
}
10+
11+
\. ../../nvm.sh
12+
\. ../common.sh
13+
14+
REMOTE="$PWD/mocks/nvm_ls_remote.txt"
15+
nvm_ls_remote() {
16+
cat "$REMOTE"
17+
}
18+
REMOTE_IOJS="$PWD/mocks/nvm_ls_remote_iojs.txt"
19+
nvm_ls_remote_iojs() {
20+
cat "$REMOTE_IOJS"
21+
}
22+
23+
test_version () {
24+
rm -f .nvmrc
25+
VERSION=${1-}
26+
27+
make_fake_node "${VERSION}"
28+
29+
nvm install --save "${VERSION}" || die "\`nvm install --save ${VERSION}\` failed"
30+
OUTPUT="$(cat .nvmrc)"
31+
EXPECTED_OUTPUT="$(nvm_ls_current)"
32+
33+
[ "_${OUTPUT}" = "_${EXPECTED_OUTPUT}" ] \
34+
|| die "\`nvm use use --save ${VERSION}\`+ \`cat .nvmrc\` did not output '${EXPECTED_OUTPUT}'; got '${OUTPUT}'"
35+
}
36+
37+
test_version '--lts' || die
38+
test_version 'lts/argon' || die
39+
test_version 'lts/*' || die
40+
test_version 'node' || die
41+
test_version 'iojs' || die
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
die () {
6+
unset -f nvm_ls_remote nvm_ls_remote_iojs
7+
>&2 echo "$@"
8+
exit 1
9+
}
10+
11+
\. ../../nvm.sh
12+
13+
REMOTE="$PWD/mocks/nvm_ls_remote.txt"
14+
nvm_ls_remote() {
15+
cat "$REMOTE"
16+
}
17+
REMOTE_IOJS="$PWD/mocks/nvm_ls_remote_iojs.txt"
18+
nvm_ls_remote_iojs() {
19+
cat "$REMOTE_IOJS"
20+
}
21+
22+
make_fake_node lts
23+
24+
(cd ..
25+
rm -rf .nvmrc
26+
nvm install --save --lts)
27+
28+
nvm install --save
29+
OUTPUT="$(cat .nvmrc)"
30+
EXPECTED_OUTPUT="$(nvm_ls_current)"
31+
32+
[ "_${OUTPUT}" = "_${EXPECTED_OUTPUT}" ] \
33+
|| die "\`nvm use use --save\`+ \`cat .nvmrc\` did not output '${EXPECTED_OUTPUT}'; got '${OUTPUT}'"
34+
35+
rm -rf ../.nvmrc .nvmrc
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
die () { echo "$@" ; exit 1; }
6+
7+
\. ../../nvm.sh
8+
9+
rm -rf .nvmrc;
10+
11+
nvm alias test node
12+
nvm use test --save || die "\`nvm use test --save\` failed"
13+
OUTPUT="$(cat .nvmrc)"
14+
EXPECTED_OUTPUT=$(nvm_ls_current)
15+
16+
[ "_${OUTPUT}" = "_${EXPECTED_OUTPUT}" ] \
17+
|| die "\`nvm use --save test\`+ \`cat .nvmrc\` did not output '${EXPECTED_OUTPUT}'; got '${OUTPUT}'"
18+
19+
nvm unalias test
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
die () { echo "$@" ; exit 1; }
6+
7+
\. ../../nvm.sh
8+
9+
rm -rf .nvmrc;
10+
OUTPUT="$(nvm use --silent --save node || die "\`nvm use --silent --save node\` failed")"
11+
EXPECTED_OUTPUT=''
12+
13+
[ "_${OUTPUT}" = "_${EXPECTED_OUTPUT}" ] \
14+
|| die "\`nvm use --silent --save node\` was not silenced; got '${OUTPUT}'"

test/fast/teardown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ set -ex
88

99
type setopt >/dev/null 2>&1 && setopt NULL_GLOB
1010
type shopt >/dev/null 2>&1 && shopt -s nullglob
11-
rm -fR v* src alias test/test-xz
11+
rm -fR v* src alias test/test-xz .nvmrc
1212
)

0 commit comments

Comments
 (0)