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
3 people committed Feb 22, 2024
1 parent c24c313 commit ae27ed5
Show file tree
Hide file tree
Showing 8 changed files with 281 additions and 0 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
34 changes: 34 additions & 0 deletions nvm.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2912,6 +2912,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 @@ -2920,6 +2921,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 @@ -3132,6 +3134,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 @@ -3230,6 +3234,10 @@ nvm() {
SKIP_DEFAULT_PACKAGES=true
shift
;;
--save)
NVM_WRITE_TO_NVMRC=1
shift
;;
*)
break # stop parsing args
;;
Expand Down Expand Up @@ -3350,6 +3358,15 @@ nvm() {
FLAVOR="$(nvm_node_prefix)"
fi

# Write .nvmrc if flag is set
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 number (${VERSION}) to .nvmrc"
exit 3
}
nvm_echo "Wrote version number (${VERSION}) to .nvmrc"
fi

local EXIT_CODE
EXIT_CODE=0

Expand Down Expand Up @@ -3466,6 +3483,7 @@ nvm() {
else
EXIT_CODE=$?
fi

return $EXIT_CODE
;;
"uninstall")
Expand Down Expand Up @@ -3606,6 +3624,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 @@ -3617,6 +3637,7 @@ nvm() {
--) ;;
--lts) NVM_LTS='*' ;;
--lts=*) NVM_LTS="${1##--lts=}" ;;
--save) NVM_WRITE_TO_NVMRC=1 ;;
--*) ;;
*)
if [ -n "${1-}" ]; then
Expand Down Expand Up @@ -3650,6 +3671,19 @@ nvm() {
return 127
fi

# Write .nvmrc if flag is set
if [ $NVM_WRITE_TO_NVMRC -eq 1 ] && [ "${VERSION}" != '' ] && [ "${VERSION}" != 'N/A' ]; then
echo "${VERSION}" | tee "${PWD}"/.nvmrc > /dev/null || {
if [ "${NVM_SILENT:-0}" -ne 1 ]; then
nvm_err "Warning: Unable to write version number (${VERSION}) to .nvmrc"
fi
exit 3
}
if [ "${NVM_SILENT:-0}" -ne 1 ]; then
nvm_echo "Wrote version number (${VERSION}) to .nvmrc"
fi
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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/sh

set -e

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

TEST_VERSION="$(nvm_version node)"
echo "Using test version '$TEST_VERSION'"

if [ -f .nvmrc ]; then mv .nvmrc .nvmrc.orig; fi

cleanup () {
rm -f .nvmrc
if [ -f .nvmrc.orig ]; then mv .nvmrc.orig .nvmrc; fi
}

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

rm -f .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 "$TEST_VERSION"

{
(nvm install --save "$TEST_VERSION") &&
die "\`nvm install --save $TEST_VERSION\` did not fail with invalid permissions"
} || echo "\`nvm install --save $TEST_VERSION\` failed successfully"

cleanup
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/sh

set -e

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

TEST_VERSION="$(nvm_version node)"
echo "Using test version '$TEST_VERSION'"

if [ -f .nvmrc ]; then mv .nvmrc .nvmrc.orig; fi
if [ -f ../.nvmrc ]; then mv ../.nvmrc ../.nvmrc.orig; fi

cleanup () {
rm -f .nvmrc ../.nvmrc
if [ -f .nvmrc.orig ]; then mv .nvmrc.orig .nvmrc; fi
if [ -f ../.nvmrc.orig ]; then mv ../.nvmrc.orig ../.nvmrc; fi
unset -f nvm_ls_remote nvm_ls_remote_iojs
}

die () {
>&2 echo "$@"
cleanup
exit 1
}

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 "$TEST_VERSION"

rm -f .nvmrc ../.nvmrc
(cd ..
nvm install --save "$TEST_VERSION")

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

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

cleanup
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/sh

set -e

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

if [ -f .nvmrc ]; then mv .nvmrc .nvmrc.orig; fi

cleanup () {
rm -f .nvmrc
if [ -f .nvmrc.orig ]; then mv .nvmrc.orig .nvmrc; fi
unset -f nvm_ls_remote nvm_ls_remote_iojs
}

die () {
>&2 echo "$@"
cleanup
exit 1
}

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-}
TEST_VERSION="$(nvm_version "$VERSION")"

make_fake_node "${TEST_VERSION}"

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

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

test_version "--lts" || die
test_version "lts/iron" || die
test_version "lts/*" || die
#test_version "node" || die
test_version "iojs" || die

cleanup
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/sh

set -e

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

TEST_VERSION=$(nvm_version node)
echo "Using test version '$TEST_VERSION'"

if [ -f .nvmrc ]; then mv .nvmrc .nvmrc.orig; fi

cleanup () {
rm -f .nvmrc
if [ -f .nvmrc.orig ]; then mv .nvmrc.orig .nvmrc; fi
unset -f nvm_ls_remote nvm_ls_remote_iojs
nvm unalias test
}

die () {
>&2 echo "$@"
cleanup
exit 1
}

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 "$TEST_VERSION"

echo "+ nvm unalias test"
nvm unalias test &>/dev/null
echo "+ nvm install $TEST_VERSION"
nvm install "$TEST_VERSION"
echo "+ nvm alias test $TEST_VERSION"
nvm alias test "$TEST_VERSION"
echo "+ nvm install test"
nvm install test
echo "nvm use --save test"
nvm use --save test

OUTPUT="$(nvm_version "$(cat .nvmrc)")"
EXPECTED_OUTPUT="$TEST_VERSION"

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

cleanup
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/sh

set -e

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

TEST_VERSION=$(nvm_version node)
echo "Using test version '$TEST_VERSION'"

if [ -f .nvmrc ]; then mv .nvmrc .nvmrc.orig; fi

cleanup () {
rm -f .nvmrc
if [ -f .nvmrc.orig ]; then mv .nvmrc.orig .nvmrc; fi
unset -f nvm_ls_remote nvm_ls_remote_iojs
}

die () {
>&2 echo "$@"
cleanup
exit 1
}

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 "$TEST_VERSION"
nvm install "$TEST_VERSION" || die "'nvm install $TEST_VERSION' failed"

echo "+ nvm use --save --silent $TEST_VERSION"
OUTPUT=$(nvm use --save --silent "$TEST_VERSION" || die "\`nvm use --save --silent $TEST_VERSION\` failed")
EXPECTED_OUTPUT=""

[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] \
|| die "\`nvm use --save --silent $TEST_VERSION\` output was not silenced to '$EXPECTED_OUTPUT'; got '$OUTPUT'"

cleanup

0 comments on commit ae27ed5

Please sign in to comment.