Files
monkey-ai-web/scripts/verify-build.mjs
T
zhanjunhao 92380ebab6 chore(tooling): 对齐协作规范与浏览器验收工具
验证:713 项单测、8 视口环境、代码及格式检查通过;未运行生产构建。
2026-11-17 14:48:50 +08:00

57 lines
2.2 KiB
JavaScript

/*
Copyright (C) 2025 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import { readFile, access } from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
export async function verifyBuild(directory) {
const root = path.resolve(directory);
const html = await readFile(path.join(root, 'index.html'), 'utf8');
// Inspect actual resource tags, not URLs embedded in comments or inline scripts.
const markup = html
.replace(/<!--[\s\S]*?-->/g, '')
.replace(/(<script\b[^>]*>)[\s\S]*?<\/script\s*>/gi, '$1</script>');
const tags = [
...markup.matchAll(/<(?:script|link|img|source)\b[^>]*>/gi),
].map((match) => match[0]);
const assets = tags
.flatMap((tag) => [...tag.matchAll(/\s(?:src|href)\s*=["']([^"']+)["']/g)])
.map((match) => match[1].split(/[?#]/)[0])
// The inline brand bootstrap resolves this server placeholder to /logo.svg.
.map((value) => (value === '__WL_BRAND_FAVICON__' ? '/logo.svg' : value))
.filter((value) => value && !/^(?:[a-z]+:|\/\/|#)/i.test(value));
if (!assets.some((value) => /\.js$/.test(value)))
throw new Error('Build has no JavaScript entry');
for (const asset of assets) {
const filename = path.resolve(root, asset.replace(/^\//, ''));
if (!filename.startsWith(root + path.sep))
throw new Error(`Build asset escapes output directory: ${asset}`);
await access(filename);
}
return { root, assets: assets.length };
}
if (
process.argv[1] &&
path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)
) {
console.log(await verifyBuild(process.argv[2] || 'dist'));
}