|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Verifies Electron's platform binary and repairs the install when GitHub |
| 4 | + * Actions leaves the npm package present but without a usable dist/path.txt. |
| 5 | + * |
| 6 | + * The repair path intentionally avoids @electron/get because that helper was |
| 7 | + * unreliable on the Node 24 Ubuntu runner during FEA-1543 CI remediation. |
| 8 | + */ |
| 9 | +import { spawnSync } from "node:child_process"; |
| 10 | +import { createHash } from "node:crypto"; |
| 11 | +import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs"; |
| 12 | +import { createRequire } from "node:module"; |
| 13 | +import { tmpdir } from "node:os"; |
| 14 | +import path from "node:path"; |
| 15 | + |
| 16 | +const appRequire = createRequire(import.meta.url); |
| 17 | +const electronPackageJsonPath = appRequire.resolve("electron/package.json"); |
| 18 | +const electronDir = path.dirname(electronPackageJsonPath); |
| 19 | +const electronRequire = createRequire(path.join(electronDir, "install.js")); |
| 20 | +const electronIndexPath = appRequire.resolve("electron"); |
| 21 | +const electronPackage = JSON.parse(readFileSync(electronPackageJsonPath, "utf8")); |
| 22 | + |
| 23 | +function getPlatformPath(platform) { |
| 24 | + const platformPaths = { |
| 25 | + darwin: "Electron.app/Contents/MacOS/Electron", |
| 26 | + freebsd: "electron", |
| 27 | + linux: "electron", |
| 28 | + mas: "Electron.app/Contents/MacOS/Electron", |
| 29 | + openbsd: "electron", |
| 30 | + win32: "electron.exe", |
| 31 | + }; |
| 32 | + const platformPath = platformPaths[platform]; |
| 33 | + if (platformPath == null) { |
| 34 | + throw new Error(`Electron builds are not available on platform: ${platform}`); |
| 35 | + } |
| 36 | + return platformPath; |
| 37 | +} |
| 38 | + |
| 39 | +function getArch(platform) { |
| 40 | + if (process.env.npm_config_arch) { |
| 41 | + return process.env.npm_config_arch; |
| 42 | + } |
| 43 | + if (platform !== "darwin" || process.platform !== "darwin" || process.arch !== "x64") { |
| 44 | + return process.arch; |
| 45 | + } |
| 46 | + |
| 47 | + const translated = spawnSync("sysctl", ["-in", "sysctl.proc_translated"], { encoding: "utf8" }); |
| 48 | + return translated.status === 0 && translated.stdout.trim() === "1" ? "arm64" : process.arch; |
| 49 | +} |
| 50 | + |
| 51 | +function verifyInstalledBinary() { |
| 52 | + delete appRequire.cache[electronIndexPath]; |
| 53 | + const electronBinary = appRequire("electron"); |
| 54 | + if (typeof electronBinary !== "string" || electronBinary.length === 0) { |
| 55 | + throw new Error("Electron module did not resolve to a binary path."); |
| 56 | + } |
| 57 | + if (!existsSync(electronBinary)) { |
| 58 | + throw new Error(`Electron binary is missing at ${electronBinary}`); |
| 59 | + } |
| 60 | + const binaryStats = statSync(electronBinary); |
| 61 | + if (!binaryStats.isFile() || binaryStats.size === 0) { |
| 62 | + throw new Error(`Electron binary is not a non-empty file at ${electronBinary}`); |
| 63 | + } |
| 64 | + return electronBinary; |
| 65 | +} |
| 66 | + |
| 67 | +function runChecked(command, args) { |
| 68 | + const result = spawnSync(command, args, { stdio: "inherit" }); |
| 69 | + if (result.error) { |
| 70 | + throw result.error; |
| 71 | + } |
| 72 | + if (result.status !== 0) { |
| 73 | + throw new Error(`${command} ${args.join(" ")} exited with status ${result.status}`); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +function repairElectronBinary() { |
| 78 | + const version = electronPackage.version; |
| 79 | + const platform = process.env.npm_config_platform || process.platform; |
| 80 | + const arch = getArch(platform); |
| 81 | + const platformPath = getPlatformPath(platform); |
| 82 | + const artifact = `electron-v${version}-${platform}-${arch}.zip`; |
| 83 | + const checksums = electronRequire("./checksums.json"); |
| 84 | + const expectedChecksum = checksums[artifact]; |
| 85 | + if (expectedChecksum == null) { |
| 86 | + throw new Error(`Missing Electron checksum for ${artifact}`); |
| 87 | + } |
| 88 | + |
| 89 | + const downloadDir = process.env.RUNNER_TEMP || path.join(tmpdir(), "closedloop-electron-binary"); |
| 90 | + mkdirSync(downloadDir, { recursive: true }); |
| 91 | + const zipPath = path.join(downloadDir, artifact); |
| 92 | + const downloadUrl = `https://github.com/electron/electron/releases/download/v${version}/${artifact}`; |
| 93 | + |
| 94 | + console.log(`Downloading ${artifact}`); |
| 95 | + runChecked("curl", ["--fail", "--location", "--retry", "3", "--output", zipPath, downloadUrl]); |
| 96 | + |
| 97 | + const actualChecksum = createHash("sha256").update(readFileSync(zipPath)).digest("hex"); |
| 98 | + if (actualChecksum !== expectedChecksum) { |
| 99 | + throw new Error(`Electron checksum mismatch for ${artifact}: expected ${expectedChecksum}, got ${actualChecksum}`); |
| 100 | + } |
| 101 | + |
| 102 | + const distDir = path.join(electronDir, "dist"); |
| 103 | + rmSync(distDir, { recursive: true, force: true }); |
| 104 | + mkdirSync(distDir, { recursive: true }); |
| 105 | + runChecked("unzip", ["-q", zipPath, "-d", distDir]); |
| 106 | + |
| 107 | + const extractedTypes = path.join(distDir, "electron.d.ts"); |
| 108 | + if (existsSync(extractedTypes)) { |
| 109 | + renameSync(extractedTypes, path.join(electronDir, "electron.d.ts")); |
| 110 | + } |
| 111 | + writeFileSync(path.join(electronDir, "path.txt"), platformPath); |
| 112 | +} |
| 113 | + |
| 114 | +try { |
| 115 | + try { |
| 116 | + const electronBinary = verifyInstalledBinary(); |
| 117 | + console.log(`Electron binary verified at ${electronBinary}`); |
| 118 | + } catch (verificationError) { |
| 119 | + console.log(`Electron binary verification failed: ${verificationError.message}`); |
| 120 | + repairElectronBinary(); |
| 121 | + const electronBinary = verifyInstalledBinary(); |
| 122 | + console.log(`Electron binary repaired and verified at ${electronBinary}`); |
| 123 | + } |
| 124 | +} catch (error) { |
| 125 | + console.error(error.stack || error); |
| 126 | + process.exitCode = 1; |
| 127 | +} |
0 commit comments