This commit is contained in:
ryenus 2024-07-07 21:12:07 +09:00 committed by GitHub
commit e7a73dc43c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 155 additions and 8 deletions

24
nvm.sh
View File

@ -1820,12 +1820,14 @@ nvm_print_versions() {
fi fi
command awk \ command awk \
-v remote_versions="$(printf '%s' "${1-}" | tr '\n' '|')" \ -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_versions="$(nvm_ls | tr '\n' '|')" -v current="$NVM_CURRENT" \
-v installed_color="$INSTALLED_COLOR" -v system_color="$SYSTEM_COLOR" \ -v installed_color="$INSTALLED_COLOR" -v system_color="$SYSTEM_COLOR" \
-v current_color="$CURRENT_COLOR" -v default_color="$DEFAULT_COLOR" \ -v current_color="$CURRENT_COLOR" -v default_color="$DEFAULT_COLOR" \
-v old_lts_color="$DEFAULT_COLOR" -v has_colors="$NVM_HAS_COLORS" ' -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 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 { BEGIN {
fmt_installed = has_colors ? (installed_color ? "\033[" installed_color "%15s\033[0m" : "%15s") : "%15s *"; 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 *"; fmt_system = has_colors ? (system_color ? "\033[" system_color "%15s\033[0m" : "%15s") : "%15s *";
@ -1840,13 +1842,12 @@ BEGIN {
split(remote_versions, lines, "|"); split(remote_versions, lines, "|");
split(installed_versions, installed, "|"); split(installed_versions, installed, "|");
rows = alen(lines); rows = alen(lines);
filter_on = (vcmp("v0.0.0", min_ver) != 0);
for (n = 1; n <= rows; n++) { for (m = n = 1; n <= rows; n++) {
split(lines[n], fields, "[[:blank:]]+"); split(lines[n], fields, "[[:blank:]]+");
cols = alen(fields); cols = alen(fields);
version = fields[1]; version = fields[1];
is_installed = 0; is_installed = 0;
for (i in installed) { for (i in installed) {
if (version == installed[i]) { if (version == installed[i]) {
is_installed = 1; is_installed = 1;
@ -1854,6 +1855,14 @@ BEGIN {
} }
} }
if (filter_on && !is_installed) {
if (vcmp(version, min_ver) >= 0) {
filter_on = 0;
} else {
continue;
}
}
fmt_version = "%15s"; fmt_version = "%15s";
if (version == current) { if (version == current) {
fmt_version = fmt_current; fmt_version = fmt_current;
@ -1863,8 +1872,7 @@ BEGIN {
fmt_version = fmt_installed; fmt_version = fmt_installed;
} }
padding = (!has_colors && is_installed) ? "" : " "; padding = (is_installed && !has_colors) ? "" : " ";
if (cols == 1) { if (cols == 1) {
formatted = sprintf(fmt_version, version); formatted = sprintf(fmt_version, version);
} else if (cols == 2) { } else if (cols == 2) {
@ -1873,10 +1881,10 @@ BEGIN {
formatted = sprintf((fmt_version padding fmt_latest_lts), version, fields[2]); 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] print output[n]
} }

View File

@ -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" ]; then
export NVM_MIN="$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 *
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)" | 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"
[ "_$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=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
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=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)
-> 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=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)
-> 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"
# 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=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=20.1 nvm_print_versions did not output all expected versions; got $OUTPUT"
cleanup