#!/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 修) [/(? 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✓ 全部一致,无需同步') }