Files
image-workbench-desktop/scripts/check-dist.js
T

72 lines
3.2 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)
if (html.includes('src/')) fail('dist/index.html 仍含 src/ 引用')
pass('dist/index.html 含 app.js?v=,<script 数量为 3,无 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 保留')