patch-nsis-install-section.mjs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { readFile, writeFile } from "node:fs/promises";
  2. import { writeFileSync } from "node:fs";
  3. const PATCH_MARKER = "; zcode-installer-details-v1";
  4. function optionalMacro(name) {
  5. return [`!ifmacrodef ${name}`, ` !insertmacro ${name}`, "!endif"].join("\n");
  6. }
  7. function replaceRequired(source, needle, replacement, label) {
  8. const index = source.indexOf(needle);
  9. if (index === -1) {
  10. throw new Error(`electron-builder installSection.nsh 缺少预期锚点:${label}`);
  11. }
  12. return `${source.slice(0, index)}${replacement}${source.slice(index + needle.length)}`;
  13. }
  14. /**
  15. * 给 electron-builder 的安装段补上可见阶段,并开启 NSIS 内置逐文件详情。
  16. *
  17. * 这里不复制整份上游模板,避免 electron-builder 升级时静默带入旧模板;每次打包
  18. * 都必须命中下面的结构锚点,模板结构变化会立即失败并提醒维护者重新对齐。
  19. */
  20. export function patchNsisInstallSectionSource(source) {
  21. const sourceEol = source.includes("\r\n") ? "\r\n" : "\n";
  22. let patched = source.replaceAll("\r\n", "\n").replaceAll("\r", "\n");
  23. if (patched.includes(PATCH_MARKER)) {
  24. return source;
  25. }
  26. const detailsAnchor = "${IfNot} ${Silent}\n SetDetailsPrint none\n${endif}";
  27. patched = replaceRequired(
  28. patched,
  29. detailsAnchor,
  30. [
  31. PATCH_MARKER,
  32. "# 详情区需要同时显示 electron-builder 的 File 条目和项目阶段。",
  33. "${IfNot} ${Silent}",
  34. " SetDetailsPrint listonly",
  35. "${endif}",
  36. optionalMacro("customInstallSectionStarted"),
  37. ].join("\n"),
  38. "SetDetailsPrint none",
  39. );
  40. const cleanupStartAnchor =
  41. "!insertmacro uninstallOldVersion SHELL_CONTEXT\n!insertmacro handleUninstallResult SHELL_CONTEXT";
  42. patched = replaceRequired(
  43. patched,
  44. cleanupStartAnchor,
  45. `${optionalMacro("customInstallCleanupStarted")}\n${cleanupStartAnchor}`,
  46. "SHELL_CONTEXT cleanup",
  47. );
  48. const cleanupEndAnchor =
  49. '${if} $installMode == "all"\n !insertmacro uninstallOldVersion HKEY_CURRENT_USER\n !insertmacro handleUninstallResult HKEY_CURRENT_USER\n${endIf}';
  50. patched = replaceRequired(
  51. patched,
  52. cleanupEndAnchor,
  53. `${cleanupEndAnchor}\n${optionalMacro("customInstallCleanupCompleted")}`,
  54. "HKEY_CURRENT_USER cleanup",
  55. );
  56. const extractAnchor = "!insertmacro installApplicationFiles";
  57. patched = replaceRequired(
  58. patched,
  59. extractAnchor,
  60. `${optionalMacro("customInstallExtractStarted")}\n${extractAnchor}\n${optionalMacro("customInstallExtractCompleted")}`,
  61. "application extraction",
  62. );
  63. const shortcutsAnchor =
  64. "!insertmacro addStartMenuLink $keepShortcuts\n!insertmacro addDesktopLink $keepShortcuts";
  65. patched = replaceRequired(
  66. patched,
  67. shortcutsAnchor,
  68. `${optionalMacro("customInstallShortcutsStarted")}\n${shortcutsAnchor}\n${optionalMacro("customInstallShortcutsCompleted")}`,
  69. "shortcut installation",
  70. );
  71. return sourceEol === "\r\n" ? patched.replaceAll("\n", "\r\n") : patched;
  72. }
  73. export async function patchNsisInstallSectionFile(filePath) {
  74. const source = await readFile(filePath, "utf8");
  75. const patched = patchNsisInstallSectionSource(source);
  76. if (patched === source) {
  77. return { changed: false, originalSource: null };
  78. }
  79. await writeFile(filePath, patched, "utf8");
  80. return { changed: true, originalSource: source };
  81. }
  82. export function restoreNsisInstallSectionFileSync({ filePath, originalSource }) {
  83. if (originalSource !== null) {
  84. writeFileSync(filePath, originalSource, "utf8");
  85. }
  86. }
  87. export { PATCH_MARKER };