openclaw/scripts/pre-commit/resolve-node.sh
jiarung d3971e77fd fix(git-hooks): replace GNU-only sort -V with portable zero-pad sort
sort -V is a GNU extension; BSD sort on macOS does not support it. When
node is absent from PATH and the nvm fallback runs, set -euo pipefail
causes the unsupported flag to abort the hook before lint/format can
run, blocking commits on macOS.

Replace the sort -V | tail -1 pipeline with a Bash for-loop that
zero-pads each semver component to five digits and emits a tab-delimited
key+path line. Plain sort + tail -1 + cut then selects the highest
semantic version — no GNU-only flags required.

Smoke-tested with v18 vs v22 paths; v22 is correctly selected on both
GNU and BSD sort.
2026-03-15 05:14:07 +00:00

23 lines
886 B
Bash

#!/usr/bin/env bash
# Resolve the newest nvm-managed Node when it is not already in PATH.
# Source this file; do not execute it directly.
#
# Cross-platform: avoids GNU-only `sort -V` (not supported by BSD sort on
# macOS). Instead, each semver component is zero-padded to five digits so
# that a plain lexicographic sort correctly selects the highest semantic
# version (e.g. v22.x wins over v18.x).
if ! command -v node >/dev/null 2>&1; then
_nvm_node=$(
for _p in "$HOME/.nvm/versions/node"/*/bin/node; do
[[ -x "$_p" ]] || continue
_ver="${_p%/bin/node}"; _ver="${_ver##*/v}"
IFS=. read -r _ma _mi _pa <<< "$_ver"
printf '%05d%05d%05d\t%s\n' "${_ma:-0}" "${_mi:-0}" "${_pa:-0}" "$_p"
done | sort | tail -1 | cut -f2-
)
if [[ -x "$_nvm_node" ]]; then
export PATH="$(dirname "$_nvm_node"):$PATH"
fi
unset _nvm_node _p _ver _ma _mi _pa
fi