app-asar-repack.mjs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { access, rename, rm } from "node:fs/promises";
  2. export const ASAR_UNPACK_NATIVE_GLOB = "*.{node,dll,dylib,exe}";
  3. async function pathExists(path) {
  4. try {
  5. await access(path);
  6. return true;
  7. } catch {
  8. return false;
  9. }
  10. }
  11. function normalizeAsarEntryPath(entry) {
  12. const normalized = entry.trim().replaceAll("\\", "/");
  13. return normalized.startsWith("/") ? normalized.slice(1) : normalized;
  14. }
  15. /**
  16. * 从 `asar list --is-pack` 的输出里取出「存在 unpack 文件」的包目录。
  17. *
  18. * 修复点:afterPack 会用 `asar pack` 重写 app.asar,而 `--unpack-dir` 是单值选项
  19. * (commander 只保留最后一次),并且本版 @electron/asar 下 `**` 形式的 glob 完全
  20. * 不生效(`*` 已能跨目录匹配)。
  21. *
  22. * 旧实现固定只传 `node_modules/node-pty/prebuilds/<targetPlatformKey>`,于是重写后:
  23. * - electron-builder 原本整包 unpack 的依赖(如 ssh2)被重新塞回 archive;
  24. * - 走 sourcemap 清理那次重写时连 targetPlatformKey 都没有,会退化成
  25. * `node-pty/prebuilds/undefined`,macOS 上不带扩展名的 `spawn-helper` 因此被 pack。
  26. *
  27. * 这里改为先把当前 archive 的 unpack 布局读出来,重写时原样保留,避免「重写一次
  28. * 就静默丢一次布局」。返回的是包级目录(如 `node_modules/jszip`),整体 unpack
  29. * 比逐文件 unpack 更稳,代价只是 sidecar 稍大。
  30. */
  31. export function resolveUnpackPackageDirsFromAsarList(asarListOutput) {
  32. const packageDirs = new Set();
  33. for (const line of asarListOutput.split("\n")) {
  34. const match = /^unpack\s*:\s*(.+)$/.exec(line.trim());
  35. if (!match) {
  36. continue;
  37. }
  38. const segments = normalizeAsarEntryPath(match[1]).split("/");
  39. if (segments[0] !== "node_modules" || !segments[1]) {
  40. continue;
  41. }
  42. // 作用域包占两段:node_modules/@scope/name
  43. const packageLength = segments[1].startsWith("@") ? 3 : 2;
  44. if (segments.length < packageLength) {
  45. continue;
  46. }
  47. packageDirs.add(segments.slice(0, packageLength).join("/"));
  48. }
  49. return [...packageDirs].sort();
  50. }
  51. export function createAppAsarPackArgs({ sourceDir, destinationPath, unpackDirs = [] }) {
  52. const args = ["pack", sourceDir, destinationPath, "--unpack", ASAR_UNPACK_NATIVE_GLOB];
  53. if (unpackDirs.length > 0) {
  54. // `--unpack-dir` 只能接收一个表达式,用 brace glob 一次覆盖多个目录。
  55. args.push("--unpack-dir", `{${unpackDirs.join(",")}}`);
  56. }
  57. return args;
  58. }
  59. export async function replaceAppAsarFromStaging({
  60. sourceDir,
  61. appAsarPath,
  62. unpackDirs = [],
  63. runAsarCommand,
  64. }) {
  65. const candidateAsarPath = `${appAsarPath}.next`;
  66. const candidateUnpackedPath = `${candidateAsarPath}.unpacked`;
  67. const unpackedPath = `${appAsarPath}.unpacked`;
  68. await Promise.all([
  69. rm(candidateAsarPath, { force: true, recursive: true }),
  70. rm(candidateUnpackedPath, { force: true, recursive: true }),
  71. ]);
  72. try {
  73. runAsarCommand(
  74. createAppAsarPackArgs({
  75. sourceDir,
  76. destinationPath: candidateAsarPath,
  77. unpackDirs,
  78. }),
  79. );
  80. if (!(await pathExists(candidateAsarPath)) || !(await pathExists(candidateUnpackedPath))) {
  81. // CI 的 TMPDIR 位于隐藏目录 `.tmp`。旧 glob 含 `**/`,@electron/asar 用绝对路径
  82. // 匹配时不会跨过隐藏目录,导致 native 被写回 asar,同时遗留旧 unpacked 形成物理双份。
  83. throw new Error(`重打包结果缺少 app.asar 或 app.asar.unpacked: ${candidateAsarPath}`);
  84. }
  85. // 先完整生成候选文件,再替换旧 archive 和 sidecar;不会把上一次打包的跨平台 native 留在 unpacked。
  86. await rm(unpackedPath, { force: true, recursive: true });
  87. await rename(candidateUnpackedPath, unpackedPath);
  88. await rm(appAsarPath, { force: true });
  89. await rename(candidateAsarPath, appAsarPath);
  90. } finally {
  91. await Promise.all([
  92. rm(candidateAsarPath, { force: true, recursive: true }),
  93. rm(candidateUnpackedPath, { force: true, recursive: true }),
  94. ]);
  95. }
  96. }