bookworm-smart-assistant/scripts/patches/patch-w1-disambig-count-80to88.js

59 lines
1.6 KiB
JavaScript
Raw Permalink Normal View History

#!/usr/bin/env node
/**
* W1 消歧规则数漂移修复 (2026-04-25)
* CLAUDE.md "完整 80 条" "完整 88 条"
*
* 幂等: 若已是 88 则跳过
* 备份: .bak.w1-count.<ISO>
* sentinel: PATCH-W1-DISAMBIG-COUNT-80TO88-APPLIED
*/
'use strict';
const fs = require('fs');
const path = require('path');
const TARGET = path.join(__dirname, '..', '..', 'CLAUDE.md');
const SENTINEL = 'PATCH-W1-DISAMBIG-COUNT-80TO88-APPLIED';
const OLD_STR = '完整 80 条见 scripts/disambiguation-rules.json';
const NEW_STR = '完整 88 条见 scripts/disambiguation-rules.json';
function main() {
if (!fs.existsSync(TARGET)) {
console.error('[W1] CLAUDE.md 不存在:', TARGET);
process.exit(1);
}
const content = fs.readFileSync(TARGET, 'utf8');
// 幂等检查
if (content.includes(SENTINEL) || content.includes(NEW_STR)) {
console.log('[W1] 已是最新状态,跳过');
process.exit(0);
}
if (!content.includes(OLD_STR)) {
console.error('[W1] 未找到目标字符串,可能已被修改或不存在');
process.exit(1);
}
// 备份
const bak = TARGET + '.bak.w1-count.' + new Date().toISOString().replace(/[:.]/g, '-');
fs.copyFileSync(TARGET, bak);
console.log('[W1] 备份:', bak);
// 替换
const newContent = content.replace(OLD_STR, NEW_STR);
// 原子写入 (tmp + rename)
const tmp = TARGET + '.w1.tmp.' + process.pid;
fs.writeFileSync(tmp, newContent, 'utf8');
fs.renameSync(tmp, TARGET);
console.log('[W1] 成功: "80 条" → "88 条"');
console.log('[W1] ' + SENTINEL);
}
try { main(); } catch (e) {
console.error('[W1] 异常:', e.message);
process.exit(1);
}