|
| 1 | +/** |
| 2 | + * 离线安装包打包脚本:把 src-ztools 打成 ZTools 可离线安装的 .zpx。 |
| 3 | + * |
| 4 | + * ZPX 格式与宿主 src/main/utils/zpxArchive.ts 的 packZpx 一致: |
| 5 | + * zpx = brotli(quality 5)( asar( src-ztools ) ) |
| 6 | + * |
| 7 | + * 打包后自动做产物内容检查(与 test/host-load-check.mjs 同一标准): |
| 8 | + * - 必需文件:plugin.json、preload/dist/services.js、dist/index.html |
| 9 | + * - 违禁条目:test/、node_modules/、.throttle-bench*、.dav-root* |
| 10 | + * |
| 11 | + * 用法:npm run build && node scripts/pack-zpx.mjs |
| 12 | + * 产物:release/webdav-sync-<version>.zpx |
| 13 | + */ |
| 14 | +import fs from 'node:fs' |
| 15 | +import fsp from 'node:fs/promises' |
| 16 | +import os from 'node:os' |
| 17 | +import path from 'node:path' |
| 18 | +import { pipeline } from 'node:stream/promises' |
| 19 | +import { fileURLToPath } from 'node:url' |
| 20 | +import { createBrotliCompress, constants as zlibConstants } from 'node:zlib' |
| 21 | + |
| 22 | +const HERE = path.dirname(fileURLToPath(import.meta.url)) |
| 23 | +const PLUGIN = path.join(HERE, '..') |
| 24 | +const SRC = path.join(PLUGIN, 'src-ztools') |
| 25 | +const OUT_DIR = path.join(PLUGIN, 'release') |
| 26 | + |
| 27 | +// @electron/asar 优先用本地依赖,缺失时借用宿主仓库(与 host-load-check.mjs 相同的候选顺序) |
| 28 | +const HOST_CANDIDATES = [ |
| 29 | + path.join(PLUGIN, '..', '..', 'ZTools'), |
| 30 | + 'D:\\workspace\\self\\ZTools', |
| 31 | +] |
| 32 | + |
| 33 | +async function resolveAsar() { |
| 34 | + try { |
| 35 | + return await import('@electron/asar') |
| 36 | + } catch {} |
| 37 | + for (const host of HOST_CANDIDATES) { |
| 38 | + const pkgDir = path.join(host, 'node_modules', '@electron', 'asar') |
| 39 | + const pkgJsonPath = path.join(pkgDir, 'package.json') |
| 40 | + if (!fs.existsSync(pkgJsonPath)) continue |
| 41 | + // 按该包自己的 exports / main 解析入口,避免依赖特定版本的内部分支路径 |
| 42 | + const pkg = JSON.parse(fs.readFileSync(pkgJsonPath, 'utf-8')) |
| 43 | + const entry = typeof pkg.exports === 'string' ? pkg.exports : pkg.main |
| 44 | + if (!entry) continue |
| 45 | + return import(pathToFileUrl(pkgDir, entry)) |
| 46 | + } |
| 47 | + throw new Error('找不到 @electron/asar:请先 npm i -D @electron/asar,或确认宿主仓库 ZTools 已检出') |
| 48 | +} |
| 49 | + |
| 50 | +function pathToFileUrl(p, entry) { |
| 51 | + return 'file:///' + path.join(p, entry).replace(/\\/g, '/') |
| 52 | +} |
| 53 | + |
| 54 | +async function main() { |
| 55 | + const asar = await resolveAsar() |
| 56 | + const pluginConfig = JSON.parse(await fsp.readFile(path.join(SRC, 'plugin.json'), 'utf-8')) |
| 57 | + const version = pluginConfig.version |
| 58 | + if (!version) throw new Error('plugin.json 缺少 version') |
| 59 | + |
| 60 | + // 构建产物前置检查:缺了就提示先跑构建,避免打出残缺包 |
| 61 | + const required = ['plugin.json', 'logo.png', 'dist/index.html', 'preload/dist/services.js'] |
| 62 | + const missing = required.filter((r) => !fs.existsSync(path.join(SRC, r))) |
| 63 | + if (missing.length) { |
| 64 | + throw new Error(`缺少构建产物:${missing.join(', ')} —— 请先执行 npm run build`) |
| 65 | + } |
| 66 | + |
| 67 | + await fsp.mkdir(OUT_DIR, { recursive: true }) |
| 68 | + const outPath = path.join(OUT_DIR, `webdav-sync-${version}.zpx`) |
| 69 | + |
| 70 | + // 1) src-ztools → asar(宿主 packZpx 同款 createPackage) |
| 71 | + const tempAsar = path.join(os.tmpdir(), `zpx-pack-${Date.now()}-${Math.random().toString(36).slice(2, 8)}.asar`) |
| 72 | + let entryCount = 0 |
| 73 | + try { |
| 74 | + await asar.createPackage(SRC, tempAsar) |
| 75 | + |
| 76 | + // 2) 产物内容检查:必需文件齐全、无仓库资产混入 |
| 77 | + asar.uncache(tempAsar) |
| 78 | + const entries = asar |
| 79 | + .listPackage(tempAsar, { isPack: false }) |
| 80 | + .map((p) => p.replace(/\\/g, '/').replace(/^\/+/, '')) |
| 81 | + const forbidden = entries.filter((e) => |
| 82 | + /(^|\/)(test|node_modules)(\/|$)|\.throttle-bench|\.dav-root/.test(e) |
| 83 | + ) |
| 84 | + const missingInAsar = required.filter((r) => !entries.includes(r)) |
| 85 | + if (forbidden.length || missingInAsar.length) { |
| 86 | + if (forbidden.length) console.error('违禁条目样例:', forbidden.slice(0, 5).join(' | ')) |
| 87 | + if (missingInAsar.length) console.error('缺失文件:', missingInAsar.join(', ')) |
| 88 | + throw new Error('打包产物内容检查未通过') |
| 89 | + } |
| 90 | + entryCount = entries.length |
| 91 | + |
| 92 | + // 3) asar → brotli(quality 5) → .zpx |
| 93 | + await pipeline( |
| 94 | + fs.createReadStream(tempAsar), |
| 95 | + createBrotliCompress({ params: { [zlibConstants.BROTLI_PARAM_QUALITY]: 5 } }), |
| 96 | + fs.createWriteStream(outPath) |
| 97 | + ) |
| 98 | + } finally { |
| 99 | + await fsp.rm(tempAsar, { force: true }) |
| 100 | + } |
| 101 | + |
| 102 | + const size = (await fsp.stat(outPath)).size |
| 103 | + console.log(`✅ ${outPath} (${(size / 1024).toFixed(1)} KB, ${entryCount} 个文件, v${version})`) |
| 104 | +} |
| 105 | + |
| 106 | +main().catch((e) => { |
| 107 | + console.error('❌ 打包失败:', e && e.message) |
| 108 | + process.exit(1) |
| 109 | +}) |
0 commit comments