Skip to content

Commit

Permalink
[Fix] --save: avoid a conflict with set -e
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Aug 10, 2024
1 parent 81f18bc commit 09cf8c0
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions nvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3360,6 +3360,10 @@ nvm() {
shift
;;
--save | -w)
if [ $NVM_WRITE_TO_NVMRC -eq 1 ]; then
nvm_err '--save and -w may only be provided once'
return 6
fi
NVM_WRITE_TO_NVMRC=1
shift
;;
Expand Down Expand Up @@ -3511,6 +3515,11 @@ nvm() {
nvm_ensure_default_set "${provided_version}"
fi

if [ $NVM_WRITE_TO_NVMRC -eq 1 ]; then
nvm_write_nvmrc "${VERSION}"
EXIT_CODE=$?
fi

if [ $EXIT_CODE -ne 0 ] && [ -n "${ALIAS-}" ]; then
nvm alias "${ALIAS}" "${provided_version}"
EXIT_CODE=$?
Expand Down Expand Up @@ -3746,6 +3755,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 @@ -3757,7 +3768,13 @@ nvm() {
--) ;;
--lts) NVM_LTS='*' ;;
--lts=*) NVM_LTS="${1##--lts=}" ;;
--save | -w) NVM_WRITE_TO_NVMRC=1 ;;
--save | -w)
if [ $NVM_WRITE_TO_NVMRC -eq 1 ]; then
nvm_err '--save and -w may only be provided once'
return 6
fi
NVM_WRITE_TO_NVMRC=1
;;
--*) ;;
*)
if [ -n "${1-}" ]; then
Expand Down Expand Up @@ -3791,8 +3808,8 @@ nvm() {
return 127
fi

if [ "${NVM_WRITE_TO_NVMRC:-0}" -eq 1 ]; then
nvm_write_nvmrc "$VERSION"
if [ $NVM_WRITE_TO_NVMRC -eq 1 ]; then
nvm_write_nvmrc "${VERSION}"
fi

if [ "_${VERSION}" = '_system' ]; then
Expand Down Expand Up @@ -4543,17 +4560,21 @@ nvm_auto() {
local NVM_CURRENT
if [ "_${NVM_MODE}" = '_install' ]; then
VERSION="$(nvm_alias default 2>/dev/null || nvm_echo)"
if [ -n "${VERSION}" ] && ! [ "_${VERSION}" = '_N/A' ] && nvm_is_valid_version "${VERSION}"; then
nvm install "${VERSION}" >/dev/null
if [ -n "${VERSION}" ]; then
if [ "_${VERSION}" != '_N/A' ] && nvm_is_valid_version "${VERSION}"; then
nvm install "${VERSION}" >/dev/null
fi
elif nvm_rc_version >/dev/null 2>&1; then
nvm install >/dev/null
fi
elif [ "_$NVM_MODE" = '_use' ]; then
NVM_CURRENT="$(nvm_ls_current)"
if [ "_${NVM_CURRENT}" = '_none' ] || [ "_${NVM_CURRENT}" = '_system' ]; then
VERSION="$(nvm_resolve_local_alias default 2>/dev/null || nvm_echo)"
if [ -n "${VERSION}" ] && ! [ "_${VERSION}" = '_N/A' ] && nvm_is_valid_version "${VERSION}"; then
nvm use --silent "${VERSION}" >/dev/null
if [ -n "${VERSION}" ]; then
if [ "_${VERSION}" != '_N/A' ] && nvm_is_valid_version "${VERSION}"; then
nvm use --silent "${VERSION}" >/dev/null
fi
elif nvm_rc_version >/dev/null 2>&1; then
nvm use --silent >/dev/null
fi
Expand Down

0 comments on commit 09cf8c0

Please sign in to comment.