bookworm-smart-assistant/scripts/patches/patch-r3-claudemd-doc.js

40 lines
1.4 KiB
JavaScript
Raw Normal View History

#!/usr/bin/env node
/**
* patch-r3-claudemd-doc.js · 2026-04-26
*
* R3: CLAUDE.md §上下文管理 追加项目级 .bookworm-context.md 说明
*
* 幂等: sentinel "项目级稳定上下文"
*/
'use strict';
const fs = require('fs');
const path = require('path');
const TARGET = path.join(__dirname, '..', '..', 'CLAUDE.md');
const SENTINEL = '项目级稳定上下文';
const ANCHOR = '- **批量任务切片**';
const NEW_LINE = `- **项目级稳定上下文** (R3): 项目根放 \`.bookworm-context.md\` (执行 \`node ~/.claude/scripts/bookworm-context-init.js\` 生成模板), 每会话首次在该项目目录提交 prompt 时, 头 100 行 (可 \`<!-- max-lines: N -->\` 覆盖) 自动注入, 用于固化项目身份/关键路径/已知陷阱; 与 \`.bookworm-progress.md\` (R1 动态进度) 互补
- **批量任务切片**`;
function main() {
const src = fs.readFileSync(TARGET, 'utf8');
if (src.includes(SENTINEL)) {
console.log('[r3-doc] already applied, skip');
return;
}
if (!src.includes(ANCHOR)) {
console.error('[r3-doc] anchor not found, manual review needed');
process.exit(1);
}
const next = src.replace(ANCHOR, NEW_LINE);
const bak = TARGET + '.bak.r3doc.' + Date.now();
fs.copyFileSync(TARGET, bak);
const tmp = TARGET + '.tmp.' + process.pid;
fs.writeFileSync(tmp, next, 'utf8');
fs.renameSync(tmp, TARGET);
console.log('[r3-doc] OK, bak:', path.basename(bak));
}
main();