[New] select versions by category: alpha, lts, non-lts, iojs; exclude alphas by default

Node 27 and later publish Alpha prereleases, like `v27.0.0-alpha.1.0.0`, in the main `index.tab`,
so `nvm install node` or `nvm install 27` would otherwise pick one.
Every version is in exactly one category: `alpha` and `lts` or `non-lts` (by index.tab's LTS column) on the node mirror, and `iojs` on the io.js mirror.
Commands select from every category but `alpha`, unless `$NVM_ONLY` lists others,
and every command that selects or lists versions takes `--only=<categories>` to override both;
an exact alpha needs `alpha` selected, like any other.
An LTS selection, like `--lts` or an `lts/` version, can't combine with an `--only=` that excludes `lts`.
Telling installed `lts` and `non-lts` versions apart uses the recorded LTS status, and fails rather than guess without it.
`nvm alias` also stores `--only=`, so `nvm alias default --only=alpha node` makes the latest installed alpha the default, including in a new shell.
This commit is contained in:
Jordan Harband committed 2026-10-21 14:09:18 -07:00
1 parent 11df5ed6f1
commit 3fda4d5542
13 files changed
+1213 -51

No files matched your search

+75
View File
@@ -0,0 +1,75 @@
#!/bin/sh
ORIG_NVM_DIR="${NVM_DIR}"
TMP_DIR=''
cleanup () {
case "${TMP_DIR}" in
/*) rm -rf "${TMP_DIR}" ;;
esac
NVM_DIR="${ORIG_NVM_DIR}"
export NVM_DIR
}
die () { echo "$@" ; cleanup ; exit 1; }
: nvm.sh
\. ../../../nvm.sh
\. ../../common.sh
TMP_DIR="$(mktemp -d)"
NVM_DIR="${TMP_DIR}/nvm"
export NVM_DIR
mkdir -p "${NVM_DIR}/alias" || die 'could not create alias dir'
unset NVM_ONLY
for VERSION in v24.11.0 v26.12.1 v27.0.0-alpha.1.2.0 v27.0.0-alpha.1.10.2; do
mkdir -p "${NVM_DIR}/versions/node/${VERSION}/bin" || die "fake node ${VERSION} could not be made"
make_echo "${NVM_DIR}/versions/node/${VERSION}/bin/node" "${VERSION}"
done
printf 'v26.12.1\t2026-10-21\tsrc\t-\t-\t-\t-\t-\t-\t-\t-\nv24.11.0\t2025-10-28\tsrc\t-\t-\t-\t-\t-\t-\tKrypton\t-\n' | nvm_record_lts_status || die 'could not record LTS status'
nvm alias default --only=alpha node >/dev/null 2>&1 || die 'nvm alias default --only=alpha node failed'
[ "_$(cat "$(nvm_alias_path)/default")" = '_--only=alpha node' ] || die "the alias file should store >--only=alpha node<; got >$(cat "$(nvm_alias_path)/default")<"
[ "_$(nvm version default)" = '_v27.0.0-alpha.1.10.2' ] || die "default should be the latest installed alpha; got >$(nvm version default)<"
[ "_$(nvm version node)" = '_v26.12.1' ] || die "node itself should still exclude alphas; got >$(nvm version node)<"
nvm use --silent default || die 'nvm use default should use the alpha that default selects'
[ "_$(nvm current)" = '_v27.0.0-alpha.1.10.2' ] || die "nvm current should be the alpha; got >$(nvm current)<"
nvm deactivate >/dev/null 2>&1
nvm_auto use || die 'nvm_auto use should use the alpha that default selects'
[ "_$(nvm current)" = '_v27.0.0-alpha.1.10.2' ] || die "a new shell should start with the alpha; got >$(nvm current)<"
nvm_auto use || die 'nvm_auto use should keep using the current alpha'
nvm deactivate >/dev/null 2>&1
NVM_ONLY='lts,non-lts'
nvm use --silent --only=alpha v27.0.0-alpha.1.2.0 || die 'nvm use --only=alpha should use an alpha'
[ "_${NVM_ONLY}" = '_lts,non-lts' ] || die "--only should not change NVM_ONLY; got >${NVM_ONLY}<"
[ -z "${NVM_ONLY_SELECTED-}" ] || die "a command's selection should not outlive it; got >${NVM_ONLY_SELECTED}<"
unset NVM_ONLY
nvm deactivate >/dev/null 2>&1
nvm alias both --only=lts,alpha --lts node >/dev/null 2>&1 || die 'nvm alias both --only=lts,alpha --lts node failed'
[ "_$(cat "$(nvm_alias_path)/both")" = '_--only=alpha,lts --lts node' ] || die "the alias file should store the normalized flags; got >$(cat "$(nvm_alias_path)/both")<"
[ "_$(nvm version both)" = '_v24.11.0' ] || die "both should be the latest installed LTS version; got >$(nvm version both)<"
try_err nvm alias bad --only=alpha --lts node
[ "_${CAPTURED_EXIT_CODE}" = '_55' ] || die "--lts with an --only that excludes LTS should exit 55; got ${CAPTURED_EXIT_CODE}"
[ ! -e "$(nvm_alias_path)/bad" ] || die 'a conflicting alias should not be written'
# a new shell survives `set -e` with a default that resolves to nothing
nvm_make_alias default v99.0.0 || die 'could not alias default'
(set -e; nvm_auto use 2>/dev/null; true) || die 'nvm_auto use should not fail with a default that resolves to nothing'
# and explains a default whose flags need LTS status that isn't recorded yet
rm -f "$(nvm_lts_status_path)"
nvm_make_alias default '--only=lts node' || die 'could not alias default'
try_err nvm_auto use
[ "_${CAPTURED_STDERR}" = '_The default alias needs the LTS status of installed versions, which is not recorded yet; run `nvm ls-remote` to record it.' ] || die "nvm_auto use should explain the missing LTS status; got >${CAPTURED_STDERR}<"
echo '--only=beta node' > "$(nvm_alias_path)/invalid"
[ "_$(nvm version invalid 2>/dev/null)" = '_N/A' ] || die "an alias with an invalid --only should not resolve; got >$(nvm version invalid 2>/dev/null)<"
cleanup