import { access, rename, rm } from "node:fs/promises"; export const ASAR_UNPACK_NATIVE_GLOB = "*.{node,dll,dylib,exe}"; async function pathExists(path) { try { await access(path); return true; } catch { return false; } } function normalizeAsarEntryPath(entry) { const normalized = entry.trim().replaceAll("\\", "/"); return normalized.startsWith("/") ? normalized.slice(1) : normalized; } /** * 从 `asar list --is-pack` 的输出里取出「存在 unpack 文件」的包目录。 * * 修复点:afterPack 会用 `asar pack` 重写 app.asar,而 `--unpack-dir` 是单值选项 * (commander 只保留最后一次),并且本版 @electron/asar 下 `**` 形式的 glob 完全 * 不生效(`*` 已能跨目录匹配)。 * * 旧实现固定只传 `node_modules/node-pty/prebuilds/`,于是重写后: * - electron-builder 原本整包 unpack 的依赖(如 ssh2)被重新塞回 archive; * - 走 sourcemap 清理那次重写时连 targetPlatformKey 都没有,会退化成 * `node-pty/prebuilds/undefined`,macOS 上不带扩展名的 `spawn-helper` 因此被 pack。 * * 这里改为先把当前 archive 的 unpack 布局读出来,重写时原样保留,避免「重写一次 * 就静默丢一次布局」。返回的是包级目录(如 `node_modules/jszip`),整体 unpack * 比逐文件 unpack 更稳,代价只是 sidecar 稍大。 */ export function resolveUnpackPackageDirsFromAsarList(asarListOutput) { const packageDirs = new Set(); for (const line of asarListOutput.split("\n")) { const match = /^unpack\s*:\s*(.+)$/.exec(line.trim()); if (!match) { continue; } const segments = normalizeAsarEntryPath(match[1]).split("/"); if (segments[0] !== "node_modules" || !segments[1]) { continue; } // 作用域包占两段:node_modules/@scope/name const packageLength = segments[1].startsWith("@") ? 3 : 2; if (segments.length < packageLength) { continue; } packageDirs.add(segments.slice(0, packageLength).join("/")); } return [...packageDirs].sort(); } export function createAppAsarPackArgs({ sourceDir, destinationPath, unpackDirs = [] }) { const args = ["pack", sourceDir, destinationPath, "--unpack", ASAR_UNPACK_NATIVE_GLOB]; if (unpackDirs.length > 0) { // `--unpack-dir` 只能接收一个表达式,用 brace glob 一次覆盖多个目录。 args.push("--unpack-dir", `{${unpackDirs.join(",")}}`); } return args; } export async function replaceAppAsarFromStaging({ sourceDir, appAsarPath, unpackDirs = [], runAsarCommand, }) { const candidateAsarPath = `${appAsarPath}.next`; const candidateUnpackedPath = `${candidateAsarPath}.unpacked`; const unpackedPath = `${appAsarPath}.unpacked`; await Promise.all([ rm(candidateAsarPath, { force: true, recursive: true }), rm(candidateUnpackedPath, { force: true, recursive: true }), ]); try { runAsarCommand( createAppAsarPackArgs({ sourceDir, destinationPath: candidateAsarPath, unpackDirs, }), ); if (!(await pathExists(candidateAsarPath)) || !(await pathExists(candidateUnpackedPath))) { // CI 的 TMPDIR 位于隐藏目录 `.tmp`。旧 glob 含 `**/`,@electron/asar 用绝对路径 // 匹配时不会跨过隐藏目录,导致 native 被写回 asar,同时遗留旧 unpacked 形成物理双份。 throw new Error(`重打包结果缺少 app.asar 或 app.asar.unpacked: ${candidateAsarPath}`); } // 先完整生成候选文件,再替换旧 archive 和 sidecar;不会把上一次打包的跨平台 native 留在 unpacked。 await rm(unpackedPath, { force: true, recursive: true }); await rename(candidateUnpackedPath, unpackedPath); await rm(appAsarPath, { force: true }); await rename(candidateAsarPath, appAsarPath); } finally { await Promise.all([ rm(candidateAsarPath, { force: true, recursive: true }), rm(candidateUnpackedPath, { force: true, recursive: true }), ]); } }