release-verify.mjs
4.77 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
#!/usr/bin/env node
/**
* 发布自检(冒烟)—— 本地产物或线上站点跑同一套检查。
*
* 本地模式:临时起内置静态服务(/mfe/ 缺失即 404,对齐线上 nginx),跑完即退。
* 线上模式:--base https://<域名>,直接检查真实部署。
*
* 检查项(任一失败 → 退出码 1):
* 1. 站点根 index.html 可取、且指向版本化主壳目录(不是裸 /assets/)
* 2. mfe/manifest.json 可取、含 subapps、Cache-Control: no-cache
* 3. 清单里每个 subapp 的版本目录可取、返回 HTML
* 4. host 版本目录(若清单有)可取、返回 HTML
*/
import fs from 'node:fs';
import path from 'node:path';
import { startStaticServer } from './static-server.mjs';
import { ROOT, pathsOf, readReleaseConfig } from './release-core.mjs';
const args = process.argv.slice(2);
const getArg = (name) => {
const i = args.indexOf(`--${name}`);
return i >= 0 ? args[i + 1] : undefined;
};
async function check() {
let base = (getArg('base') ?? '').replace(/\/+$/, '');
let server = null;
if (!base) {
const config = readReleaseConfig();
const { outRoot } = pathsOf({}, config);
if (!fs.existsSync(path.join(outRoot, 'index.html'))) {
console.error(`✗ ${outRoot} 下没有 index.html,不是有效产物。先跑 release:all / release --app。`);
process.exit(1);
}
server = await startStaticServer(outRoot, { port: 0 });
base = server.url;
console.log(`[verify] 本地模式:${base}(产物根 ${path.relative(ROOT, outRoot)})`);
} else {
console.log(`[verify] 线上模式:${base}`);
}
const results = [];
const check = async (name, fn) => {
try { const msg = await fn(); results.push({ ok: true, name, msg }); }
catch (e) { results.push({ ok: false, name, msg: e.message }); }
};
const get = async (urlPath) => {
const res = await fetch(base + urlPath, {
signal: AbortSignal.timeout(8000),
headers: { 'cache-control': 'no-cache' },
});
return res;
};
const isHtml = (t) => /<!doctype html|<html[\s>]/i.test(t);
await check('站点根 index.html', async () => {
const res = await get('/index.html');
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const html = await res.text();
if (!isHtml(html)) throw new Error('返回的不是 HTML');
const refs = [...html.matchAll(/\b(?:src|href)="([^"]+)"/g)].map((m) => m[1]);
const hasVersioned = refs.some((u) => /\/mfe\/host\/[^/]+\/assets\//.test(u));
if (!hasVersioned) throw new Error('index.html 的脚本路径未指向 /mfe/host/<ver>/assets/(可能部署了旧结构)');
return `${refs.length} 个资源引用,指向版本化主壳`;
});
let manifest = null;
await check('mfe/manifest.json', async () => {
const res = await get('/mfe/manifest.json');
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const cc = res.headers.get('cache-control') ?? '';
if (!/no-cache|no-store|max-age=0/i.test(cc)) throw new Error(`Cache-Control 不是 no-cache(${cc || '缺失'}),发布会被缓存卡住`);
manifest = await res.json();
if (!manifest?.subapps || typeof manifest.subapps !== 'object') throw new Error('缺少 subapps 字段');
return `${Object.keys(manifest.subapps).length} 个子应用,no-cache 已配置`;
});
if (manifest) {
for (const [name, entry] of Object.entries(manifest.subapps)) {
await check(`子应用 ${name}@${entry.version ?? '?'}`, async () => {
const urlPath = new URL(entry.url, 'http://x').pathname;
const res = await get(urlPath);
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const text = await res.text();
if (!isHtml(text)) throw new Error('版本目录未返回 HTML(目录不存在或上传不全)');
return '版本目录可访问';
});
}
if (manifest.host?.url) {
await check(`主壳@${manifest.host.version ?? '?'}`, async () => {
const urlPath = new URL(manifest.host.url, 'http://x').pathname;
const res = await get(urlPath);
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const text = await res.text();
if (!isHtml(text)) throw new Error('主壳版本目录未返回 HTML');
return '版本目录可访问';
});
}
}
console.log('');
for (const r of results) {
console.log(` ${r.ok ? '✓' : '✗'} ${r.name.padEnd(28)} ${r.msg}`);
}
console.log('');
if (server) server.server.close();
const failed = results.filter((r) => !r.ok);
if (failed.length) {
console.error(`✗ ${failed.length}/${results.length} 项未通过。`);
process.exit(1);
}
console.log(`✓ ${results.length}/${results.length} 项全过。`);
}
check().catch((err) => {
console.error(`✗ 自检失败:${err.message}`);
process.exit(1);
});