diff --git a/git-hooks/pre-commit b/git-hooks/pre-commit index 948f2087ada..9650fe936eb 100755 --- a/git-hooks/pre-commit +++ b/git-hooks/pre-commit @@ -2,6 +2,17 @@ set -euo pipefail +# Resolve node when not in PATH (e.g. nvm environments where shell profile +# hasn't been sourced by the git hook). Picks the highest nvm version found. +if ! command -v node >/dev/null 2>&1; then + for _nvm_node in "$HOME/.nvm/versions/node"/*/bin/node; do + if [[ -x "$_nvm_node" ]]; then + export PATH="$(dirname "$_nvm_node"):$PATH" + break + fi + done +fi + ROOT_DIR="$(git rev-parse --show-toplevel 2>/dev/null || pwd)" RUN_NODE_TOOL="$ROOT_DIR/scripts/pre-commit/run-node-tool.sh" FILTER_FILES="$ROOT_DIR/scripts/pre-commit/filter-staged-files.mjs" diff --git a/scripts/pre-commit/run-node-tool.sh b/scripts/pre-commit/run-node-tool.sh index 34163075517..69a26f45e0e 100755 --- a/scripts/pre-commit/run-node-tool.sh +++ b/scripts/pre-commit/run-node-tool.sh @@ -3,6 +3,16 @@ set -euo pipefail ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" +# Resolve node when not in PATH (nvm environments). +if ! command -v node >/dev/null 2>&1; then + for _nvm_node in "$HOME/.nvm/versions/node"/*/bin/node; do + if [[ -x "$_nvm_node" ]]; then + export PATH="$(dirname "$_nvm_node"):$PATH" + break + fi + done +fi + if [[ $# -lt 1 ]]; then echo "usage: run-node-tool.sh [args...]" >&2 exit 2