reconcile.mjs
6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { createAliases } from '../packages/vite-config/src/aliases.mjs';
/**
* 工程对账:不依赖 pnpm install,纯读磁盘把「配置漂移」翻出来。
*
* 检查六类最容易静默出错的事:
* 1. workspace:* 指向的包是否真的在 workspace 里
* 2. catalog: 引用的依赖是否在 pnpm-workspace.yaml 的 default catalog 里
* 3. Vite alias / tsconfig paths 的目标文件是否真实存在(否则 dev 报「无法解析」且看不出是路径算错)
* 4. 每个子应用的必需文件与端口是否齐备、有没有撞端口
* 5. verify 门禁链里是否仍包含测试(否则改了脚本就会静默不再跑测试)
* 6. mp lane 的根工具链边界在 eslint 与 prettier 两处是否同一口径
*/
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const problems = [];
const notes = [];
function collect(dir, filename) {
const out = [];
if (!fs.existsSync(dir)) return out;
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
if (['node_modules', '.git', 'dist', '.turbo', '.tmp'].includes(entry.name)) continue;
const full = path.join(dir, entry.name);
if (entry.isDirectory()) out.push(...collect(full, filename));
else if (entry.name === filename) out.push(full);
}
return out;
}
const pkgFiles = collect(ROOT, 'package.json');
const byName = new Map();
for (const file of pkgFiles) {
try {
const pkg = JSON.parse(fs.readFileSync(file, 'utf8'));
if (pkg.name) byName.set(pkg.name, { file, pkg });
} catch (err) {
problems.push(`package.json 解析失败 ${path.relative(ROOT, file)}: ${err.message}`);
}
}
// ---- 1 / 2:依赖声明 ----
const workspaceYaml = fs.readFileSync(path.join(ROOT, 'pnpm-workspace.yaml'), 'utf8');
const catalogKeys = new Set();
const catalogBlock = workspaceYaml.split('catalogs:')[1] ?? '';
for (const line of catalogBlock.split(/\r?\n/)) {
const m = /^\s{4}'?([^:\s']+)'?:\s*\S/.exec(line);
if (m) catalogKeys.add(m[1]);
}
for (const { file, pkg } of byName.values()) {
const rel = path.relative(ROOT, file).split(path.sep).join('/');
for (const section of ['dependencies', 'devDependencies', 'peerDependencies']) {
for (const [dep, version] of Object.entries(pkg[section] ?? {})) {
if (typeof version !== 'string') continue;
if (version.startsWith('workspace:') && !byName.has(dep)) {
problems.push(`${rel}: ${dep} 声明为 ${version},但 workspace 里没有这个包`);
}
if (version === 'catalog:' && !catalogKeys.has(dep)) {
problems.push(
`${rel}: ${dep} 写了 catalog:,但 pnpm-workspace.yaml 的 default catalog 没有它`,
);
}
}
}
}
notes.push(`workspace 包 ${byName.size} 个 / catalog 条目 ${catalogKeys.size} 个`);
// ---- 3:alias 目标 ----
const APPS = [
{ dir: 'apps/web/shell', kind: 'shell' },
{ dir: 'apps/web/subapps/system', kind: 'subapp' },
{ dir: 'apps/web/subapps/store', kind: 'subapp' },
];
for (const { dir, kind } of APPS) {
const appDir = path.join(ROOT, dir);
if (!fs.existsSync(appDir)) {
problems.push(`缺应用目录 ${dir}`);
continue;
}
for (const alias of createAliases({ appDir, kind })) {
const target = alias.replacement.replace(/\$1$/, '').replace(/[\\/]$/, '');
if (!fs.existsSync(target)) {
problems.push(`${dir}: alias ${alias.find} -> ${alias.replacement} 目标不存在`);
}
}
}
// ---- 4:子应用必需文件与端口 ----
// shell 与子应用的必需文件不同:主壳没有 App.tsx/wujie.d.ts,子应用没有 router/MicroApp。
const COMMON = [
'index.html',
'vite.config.ts',
'tsconfig.json',
'src/main.tsx',
'src/api/index.ts',
];
const SHELL_ONLY = [
'src/router.tsx',
'src/MicroApp.tsx',
'src/subapps.ts',
'src/subapp-props.ts',
'src/subapp-handshake.ts',
'src/layouts/BasicLayout.tsx',
'src/theme/design.css',
];
const SUBAPP_ONLY = ['src/App.tsx', 'src/wujie.d.ts', 'src/index.css', 'src/api/core/client.ts'];
const ports = new Map();
for (const { dir, kind } of APPS) {
const appDir = path.join(ROOT, dir);
const REQUIRED = [...COMMON, ...(kind === 'shell' ? SHELL_ONLY : SUBAPP_ONLY)];
for (const f of REQUIRED) {
if (!fs.existsSync(path.join(appDir, f))) problems.push(`${dir}: 缺 ${f}`);
}
const pkg = JSON.parse(fs.readFileSync(path.join(appDir, 'package.json'), 'utf8'));
const vite = fs.readFileSync(path.join(appDir, 'vite.config.ts'), 'utf8');
const portMatch = /port:\s*(\d+)/.exec(vite);
const declared = pkg.uj?.entry;
if (portMatch) {
const port = portMatch[1];
if (ports.has(port)) problems.push(`${dir} 与 ${ports.get(port)} 撞端口 ${port}`);
ports.set(port, dir);
if (declared && !declared.includes(`:${port}`)) {
problems.push(`${dir}: uj.entry=${declared} 与 vite.config.ts 的 port=${port} 不一致`);
}
}
if (pkg.uj?.route && !/^[\w-]+$/.test(pkg.uj.route)) {
problems.push(`${dir}: uj.route=${pkg.uj.route} 不是合法路由前缀`);
}
}
notes.push(`端口:${[...ports.entries()].map(([p, d]) => `${d}=${p}`).join(' / ')}`);
// ---- 5:verify 门禁链是否仍包含测试 ----
// 只认「根 test 走 turbo,verify 里出现 pnpm test」这两条:任意一条被改动,
// 声明了 test 脚本的包就会从 CI 里静默消失,而流水线依旧全绿。
const rootScripts = byName.get('@uj/admin')?.pkg.scripts ?? {};
const testScripts = [...byName.values()]
.filter(
({ file, pkg }) => pkg.scripts?.test && path.resolve(file) !== path.join(ROOT, 'package.json'),
)
.map(({ pkg }) => pkg.name);
if (!/\bturbo run test\b/.test(rootScripts.test ?? '')) {
problems.push(
`根 package.json 的 test 不是 "turbo run test",${testScripts.length} 个包的 test 脚本可能不在执行图里`,
);
}
if (!/\bpnpm test\b/.test(rootScripts.verify ?? '')) {
problems.push('根 package.json 的 verify 链里没有 pnpm test,CI 将不再跑任何测试');
}
notes.push(`test 脚本:${testScripts.join(' / ')}`);
// ---- 6:mp lane 的「不归根工具链管」边界只能有一套口径 ----
// eslint 与 prettier 各有各的 ignore;只改一边就会出现「lint 放行、format 判红」的分裂门禁。
const eslintIgnoresMp = /apps\/mp\/\*\*\/\*\.js/.test(
fs.readFileSync(path.join(ROOT, 'eslint.config.js'), 'utf8'),
);
const prettierIgnoresMp = /^(?:\*\*\/)?apps\/mp\/\*\*$/m.test(
fs.readFileSync(path.join(ROOT, '.prettierignore'), 'utf8'),
);
if (eslintIgnoresMp !== prettierIgnoresMp) {
problems.push(
`mp lane 边界口径不一致:eslint=${eslintIgnoresMp} / prettier=${prettierIgnoresMp},两份 ignore 必须一起改`,
);
}
for (const n of notes) console.log(`[reconcile] ${n}`);
for (const p of problems) console.error(` ✗ ${p}`);
console.log(`[reconcile] 问题 ${problems.length}`);
if (problems.length) process.exit(1);
console.log('[reconcile] 通过');