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.
This commit is contained in:
parent
8c162d0ba4
commit
d3971e77fd
@ -2,17 +2,21 @@
|
||||
# Resolve the newest nvm-managed Node when it is not already in PATH.
|
||||
# Source this file; do not execute it directly.
|
||||
#
|
||||
# Uses `sort -V` (version-aware sort) so that semantic version order is
|
||||
# respected — e.g. v22.x is chosen over v18.x even though "v1" sorts
|
||||
# before "v2" lexicographically.
|
||||
# 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=$(
|
||||
ls -d "$HOME/.nvm/versions/node"/*/bin/node 2>/dev/null \
|
||||
| sort -V \
|
||||
| tail -1
|
||||
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
|
||||
unset _nvm_node _p _ver _ma _mi _pa
|
||||
fi
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user