[New] adding default and alias option to `nvm install`

For: `nvm install 8.12.0` after install:

--alias=8 is equivalent to `nvm alias 8 8.12.0`
--default is equivalent to `nvm alias default 8.12.0`

Co-authored-by: Kayla Altepeter <kayla@kaylaaltepeter.com>
Co-authored-by: Dena Burd <me@Denas-MacBook-Air.local>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
Kayla Altepeter 2018-10-22 08:32:29 -05:00 committed by Jordan Harband
parent 18c6f98acc
commit a5aec107df
No known key found for this signature in database
GPG Key ID: 9F6A681E35EF8B56
3 changed files with 50 additions and 0 deletions

28
nvm.sh
View File

@ -1771,6 +1771,10 @@ nvm_install_binary() {
command mv "${TMPDIR}/"* "${VERSION_PATH}" && \ command mv "${TMPDIR}/"* "${VERSION_PATH}" && \
command rm -rf "${TMPDIR}" command rm -rf "${TMPDIR}"
); then ); then
if [ -n "${ALIAS-}" ]; then
nvm alias "${ALIAS}" "${provided_version}"
fi
return 0 return 0
fi fi
@ -2370,6 +2374,8 @@ nvm() {
nvm_echo ' --skip-default-packages When installing, skip the default-packages file if it exists' nvm_echo ' --skip-default-packages When installing, skip the default-packages file if it exists'
nvm_echo ' --latest-npm After installing, attempt to upgrade to the latest working npm on the given node version' nvm_echo ' --latest-npm After installing, attempt to upgrade to the latest working npm on the given node version'
nvm_echo ' --no-progress Disable the progress bar on any downloads' 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 ' nvm uninstall <version> Uninstall a version' 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 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.' nvm_echo ' nvm uninstall --lts=<LTS name> Uninstall using automatic alias for provided LTS line, if available.'
@ -2546,6 +2552,7 @@ nvm() {
nobinary=0 nobinary=0
noprogress=0 noprogress=0
local LTS local LTS
local ALIAS
local NVM_UPGRADE_NPM local NVM_UPGRADE_NPM
NVM_UPGRADE_NPM=0 NVM_UPGRADE_NPM=0
@ -2585,6 +2592,22 @@ nvm() {
NVM_UPGRADE_NPM=1 NVM_UPGRADE_NPM=1
shift shift
;; ;;
--default)
if [ -n "${ALIAS-}" ]; then
nvm_err '--default and --alias are mutually exclusive, and may not be provided more than once'
return 6
fi
ALIAS='default'
shift
;;
--alias=*)
if [ -n "${ALIAS-}" ]; then
nvm_err '--default and --alias are mutually exclusive, and may not be provided more than once'
return 6
fi
ALIAS="${1##--alias=}"
shift
;;
--reinstall-packages-from=*) --reinstall-packages-from=*)
if [ -n "${PROVIDED_REINSTALL_PACKAGES_FROM-}" ]; then if [ -n "${PROVIDED_REINSTALL_PACKAGES_FROM-}" ]; then
nvm_err '--reinstall-packages-from may not be provided more than once' nvm_err '--reinstall-packages-from may not be provided more than once'
@ -2766,6 +2789,11 @@ nvm() {
else else
nvm_ensure_default_set "${provided_version}" nvm_ensure_default_set "${provided_version}"
fi fi
if [ -n "${ALIAS}" ]; then
nvm alias "${ALIAS}" "${provided_version}"
fi
return $? return $?
fi fi

View File

@ -0,0 +1,11 @@
#!/bin/sh
set -ex
die () { echo "$@" ; exit 1; }
\. ../../nvm.sh
nvm install --alias=9 9.11.2 || die '`nvm install --alias=9 9.11.2` failed'
TERM=dumb nvm alias | grep '9 -> 9.11.2 (-> v9.11.2 \*)' || die 'did not make the expected alias'

View File

@ -0,0 +1,11 @@
#!/bin/sh
set -ex
die () { echo "$@" ; exit 1; }
\. ../../nvm.sh
nvm install --default node || die '`nvm install --default` failed'
TERM=dumb nvm alias | grep "default -> node (-> $(nvm version node) \*)" || die 'did not make the expected alias'