2026-03-17 19:58:30 +08:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
2026-07-03 13:58:36 +07:00
|
|
|
# afterFileEdit/PostToolUse hook: Run Corepack-managed Yarn install when package.json is changed
|
|
|
|
|
# Stdin JSON differs per harness:
|
|
|
|
|
# Cursor afterFileEdit: {"file_path": "...", "edits": [...]}
|
|
|
|
|
# Claude/Codex PostToolUse: {"tool_input": {"file_path": "..."}, ...}
|
2026-03-17 19:58:30 +08:00
|
|
|
|
|
|
|
|
input=$(cat)
|
2026-07-03 13:58:36 +07:00
|
|
|
if command -v jq >/dev/null 2>&1; then
|
|
|
|
|
file_path=$(printf '%s' "$input" | jq -r '.tool_input.file_path // .file_path // empty' 2>/dev/null)
|
|
|
|
|
else
|
|
|
|
|
file_path=$(echo "$input" | grep -o '"file_path"[[:space:]]*:[[:space:]]*"[^"]*"' | head -1 | sed 's/.*:.*"\([^"]*\)"/\1/')
|
|
|
|
|
fi
|
2026-03-17 19:58:30 +08:00
|
|
|
|
|
|
|
|
if [ -z "$file_path" ]; then
|
|
|
|
|
exit 0
|
|
|
|
|
fi
|
|
|
|
|
|
2026-07-03 13:58:36 +07:00
|
|
|
repo_root="$(git rev-parse --show-toplevel 2>/dev/null || pwd)"
|
|
|
|
|
|
|
|
|
|
# Claude Code and Codex deliver absolute paths; compare repo-relative so the
|
|
|
|
|
# root package.json matches in every harness.
|
|
|
|
|
case "$file_path" in
|
|
|
|
|
"$repo_root"/*) file_path="${file_path#"$repo_root"/}" ;;
|
|
|
|
|
/*) exit 0 ;;
|
|
|
|
|
esac
|
|
|
|
|
|
2026-03-17 19:58:30 +08:00
|
|
|
if [ "$file_path" = "package.json" ]; then
|
|
|
|
|
cd "$repo_root" || exit 0
|
2026-03-19 13:41:03 +08:00
|
|
|
echo "package.json changed - running corepack yarn install to update yarn.lock..."
|
|
|
|
|
corepack yarn install
|
2026-03-17 19:58:30 +08:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
exit 0
|