check-boundaries.mjs
4.93 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
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
/**
* 架构边界闸门(CI 必跑)。
*
* 内核(.kernel/)与业务(apps/*)物理分离后,本脚本从磁盘事实复查硬约束:
* 【业务子应用】不得:引用 @venus/platform-core(host 独占运行时)/ 出现 PEM 虚拟模块或签名密钥
* env / vite.config 挂 /api 代理或读密钥 / 直接 import wujie core / 运行时 import axios /
* 跨子应用依赖 @venus/web-* / 自行落地文件下载(下载中心唯一出口)。
*
* 这些约束任何一条破了,症状都不是编译错误,而是「登录 10011」「两份签名状态」
* 「两份 Context」这类运行时故障,必须静态拦住。
*
* ROOT 从 .kernel/scripts 上溯两级到仓库根;扫描根是 apps/*(业务区)。
*/
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..');
const SUBAPPS_ROOT = path.join(ROOT, 'apps');
const errors = [];
const warnings = [];
const RULES = [
{
id: 'host-runtime',
re: /@venus\/platform-core/,
message: '引用了 host 独占运行时 @venus/platform-core;请改用 @venus/platform-sdk',
},
{
id: 'virtual-key',
re: /virtual:(client-private-key|server-public-key)/,
message: '引用了 PEM 虚拟模块;子应用构建不得注入密钥',
},
{
id: 'secret-env',
re: /VITE_(CLIENT_PRIVATE_KEY|SERVER_PUBLIC_KEY|API_TARGET|SIGN_ENABLED|STRICT_VERIFY)/,
message: '读取了签名/后端环境变量;这些只属于 shell',
},
{
id: 'wujie-core',
re: /from\s+['"]wujie['"]/,
message: '直接 import wujie core;子应用应只用 window.$wujie.bus / props',
},
{
id: 'axios-runtime',
re: /^import\s+(?!type\s)[^;]*from\s+['"]axios['"]/m,
message: '运行时 import axios(type-only 允许);请求必须经 props.request',
},
{
id: 'cross-subapp',
re: /@venus\/web-/,
message: '引用了主壳或另一个子应用的包;业务域之间禁止直接依赖',
},
{
id: 'download-direct',
re: /\bcreateObjectURL\s*\(|\bwindow\.open\s*\(|\.download\s*=\s*[^=]|\ba\.click\s*\(/,
message:
'子应用自行落地下载;所有下载必须经 @venus/platform-sdk 的 downloadCenter 登记到主壳下载中心',
},
];
function collect(dir, exts) {
const out = [];
if (!fs.existsSync(dir)) return out;
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
if (entry.name === 'node_modules' || entry.name === 'dist') continue;
const full = path.join(dir, entry.name);
if (entry.isDirectory()) out.push(...collect(full, exts));
else if (exts.some((e) => entry.name.endsWith(e))) out.push(full);
}
return out;
}
const subappDirs = fs.existsSync(SUBAPPS_ROOT)
? fs
.readdirSync(SUBAPPS_ROOT, { withFileTypes: true })
.filter((e) => e.isDirectory() && fs.existsSync(path.join(SUBAPPS_ROOT, e.name, 'package.json')))
.map((e) => path.join(SUBAPPS_ROOT, e.name))
: [];
for (const appDir of subappDirs) {
const files = collect(path.join(appDir, 'src'), ['.ts', '.tsx']);
const viteConfig = path.join(appDir, 'vite.config.ts');
for (const file of [...files, ...(fs.existsSync(viteConfig) ? [viteConfig] : [])]) {
const text = fs.readFileSync(file, 'utf8');
const rel = path.relative(ROOT, file).split(path.sep).join('/');
for (const rule of RULES) {
if (rule.re.test(text)) errors.push(`[${rule.id}] ${rel}: ${rule.message}`);
}
}
for (const file of collect(path.join(appDir, 'src'), ['.css'])) {
const text = fs.readFileSync(file, 'utf8');
const rel = path.relative(ROOT, file).split(path.sep).join('/');
const bareAnt = [...text.matchAll(/(^|\n)\s*([^{}]+)\{/g)].some((m) =>
m[2].split(',').some((part) => {
const chain = part.trim().split(/\s+|>|\+|~/).filter(Boolean);
return chain.length > 0 && chain.every((s) => /^\.ant-/.test(s));
}),
);
if (bareAnt) {
warnings.push(`[css] ${rel}: 含裸 .ant-* 选择器,确认未覆盖组件内部几何属性`);
}
}
}
// 反向检查:只有内核 shell 允许读 PEM 虚拟模块。
const shellConfig = path.join(ROOT, '.kernel', 'apps', 'shell', 'vite.config.ts');
if (fs.existsSync(shellConfig)) {
const text = fs.readFileSync(shellConfig, 'utf8');
if (!text.includes('createShellConfig')) {
errors.push('[shell] vite.config.ts 未使用 createShellConfig,PEM 虚拟模块可能缺失');
}
} else {
errors.push('[shell] 缺少 .kernel/apps/shell/vite.config.ts');
}
console.log(`[boundaries] 扫描 ${subappDirs.length} 个业务子应用(apps/*)`);
for (const w of warnings) console.warn(` ⚠ ${w}`);
for (const e of errors) console.error(` ✗ ${e}`);
console.log(`[boundaries] 错误 ${errors.length} / 告警 ${warnings.length}`);
if (errors.length) {
console.error('[boundaries] 架构边界被破坏,请修复后再提交。');
process.exit(1);
}
console.log('[boundaries] 通过');