Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ensure-electron-runtime.mjs

188 lines5.6 KB

1import * as NodeFS from "node:fs";
2import * as NodeModule from "node:module";
3import * as NodeOS from "node:os";
4import * as NodePath from "node:path";
5import * as NodeChildProcess from "node:child_process";
6import { fileURLToPath } from "node:url";
7​
8const require = NodeModule.createRequire(import.meta.url);
9// oxlint-disable-next-line t3code/no-global-process-runtime -- Standalone repair script has no Effect runtime.
10const hostPlatform = NodeOS.platform();
11​
12function getPlatformPath() {
13 switch (hostPlatform) {
14 case "darwin":
15 return "Electron.app/Contents/MacOS/Electron";
16 case "freebsd":
17 case "openbsd":
18 case "linux":
19 return "electron";
20 case "win32":
21 return "electron.exe";
22 default:
23 throw new Error(`Electron builds are not available on platform: ${hostPlatform}`);
24 }
26​
27function ensureExecutable(filePath) {
28 if (hostPlatform !== "win32") {
29 NodeFS.chmodSync(filePath, 0o755);
30 }
32​
33function repairPathFile(electronDir, platformPath) {
34 const pathFile = NodePath.join(electronDir, "path.txt");
35 const currentPath = NodeFS.existsSync(pathFile)
36 ? NodeFS.readFileSync(pathFile, "utf8")
37 : undefined;
38​
39 if (currentPath !== platformPath) {
40 NodeFS.writeFileSync(pathFile, platformPath);
41 }
43​
44function getRequiredRuntimePaths(electronDir, platformPath) {
45 const paths = [NodePath.join(electronDir, "dist", platformPath)];
46​
47 if (hostPlatform === "darwin") {
48 paths.push(
49 NodePath.join(electronDir, "dist", "Electron.app", "Contents", "Info.plist"),
50 NodePath.join(
51 electronDir,
52 "dist",
53 "Electron.app",
54 "Contents",
55 "Frameworks",
56 "Electron Framework.framework",
57 "Electron Framework",
58 ),
59 );
60 }
61​
62 return paths;
64​
65function isMachO(filePath) {
66 if (hostPlatform !== "darwin") {
67 return true;
68 }
69​
70 const result = NodeChildProcess.spawnSync("file", ["-b", filePath], {
71 encoding: "utf8",
72 });
73​
74 return result.status === 0 && result.stdout.includes("Mach-O");
76​
77function missingRuntimePaths(electronDir, platformPath) {
78 return getRequiredRuntimePaths(electronDir, platformPath).filter((runtimePath) => {
79 return !NodeFS.existsSync(runtimePath);
80 });
82​
83function invalidRuntimePaths(electronDir, platformPath) {
84 if (hostPlatform !== "darwin") {
85 return [];
86 }
87​
88 return [
89 NodePath.join(electronDir, "dist", platformPath),
90 NodePath.join(
91 electronDir,
92 "dist",
93 "Electron.app",
94 "Contents",
95 "Frameworks",
96 "Electron Framework.framework",
97 "Electron Framework",
98 ),
99 ].filter((runtimePath) => NodeFS.existsSync(runtimePath) && !isMachO(runtimePath));
101​
102function runChecked(command, args, options = {}) {
103 const result = NodeChildProcess.spawnSync(command, args, {
104 cwd: options.cwd,
105 env: options.env,
106 encoding: "utf8",
107 stdio: "inherit",
108 windowsHide: true,
109 });
110​
111 if (result.status === 0) {
112 return;
113 }
114​
115 const cause = result.error ? `: ${result.error.message}` : "";
116 throw new Error(
117 `${command} ${args.join(" ")} failed with exit code ${result.status ?? "unknown"}${cause}`,
118 );
120​
121export function createElectronInstallInvocation(electronDir, env = process.env) {
122 const installScript = NodePath.join(electronDir, "install.js");
123 if (!NodeFS.existsSync(installScript)) {
124 throw new Error(`Electron installer script is missing: ${installScript}`);
125 }
126​
127 // This function is itself the recovery path for an intentionally skipped or
128 // incomplete Electron postinstall. Do not let a stale skip flag suppress the
129 // explicit repair attempt. Mirrors/proxies remain inherited.
130 const repairEnv = { ...env };
131 delete repairEnv.ELECTRON_SKIP_BINARY_DOWNLOAD;
132​
133 return {
134 command: process.execPath,
135 args: [installScript],
136 cwd: electronDir,
137 env: repairEnv,
138 };
140​
141function installElectronRuntime(electronDir) {
142 const invocation = createElectronInstallInvocation(electronDir);
143 runChecked(invocation.command, invocation.args, {
144 cwd: invocation.cwd,
145 env: invocation.env,
146 });
148​
149export function ensureElectronRuntime() {
150 const electronPackageJsonPath = require.resolve("electron/package.json");
151 const electronDir = NodePath.dirname(electronPackageJsonPath);
152 const platformPath = getPlatformPath();
153 const electronPath = NodePath.join(electronDir, "dist", platformPath);
154 const missingBeforeInstall = missingRuntimePaths(electronDir, platformPath);
155 const invalidBeforeInstall = invalidRuntimePaths(electronDir, platformPath);
156​
157 if (missingBeforeInstall.length > 0 || invalidBeforeInstall.length > 0) {
158 if (NodeFS.existsSync(NodePath.join(electronDir, "dist"))) {
159 NodeFS.rmSync(NodePath.join(electronDir, "dist"), { recursive: true, force: true });
160 }
161 NodeFS.rmSync(NodePath.join(electronDir, "path.txt"), { force: true });
162 installElectronRuntime(electronDir);
163 }
164​
165 const missingAfterInstall = missingRuntimePaths(electronDir, platformPath);
166 const invalidAfterInstall = invalidRuntimePaths(electronDir, platformPath);
167 if (missingAfterInstall.length > 0 || invalidAfterInstall.length > 0) {
168 throw new Error(
169 `Electron runtime is incomplete after install.\nMissing:\n${missingAfterInstall
170 .map((runtimePath) => `- ${runtimePath}`)
171 .join("\n")}\nInvalid:\n${invalidAfterInstall
172 .map((runtimePath) => `- ${runtimePath}`)
173 .join("\n")}`,
174 );
175 }
176​
177 ensureExecutable(electronPath);
178 repairPathFile(electronDir, platformPath);
179​
180 return electronPath;
182​
183const scriptPath = fileURLToPath(import.meta.url);
184​
185if (process.argv[1] && NodePath.resolve(process.argv[1]) === scriptPath) {
186 const electronPath = ensureElectronRuntime();
187 process.stdout.write(`${electronPath}\n`);