windows-update-publisher.mjs 1.2 KB

12345678910111213141516171819202122232425262728
  1. import { readFile, writeFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import { parse, stringify } from "yaml";
  4. import { readWindowsAuthenticodeSignature } from "./powershell-command.mjs";
  5. export async function writeWindowsUpdatePublisher({
  6. appOutDir,
  7. executableName,
  8. readSignature = readWindowsAuthenticodeSignature,
  9. }) {
  10. // 当前 electron-updater 缺少 publisherName 时会跳过自定义校验。
  11. // 保留签名身份以激活运行时的有效性检查;它不再作为 DN 白名单。
  12. const signature = await readSignature(join(appOutDir, `${executableName}.exe`));
  13. if (
  14. signature.status !== 0 ||
  15. typeof signature.subject !== "string" ||
  16. !signature.subject.trim()
  17. ) {
  18. throw new Error("Windows update publisher requires a valid signed application certificate");
  19. }
  20. const configPath = join(appOutDir, "resources", "app-update.yml");
  21. const config = parse(await readFile(configPath, "utf8"));
  22. if (!config || typeof config !== "object" || Array.isArray(config) || !config.provider) {
  23. throw new Error("Windows update configuration is missing a provider");
  24. }
  25. config.publisherName = [signature.subject.trim()];
  26. await writeFile(configPath, stringify(config), "utf8");
  27. }