146 lines
7.1 KiB
JavaScript
146 lines
7.1 KiB
JavaScript
#!/usr/bin/env node
|
||
'use strict'
|
||
// 事实同步器(2026-11-07)
|
||
//
|
||
// 痛点:新增/移动一个 src 模块,要手工同步 9 处 ——
|
||
// ① scripts/build.js 的 sourceModules 顺序
|
||
// ② tests/fixtures/iwb-source-modules-order.json
|
||
// ③ tests/fixtures/iwb-app-global-reads.json(耦合面)
|
||
// ④~⑧ docs/standards/architecture-current-state.md 的 4 个计数
|
||
// ⑨ CONTRIBUTING.md 的单测文件数
|
||
// 漏一处门禁就红,且报错只说"应为 N",不说该改哪个文件哪一行。
|
||
//
|
||
// 用法:
|
||
// node scripts/sync-facts.js # 只报告差异(dry-run,退出码 1 表示有差异)
|
||
// node scripts/sync-facts.js --write # 写入全部可自动修的事实
|
||
//
|
||
// 只做**机械同步**(数字/快照/排序),不猜业务语义。
|
||
const fs = require('fs')
|
||
const path = require('path')
|
||
|
||
const ROOT = path.join(__dirname, '..')
|
||
const WRITE = process.argv.includes('--write')
|
||
const read = (rel) => fs.readFileSync(path.join(ROOT, rel), 'utf8')
|
||
const write = (rel, txt) => fs.writeFileSync(path.join(ROOT, rel), txt)
|
||
|
||
const report = []
|
||
const diffs = []
|
||
const diff = (what, cur, want, fix) => {
|
||
if (cur === want) { report.push(' · ' + what + ': ' + cur + '(一致)'); return false }
|
||
diffs.push(' ✗ ' + what + ': ' + cur + ' → ' + want)
|
||
if (WRITE && fix) fix()
|
||
return true
|
||
}
|
||
|
||
// ── 事实来源(唯一真源:文件系统 + 代码)──
|
||
const srcFiles = fs.readdirSync(path.join(ROOT, 'src')).filter((n) => n.endsWith('.js')).sort()
|
||
const srcCount = srcFiles.length
|
||
|
||
const buildPath = 'scripts/build.js'
|
||
let buildSrc = read(buildPath)
|
||
const buildList = [...buildSrc.matchAll(/^ '([a-z0-9-]+\.js)',$/gm)].map((m) => m[1])
|
||
|
||
// ── ① build.js 与 src 目录是否一一对应(顺序必须人工决定,只报告不一致)──
|
||
const missingInBuild = srcFiles.filter((f) => !buildList.includes(f))
|
||
const extraInBuild = buildList.filter((f) => !srcFiles.includes(f))
|
||
if (missingInBuild.length) report.push(' ✗ build.js 缺少: ' + missingInBuild.join(', ') + '(顺序需人工确定后插入)')
|
||
if (extraInBuild.length) report.push(' ✗ build.js 多出: ' + extraInBuild.join(', ') + '(src 里已不存在)')
|
||
if (!missingInBuild.length && !extraInBuild.length) report.push(' · build.js 与 src/ 一一对应:' + buildList.length + ' 个')
|
||
|
||
// ── ② sourceModules 顺序快照 == build.js ──
|
||
const orderPath = 'tests/fixtures/iwb-source-modules-order.json'
|
||
const orderSnapshot = JSON.parse(read(orderPath))
|
||
diff('顺序快照项数', orderSnapshot.length, buildList.length, () => {
|
||
write(orderPath, JSON.stringify(buildList, null, 2) + '\n')
|
||
})
|
||
if (JSON.stringify(orderSnapshot) !== JSON.stringify(buildList)) {
|
||
if (orderSnapshot.length === buildList.length) {
|
||
diffs.push(' ✗ 顺序快照内容与 build.js 顺序不同(可 --write 覆盖)')
|
||
if (WRITE) write(orderPath, JSON.stringify(buildList, null, 2) + '\n')
|
||
}
|
||
}
|
||
|
||
// ── ③ 耦合面快照:**不自行推导**(边界守卫的检测规则更严格,自行猜会误报)──
|
||
// 需要更新时跑:IWB_WRITE_BOUNDARY_SNAPSHOT=1 node tests/architecture-boundary-guard-test.js
|
||
const readsPath = 'tests/fixtures/iwb-app-global-reads.json'
|
||
const readsSnapshot = JSON.parse(read(readsPath))
|
||
const orderSnapshotLen = JSON.parse(read('tests/fixtures/iwb-source-modules-order.json')).length
|
||
report.push(' · 耦合面快照: ' + readsSnapshot.length + ' 项(更新用 IWB_WRITE_BOUNDARY_SNAPSHOT=1)')
|
||
|
||
// ── ④ 文档计数(架构文档 + CONTRIBUTING)──
|
||
const archPath = 'docs/standards/architecture-current-state.md'
|
||
let arch = read(archPath)
|
||
const archWant = [
|
||
[/现 \*\*\d+\*\* 个模块/g, '现 **' + srcCount + '** 个模块'],
|
||
[/\*\*\d+ 个模块/g, '**' + srcCount + ' 个模块'],
|
||
// 负向后视排除「顺序(**N** 项)」—— 否则会被本模式先改成耦合面数、再被下一条改回来,
|
||
// 净结果不变但 archChanged 被置真 ⇒ 永远误报"架构文档计数过时"(2026-11-07 修)
|
||
[/(?<!顺序)(\*\*\d+\*\* 项)/g, '(**' + readsSnapshot.length + '** 项)'],
|
||
[/顺序(\*\*\d+\*\* 项)/g, '顺序(**' + orderSnapshotLen + '** 项)'],
|
||
[/`sourceModules` \d+ 项/g, '`sourceModules` ' + buildList.length + ' 项'],
|
||
]
|
||
let archChanged = false
|
||
for (const [re, rep] of archWant) {
|
||
const before = arch
|
||
arch = arch.replace(re, rep)
|
||
if (arch !== before) archChanged = true
|
||
}
|
||
|
||
// ── ④b 行数棘轮硬数字(2026-11-24,P0-②)──
|
||
// 为什么单列一步:CONTRIBUTING / 架构文档里抄着 MAX_APP_JS_LINES 等上限,
|
||
// 此前无人校验,实测已漂 415 行。测量与改写规则在 scripts/ratchet-metrics.js
|
||
// (守卫 tests/architecture-facts-guard-test.js 用同一份规则断言,口径不会分叉)。
|
||
const rm = require('./ratchet-metrics')
|
||
const metrics = rm.measure()
|
||
const archRatchet = rm.rewriteDoc('arch', arch, metrics)
|
||
arch = archRatchet.text
|
||
if (archRatchet.changed.length) archChanged = true
|
||
|
||
if (archChanged) {
|
||
diffs.push(' ✗ 架构文档计数/棘轮数字过时(可 --write 自动修;棘轮项:' + (archRatchet.changed.join(', ') || '无') + ')')
|
||
if (WRITE) write(archPath, arch)
|
||
} else report.push(' · 架构文档计数与棘轮数字(一致)')
|
||
|
||
// 单测文件数:test:unit 里 node tests/*.js 的个数
|
||
const pkg = JSON.parse(read('package.json'))
|
||
// 与 tests/architecture-facts-guard-test.js 的计数口径保持一致(三个脚本合并去重)
|
||
const unitCount = [...new Set(((pkg.scripts['test:unit'] || '') + ' ' + (pkg.scripts.test || '') + ' ' + (pkg.scripts['test:facts'] || '')).match(/tests\/[a-z0-9-]+\.js/g) || [])].length
|
||
const contribPath = 'CONTRIBUTING.md'
|
||
let contrib = read(contribPath)
|
||
const contribRe = /test:unit {2,}\d+ 个单测文件/
|
||
const m = contrib.match(contribRe)
|
||
let contribChanged = false
|
||
if (m) {
|
||
const cur = Number(m[0].match(/(\d+) 个/)[1])
|
||
if (cur !== unitCount) {
|
||
contribChanged = true
|
||
diffs.push(' ✗ CONTRIBUTING 单测文件数: ' + cur + ' → ' + unitCount)
|
||
contrib = contrib.replace(contribRe, 'test:unit ' + unitCount + ' 个单测文件')
|
||
} else report.push(' · CONTRIBUTING 单测文件数: ' + unitCount + '(一致)')
|
||
}
|
||
// 棘轮上限三个数字(同一份规则,见 scripts/ratchet-metrics.js)
|
||
const contribRatchet = rm.rewriteDoc('contributing', contrib, metrics)
|
||
if (contribRatchet.changed.length) {
|
||
contribChanged = true
|
||
diffs.push(' ✗ CONTRIBUTING 棘轮上限过时:' + contribRatchet.changed.join(', '))
|
||
}
|
||
contrib = contribRatchet.text
|
||
if (contribChanged) { if (WRITE) write(contribPath, contrib) }
|
||
else report.push(' · CONTRIBUTING 棘轮上限(一致)')
|
||
|
||
// ── 输出 ──
|
||
console.log('\n事实同步器(' + (WRITE ? '写入模式' : 'dry-run') + ')\n')
|
||
report.forEach((l) => console.log(l))
|
||
if (diffs.length) {
|
||
console.log('\n需要同步:')
|
||
diffs.forEach((l) => console.log(l))
|
||
if (!WRITE) {
|
||
console.log('\n跑 `node scripts/sync-facts.js --write` 自动修可机械同步的部分。')
|
||
console.log('(build.js 顺序 / 耦合面新增 属需人工确认项,脚本只报告)')
|
||
process.exit(1)
|
||
}
|
||
console.log('\n✓ 已写入')
|
||
} else {
|
||
console.log('\n✓ 全部一致,无需同步')
|
||
}
|