Skip to content

Commit 7404735

Browse files
ryenusljharb
andcommitted
ls-remote: show only versions newer than NVM_MIN
add CLI option --min=<version>, fallback to env var "NVM_MIN" if set. display a hint when version filtering is in effect Other minor changes: - fix: disable color in nvm_print_versions tests - trim leading space in mock output due to eslint errors - prefixed versions like v18 also work - remove function ref duplication - avoid inline initialization for ksh compatibility Co-authored-by: Jordan Harband <ljharb@gmail.com>
1 parent 001ea8c commit 7404735

2 files changed

Lines changed: 166 additions & 9 deletions

File tree

nvm.sh

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,12 +1935,14 @@ nvm_print_versions() {
19351935
fi
19361936

19371937
command awk \
1938-
-v remote_versions="$(printf '%s' "${1-}" | tr '\n' '|')" \
1938+
-v remote_versions="$(printf '%s' "${1-}" | tr '\n' '|')" -v min="${NVM_MIN:-v0}" \
19391939
-v installed_versions="$(nvm_ls | tr '\n' '|')" -v current="$NVM_CURRENT" \
19401940
-v installed_color="$INSTALLED_COLOR" -v system_color="$SYSTEM_COLOR" \
19411941
-v current_color="$CURRENT_COLOR" -v default_color="$DEFAULT_COLOR" \
19421942
-v old_lts_color="$DEFAULT_COLOR" -v has_colors="$NVM_HAS_COLORS" '
19431943
function alen(arr, i, len) { len=0; for(i in arr) len++; return len; }
1944+
function v2a(v, a) { sub(/^(iojs-)?v/, "", v); split(v, a, "."); }
1945+
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; }
19441946
BEGIN {
19451947
fmt_installed = has_colors ? (installed_color ? "\033[" installed_color "%15s\033[0m" : "%15s") : "%15s *";
19461948
fmt_system = has_colors ? (system_color ? "\033[" system_color "%15s\033[0m" : "%15s") : "%15s *";
@@ -1956,20 +1958,24 @@ BEGIN {
19561958
split(remote_versions, lines, "|");
19571959
split(installed_versions, installed, "|");
19581960
rows = alen(lines);
1959-
1960-
for (n = 1; n <= rows; n++) {
1961+
filter_on = (vcmp("v0.0.0", min) != 0);
1962+
for (m = n = 1; n <= rows; n++) {
19611963
split(lines[n], fields, "[[:blank:]]+");
19621964
cols = alen(fields);
19631965
version = fields[1];
19641966
is_installed = 0;
1965-
19661967
for (i in installed) {
19671968
if (version == installed[i]) {
19681969
is_installed = 1;
19691970
break;
19701971
}
19711972
}
19721973
1974+
if (filter_on && vcmp(version, min) < 0) {
1975+
continue;
1976+
}
1977+
1978+
filter_on = 0;
19731979
fmt_version = "%15s";
19741980
if (version == current) {
19751981
fmt_version = fmt_current;
@@ -1979,8 +1985,7 @@ BEGIN {
19791985
fmt_version = fmt_installed;
19801986
}
19811987
1982-
padding = (!has_colors && is_installed) ? "" : " ";
1983-
1988+
padding = (is_installed && !has_colors) ? "" : " ";
19841989
if (cols == 1) {
19851990
formatted = sprintf(fmt_version, version);
19861991
} else if (version == "system" && cols >= 2) {
@@ -1991,13 +1996,17 @@ BEGIN {
19911996
formatted = sprintf((fmt_version padding fmt_latest_lts), version, fields[2]);
19921997
}
19931998
1994-
output[n] = formatted;
1999+
output[m++] = formatted;
19952000
}
19962001
1997-
for (n = 1; n <= rows; n++) {
2002+
for (n = 1; n < m; n++) {
19982003
print output[n]
19992004
}
20002005
2006+
if (rows > --m) {
2007+
printf("[INFO] showing %d (of %d) versions.\n", m, rows) > "/dev/stderr"
2008+
}
2009+
20012010
exit
20022011
}'
20032012
}
@@ -3236,6 +3245,7 @@ nvm() {
32363245
nvm_echo ' nvm ls-remote [<version>] List remote versions available for install, matching a given <version> if provided'
32373246
nvm_echo ' --lts When listing, only show LTS (long-term support) versions'
32383247
nvm_echo ' --lts=<LTS name> When listing, only show versions for a specific LTS line'
3248+
nvm_echo ' --min=<version> When listing, only show versions greater than or equal to <version>, including minor/patch updates for installed versions'
32393249
nvm_echo ' --no-colors Suppress colored output'
32403250
nvm_echo ' nvm version <version> Resolve the given description to a single local version'
32413251
nvm_echo ' nvm version-remote <version> Resolve the given description to a single remote version'
@@ -4271,7 +4281,7 @@ nvm() {
42714281
local NVM_LS_EXIT_CODE
42724282
NVM_LS_OUTPUT=$(nvm_ls "${PATTERN-}")
42734283
NVM_LS_EXIT_CODE=$?
4274-
NVM_NO_COLORS="${NVM_NO_COLORS-}" nvm_print_versions "${NVM_LS_OUTPUT}"
4284+
NVM_NO_COLORS="${NVM_NO_COLORS-}" NVM_MIN='' nvm_print_versions "${NVM_LS_OUTPUT}"
42754285
if [ -z "${NVM_NO_ALIAS-}" ] && [ -z "${PATTERN-}" ]; then
42764286
if [ -n "${NVM_NO_COLORS-}" ]; then
42774287
nvm alias --no-colors
@@ -4285,6 +4295,10 @@ nvm() {
42854295
local NVM_LTS
42864296
local PATTERN
42874297
local NVM_NO_COLORS
4298+
local NVM_MIN_ENV
4299+
NVM_MIN_ENV="${NVM_MIN-}"
4300+
local NVM_MIN
4301+
NVM_MIN="${NVM_MIN_ENV-}"
42884302

42894303
while [ $# -gt 0 ]; do
42904304
case "${1-}" in
@@ -4295,6 +4309,9 @@ nvm() {
42954309
--lts=*)
42964310
NVM_LTS="${1##--lts=}"
42974311
;;
4312+
--min=*)
4313+
NVM_MIN="${1##--min=}"
4314+
;;
42984315
--no-colors) NVM_NO_COLORS="${1}" ;;
42994316
--*)
43004317
nvm_err "Unsupported option \"${1}\"."
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
#!/bin/sh
2+
3+
# shellcheck disable=SC2317
4+
5+
die () { echo "$@" ; cleanup ; exit 1; }
6+
7+
cleanup() {
8+
unset -f nvm_remote_versions nvm_ls nvm_ls_current
9+
if [ -n "$TEMP_NVM_MIN" ]; then
10+
export NVM_MIN="$TEMP_NVM_MIN"
11+
unset TEMP_NVM_MIN
12+
fi
13+
}
14+
15+
\. ../../../nvm.sh
16+
17+
18+
if [ -n "$NVM_MIN" ]; then
19+
TEMP_NVM_MIN="$NVM_MIN"
20+
unset NVM_MIN
21+
fi
22+
23+
# mock currently installed versions
24+
nvm_ls() {
25+
echo "v16.20.2
26+
v18.20.3
27+
system"
28+
}
29+
30+
# mock currently active version
31+
nvm_ls_current() {
32+
echo "v18.20.3"
33+
}
34+
35+
nvm_remote_versions() {
36+
echo "v16.0.0
37+
v16.20.2 Gallium *
38+
v17.0.0
39+
v17.9.1
40+
v18.0.0
41+
v18.1.0
42+
v18.20.2 Hydrogen
43+
v18.20.3 Hydrogen *
44+
v19.0.0
45+
v19.9.0
46+
v20.0.0
47+
v20.8.1
48+
v20.9.0 Iron *
49+
v21.0.0
50+
v21.1.0"
51+
}
52+
53+
54+
# nvm_print_versions should print all versions from nvm_remote_versions
55+
OUTPUT="$(NVM_NO_COLORS='--no-colors' nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')"
56+
EXPECTED_OUTPUT="v16.0.0
57+
v16.20.2 * (Latest LTS: Gallium)
58+
v17.0.0
59+
v17.9.1
60+
v18.0.0
61+
v18.1.0
62+
v18.20.2 (LTS: Hydrogen)
63+
-> v18.20.3 * (Latest LTS: Hydrogen)
64+
v19.0.0
65+
v19.9.0
66+
v20.0.0
67+
v20.8.1
68+
v20.9.0 (Latest LTS: Iron)
69+
v21.0.0
70+
v21.1.0"
71+
72+
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "nvm_print_versions did not output all expected versions; got $OUTPUT"
73+
74+
75+
# versions lower than 18 should be filtered out, but v16.20.2 should be kept since it's installed
76+
OUTPUT="$(NVM_NO_COLORS='--no-colors' NVM_MIN=v18 nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')"
77+
EXPECTED_OUTPUT="v16.20.2 * (Latest LTS: Gallium)
78+
v18.0.0
79+
v18.1.0
80+
v18.20.2 (LTS: Hydrogen)
81+
-> v18.20.3 * (Latest LTS: Hydrogen)
82+
v19.0.0
83+
v19.9.0
84+
v20.0.0
85+
v20.8.1
86+
v20.9.0 (Latest LTS: Iron)
87+
v21.0.0
88+
v21.1.0"
89+
90+
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "NVM_MIN=18 nvm_print_versions did not output all expected versions; got $OUTPUT"
91+
92+
93+
# versions lower than 19 should be filtered out
94+
OUTPUT="$(NVM_NO_COLORS='--no-colors' NVM_MIN=19 nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')"
95+
EXPECTED_OUTPUT="v16.20.2 * (Latest LTS: Gallium)
96+
-> v18.20.3 * (Latest LTS: Hydrogen)
97+
v19.0.0
98+
v19.9.0
99+
v20.0.0
100+
v20.8.1
101+
v20.9.0 (Latest LTS: Iron)
102+
v21.0.0
103+
v21.1.0"
104+
105+
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "NVM_MIN=19 nvm_print_versions did not output all expected versions; got $OUTPUT"
106+
107+
108+
# versions lower than 20.1 should be filtered out, so v20.0.0 is out
109+
OUTPUT="$(NVM_NO_COLORS='--no-colors' NVM_MIN=v20.1 nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')"
110+
EXPECTED_OUTPUT="v16.20.2 * (Latest LTS: Gallium)
111+
-> v18.20.3 * (Latest LTS: Hydrogen)
112+
v20.8.1
113+
v20.9.0 (Latest LTS: Iron)
114+
v21.0.0
115+
v21.1.0"
116+
117+
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "NVM_MIN=20.1 nvm_print_versions did not output all expected versions; got $OUTPUT"
118+
119+
120+
# assume v18.20.3 is NOT installed, so now it should be filtered out
121+
nvm_ls() {
122+
echo "v16.20.2
123+
system"
124+
}
125+
126+
nvm_ls_current() {
127+
echo "v16.20.2"
128+
}
129+
130+
OUTPUT="$(NVM_NO_COLORS='--no-colors' NVM_MIN=20.1 nvm_print_versions "$(nvm_remote_versions)" | sed -r 's/^[ \t]+//')"
131+
EXPECTED_OUTPUT="-> v16.20.2 * (Latest LTS: Gallium)
132+
v20.8.1
133+
v20.9.0 (Latest LTS: Iron)
134+
v21.0.0
135+
v21.1.0"
136+
137+
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "NVM_MIN=20.1 nvm_print_versions did not output all expected versions; got $OUTPUT"
138+
139+
140+
cleanup

0 commit comments

Comments
 (0)