Files
image-workbench-desktop/scripts/check-dist.js
T
2026-12-03 12:34:01 +08:00

109 lines
5.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env node
'use strict'
const fs = require('fs')
const path = require('path')
const root = path.resolve(__dirname, '..')
const dist = path.join(root, 'dist')
function fail(reason) {
console.log('FAIL ' + reason)
process.exit(1)
}
function pass(msg) {
console.log('PASS ' + msg)
}
const versionJsonPath = path.join(dist, 'version.json')
if (!fs.existsSync(versionJsonPath)) fail('dist/version.json 不存在')
let meta
try {
meta = JSON.parse(fs.readFileSync(versionJsonPath, 'utf8'))
} catch (e) {
fail('dist/version.json 解析失败: ' + e.message)
}
// 1. dist/app.js 以 banner `/*! IWB <version> */` 开头,且 <version> 与 version.json.version 一致
const appPath = path.join(dist, 'app.js')
if (!fs.existsSync(appPath)) fail('dist/app.js 不存在')
const app = fs.readFileSync(appPath, 'utf8')
const expectedBanner = '/*! IWB ' + meta.version + ' */'
if (!app.startsWith(expectedBanner)) {
fail('dist/app.js 未以 banner 开头(期望: ' + JSON.stringify(expectedBanner) + ',实际前 48 字符: ' + JSON.stringify(app.slice(0, 48)) + ')')
}
pass('dist/app.js banner 与 version.json.version 一致: ' + expectedBanner)
// 2. dist/index.html 含 `app.js?v=`,恰好 3 个 <script,且不含 src/
const htmlPath = path.join(dist, 'index.html')
if (!fs.existsSync(htmlPath)) fail('dist/index.html 不存在')
const html = fs.readFileSync(htmlPath, 'utf8')
if (!html.includes('app.js?v=')) fail('dist/index.html 缺少 app.js?v=')
const scriptCount = (html.match(/<script/g) || []).length
if (scriptCount !== 3) fail('dist/index.html <script 数量应为 3,实际 ' + scriptCount)
// v3.26.109:index.html 里不允许出现内联 <style>/<script> —— Tauri 会给它们加 CSP nonce,
// CSP3 下 nonce 一出现 'unsafe-inline' 就失效,插件动态注入的 <style> 会被整段丢弃(v3.26.9 事故)。
const inlineStyle = /<style[^>]*>[\s\S]*?<\/style>/i.test(html)
const inlineScript = /<script(?![^>]*\bsrc=)[^>]*>[\s\S]*?<\/script>/i.test(html)
if (inlineStyle || inlineScript) fail('dist/index.html 含内联 <style>/<script>:会触发 CSP nonce,插件样式将全部失效')
if (!html.includes('iwb-boot-static')) fail('dist/index.html 缺少静态启动屏(局域网资料目录启动时的白屏兜底)')
if (html.includes('src/')) fail('dist/index.html 仍含 src/ 引用')
pass('dist/index.html 含 app.js?v=、静态启动屏标记、无内联 style/script,无 src/ 引用')
// 3. dist/app.js 含无引号标记子串,证明内联压缩 worker 字面量在 minify 后仍存活
if (!app.includes('self.onmessage=async function(ev)')) {
fail('dist/app.js 缺少 self.onmessage=async function(ev)(内联压缩 worker 未存活)')
}
pass('dist/app.js 保留内联压缩 worker 标记 self.onmessage=async function(ev)')
// 4. dist/src 目录不应存在
if (fs.existsSync(path.join(dist, 'src'))) fail('dist/src 目录仍存在')
pass('dist/src 目录不存在')
// 5. dist/version.json 可解析且 version/packageVersion/commit 均非空
for (const key of ['version', 'packageVersion', 'commit']) {
if (!meta[key] || String(meta[key]).length === 0) fail('dist/version.json.' + key + ' 为空')
}
pass('dist/version.json 的 version/packageVersion/commit 均非空')
// 6. PWA 安装面已移除(2026-11-26):根图标/manifest 不再进 dist,也不能再被 index.html 引用。
// sw.js 必须保留:它负责更新检测和 /__iwb_cache__ 图片直出,不是纯 PWA 安装面。
for (const f of ['favicon.svg', 'manifest.webmanifest', 'icon-192.png', 'icon-512.png', 'apple-touch-icon.png']) {
if (fs.existsSync(path.join(dist, f))) fail('dist/' + f + ' 不应存在(PWA 安装面已移除)')
if (html.includes(f)) fail('dist/index.html 仍引用已删除的 ' + f)
}
if (!fs.existsSync(path.join(dist, 'sw.js'))) fail('dist/sw.js 必须保留(更新检测 + 图片缓存)')
pass('PWA 图标/manifest 未进 dist,index.html 无引用;sw.js 保留')
// 7. dist/plugins 逐条与 plugins/index.json 的哈希一致(2026-12-03)。
// 动机:Windows 构建机 autocrlf 会把插件 .js 检出成 CRLF,SRI 对不上 → WebView2 拒绝执行该插件,
// 而构建本身不报错(style-source 真实踩过)。这条在 verify:ci 里跑,跨平台都能拦住。
const crypto = require('crypto')
const pluginsDist = path.join(dist, 'plugins')
const pluginsIdxPath = path.join(pluginsDist, 'index.json')
if (!fs.existsSync(pluginsIdxPath)) fail('dist/plugins/index.json 不存在')
const pidx = JSON.parse(fs.readFileSync(pluginsIdxPath, 'utf8'))
const sha256of = (p) => crypto.createHash('sha256').update(fs.readFileSync(p)).digest('hex')
let pin = 0
for (const ent of (pidx || [])) {
if (ent.file) {
const p = path.join(pluginsDist, ent.file)
if (!fs.existsSync(p)) fail('dist/plugins/' + ent.file + ' 不存在')
if (ent.integrity) {
const want = 'sha256-' + crypto.createHash('sha256').update(fs.readFileSync(p)).digest('base64')
if (want !== ent.integrity) fail('dist/plugins/' + ent.file + ' 的 SRI 与 index.json 不一致(换行符/字节被改动?)')
}
if (ent.sha256 && sha256of(p) !== ent.sha256) fail('dist/plugins/' + ent.file + ' 的 sha256 与 index.json 不一致')
pin++
} else if (ent.dir) {
for (const [rel, want] of Object.entries(ent.files || {})) {
const p = path.join(pluginsDist, ent.dir, rel)
if (!fs.existsSync(p)) fail('dist/plugins/' + ent.dir + '/' + rel + ' 不存在')
if (sha256of(p) !== want) fail('dist/plugins/' + ent.dir + '/' + rel + ' 的 sha256 与 index.json 不一致(换行符/字节被改动?)')
pin++
}
}
}
pass('dist/plugins 与 index.json 哈希一致(' + pin + ' 个文件)')