Skip to content

Commit ac0b198

Browse files
fix: replace jq with node in hooks for Windows compatibility
check-readme.sh and enrich-context.sh used jq for JSON parsing, which isn't available on Windows. Switch to node inline scripts matching the pattern used by guard-git.sh and track-edits.sh. Also commit rebuild-graph.sh which was wired into settings.json but never tracked.
1 parent 0a679aa commit ac0b198

File tree

3 files changed

+99
-19
lines changed

3 files changed

+99
-19
lines changed

.claude/hooks/check-readme.sh

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33
# Runs as a PreToolUse hook on Bash tool calls.
44

55
INPUT=$(cat)
6-
COMMAND=$(echo "$INPUT" | jq -r '.tool_input.command // empty')
6+
COMMAND=$(echo "$INPUT" | node -e "
7+
let d='';
8+
process.stdin.on('data',c=>d+=c);
9+
process.stdin.on('end',()=>{
10+
const p=JSON.parse(d).tool_input?.command||'';
11+
if(p)process.stdout.write(p);
12+
});
13+
" 2>/dev/null) || true
714

815
# Only act on git commit commands
916
if ! echo "$COMMAND" | grep -qE '^\s*git\s+commit'; then
@@ -30,13 +37,16 @@ if [ "$NEEDS_CHECK" -gt 0 ]; then
3037
[ "$CLAUDE_STAGED" -eq 0 ] && MISSING="${MISSING:+$MISSING, }CLAUDE.md"
3138
[ "$ROADMAP_STAGED" -eq 0 ] && MISSING="${MISSING:+$MISSING, }ROADMAP.md"
3239

33-
jq -n --arg missing "$MISSING" '{
34-
hookSpecificOutput: {
35-
hookEventName: "PreToolUse",
36-
permissionDecision: "deny",
37-
permissionDecisionReason: ($missing + " not staged but source files were changed. Review whether these docs need updating — README.md (language support table, feature list, command docs), CLAUDE.md (architecture table, supported languages, key design decisions), and ROADMAP.md (phase status, new features, deliverables). If they truly do not need changes, re-run the commit with docs check acknowledged.")
38-
}
39-
}'
40+
node -e "
41+
const missing = process.argv[1];
42+
console.log(JSON.stringify({
43+
hookSpecificOutput: {
44+
hookEventName: 'PreToolUse',
45+
permissionDecision: 'deny',
46+
permissionDecisionReason: missing + ' not staged but source files were changed. Review whether these docs need updating — README.md (language support table, feature list, command docs), CLAUDE.md (architecture table, supported languages, key design decisions), and ROADMAP.md (phase status, new features, deliverables). If they truly do not need changes, re-run the commit with docs check acknowledged.'
47+
}
48+
}));
49+
" "$MISSING"
4050
exit 0
4151
fi
4252

.claude/hooks/enrich-context.sh

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@ INPUT=$(cat)
1010

1111
# Extract file path based on tool type
1212
# Read tool uses tool_input.file_path, Grep uses tool_input.path
13-
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // .tool_input.path // empty' 2>/dev/null)
13+
FILE_PATH=$(echo "$INPUT" | node -e "
14+
let d='';
15+
process.stdin.on('data',c=>d+=c);
16+
process.stdin.on('end',()=>{
17+
const o=JSON.parse(d).tool_input||{};
18+
const p=o.file_path||o.path||'';
19+
if(p)process.stdout.write(p);
20+
});
21+
" 2>/dev/null) || true
1422

1523
# Guard: no file path found
1624
if [ -z "$FILE_PATH" ]; then
@@ -30,8 +38,9 @@ fi
3038

3139
# Convert absolute path to relative (strip project dir prefix)
3240
REL_PATH="$FILE_PATH"
33-
if [[ "$FILE_PATH" == "${CLAUDE_PROJECT_DIR}"* ]]; then
34-
REL_PATH="${FILE_PATH#"${CLAUDE_PROJECT_DIR}"/}"
41+
PROJECT_DIR="${CLAUDE_PROJECT_DIR:-.}"
42+
if [[ "$FILE_PATH" == "${PROJECT_DIR}"* ]]; then
43+
REL_PATH="${FILE_PATH#"${PROJECT_DIR}"/}"
3544
fi
3645
# Normalize backslashes to forward slashes (Windows compatibility)
3746
REL_PATH="${REL_PATH//\\//}"
@@ -50,13 +59,22 @@ if [ -z "$DEPS" ] || [ "$DEPS" = "null" ]; then
5059
fi
5160

5261
# Output as informational context (never deny)
53-
echo "$DEPS" | jq -c '{
54-
hookSpecificOutput: (
55-
"Codegraph context for " + (.file // "unknown") + ":\n" +
56-
" Imports: " + ((.results[0].imports // []) | length | tostring) + " files\n" +
57-
" Imported by: " + ((.results[0].importedBy // []) | length | tostring) + " files\n" +
58-
" Definitions: " + ((.results[0].definitions // []) | length | tostring) + " symbols"
59-
)
60-
}' 2>/dev/null || true
62+
echo "$DEPS" | node -e "
63+
let d='';
64+
process.stdin.on('data',c=>d+=c);
65+
process.stdin.on('end',()=>{
66+
try {
67+
const o=JSON.parse(d);
68+
const r=o.results?.[0]||{};
69+
const imports=(r.imports||[]).length;
70+
const importedBy=(r.importedBy||[]).length;
71+
const defs=(r.definitions||[]).length;
72+
const file=o.file||'unknown';
73+
console.log(JSON.stringify({
74+
hookSpecificOutput: 'Codegraph context for '+file+':\\n Imports: '+imports+' files\\n Imported by: '+importedBy+' files\\n Definitions: '+defs+' symbols'
75+
}));
76+
} catch(e) {}
77+
});
78+
" 2>/dev/null || true
6179

6280
exit 0

.claude/hooks/rebuild-graph.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env bash
2+
# rebuild-graph.sh — PostToolUse hook for Edit and Write tools
3+
# Incrementally rebuilds the codegraph after source file edits.
4+
# Always exits 0 (informational only, never blocks).
5+
6+
set -euo pipefail
7+
8+
INPUT=$(cat)
9+
10+
# Extract file path using node (jq may not be available on Windows)
11+
FILE_PATH=$(echo "$INPUT" | node -e "
12+
let d='';
13+
process.stdin.on('data',c=>d+=c);
14+
process.stdin.on('end',()=>{
15+
const p=JSON.parse(d).tool_input?.file_path||'';
16+
if(p)process.stdout.write(p);
17+
});
18+
" 2>/dev/null) || true
19+
20+
if [ -z "$FILE_PATH" ]; then
21+
exit 0
22+
fi
23+
24+
# Only rebuild for source files codegraph tracks
25+
# Skip docs, configs, test fixtures, and non-code files
26+
case "$FILE_PATH" in
27+
*.js|*.ts|*.tsx|*.jsx|*.py|*.go|*.rs|*.java|*.cs|*.php|*.rb|*.tf|*.hcl)
28+
;;
29+
*)
30+
exit 0
31+
;;
32+
esac
33+
34+
# Skip test fixtures — they're copied to tmp dirs anyway
35+
if echo "$FILE_PATH" | grep -qE '(fixtures|__fixtures__|testdata)/'; then
36+
exit 0
37+
fi
38+
39+
# Guard: codegraph DB must exist (project has been built at least once)
40+
DB_PATH="${CLAUDE_PROJECT_DIR:-.}/.codegraph/graph.db"
41+
if [ ! -f "$DB_PATH" ]; then
42+
exit 0
43+
fi
44+
45+
# Run incremental build (skips unchanged files via hash check)
46+
if command -v codegraph &>/dev/null; then
47+
codegraph build "${CLAUDE_PROJECT_DIR:-.}" -d "$DB_PATH" 2>/dev/null || true
48+
else
49+
node "${CLAUDE_PROJECT_DIR}/src/cli.js" build "${CLAUDE_PROJECT_DIR:-.}" -d "$DB_PATH" 2>/dev/null || true
50+
fi
51+
52+
exit 0

0 commit comments

Comments
 (0)