openclaw/scripts/pre-commit/resolve-node.sh
jiarung 98822509a8 fix(git-hooks): pick newest nvm Node with version-aware sort
The previous loop used Bash glob expansion (lexicographic order) and
stopped at the first match, so environments with multiple Node installs
could select an older runtime (e.g. v18 before v22).

Extract the nvm resolution into a shared scripts/pre-commit/resolve-node.sh
that pipes `ls` output through `sort -V | tail -1` to select the
semantically newest version. Both pre-commit and run-node-tool.sh now
source the shared script, eliminating the duplicated logic.
2026-03-13 14:17:36 +00:00

19 lines
581 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.
#
# 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.
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
)
if [[ -x "$_nvm_node" ]]; then
export PATH="$(dirname "$_nvm_node"):$PATH"
fi
unset _nvm_node
fi