Compare commits

...

19 Commits

Author SHA1 Message Date
ryenus
b9ad3ef12b
Merge 3dd8beb6ea into 759f70f196 2025-02-07 13:45:36 +01:00
Jordan Harband
759f70f196
[Refactor] prefer case over if/else chains 2025-02-04 22:45:35 -08:00
Jordan Harband
06a9179309
[Refactor] combine sed -e invocations/arguments 2025-02-04 15:55:10 -08:00
ryenus
3dd8beb6ea indent awk script properly 2024-12-23 10:07:06 +08:00
ryenus
c6bb417804 display a hint when version filtering is in effect
due the use of --min or NVM_MIN, which would show only the versions
higher than the specified min version, and/or the available minor/patch
updates for the installed versions.
2024-12-23 10:02:56 +08:00
ryenus
ae8b033db9 set NVM_NO_COLORS to --no-colors to disable color 2024-12-13 09:35:37 +08:00
ryenus
dbf7c3ed3c rename min_ver to min for consistency 2024-12-13 09:35:37 +08:00
ryenus
b145f236a8 [test] avoid leaking temp variable 2024-12-13 09:35:37 +08:00
ryenus
794881332d ls-remote: introduce temp var for env inheritance
This is to inherit $NVM_MIN from env when defined, meanwhile avoiding
inline local variable initialization for ksh compatibility.

Co-authored-by: Jordan Harband <ljharb@gmail.com>
2024-12-13 09:35:37 +08:00
ryenus
5cffcaf8f2 avoid inline initialization for ksh compatibility
Co-authored-by: Jordan Harband <ljharb@gmail.com>
2024-12-13 09:35:37 +08:00
ryenus
1f1ea6f88c ls-remote: add CLI option --min=<version>
When omitted, fallback to the environment variable "NVM_MIN" if set.
And the CLI option --min=<version> takes precedence over the environment
variable "NVM_MIN" if both are present.
2024-12-13 09:35:37 +08:00
ryenus
1fbd8ca3dd always show updates for installed versions
even if they're older than $NVM_MIN
2024-12-13 09:35:37 +08:00
ryenus
ea56ffda30 remove function ref duplication 2024-12-13 09:35:23 +08:00
ryenus
c6492fd8ad prefixed versions like v18 also work 2024-12-13 09:35:23 +08:00
ryenus
76c41c68b4 rename NVM_MIN_VER to NVM_MIN
since nvm is all about versions, so no need for the explicit suffix.
2024-12-13 09:35:23 +08:00
ryenus
b3148ae5c0 trim leading space in mock output due to eclint errors 2024-12-13 09:35:23 +08:00
ryenus
cc2222cb1b add test for NVM_MIN_VER support
show only versions newer than NVM_MIN_VER if set
2024-12-13 09:35:23 +08:00
ryenus
646d694379 always list installed versions 2024-12-13 09:35:23 +08:00
ryenus
cedbb16256 show only versions newer than NVM_MIN_VER if set 2024-12-13 09:35:23 +08:00
2 changed files with 244 additions and 69 deletions

81
nvm.sh
View File

@ -136,15 +136,17 @@ nvm_download() {
eval "curl -q --fail ${CURL_COMPRESSED_FLAG:-} ${CURL_HEADER_FLAG:-} ${NVM_DOWNLOAD_ARGS}"
elif nvm_has "wget"; then
# Emulate curl with wget
ARGS=$(nvm_echo "$@" | command sed -e 's/--progress-bar /--progress=bar /' \
-e 's/--compressed //' \
-e 's/--fail //' \
-e 's/-L //' \
-e 's/-I /--server-response /' \
-e 's/-s /-q /' \
-e 's/-sS /-nv /' \
-e 's/-o /-O /' \
-e 's/-C - /-c /')
ARGS=$(nvm_echo "$@" | command sed "
s/--progress-bar /--progress=bar /
s/--compressed //
s/--fail //
s/-L //
s/-I /--server-response /
s/-s /-q /
s/-sS /-nv /
s/-o /-O /
s/-C - /-c /
")
if [ -n "${NVM_AUTH_HEADER:-}" ]; then
ARGS="${ARGS} --header \"${NVM_AUTH_HEADER}\""
@ -1413,11 +1415,11 @@ nvm_add_iojs_prefix() {
nvm_strip_iojs_prefix() {
local NVM_IOJS_PREFIX
NVM_IOJS_PREFIX="$(nvm_iojs_prefix)"
if [ "${1-}" = "${NVM_IOJS_PREFIX}" ]; then
nvm_echo
else
nvm_echo "${1#"${NVM_IOJS_PREFIX}"-}"
fi
case "${1-}" in
"${NVM_IOJS_PREFIX}") nvm_echo ;;
*) nvm_echo "${1#"${NVM_IOJS_PREFIX}"-}" ;;
esac
}
nvm_ls() {
@ -1549,12 +1551,15 @@ nvm_ls() {
fi
if [ "${NVM_ADD_SYSTEM-}" = true ]; then
if [ -z "${PATTERN}" ] || [ "${PATTERN}" = 'v' ]; then
case "${PATTERN}" in
'' | v)
VERSIONS="${VERSIONS}
system"
elif [ "${PATTERN}" = 'system' ]; then
;;
system)
VERSIONS="system"
fi
;;
esac
fi
if [ -z "${VERSIONS}" ]; then
@ -1878,12 +1883,15 @@ nvm_print_versions() {
fi
command awk \
-v remote_versions="$(printf '%s' "${1-}" | tr '\n' '|')" \
-v remote_versions="$(printf '%s' "${1-}" | tr '\n' '|')" -v min="${NVM_MIN:-v0}" \
-v installed_versions="$(nvm_ls | tr '\n' '|')" -v current="$NVM_CURRENT" \
-v installed_color="$INSTALLED_COLOR" -v system_color="$SYSTEM_COLOR" \
-v current_color="$CURRENT_COLOR" -v default_color="$DEFAULT_COLOR" \
-v old_lts_color="$DEFAULT_COLOR" -v has_colors="$NVM_HAS_COLORS" '
function alen(arr, i, len) { len=0; for(i in arr) len++; return len; }
function v2a(v, a) { sub(/^(iojs-)?v/, "", v); split(v, a, "."); }
function v2m(v, a) { sub(/^(iojs-)?v/, "", v); split(v, a, "."); return a[1]; }
function vcmp(v1,v2,a1,a2,i,d) { v2a(v1,a1); v2a(v2,a2); for(i=1;i<4;i++) { d = a1[i] - a2[i]; if(d!=0) return d; } return 0; }
BEGIN {
fmt_installed = has_colors ? (installed_color ? "\033[" installed_color "%15s\033[0m" : "%15s") : "%15s *";
fmt_system = has_colors ? (system_color ? "\033[" system_color "%15s\033[0m" : "%15s") : "%15s *";
@ -1898,13 +1906,13 @@ BEGIN {
split(remote_versions, lines, "|");
split(installed_versions, installed, "|");
rows = alen(lines);
for (n = 1; n <= rows; n++) {
filter_on = (vcmp("v0.0.0", min) != 0);
current_major = -1;
for (m = n = 1; n <= rows; n++) {
split(lines[n], fields, "[[:blank:]]+");
cols = alen(fields);
version = fields[1];
is_installed = 0;
for (i in installed) {
if (version == installed[i]) {
is_installed = 1;
@ -1912,6 +1920,16 @@ BEGIN {
}
}
if (filter_on != 0) {
if (is_installed) {
current_major = v2m(version);
} else if (vcmp(version, min) >= 0) {
filter_on = 0;
} else if (v2m(version) != current_major) {
continue;
}
}
fmt_version = "%15s";
if (version == current) {
fmt_version = fmt_current;
@ -1921,8 +1939,7 @@ BEGIN {
fmt_version = fmt_installed;
}
padding = (!has_colors && is_installed) ? "" : " ";
padding = (is_installed && !has_colors) ? "" : " ";
if (cols == 1) {
formatted = sprintf(fmt_version, version);
} else if (cols == 2) {
@ -1931,13 +1948,17 @@ BEGIN {
formatted = sprintf((fmt_version padding fmt_latest_lts), version, fields[2]);
}
output[n] = formatted;
output[m++] = formatted;
}
for (n = 1; n <= rows; n++) {
for (n = 1; n < m; n++) {
print output[n]
}
if (rows > --m) {
printf("[INFO] showing %d (of %d) versions.\n", m, rows) > "/dev/stderr"
}
exit
}'
}
@ -2732,7 +2753,7 @@ nvm_npm_global_modules() {
local NPMLIST
local VERSION
VERSION="$1"
NPMLIST=$(nvm use "${VERSION}" >/dev/null && npm list -g --depth=0 2>/dev/null | command sed 1,1d | nvm_grep -v 'UNMET PEER DEPENDENCY')
NPMLIST=$(nvm use "${VERSION}" >/dev/null && npm list -g --depth=0 2>/dev/null | command sed -e '1d' -e '/UNMET PEER DEPENDENCY/d')
local INSTALLS
INSTALLS=$(nvm_echo "${NPMLIST}" | command sed -e '/ -> / d' -e '/\(empty\)/ d' -e 's/^.* \(.*@[^ ]*\).*/\1/' -e '/^npm@[^ ]*.*$/ d' | command xargs)
@ -3101,6 +3122,7 @@ nvm() {
nvm_echo ' nvm ls-remote [<version>] List remote versions available for install, matching a given <version> if provided'
nvm_echo ' --lts When listing, only show LTS (long-term support) versions'
nvm_echo ' --lts=<LTS name> When listing, only show versions for a specific LTS line'
nvm_echo ' --min=<version> When listing, only show versions greater than or equal to <version>, including minor/patch updates for installed versions'
nvm_echo ' --no-colors Suppress colored output'
nvm_echo ' nvm version <version> Resolve the given description to a single local version'
nvm_echo ' nvm version-remote <version> Resolve the given description to a single remote version'
@ -4119,6 +4141,10 @@ nvm() {
local NVM_LTS
local PATTERN
local NVM_NO_COLORS
local NVM_MIN_ENV
NVM_MIN_ENV="${NVM_MIN-}"
local NVM_MIN
NVM_MIN="${NVM_MIN_ENV-}"
while [ $# -gt 0 ]; do
case "${1-}" in
@ -4129,6 +4155,9 @@ nvm() {
--lts=*)
NVM_LTS="${1##--lts=}"
;;
--min=*)
NVM_MIN="${1##--min=}"
;;
--no-colors) NVM_NO_COLORS="${1}" ;;
--*)
nvm_err "Unsupported option \"${1}\"."

View File

@ -0,0 +1,146 @@
#!/bin/sh
# shellcheck disable=SC2317
die () { echo "$@" ; cleanup ; exit 1; }
cleanup() {
unset -f nvm_remote_versions nvm_ls nvm_ls_current
if [ -n "$TEMP_NVM_MIN" ]; then
export NVM_MIN="$TEMP_NVM_MIN"
unset TEMP_NVM_MIN
fi
}
\. ../../../nvm.sh
if [ -n "$NVM_MIN" ]; then
TEMP_NVM_MIN="$NVM_MIN"
unset NVM_MIN
fi
# mock currently installed versions
nvm_ls() {
echo "v16.20.2
v18.20.3
system"
}
# mock currently active version
nvm_ls_current() {
echo "v18.20.3"
}
nvm_remote_versions() {
echo "v16.0.0
v16.20.2 Gallium
v16.20.3 Gallium *
v17.0.0
v17.9.1
v18.0.0
v18.1.0
v18.20.2 Hydrogen
v18.20.3 Hydrogen *
v19.0.0
v19.9.0
v20.0.0
v20.8.1
v20.9.0 Iron *
v21.0.0
v21.1.0"
}
# nvm_print_versions should print all versions from nvm_remote_versions
OUTPUT="$(NVM_NO_COLORS='--no-colors' nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')"
EXPECTED_OUTPUT="v16.0.0
v16.20.2 * (LTS: Gallium)
v16.20.3 (Latest LTS: Gallium)
v17.0.0
v17.9.1
v18.0.0
v18.1.0
v18.20.2 (LTS: Hydrogen)
-> v18.20.3 * (Latest LTS: Hydrogen)
v19.0.0
v19.9.0
v20.0.0
v20.8.1
v20.9.0 (Latest LTS: Iron)
v21.0.0
v21.1.0"
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "(1) nvm_print_versions did not output all expected versions; got $OUTPUT"
# versions lower than 18 should be filtered out, but v16.20.2 should be kept since it's installed
OUTPUT="$(NVM_NO_COLORS='--no-colors' NVM_MIN=v18 nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')"
EXPECTED_OUTPUT="v16.20.2 * (LTS: Gallium)
v16.20.3 (Latest LTS: Gallium)
v18.0.0
v18.1.0
v18.20.2 (LTS: Hydrogen)
-> v18.20.3 * (Latest LTS: Hydrogen)
v19.0.0
v19.9.0
v20.0.0
v20.8.1
v20.9.0 (Latest LTS: Iron)
v21.0.0
v21.1.0"
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "(2) NVM_MIN=18 nvm_print_versions did not output all expected versions; got $OUTPUT"
# versions lower than 19 should be filtered out
OUTPUT="$(NVM_NO_COLORS='--no-colors' NVM_MIN=19 nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')"
EXPECTED_OUTPUT="v16.20.2 * (LTS: Gallium)
v16.20.3 (Latest LTS: Gallium)
-> v18.20.3 * (Latest LTS: Hydrogen)
v19.0.0
v19.9.0
v20.0.0
v20.8.1
v20.9.0 (Latest LTS: Iron)
v21.0.0
v21.1.0"
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "(3) NVM_MIN=19 nvm_print_versions did not output all expected versions; got $OUTPUT"
# versions lower than 20.1 should be filtered out, so v20.0.0 is out
OUTPUT="$(NVM_NO_COLORS='--no-colors' NVM_MIN=v20.1 nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')"
EXPECTED_OUTPUT="v16.20.2 * (LTS: Gallium)
v16.20.3 (Latest LTS: Gallium)
-> v18.20.3 * (Latest LTS: Hydrogen)
v20.8.1
v20.9.0 (Latest LTS: Iron)
v21.0.0
v21.1.0"
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "(4) NVM_MIN=20.1 nvm_print_versions did not output all expected versions; got $OUTPUT"
# assume v18.20.3 is NOT installed, so now it should be filtered out
nvm_ls() {
echo "v16.20.2
system"
}
nvm_ls_current() {
echo "v16.20.2"
}
OUTPUT="$(NVM_NO_COLORS='--no-colors' NVM_MIN=20.1 nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')"
EXPECTED_OUTPUT="-> v16.20.2 * (LTS: Gallium)
v16.20.3 (Latest LTS: Gallium)
v20.8.1
v20.9.0 (Latest LTS: Iron)
v21.0.0
v21.1.0"
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "(5) NVM_MIN=20.1 nvm_print_versions did not output all expected versions; got $OUTPUT"
cleanup