Skip to content
Myr

[New] `nvm ls-remote`: annotate versions with the `node` alias and local aliases

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fixes #2786. Ben Younes added the alias annotations to bare `nvm ls-remote`, with their regression tests; Jordan Harband moved `(Latest: node)` onto the newest node release, gave each kind of annotation a fixed column so the output can be sliced with `cut`, matched aliases to versions in one pass, and kept local aliases out of the mocks and tests.

authored by Ben Younes · committed by Jordan Harband · parent 04fbd95 · Browse files · 6 files+326 added−14 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Ben Younes <2910651+ousamabenyounes@users.noreply.github.com>
  • Co-authored-by: Jordan Harband <ljharb@gmail.com>

Diff stats

6 files+326 added−14 removed

  • tests:+131 added−2 removed
  • other:+195 added−12 removed

6 files+326 added−14 removed

modified:test/fast/Unit tests/mocks/nvm ls-remote.txt+1 added-1 removed
795 unmodified lines
796 v22.9.0797 v22.10.0798 v22.11.0  (Latest LTS: Jod)799 v23.0.0800 v23.1.0800 v23.1.0  (Latest: node)
modified:test/fast/Unit tests/nvm ls-remote+4 added-1 removed
1 unmodified line
23die () { echo "$@" ; cleanup ; exit 1; }45cleanup() {6 unset -f nvm_download nvm_ls_remote nvm_ls_remote_iojs6 unset -f nvm_download nvm_ls_remote nvm_ls_remote_iojs nvm_get_remote_aliases nvm_has_italics7 if [ -n "${TEMP_NVM_COLORS-}" ]; then8 export NVM_COLORS=TEMP_NVM_COLORS9 fi10 unset TEMP_NVM_COLORS
75 unmodified lines
86REMOTE_IOJS="${PWD}/mocks/nvm_ls_remote_iojs.txt"87nvm_ls_remote_iojs() {88 cat "${REMOTE_IOJS}"89}90# the mock must not depend on whatever local aliases exist in $NVM_DIR91nvm_get_remote_aliases() { :; }92nvm_has_italics() { return 0; }9394EXPECTED_OUTPUT_PATH="${MOCKS_DIR}/nvm ls-remote.txt"95OUTPUT="$(nvm ls-remote | sed 's/[ \t]*$//')"96EXPECTED_OUTPUT="$(cat "${EXPECTED_OUTPUT_PATH}" | sed 's/[ \t]*$//' )"
3 unmodified lines
added:test/fast/Unit tests/nvm ls-remote aliases+126 added
1#!/bin/sh23WORK="${PWD}/nvm-ls-remote-aliases-work.$$"4export HOME="${WORK}/home"5export NVM_DIR="${HOME}/.nvm"67cleanup() {8 command rm -rf "${WORK}"9 unset -f cleanup die nvm_has_colors nvm_has_italics nvm_remote_versions expected_row10}11die () { echo "$@" ; cleanup ; exit 1; }1213command mkdir -p "${NVM_DIR}/alias/lts" "${NVM_DIR}/versions/node/v1.5.0"1415: nvm.sh16\. ../../../nvm.sh1718nvm_has_colors() { return 1; }19nvm_remote_versions() {20 if [ -n "${NVM_LTS-}" ] || [ -n "${1-}" ]; then21 command printf '%s\n' 'v2.0.0'22 else23 # io.js sorts last here, so the latest node release is not the last row24 command printf '%s\n' 'v0.11.16' 'v0.12.18' 'v1.0.0 Argon' 'v1.5.0' 'v2.0.0 Iron *' 'iojs-v3.3.1'25 fi26 return "${NVM_REMOTE_STATUS-0}"27}2829nvm_make_alias 'project' '1' >/dev/null30nvm_make_alias 'argon-project' '1.0' >/dev/null31nvm_make_alias 'lts/iron' 'v2.0.0' >/dev/null32nvm_make_alias 'iron-project' 'lts/iron' >/dev/null33nvm_make_alias 'latest-project' 'node' >/dev/null34nvm_make_alias 'stable-project' 'stable' >/dev/null35nvm_make_alias 'unstable-project' 'unstable' >/dev/null36nvm_make_alias 'io-project' 'iojs' >/dev/null37nvm_make_alias '.hidden-project' '1' >/dev/null38command printf '%s\n' '[' > "${NVM_DIR}/alias/invalid-pattern"39command printf '%s\n' '1' > "${NVM_DIR}/alias/$(command printf 'control\rname')"4041# the version cell is 17 wide (`%15s` plus the ` *` installed marker or two spaces), then LTS, Latest, and Aliases columns42expected_row() {43 command printf '%-17s%-19s %-15s %s\n' "$1" "$2" "$3" "$4" | command sed 's/[[:space:]]*$//'44}45EXPECTED_OUTPUT="$(46 expected_row ' v0.11.16' '' '' ' (Aliases: unstable-project)'47 expected_row ' v0.12.18'48 expected_row ' v1.0.0' ' (LTS: Argon)' '' ' (Aliases: argon-project)'49 expected_row ' v1.5.0 *' '' '' ' (Aliases: project)'50 expected_row ' v2.0.0' ' (Latest LTS: Iron)' ' (Latest: node)' ' (Aliases: iron-project, latest-project, stable-project)'51 expected_row ' iojs-v3.3.1' '' '' ' (Aliases: io-project)'52)"5354REMOTE_ERROR="${WORK}/remote-error"55OUTPUT="$(NVM_REMOTE_LATEST_ALIAS='injected' NVM_REMOTE_NAMED_ALIASES="$(command printf 'v0.12.18\tinjected')" nvm ls-remote --no-colors 2>"${REMOTE_ERROR}")"5657[ ! -s "${REMOTE_ERROR}" ] \58 || die "bare nvm ls-remote emitted an error; got >$(command cat "${REMOTE_ERROR}")<"59[ "_${OUTPUT}" = "_${EXPECTED_OUTPUT}" ] \60 || die "bare nvm ls-remote did not annotate aliases in stable columns; got >${OUTPUT}< expected >${EXPECTED_OUTPUT}<"6162ALIASES_COLUMN="$(command printf '%s\n' "${OUTPUT}" | command cut -c56-65 | command sort -u)"63[ "_${ALIASES_COLUMN}" = "_$(command printf '\n (Aliases:')" ] \64 || die "bare nvm ls-remote did not start every Aliases annotation in the same column; got >${ALIASES_COLUMN}<"6566case "${OUTPUT}" in67 *'node, stable'* | *unstable,* | *hidden* | *invalid* | *control* | *injected*)68 die "bare nvm ls-remote showed an alias it should omit; got >${OUTPUT}<"69 ;;70esac7172EXPECTED_FILTERED_OUTPUT='v2.0.0'73OUTPUT="$(nvm ls-remote 2 --no-colors | command sed 's/^[[:space:]]*//')"74[ "_${OUTPUT}" = "_${EXPECTED_FILTERED_OUTPUT}" ] \75 || die "filtered nvm ls-remote annotated aliases; got >${OUTPUT}< expected >${EXPECTED_FILTERED_OUTPUT}<"7677OUTPUT="$(nvm ls-remote --lts --no-colors | command sed 's/^[[:space:]]*//')"78[ "_${OUTPUT}" = "_${EXPECTED_FILTERED_OUTPUT}" ] \79 || die "LTS nvm ls-remote annotated aliases; got >${OUTPUT}< expected >${EXPECTED_FILTERED_OUTPUT}<"8081NVM_REMOTE_STATUS=182OUTPUT="$(nvm ls-remote --no-colors 2>/dev/null)"83COMMAND_STATUS=$?84NVM_REMOTE_STATUS=085[ "${COMMAND_STATUS}" -eq 1 ] \86 || die "failed nvm ls-remote returned ${COMMAND_STATUS}; expected 1"87case "${OUTPUT}" in88 *Latest:* | *Aliases:*) die "failed nvm ls-remote annotated aliases; got >${OUTPUT}<" ;;89esac9091nvm_has_colors() { return 0; }92nvm_has_italics() { return 0; }9394ESC="$(command printf '\033')"95OUTPUT="$(nvm ls-remote)"96EXPECTED_LATEST_ROW="$(command printf ' v2.0.0 \033[1;32m (Latest LTS: Iron)\033[0m \033[0;32m (Latest: \033[3mnode\033[23m)\033[0m \033[0;32m (Aliases: \033[3miron-project\033[23m, \033[3mlatest-project\033[23m, \033[3mstable-project\033[23m)\033[0m')"97case "${OUTPUT}" in98 *"${EXPECTED_LATEST_ROW}"*) ;;99 *) die "colored nvm ls-remote did not use an unbolded color and italic names for aliases; got >${OUTPUT}<" ;;100esac101102nvm_has_italics() { return 1; }103NO_ITALICS_OUTPUT="$(nvm ls-remote)"104nvm_has_italics() { return 0; }105EXPECTED_LATEST_ROW="$(command printf ' v2.0.0 \033[1;32m (Latest LTS: Iron)\033[0m \033[0;32m (Latest: node)\033[0m \033[0;32m (Aliases: iron-project, latest-project, stable-project)\033[0m')"106case "${NO_ITALICS_OUTPUT}" in107 *"${EXPECTED_LATEST_ROW}"*) ;;108 *) die "colored nvm ls-remote used italics on a terminal without italics support; got >${NO_ITALICS_OUTPUT}<" ;;109esac110111OUTPUT="$(command printf '%s\n' "${OUTPUT}" | command sed "s/${ESC}\[[0-9;]*m//g")"112EXPECTED_OUTPUT="$(command printf '%s\n' "${EXPECTED_OUTPUT}" | command sed 's/^\( v1\.5\.0\) \*/\1 /')"113[ "_${OUTPUT}" = "_${EXPECTED_OUTPUT}" ] \114 || die "colored nvm ls-remote did not keep annotation columns aligned; got >${OUTPUT}< expected >${EXPECTED_OUTPUT}<"115116nvm_has_colors() { return 1; }117nvm_remote_versions() {118 command printf '%s\n' 'v2.0.0-nightly20240101abcdef0123 Iron *' 'v3.0.0-nightly2024020201234567890abc'119}120nvm_make_alias 'nightly-project' 'v2.0.0-nightly20240101abcdef0123' >/dev/null121OUTPUT="$(nvm ls-remote --no-colors)"122ALIASES_COLUMNS="$(command printf '%s\n' "${OUTPUT}" | command awk 'index($0, "(Aliases:") { print index($0, "(Aliases:") }' | command sort -u)"123[ "$(command printf '%s\n' "${ALIASES_COLUMNS}" | command wc -l | command tr -d ' ')" = 1 ] \124 || die "versions wider than the version column did not keep Aliases annotations in one column; got >${OUTPUT}<"125126cleanup
modified:nvm.sh+176 added-12 removed
2159 unmodified lines
21602161 nvm_download -L -s "${SHASUMS_URL}" -o - | command awk -v tarball="${4}.${5}" '{ if (tarball == $2) print $1 }'2162}21632164nvm_get_remote_aliases() {2165 local NVM_ALIAS_DIR2166 NVM_ALIAS_DIR="$(nvm_alias_path)"2167 if [ ! -d "${NVM_ALIAS_DIR}" ]; then2168 return 02169 fi21702171 local NVM_NODE_PREFIX2172 NVM_NODE_PREFIX="$(nvm_node_prefix)"2173 local NVM_IOJS_PREFIX2174 NVM_IOJS_PREFIX="$(nvm_iojs_prefix)"2175 local NVM_ALIAS_PATH2176 local NVM_ALIAS_NAME2177 local NVM_ALIAS_TARGET21782179 nvm_is_zsh && setopt local_options nonomatch2180 # the same glob as `nvm_list_aliases`, so this never shows an alias that `nvm alias` omits2181 for NVM_ALIAS_PATH in "${NVM_ALIAS_DIR}/"*; do2182 if [ ! -f "${NVM_ALIAS_PATH}" ]; then2183 continue2184 fi21852186 NVM_ALIAS_NAME="${NVM_ALIAS_PATH##*/}"2187 case "${NVM_ALIAS_NAME}" in2188 "${NVM_NODE_PREFIX}" | "${NVM_IOJS_PREFIX}" | stable | unstable) continue ;;2189 # control characters would corrupt the terminal, or the tab-separated output2190 *[[:cntrl:]]*) continue ;;2191 esac21922193 NVM_ALIAS_TARGET="$(nvm_resolve_alias "${NVM_ALIAS_NAME}" 2>/dev/null)" || continue2194 case "${NVM_ALIAS_TARGET}" in2195 '' | *[[:cntrl:]]*) continue ;;2196 esac21972198 command printf '%s\t%s\n' "${NVM_ALIAS_TARGET}" "${NVM_ALIAS_NAME}"2199 done2200}22012202nvm_print_versions() {2203 local NVM_CURRENT2204 NVM_CURRENT=$(nvm_ls_current)22052206 local NVM_LATEST_ALIAS2207 NVM_LATEST_ALIAS="${2-}"2208 local NVM_NAMED_ALIASES2209 NVM_NAMED_ALIASES="${3-}"22102211 local INSTALLED_COLOR2212 local SYSTEM_COLOR2213 local CURRENT_COLOR2214 local NOT_INSTALLED_COLOR
12 unmodified lines
2227 if nvm_has_colors; then2228 NVM_HAS_COLORS=12229 fi22302231 local NVM_HAS_ITALICS2232 NVM_HAS_ITALICS=02233 if [ "${NVM_HAS_COLORS}" = 1 ] && [ -n "${NVM_LATEST_ALIAS}${NVM_NAMED_ALIASES}" ] && nvm_has_italics; then2234 NVM_HAS_ITALICS=12235 fi22362237 NVM_NAMED_ALIASES="${NVM_NAMED_ALIASES}" \2238 command awk \2239 -v remote_versions="$(printf '%s' "${1-}" | tr '\n' '|')" \2240 -v latest_alias="${NVM_LATEST_ALIAS}" \2241 -v node_prefix="$(nvm_node_prefix)" -v iojs_prefix="$(nvm_iojs_prefix)" \2242 -v installed_versions="$(nvm_ls | tr '\n' '|')" -v current="$NVM_CURRENT" \2243 -v installed_color="$INSTALLED_COLOR" -v system_color="$SYSTEM_COLOR" \2244 -v current_color="$CURRENT_COLOR" -v default_color="$DEFAULT_COLOR" \2193 -v old_lts_color="$DEFAULT_COLOR" -v has_colors="$NVM_HAS_COLORS" '2245 -v old_lts_color="$DEFAULT_COLOR" -v has_colors="$NVM_HAS_COLORS" \2246 -v has_italics="$NVM_HAS_ITALICS" '2247function alen(arr, i, len) { len=0; for(i in arr) len++; return len; }2248function paint(color, text) { return (has_colors && color) ? ("\033[" color text "\033[0m") : text; }2249function spaces(count, result) { result = ""; while (count-- > 0) result = result " "; return result; }2250function italicize(text) { return has_italics ? ("\033[3m" text "\033[23m") : text; }2251BEGIN {2252 named_alias_list = ENVIRON["NVM_NAMED_ALIASES"];2253 fmt_installed = has_colors ? (installed_color ? "\033[" installed_color "%15s\033[0m" : "%15s") : "%15s *";2254 fmt_system = has_colors ? (system_color ? "\033[" system_color "%15s\033[0m" : "%15s") : "%15s *";2255 fmt_current = has_colors ? (current_color ? "\033[" current_color "->%13s\033[0m" : "%15s") : "->%13s *";22562257 latest_lts_color = current_color;2258 sub(/0;/, "1;", latest_lts_color);2259 alias_color = current_color;2260 sub(/^1;/, "0;", alias_color);22612262 fmt_latest_lts = has_colors && latest_lts_color ? ("\033[" latest_lts_color " (Latest LTS: %s)\033[0m") : " (Latest LTS: %s)";2263 fmt_old_lts = has_colors && old_lts_color ? ("\033[" old_lts_color " (LTS: %s)\033[0m") : " (LTS: %s)";2264 fmt_system_target = has_colors && system_color ? (" (\033[" system_color "-> %s\033[0m)") : " (-> %s)";
3 unmodified lines
2268 rows = alen(lines);22692270 for (n = 1; n <= rows; n++) {2271 split(lines[n], fields, "[[:blank:]]+");2272 if (index(fields[1], iojs_prefix "-") == 1) {2273 latest_iojs = fields[1];2274 } else if (fields[1] ~ /^v0\.[0-9]*[13579]\./) {2275 latest_unstable = fields[1];2276 } else if (fields[1] ~ /^v[0-9]/) {2277 latest_stable = fields[1];2278 }2279 }22802281 named_alias_rows = split(named_alias_list, named_alias_lines, "\n");2282 for (a = 1; a <= named_alias_rows; a++) {2283 tab_position = index(named_alias_lines[a], "\t");2284 if (tab_position < 2) {2285 continue;2286 }2287 alias_target = substr(named_alias_lines[a], 1, tab_position - 1);2288 alias_name = substr(named_alias_lines[a], tab_position + 1);2289 if (alias_name == "") {2290 continue;2291 }22922293 alias_version = "";2294 if (alias_target == node_prefix || alias_target == "stable") {2295 alias_version = latest_stable;2296 } else if (alias_target == "unstable") {2297 alias_version = latest_unstable;2298 } else if (alias_target == iojs_prefix || alias_target == (iojs_prefix "-")) {2299 alias_version = latest_iojs;2300 } else {2301 for (n = 1; n <= rows; n++) {2302 split(lines[n], fields, "[[:blank:]]+");2303 if (fields[1] == alias_target || index(fields[1], alias_target ".") == 1) {2304 alias_version = fields[1];2305 }2306 }2307 }23082309 if (alias_version != "") {2310 # mawk creates the assigned element before evaluating the right side, so `in` must be checked first2311 if (alias_version in named_aliases) {2312 named_aliases[alias_version] = named_aliases[alias_version] ", " alias_name;2313 italic_aliases[alias_version] = italic_aliases[alias_version] ", " italicize(alias_name);2314 } else {2315 named_aliases[alias_version] = alias_name;2316 italic_aliases[alias_version] = italicize(alias_name);2317 }2318 }2319 }23202321 for (n = 1; n <= rows; n++) {2322 split(lines[n], fields, "[[:blank:]]+");2323 cols = alen(fields);2324 version = fields[1];2325 is_installed = 0;2326
12 unmodified lines
2339 } else if (is_installed) {2340 fmt_version = fmt_installed;2341 }23422233 padding = (!has_colors && is_installed) ? "" : " ";2343 padding[n] = (!has_colors && is_installed) ? "" : " ";23442235 if (cols == 1) {2236 formatted = sprintf(fmt_version, version);2237 } else if (version == "system" && cols >= 2) {2238 formatted = sprintf((fmt_version fmt_system_target), version, fields[2]);2345 output[n] = sprintf(fmt_version, version);2346 version_width[n] = output[n] padding[n];2347 gsub(/\033\[[0-9;]*m/, "", version_width[n]);2348 version_width[n] = length(version_width[n]);2349 if (version_width[n] > max_version_width) {2350 max_version_width = version_width[n];2351 }2352 for (c = 1; c <= 3; c++) {2353 text[n, c] = "";2354 cell[n, c] = "";2355 }2356 if (version == "system" && cols >= 2) {2357 output[n] = output[n] sprintf(fmt_system_target, fields[2]);2358 } else if (cols == 2) {2240 formatted = sprintf((fmt_version padding fmt_old_lts), version, fields[2]);2359 text[n, 1] = sprintf(" (LTS: %s)", fields[2]);2360 cell[n, 1] = sprintf(fmt_old_lts, fields[2]);2361 } else if (cols == 3 && fields[3] == "*") {2242 formatted = sprintf((fmt_version padding fmt_latest_lts), version, fields[2]);2362 text[n, 1] = sprintf(" (Latest LTS: %s)", fields[2]);2363 cell[n, 1] = sprintf(fmt_latest_lts, fields[2]);2364 }23652245 output[n] = formatted;2366 if (latest_alias != "" && version == latest_stable) {2367 text[n, 2] = " (Latest: " latest_alias ")";2368 cell[n, 2] = paint(alias_color, " (Latest: " italicize(latest_alias) ")");2369 }23702371 if (version in named_aliases) {2372 text[n, 3] = " (Aliases: " named_aliases[version] ")";2373 cell[n, 3] = paint(alias_color, " (Aliases: " italic_aliases[version] ")");2374 }23752376 for (c = 1; c <= 3; c++) {2377 if (length(text[n, c]) > width[c]) {2378 width[c] = length(text[n, c]);2379 }2380 }2381 }23822383 # each kind of annotation starts in the same column on every row, so the output stays `cut`-able2384 for (n = 1; n <= rows; n++) {2249 print output[n]2385 last = 0;2386 for (c = 1; c <= 3; c++) {2387 if (text[n, c] != "") {2388 last = c;2389 }2390 }2391 formatted = output[n];2392 if (last) {2393 formatted = formatted padding[n];2394 if (width[2] || width[3]) {2395 formatted = formatted spaces(max_version_width - version_width[n]);2396 }2397 gap = "";2398 for (c = 1; c <= last; c++) {2399 if (width[c]) {2400 formatted = formatted gap cell[n, c];2401 gap = spaces(width[c] - length(text[n, c])) " ";2402 }2403 }2404 }2405 print formatted2406 }24072408 exit2409}'
2496 unmodified lines
4906 local EXIT_CODE4907 NVM_OUTPUT="$(NVM_LTS="${NVM_LTS-}" nvm_remote_versions "${PATTERN-}" &&:)"4908 EXIT_CODE=$?4909 if [ -n "${NVM_OUTPUT}" ]; then4910 local NVM_REMOTE_LATEST_ALIAS4911 NVM_REMOTE_LATEST_ALIAS=''4912 local NVM_REMOTE_NAMED_ALIASES4913 NVM_REMOTE_NAMED_ALIASES=''4914 if [ "${EXIT_CODE}" -eq 0 ] && [ -z "${NVM_LTS-}" ] && [ -z "${PATTERN-}" ]; then4915 NVM_REMOTE_LATEST_ALIAS="$(nvm_node_prefix)"4916 NVM_REMOTE_NAMED_ALIASES="$(nvm_get_remote_aliases)"4917 fi4754 NVM_NO_COLORS="${NVM_NO_COLORS-}" nvm_print_versions "${NVM_OUTPUT}"4918 NVM_NO_COLORS="${NVM_NO_COLORS-}" nvm_print_versions "${NVM_OUTPUT}" "${NVM_REMOTE_LATEST_ALIAS}" "${NVM_REMOTE_NAMED_ALIASES}"4919 return $EXIT_CODE4920 fi4921 NVM_NO_COLORS="${NVM_NO_COLORS-}" nvm_print_versions "N/A"4922 return 3
275 unmodified lines
5198 unset -f nvm \5199 nvm_iojs_prefix nvm_node_prefix \5200 nvm_add_iojs_prefix nvm_strip_iojs_prefix \5201 nvm_is_iojs_version nvm_is_alias nvm_has_non_aliased \5038 nvm_ls_remote nvm_ls_remote_iojs nvm_ls_remote_index_tab \5202 nvm_ls_remote nvm_ls_remote_iojs nvm_ls_remote_index_tab nvm_get_remote_aliases \5203 nvm_ls nvm_remote_version nvm_remote_versions \5204 nvm_install_binary nvm_install_source nvm_clang_version \5205 nvm_get_mirror nvm_get_download_slug nvm_download_artifact \5206 nvm_install_npm_if_needed nvm_use_if_needed nvm_check_file_permissions \
210 unmodified lines
modified:README.md+9 added
399 unmodified lines
400```sh401nvm ls-remote402```403404When no version or `--lts` filter is given, the list also marks the version the `node` alias would install, and every version that one of your own aliases would resolve to. Each kind of annotation always starts in the same column, so the output can be sliced with `cut -c` or similar tools:405406```407 v20.9.0 (Latest LTS: Iron) (Aliases: work)408 v21.0.0409 v21.1.0410 v21.2.0 (Latest: node) (Aliases: default)411```412413And then in any new shell just use the installed version:414415```sh416nvm use node
837 unmodified lines
modified:update_test_mocks.sh+10 added