deviant/scripts/hook-reindex-snapshots.mjs
walerikus fc3f27d116 chore(hooks): auto-regenerate snapshots/INDEX.md on snapshot writes
Adds a PostToolUse hook (.claude/settings.json) that runs a guard script
after Write/Edit/MultiEdit; the guard regenerates snapshots/INDEX.md only
when the edited file is an actual snapshots/*.md (not INDEX.md), and never
fails the tool call. Keeps the domain index from going stale.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-07 01:58:15 +02:00

29 lines
1.1 KiB
JavaScript

#!/usr/bin/env node
// PostToolUse hook guard: regenerate snapshots/INDEX.md only when a Write/Edit
// actually touched a snapshot file. Reads the hook JSON from stdin; never fails
// the tool call (always exits 0). Wired from .claude/settings.json.
import { spawnSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
const repoRoot = dirname(dirname(fileURLToPath(import.meta.url))); // scripts/ -> repo root
let raw = '';
process.stdin.setEncoding('utf8');
process.stdin.on('data', (c) => (raw += c));
process.stdin.on('end', () => {
let p = '';
try {
const j = JSON.parse(raw || '{}');
p = j?.tool_input?.file_path || j?.tool_input?.filePath || j?.tool_input?.path || '';
} catch { /* ignore malformed input */ }
const norm = String(p).replace(/\\/g, '/');
const isSnapshot = /(^|\/)snapshots\/[^/]+\.md$/.test(norm) && !/\/INDEX\.md$/.test(norm);
if (!isSnapshot) process.exit(0);
const r = spawnSync(process.execPath, ['scripts/build-snapshot-index.mjs'], {
cwd: repoRoot,
encoding: 'utf8',
});
if (r.stdout) process.stdout.write(r.stdout);
process.exit(0);
});