bookworm-smart-assistant/scripts/patches/patch-w6-route-gate-refactor.js

40 lines
1.8 KiB
JavaScript
Raw Normal View History

#!/usr/bin/env node
/**
* Patch W6: route-compliance-gate.js L28 单行三目链重构
* 160+ 字符一行 IIFE 拆成可读的 resolve 函数
* 幂等: sentinel W6_DISK_CACHE_RESOLVE_v1
*/
const fs = require('fs');
const path = require('path');
const TARGET = path.join(__dirname, '..', '..', 'hooks', 'route-compliance-gate.js');
const SENTINEL = 'W6_DISK_CACHE_RESOLVE_v1';
function main() {
const src = fs.readFileSync(TARGET, 'utf8');
if (src.includes(SENTINEL)) { console.error('[patch-w6] already applied'); process.exit(0); }
const oldLine = "const DISK_CACHE_FILE = require('path').join((function(){ try { return require('../scripts/paths.config.js').PATHS.root; } catch { return require('path').join(process.env.USERPROFILE || process.env.HOME, '.claude'); } })(), 'debug', '.disk-cache.json');";
if (!src.includes(oldLine)) { console.error('[patch-w6] anchor missing'); process.exit(3); }
const newBlock =
"// " + SENTINEL + ": 拆解 resolve 路径,提升可读性\n" +
"function _resolveClaudeRootForCache() {\n" +
" try { return require('../scripts/paths.config.js').PATHS.root; }\n" +
" catch { return require('path').join(process.env.USERPROFILE || process.env.HOME, '.claude'); }\n" +
"}\n" +
"const DISK_CACHE_FILE = require('path').join(_resolveClaudeRootForCache(), 'debug', '.disk-cache.json');";
const patched = src.replace(oldLine, newBlock);
if (patched === src) { console.error('[patch-w6] no change'); process.exit(4); }
const bakDir = path.join(path.dirname(TARGET), 'archive');
if (!fs.existsSync(bakDir)) fs.mkdirSync(bakDir, { recursive: true });
fs.writeFileSync(path.join(bakDir, 'route-compliance-gate.js.bak.w6.' + Date.now()), src);
const tmp = TARGET + '.tmp.' + process.pid;
fs.writeFileSync(tmp, patched);
fs.renameSync(tmp, TARGET);
console.error('[patch-w6] applied');
}
main();