Skip to content

Commit

Permalink
[New] nvm use/nvm install: add --save option
Browse files Browse the repository at this point in the history
FIxes #2849.

Co-authored-by: Martin <[email protected]>
Co-authored-by: Jordan Harband <[email protected]>
  • Loading branch information
maartin0 and ljharb committed Aug 29, 2022
1 parent 7c929f8 commit a40bfed
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 6 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ test/bak
.urchin.log
.urchin_stdout
test/**/test_output
test/**/.nvmrc

node_modules/
npm-debug.log
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ test/bak
.urchin.log
.urchin_stdout
test/**/test_output
test/**/.nvmrc

node_modules/
npm-debug.log
Expand Down
29 changes: 24 additions & 5 deletions nvm.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2844,6 +2844,7 @@ nvm() {
nvm_echo ' --no-progress Disable the progress bar on any downloads'
nvm_echo ' --alias=<name> After installing, set the alias specified to the version specified. (same as: nvm alias <name> <version>)'
nvm_echo ' --default After installing, set default alias to the version specified. (same as: nvm alias default <version>)'
nvm_echo ' --save After installing, write the specified version to .nvmrc'
nvm_echo ' nvm uninstall <version> Uninstall a version'
nvm_echo ' nvm uninstall --lts Uninstall using automatic LTS (long-term support) alias `lts/*`, if available.'
nvm_echo ' nvm uninstall --lts=<LTS name> Uninstall using automatic alias for provided LTS line, if available.'
Expand All @@ -2852,6 +2853,7 @@ nvm() {
nvm_echo ' --silent Silences stdout/stderr output'
nvm_echo ' --lts Uses automatic LTS (long-term support) alias `lts/*`, if available.'
nvm_echo ' --lts=<LTS name> Uses automatic alias for provided LTS line, if available.'
nvm_echo ' --save Writes the specified version to .nvmrc.'
nvm_echo ' nvm exec [<version>] [<command>] Run <command> on <version>. Uses .nvmrc if available and version is omitted.'
nvm_echo ' The following optional arguments, if provided, must appear directly after `nvm exec`:'
nvm_echo ' --silent Silences stdout/stderr output'
Expand Down Expand Up @@ -3059,6 +3061,8 @@ nvm() {
local ALIAS
local NVM_UPGRADE_NPM
NVM_UPGRADE_NPM=0
local NVM_WRITE_TO_NVMRC
NVM_WRITE_TO_NVMRC=0

local PROVIDED_REINSTALL_PACKAGES_FROM
local REINSTALL_PACKAGES_FROM
Expand Down Expand Up @@ -3158,6 +3162,10 @@ nvm() {
SKIP_DEFAULT_PACKAGES=true
shift
;;
--save)
NVM_WRITE_TO_NVMRC=1
shift
;;
*)
break # stop parsing args
;;
Expand Down Expand Up @@ -3533,6 +3541,8 @@ nvm() {
local NVM_LTS
local IS_VERSION_FROM_NVMRC
IS_VERSION_FROM_NVMRC=0
local NVM_WRITE_TO_NVMRC
NVM_WRITE_TO_NVMRC=0

while [ $# -ne 0 ]; do
case "$1" in
Expand All @@ -3544,6 +3554,7 @@ nvm() {
--) ;;
--lts) NVM_LTS='*' ;;
--lts=*) NVM_LTS="${1##--lts=}" ;;
--save) NVM_WRITE_TO_NVMRC=1 ;;
--*) ;;
*)
if [ -n "${1-}" ]; then
Expand Down Expand Up @@ -3576,16 +3587,24 @@ nvm() {
>&2 nvm --help
return 127
fi

local NVMRC_SAVE_TEXT
NVMRC_SAVE_TEXT=''
if [ $NVM_WRITE_TO_NVMRC -eq 1 ] && [ "${VERSION}" != '' ] && [ "${VERSION}" != 'N/A' ]; then
echo "${VERSION}" | tee "${PWD}"/.nvmrc > /dev/null || {
nvm_err "Warning: Unable to write version \`${VERSION}\` to .nvmrc"
exit 3
}
NVMRC_SAVE_TEXT=' (saved to .nvmrc)'
fi
if [ "_${VERSION}" = '_system' ]; then
if nvm_has_system_node && nvm deactivate "${NVM_SILENT_ARG-}" >/dev/null 2>&1; then
if [ "${NVM_SILENT:-0}" -ne 1 ]; then
nvm_echo "Now using system version of node: $(node -v 2>/dev/null)$(nvm_print_npm_version)"
nvm_echo "Now using system version of node: $(node -v 2>/dev/null)$(nvm_print_npm_version)${NVMRC_SAVE_TEXT}"
fi
return
elif nvm_has_system_iojs && nvm deactivate "${NVM_SILENT_ARG-}" >/dev/null 2>&1; then
if [ "${NVM_SILENT:-0}" -ne 1 ]; then
nvm_echo "Now using system version of io.js: $(iojs --version 2>/dev/null)$(nvm_print_npm_version)"
nvm_echo "Now using system version of io.js: $(iojs --version 2>/dev/null)$(nvm_print_npm_version)${NVMRC_SAVE_TEXT}"
fi
return
elif [ "${NVM_SILENT:-0}" -ne 1 ]; then
Expand Down Expand Up @@ -3634,9 +3653,9 @@ nvm() {
NVM_USE_OUTPUT=''
if [ "${NVM_SILENT:-0}" -ne 1 ]; then
if nvm_is_iojs_version "${VERSION}"; then
NVM_USE_OUTPUT="Now using io.js $(nvm_strip_iojs_prefix "${VERSION}")$(nvm_print_npm_version)"
NVM_USE_OUTPUT="Now using io.js $(nvm_strip_iojs_prefix "${VERSION}")$(nvm_print_npm_version)${NVMRC_SAVE_TEXT}"
else
NVM_USE_OUTPUT="Now using node ${VERSION}$(nvm_print_npm_version)"
NVM_USE_OUTPUT="Now using node ${VERSION}$(nvm_print_npm_version)${NVMRC_SAVE_TEXT}"
fi
fi
if [ "_${VERSION}" != "_system" ]; then
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/sh

set -e

die () {
unset -f nvm_ls_remote nvm_ls_remote_iojs
>&2 echo "$@"
exit 1
}

\. ../../../nvm.sh
\. ../../common.sh

rm -rf .nvmrc
echo '' > .nvmrc
chmod 0 .nvmrc

REMOTE="$PWD/mocks/nvm_ls_remote.txt"
nvm_ls_remote() {
cat "$REMOTE"
}
REMOTE_IOJS="$PWD/mocks/nvm_ls_remote_iojs.txt"
nvm_ls_remote_iojs() {
cat "$REMOTE_IOJS"
}

make_fake_node lts

{
(nvm install --save --lts) &&
die "\`nvm install --save --lts\` did not fail with invalid permissions"
} || echo "\`nvm install --save --lts\` failed successfully"
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/sh

set -e

die () {
unset -f nvm_ls_remote nvm_ls_remote_iojs
>&2 echo "$@"
exit 1
}

\. ../../../nvm.sh
\. ../../common.sh

REMOTE="$PWD/mocks/nvm_ls_remote.txt"
nvm_ls_remote() {
cat "$REMOTE"
}
REMOTE_IOJS="$PWD/mocks/nvm_ls_remote_iojs.txt"
nvm_ls_remote_iojs() {
cat "$REMOTE_IOJS"
}

test_version () {
rm -f .nvmrc
VERSION=${1-}

make_fake_node "${VERSION}"

nvm install --save "${VERSION}" || die "\`nvm install --save ${VERSION}\` failed"
OUTPUT="$(cat .nvmrc)"
EXPECTED_OUTPUT="$(nvm_ls_current)"

[ "_${OUTPUT}" = "_${EXPECTED_OUTPUT}" ] \
|| die "\`nvm use use --save ${VERSION}\`+ \`cat .nvmrc\` did not output '${EXPECTED_OUTPUT}'; got '${OUTPUT}'"
}

test_version '--lts' || die
test_version 'lts/argon' || die
test_version 'lts/*' || die
test_version 'node' || die
test_version 'iojs' || die
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/sh

set -e

die () {
unset -f nvm_ls_remote nvm_ls_remote_iojs
>&2 echo "$@"
exit 1
}

\. ../../../nvm.sh

REMOTE="$PWD/mocks/nvm_ls_remote.txt"
nvm_ls_remote() {
cat "$REMOTE"
}
REMOTE_IOJS="$PWD/mocks/nvm_ls_remote_iojs.txt"
nvm_ls_remote_iojs() {
cat "$REMOTE_IOJS"
}

make_fake_node lts

(cd ..
rm -rf .nvmrc
nvm install --save --lts)

nvm install --save
OUTPUT="$(cat .nvmrc)"
EXPECTED_OUTPUT="$(nvm_ls_current)"

[ "_${OUTPUT}" = "_${EXPECTED_OUTPUT}" ] \
|| die "\`nvm use use --save\`+ \`cat .nvmrc\` did not output '${EXPECTED_OUTPUT}'; got '${OUTPUT}'"

rm -rf ../.nvmrc .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh

set -e

die () { echo "$@" ; exit 1; }

\. ../../../nvm.sh

rm -rf .nvmrc;

nvm alias test node
nvm use test --save || die "\`nvm use test --save\` failed"
OUTPUT="$(cat .nvmrc)"
EXPECTED_OUTPUT=$(nvm_ls_current)

[ "_${OUTPUT}" = "_${EXPECTED_OUTPUT}" ] \
|| die "\`nvm use --save test\`+ \`cat .nvmrc\` did not output '${EXPECTED_OUTPUT}'; got '${OUTPUT}'"

nvm unalias test
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh

set -e

die () { echo "$@" ; exit 1; }

\. ../../../nvm.sh

rm -rf .nvmrc;
OUTPUT="$(nvm use --silent --save node || die "\`nvm use --silent --save node\` failed")"
EXPECTED_OUTPUT=''

[ "_${OUTPUT}" = "_${EXPECTED_OUTPUT}" ] \
|| die "\`nvm use --silent --save node\` was not silenced; got '${OUTPUT}'"
2 changes: 1 addition & 1 deletion test/fast/teardown
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ set -ex

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

0 comments on commit a40bfed

Please sign in to comment.