From 6ebe56fb115604210e4b6ddad3813e6a7a4a5e2a Mon Sep 17 00:00:00 2001 From: ryenus Date: Mon, 29 Jan 2024 21:04:48 +0800 Subject: [PATCH 01/15] show only versions newer than NVM_MIN_VER if set --- nvm.sh | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/nvm.sh b/nvm.sh index ed0e0b3..ec2f31d 100755 --- a/nvm.sh +++ b/nvm.sh @@ -1857,12 +1857,14 @@ nvm_print_versions() { fi command awk \ - -v remote_versions="$(printf '%s' "${1-}" | tr '\n' '|')" \ + -v remote_versions="$(printf '%s' "${1-}" | tr '\n' '|')" -v min_ver="${NVM_MIN_VER:-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 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 *"; @@ -1877,13 +1879,15 @@ BEGIN { split(remote_versions, lines, "|"); split(installed_versions, installed, "|"); rows = alen(lines); - - for (n = 1; n <= rows; n++) { + filter = (min_ver != "v0"); + for (m = n = 1; n <= rows; n++) { split(lines[n], fields, "[[:blank:]]+"); cols = alen(fields); version = fields[1]; - is_installed = 0; + if (filter && comp(version, min_ver) < 0) continue; + filter = 0; + is_installed = 0; for (i in installed) { if (version == installed[i]) { is_installed = 1; @@ -1900,8 +1904,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) { @@ -1910,11 +1913,11 @@ BEGIN { formatted = sprintf((fmt_version padding fmt_latest_lts), version, fields[2]); } - output[n] = formatted; + output[m++] = formatted; } - for (n = 1; n <= rows; n++) { - print output[n] + for (m in output) { + print output[m] } exit From 307ad6d118e53f602c11168ae849c2615bf79a3a Mon Sep 17 00:00:00 2001 From: ryenus Date: Sat, 29 Jun 2024 10:18:07 +0800 Subject: [PATCH 02/15] always list installed versions --- nvm.sh | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/nvm.sh b/nvm.sh index ec2f31d..371ff79 100755 --- a/nvm.sh +++ b/nvm.sh @@ -1879,14 +1879,11 @@ BEGIN { split(remote_versions, lines, "|"); split(installed_versions, installed, "|"); rows = alen(lines); - filter = (min_ver != "v0"); + filter_on = (vcmp("v0.0.0", min_ver) != 0); for (m = n = 1; n <= rows; n++) { split(lines[n], fields, "[[:blank:]]+"); cols = alen(fields); version = fields[1]; - if (filter && comp(version, min_ver) < 0) continue; - - filter = 0; is_installed = 0; for (i in installed) { if (version == installed[i]) { @@ -1895,6 +1892,14 @@ BEGIN { } } + if (filter_on && !is_installed) { + if (vcmp(version, min_ver) >= 0) { + filter_on = 0; + } else { + continue; + } + } + fmt_version = "%15s"; if (version == current) { fmt_version = fmt_current; @@ -1916,8 +1921,8 @@ BEGIN { output[m++] = formatted; } - for (m in output) { - print output[m] + for (n = 1; n < m; n++) { + print output[n] } exit From 02bb9338e1b16d7c54606b5b8870cc6dc482af5f Mon Sep 17 00:00:00 2001 From: ryenus Date: Sat, 29 Jun 2024 13:47:51 +0800 Subject: [PATCH 03/15] add test for NVM_MIN_VER support show only versions newer than NVM_MIN_VER if set --- test/fast/Unit tests/nvm_print_versions | 139 ++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100755 test/fast/Unit tests/nvm_print_versions diff --git a/test/fast/Unit tests/nvm_print_versions b/test/fast/Unit tests/nvm_print_versions new file mode 100755 index 0000000..124c930 --- /dev/null +++ b/test/fast/Unit tests/nvm_print_versions @@ -0,0 +1,139 @@ +#!/bin/sh + +# shellcheck disable=SC2317 + +die () { echo "$@" ; cleanup ; exit 1; } + +cleanup() { + unset -f nvm_remote_versions nvm_ls nvm_ls_current nvm_remote_versions + if [ -n "$TEMP_NVM_MIN_VER" ]; then + export NVM_MIN_VER="$TEMP_NVM_MIN_VER" + fi +} + +\. ../../../nvm.sh + + +if [ -n "$NVM_MIN_VER" ]; then + TEMP_NVM_MIN_VER="$NVM_MIN_VER" + unset NVM_MIN_VER +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 * +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=1 nvm_print_versions "$(nvm_remote_versions)")" +EXPECTED_OUTPUT=" v16.0.0 + v16.20.2 * (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 "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=1 NVM_MIN_VER=18 nvm_print_versions "$(nvm_remote_versions)")" +EXPECTED_OUTPUT=" v16.20.2 * (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 "NVM_MIN_VER=18 nvm_print_versions did not output all expected versions; got $OUTPUT" + + +# versions lower than 19 should be filtered out +OUTPUT="$(NVM_NO_COLORS=1 NVM_MIN_VER=19 nvm_print_versions "$(nvm_remote_versions)")" +EXPECTED_OUTPUT=" v16.20.2 * (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 "NVM_MIN_VER=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=1 NVM_MIN_VER=20.1 nvm_print_versions "$(nvm_remote_versions)")" +EXPECTED_OUTPUT=" v16.20.2 * (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 "NVM_MIN_VER=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=1 NVM_MIN_VER=20.1 nvm_print_versions "$(nvm_remote_versions)")" +EXPECTED_OUTPUT="-> v16.20.2 * (Latest LTS: Gallium) + v20.8.1 + v20.9.0 (Latest LTS: Iron) + v21.0.0 + v21.1.0" + +[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "NVM_MIN_VER=20.1 nvm_print_versions did not output all expected versions; got $OUTPUT" + + +cleanup From 8ec7a3b5fc2a36982c0442f12d598464e522496a Mon Sep 17 00:00:00 2001 From: ryenus Date: Sat, 29 Jun 2024 13:17:25 +0800 Subject: [PATCH 04/15] trim leading space in mock output due to eclint errors --- test/fast/Unit tests/nvm_print_versions | 94 ++++++++++++------------- 1 file changed, 47 insertions(+), 47 deletions(-) diff --git a/test/fast/Unit tests/nvm_print_versions b/test/fast/Unit tests/nvm_print_versions index 124c930..dc12d55 100755 --- a/test/fast/Unit tests/nvm_print_versions +++ b/test/fast/Unit tests/nvm_print_versions @@ -51,67 +51,67 @@ v21.1.0" # nvm_print_versions should print all versions from nvm_remote_versions -OUTPUT="$(NVM_NO_COLORS=1 nvm_print_versions "$(nvm_remote_versions)")" -EXPECTED_OUTPUT=" v16.0.0 - v16.20.2 * (Latest LTS: Gallium) - v17.0.0 - v17.9.1 - v18.0.0 - v18.1.0 - v18.20.2 (LTS: Hydrogen) +OUTPUT="$(NVM_NO_COLORS=1 nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')" +EXPECTED_OUTPUT="v16.0.0 +v16.20.2 * (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" +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 "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=1 NVM_MIN_VER=18 nvm_print_versions "$(nvm_remote_versions)")" -EXPECTED_OUTPUT=" v16.20.2 * (Latest LTS: Gallium) - v18.0.0 - v18.1.0 - v18.20.2 (LTS: Hydrogen) +OUTPUT="$(NVM_NO_COLORS=1 NVM_MIN_VER=18 nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')" +EXPECTED_OUTPUT="v16.20.2 * (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" +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 "NVM_MIN_VER=18 nvm_print_versions did not output all expected versions; got $OUTPUT" # versions lower than 19 should be filtered out -OUTPUT="$(NVM_NO_COLORS=1 NVM_MIN_VER=19 nvm_print_versions "$(nvm_remote_versions)")" -EXPECTED_OUTPUT=" v16.20.2 * (Latest LTS: Gallium) +OUTPUT="$(NVM_NO_COLORS=1 NVM_MIN_VER=19 nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')" +EXPECTED_OUTPUT="v16.20.2 * (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" +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 "NVM_MIN_VER=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=1 NVM_MIN_VER=20.1 nvm_print_versions "$(nvm_remote_versions)")" -EXPECTED_OUTPUT=" v16.20.2 * (Latest LTS: Gallium) +OUTPUT="$(NVM_NO_COLORS=1 NVM_MIN_VER=20.1 nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')" +EXPECTED_OUTPUT="v16.20.2 * (Latest LTS: Gallium) -> v18.20.3 * (Latest LTS: Hydrogen) - v20.8.1 - v20.9.0 (Latest LTS: Iron) - v21.0.0 - v21.1.0" +v20.8.1 +v20.9.0 (Latest LTS: Iron) +v21.0.0 +v21.1.0" [ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "NVM_MIN_VER=20.1 nvm_print_versions did not output all expected versions; got $OUTPUT" @@ -126,12 +126,12 @@ nvm_ls_current() { echo "v16.20.2" } -OUTPUT="$(NVM_NO_COLORS=1 NVM_MIN_VER=20.1 nvm_print_versions "$(nvm_remote_versions)")" +OUTPUT="$(NVM_NO_COLORS=1 NVM_MIN_VER=20.1 nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')" EXPECTED_OUTPUT="-> v16.20.2 * (Latest LTS: Gallium) - v20.8.1 - v20.9.0 (Latest LTS: Iron) - v21.0.0 - v21.1.0" +v20.8.1 +v20.9.0 (Latest LTS: Iron) +v21.0.0 +v21.1.0" [ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "NVM_MIN_VER=20.1 nvm_print_versions did not output all expected versions; got $OUTPUT" From 2a893e5b5d8557d6f16fae82ca307de1c7b55696 Mon Sep 17 00:00:00 2001 From: ryenus Date: Sun, 30 Jun 2024 01:50:15 +0800 Subject: [PATCH 05/15] rename NVM_MIN_VER to NVM_MIN since nvm is all about versions, so no need for the explicit suffix. --- nvm.sh | 2 +- test/fast/Unit tests/nvm_print_versions | 26 ++++++++++++------------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/nvm.sh b/nvm.sh index 371ff79..6a390fb 100755 --- a/nvm.sh +++ b/nvm.sh @@ -1857,7 +1857,7 @@ nvm_print_versions() { fi command awk \ - -v remote_versions="$(printf '%s' "${1-}" | tr '\n' '|')" -v min_ver="${NVM_MIN_VER:-v0}" \ + -v remote_versions="$(printf '%s' "${1-}" | tr '\n' '|')" -v min_ver="${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" \ diff --git a/test/fast/Unit tests/nvm_print_versions b/test/fast/Unit tests/nvm_print_versions index dc12d55..39113ec 100755 --- a/test/fast/Unit tests/nvm_print_versions +++ b/test/fast/Unit tests/nvm_print_versions @@ -6,17 +6,17 @@ die () { echo "$@" ; cleanup ; exit 1; } cleanup() { unset -f nvm_remote_versions nvm_ls nvm_ls_current nvm_remote_versions - if [ -n "$TEMP_NVM_MIN_VER" ]; then - export NVM_MIN_VER="$TEMP_NVM_MIN_VER" + if [ -n "$TEMP_NVM_MIN" ]; then + export NVM_MIN="$TEMP_NVM_MIN" fi } \. ../../../nvm.sh -if [ -n "$NVM_MIN_VER" ]; then - TEMP_NVM_MIN_VER="$NVM_MIN_VER" - unset NVM_MIN_VER +if [ -n "$NVM_MIN" ]; then + TEMP_NVM_MIN="$NVM_MIN" + unset NVM_MIN fi # mock currently installed versions @@ -72,7 +72,7 @@ v21.1.0" # versions lower than 18 should be filtered out, but v16.20.2 should be kept since it's installed -OUTPUT="$(NVM_NO_COLORS=1 NVM_MIN_VER=18 nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')" +OUTPUT="$(NVM_NO_COLORS=1 NVM_MIN=18 nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')" EXPECTED_OUTPUT="v16.20.2 * (Latest LTS: Gallium) v18.0.0 v18.1.0 @@ -86,11 +86,11 @@ v20.9.0 (Latest LTS: Iron) v21.0.0 v21.1.0" -[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "NVM_MIN_VER=18 nvm_print_versions did not output all expected versions; got $OUTPUT" +[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "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=1 NVM_MIN_VER=19 nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')" +OUTPUT="$(NVM_NO_COLORS=1 NVM_MIN=19 nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')" EXPECTED_OUTPUT="v16.20.2 * (Latest LTS: Gallium) -> v18.20.3 * (Latest LTS: Hydrogen) v19.0.0 @@ -101,11 +101,11 @@ v20.9.0 (Latest LTS: Iron) v21.0.0 v21.1.0" -[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "NVM_MIN_VER=19 nvm_print_versions did not output all expected versions; got $OUTPUT" +[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "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=1 NVM_MIN_VER=20.1 nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')" +OUTPUT="$(NVM_NO_COLORS=1 NVM_MIN=20.1 nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')" EXPECTED_OUTPUT="v16.20.2 * (Latest LTS: Gallium) -> v18.20.3 * (Latest LTS: Hydrogen) v20.8.1 @@ -113,7 +113,7 @@ v20.9.0 (Latest LTS: Iron) v21.0.0 v21.1.0" -[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "NVM_MIN_VER=20.1 nvm_print_versions did not output all expected versions; got $OUTPUT" +[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "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 @@ -126,14 +126,14 @@ nvm_ls_current() { echo "v16.20.2" } -OUTPUT="$(NVM_NO_COLORS=1 NVM_MIN_VER=20.1 nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')" +OUTPUT="$(NVM_NO_COLORS=1 NVM_MIN=20.1 nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')" EXPECTED_OUTPUT="-> v16.20.2 * (Latest LTS: Gallium) v20.8.1 v20.9.0 (Latest LTS: Iron) v21.0.0 v21.1.0" -[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "NVM_MIN_VER=20.1 nvm_print_versions did not output all expected versions; got $OUTPUT" +[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "NVM_MIN=20.1 nvm_print_versions did not output all expected versions; got $OUTPUT" cleanup From 5fc1ebc91e945f512f995619be45a5325819c760 Mon Sep 17 00:00:00 2001 From: ryenus Date: Sun, 30 Jun 2024 10:46:11 +0800 Subject: [PATCH 06/15] prefixed versions like v18 also work --- test/fast/Unit tests/nvm_print_versions | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/fast/Unit tests/nvm_print_versions b/test/fast/Unit tests/nvm_print_versions index 39113ec..c042068 100755 --- a/test/fast/Unit tests/nvm_print_versions +++ b/test/fast/Unit tests/nvm_print_versions @@ -72,7 +72,7 @@ v21.1.0" # versions lower than 18 should be filtered out, but v16.20.2 should be kept since it's installed -OUTPUT="$(NVM_NO_COLORS=1 NVM_MIN=18 nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')" +OUTPUT="$(NVM_NO_COLORS=1 NVM_MIN=v18 nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')" EXPECTED_OUTPUT="v16.20.2 * (Latest LTS: Gallium) v18.0.0 v18.1.0 @@ -105,7 +105,7 @@ v21.1.0" # versions lower than 20.1 should be filtered out, so v20.0.0 is out -OUTPUT="$(NVM_NO_COLORS=1 NVM_MIN=20.1 nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')" +OUTPUT="$(NVM_NO_COLORS=1 NVM_MIN=v20.1 nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')" EXPECTED_OUTPUT="v16.20.2 * (Latest LTS: Gallium) -> v18.20.3 * (Latest LTS: Hydrogen) v20.8.1 From e7df2f2110acb28dec2418d4f1c13ea62d68dfef Mon Sep 17 00:00:00 2001 From: ryenus Date: Sun, 14 Jul 2024 11:45:52 +0800 Subject: [PATCH 07/15] remove function ref duplication --- test/fast/Unit tests/nvm_print_versions | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/fast/Unit tests/nvm_print_versions b/test/fast/Unit tests/nvm_print_versions index c042068..b9d22b8 100755 --- a/test/fast/Unit tests/nvm_print_versions +++ b/test/fast/Unit tests/nvm_print_versions @@ -5,7 +5,7 @@ die () { echo "$@" ; cleanup ; exit 1; } cleanup() { - unset -f nvm_remote_versions nvm_ls nvm_ls_current nvm_remote_versions + unset -f nvm_remote_versions nvm_ls nvm_ls_current if [ -n "$TEMP_NVM_MIN" ]; then export NVM_MIN="$TEMP_NVM_MIN" fi From e215a57abd6cb0eb48a15269a7449c8e7268ea8c Mon Sep 17 00:00:00 2001 From: ryenus Date: Sun, 14 Jul 2024 14:18:11 +0800 Subject: [PATCH 08/15] always show updates for installed versions even if they're older than $NVM_MIN --- nvm.sh | 10 ++++++--- test/fast/Unit tests/nvm_print_versions | 28 +++++++++++++++---------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/nvm.sh b/nvm.sh index 6a390fb..c5da6d1 100755 --- a/nvm.sh +++ b/nvm.sh @@ -1864,6 +1864,7 @@ nvm_print_versions() { -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 *"; @@ -1880,6 +1881,7 @@ BEGIN { split(installed_versions, installed, "|"); rows = alen(lines); filter_on = (vcmp("v0.0.0", min_ver) != 0); + current_major = -1; for (m = n = 1; n <= rows; n++) { split(lines[n], fields, "[[:blank:]]+"); cols = alen(fields); @@ -1892,10 +1894,12 @@ BEGIN { } } - if (filter_on && !is_installed) { - if (vcmp(version, min_ver) >= 0) { + if (filter_on != 0) { + if (is_installed) { + current_major = v2m(version); + } else if (vcmp(version, min_ver) >= 0) { filter_on = 0; - } else { + } else if (v2m(version) != current_major) { continue; } } diff --git a/test/fast/Unit tests/nvm_print_versions b/test/fast/Unit tests/nvm_print_versions index b9d22b8..3e21498 100755 --- a/test/fast/Unit tests/nvm_print_versions +++ b/test/fast/Unit tests/nvm_print_versions @@ -33,7 +33,8 @@ nvm_ls_current() { nvm_remote_versions() { echo "v16.0.0 -v16.20.2 Gallium * +v16.20.2 Gallium +v16.20.3 Gallium * v17.0.0 v17.9.1 v18.0.0 @@ -53,7 +54,8 @@ v21.1.0" # nvm_print_versions should print all versions from nvm_remote_versions OUTPUT="$(NVM_NO_COLORS=1 nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')" EXPECTED_OUTPUT="v16.0.0 -v16.20.2 * (Latest LTS: Gallium) +v16.20.2 * (LTS: Gallium) +v16.20.3 (Latest LTS: Gallium) v17.0.0 v17.9.1 v18.0.0 @@ -68,12 +70,13 @@ v20.9.0 (Latest LTS: Iron) v21.0.0 v21.1.0" -[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "nvm_print_versions did not output all expected versions; got $OUTPUT" +[ "_$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=1 NVM_MIN=v18 nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')" -EXPECTED_OUTPUT="v16.20.2 * (Latest LTS: Gallium) +EXPECTED_OUTPUT="v16.20.2 * (LTS: Gallium) +v16.20.3 (Latest LTS: Gallium) v18.0.0 v18.1.0 v18.20.2 (LTS: Hydrogen) @@ -86,12 +89,13 @@ v20.9.0 (Latest LTS: Iron) v21.0.0 v21.1.0" -[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "NVM_MIN=18 nvm_print_versions did not output all expected versions; got $OUTPUT" +[ "_$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=1 NVM_MIN=19 nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')" -EXPECTED_OUTPUT="v16.20.2 * (Latest LTS: Gallium) +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 @@ -101,19 +105,20 @@ v20.9.0 (Latest LTS: Iron) v21.0.0 v21.1.0" -[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "NVM_MIN=19 nvm_print_versions did not output all expected versions; got $OUTPUT" +[ "_$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=1 NVM_MIN=v20.1 nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')" -EXPECTED_OUTPUT="v16.20.2 * (Latest LTS: Gallium) +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 "NVM_MIN=20.1 nvm_print_versions did not output all expected versions; got $OUTPUT" +[ "_$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 @@ -127,13 +132,14 @@ nvm_ls_current() { } OUTPUT="$(NVM_NO_COLORS=1 NVM_MIN=20.1 nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')" -EXPECTED_OUTPUT="-> v16.20.2 * (Latest LTS: Gallium) +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 "NVM_MIN=20.1 nvm_print_versions did not output all expected versions; got $OUTPUT" +[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "(5) NVM_MIN=20.1 nvm_print_versions did not output all expected versions; got $OUTPUT" cleanup From b9304f18dcfaf8f4453325a544699fb43d7b42a0 Mon Sep 17 00:00:00 2001 From: ryenus Date: Sun, 14 Jul 2024 18:43:55 +0800 Subject: [PATCH 09/15] ls-remote: add CLI option --min= When omitted, fallback to the environment variable "NVM_MIN" if set. And the CLI option --min= takes precedence over the environment variable "NVM_MIN" if both are present. --- nvm.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/nvm.sh b/nvm.sh index c5da6d1..69af61e 100755 --- a/nvm.sh +++ b/nvm.sh @@ -3092,6 +3092,7 @@ nvm() { nvm_echo ' nvm ls-remote [] List remote versions available for install, matching a given if provided' nvm_echo ' --lts When listing, only show LTS (long-term support) versions' nvm_echo ' --lts= When listing, only show versions for a specific LTS line' + nvm_echo ' --min= When listing, only show versions greater than or equal to , including minor/patch updates for installed versions' nvm_echo ' --no-colors Suppress colored output' nvm_echo ' nvm version Resolve the given description to a single local version' nvm_echo ' nvm version-remote Resolve the given description to a single remote version' @@ -4108,6 +4109,7 @@ nvm() { ;; "ls-remote" | "list-remote") local NVM_LTS + local NVM_MIN="${NVM_MIN-}" local PATTERN local NVM_NO_COLORS @@ -4120,6 +4122,9 @@ nvm() { --lts=*) NVM_LTS="${1##--lts=}" ;; + --min=*) + NVM_MIN="${1##--min=}" + ;; --no-colors) NVM_NO_COLORS="${1}" ;; --*) nvm_err "Unsupported option \"${1}\"." From e493440b3c20509c9fdf06b66e4a684de2896cb8 Mon Sep 17 00:00:00 2001 From: ryenus Date: Mon, 15 Jul 2024 14:45:57 +0800 Subject: [PATCH 10/15] avoid inline initialization for ksh compatibility Co-authored-by: Jordan Harband --- nvm.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nvm.sh b/nvm.sh index 69af61e..cb9af85 100755 --- a/nvm.sh +++ b/nvm.sh @@ -4109,7 +4109,8 @@ nvm() { ;; "ls-remote" | "list-remote") local NVM_LTS - local NVM_MIN="${NVM_MIN-}" + local NVM_MIN + NVM_MIN="${NVM_MIN-}" local PATTERN local NVM_NO_COLORS From 4613313498539fa118107aff898e4a8ddb1211d5 Mon Sep 17 00:00:00 2001 From: ryenus Date: Mon, 15 Jul 2024 14:55:33 +0800 Subject: [PATCH 11/15] 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 --- nvm.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/nvm.sh b/nvm.sh index cb9af85..cbf8e31 100755 --- a/nvm.sh +++ b/nvm.sh @@ -4109,10 +4109,12 @@ nvm() { ;; "ls-remote" | "list-remote") local NVM_LTS - local NVM_MIN - NVM_MIN="${NVM_MIN-}" 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 From 90de98bbfc3d670d931d48c4087d055dbaa8eb5f Mon Sep 17 00:00:00 2001 From: ryenus Date: Mon, 15 Jul 2024 15:19:15 +0800 Subject: [PATCH 12/15] [test] avoid leaking temp variable --- test/fast/Unit tests/nvm_print_versions | 1 + 1 file changed, 1 insertion(+) diff --git a/test/fast/Unit tests/nvm_print_versions b/test/fast/Unit tests/nvm_print_versions index 3e21498..39c7137 100755 --- a/test/fast/Unit tests/nvm_print_versions +++ b/test/fast/Unit tests/nvm_print_versions @@ -8,6 +8,7 @@ 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 } From c13e15570991ba09cb10bc3483c1de47bfc25d73 Mon Sep 17 00:00:00 2001 From: ryenus Date: Tue, 30 Jul 2024 08:07:04 +0800 Subject: [PATCH 13/15] rename min_ver to min for consistency --- nvm.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/nvm.sh b/nvm.sh index cbf8e31..44df30f 100755 --- a/nvm.sh +++ b/nvm.sh @@ -1857,7 +1857,7 @@ nvm_print_versions() { fi command awk \ - -v remote_versions="$(printf '%s' "${1-}" | tr '\n' '|')" -v min_ver="${NVM_MIN:-v0}" \ + -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" \ @@ -1880,7 +1880,7 @@ BEGIN { split(remote_versions, lines, "|"); split(installed_versions, installed, "|"); rows = alen(lines); - filter_on = (vcmp("v0.0.0", min_ver) != 0); + filter_on = (vcmp("v0.0.0", min) != 0); current_major = -1; for (m = n = 1; n <= rows; n++) { split(lines[n], fields, "[[:blank:]]+"); @@ -1897,7 +1897,7 @@ BEGIN { if (filter_on != 0) { if (is_installed) { current_major = v2m(version); - } else if (vcmp(version, min_ver) >= 0) { + } else if (vcmp(version, min) >= 0) { filter_on = 0; } else if (v2m(version) != current_major) { continue; From cd30a37384f4fc12872241d423d4d1face4adefe Mon Sep 17 00:00:00 2001 From: ryenus Date: Tue, 30 Jul 2024 08:07:29 +0800 Subject: [PATCH 14/15] set NVM_NO_COLORS to --no-colors to disable color --- test/fast/Unit tests/nvm_print_versions | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/fast/Unit tests/nvm_print_versions b/test/fast/Unit tests/nvm_print_versions index 39c7137..f1fabee 100755 --- a/test/fast/Unit tests/nvm_print_versions +++ b/test/fast/Unit tests/nvm_print_versions @@ -53,7 +53,7 @@ v21.1.0" # nvm_print_versions should print all versions from nvm_remote_versions -OUTPUT="$(NVM_NO_COLORS=1 nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')" +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) @@ -75,7 +75,7 @@ v21.1.0" # versions lower than 18 should be filtered out, but v16.20.2 should be kept since it's installed -OUTPUT="$(NVM_NO_COLORS=1 NVM_MIN=v18 nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')" +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 @@ -94,7 +94,7 @@ v21.1.0" # versions lower than 19 should be filtered out -OUTPUT="$(NVM_NO_COLORS=1 NVM_MIN=19 nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')" +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) @@ -110,7 +110,7 @@ v21.1.0" # versions lower than 20.1 should be filtered out, so v20.0.0 is out -OUTPUT="$(NVM_NO_COLORS=1 NVM_MIN=v20.1 nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')" +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) @@ -132,7 +132,7 @@ nvm_ls_current() { echo "v16.20.2" } -OUTPUT="$(NVM_NO_COLORS=1 NVM_MIN=20.1 nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')" +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 From d655614c69569444e2f280b9f25e20f29ae24223 Mon Sep 17 00:00:00 2001 From: ryenus Date: Mon, 21 Oct 2024 09:35:50 +0800 Subject: [PATCH 15/15] show a warning about skipped versions if any 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. --- nvm.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/nvm.sh b/nvm.sh index 44df30f..1f3615a 100755 --- a/nvm.sh +++ b/nvm.sh @@ -1925,6 +1925,11 @@ BEGIN { output[m++] = formatted; } + skipped = rows - m + 1 + if (skipped > 0) { + printf("[WARN] %d version(s) skipped.\n", skipped) > "/dev/stderr" + } + for (n = 1; n < m; n++) { print output[n] }