Open History to see the last commit
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";
6
7const require = NodeModule.createRequire(import.meta.url);
8// oxlint-disable-next-line t3code/no-global-process-runtime -- Standalone repair script has no Effect runtime.
9const hostPlatform = NodeOS.platform();
10// oxlint-disable-next-line t3code/no-global-process-runtime -- Standalone repair script has no Effect runtime.
11const hostArch = NodeOS.arch();
12
13function getPlatformPath() {
14 switch (hostPlatform) {
15 case "darwin":
16 return "Electron.app/Contents/MacOS/Electron";
17 case "freebsd":
18 case "openbsd":
19 case "linux":
20 return "electron";
21 case "win32":
22 return "electron.exe";
23 default:
24 throw new Error(`Electron builds are not available on platform: ${hostPlatform}`);
25 }
26}
27
28function ensureExecutable(filePath) {
29 if (hostPlatform !== "win32") {
30 NodeFS.chmodSync(filePath, 0o755);
31 }
32}
33
34function repairPathFile(electronDir, platformPath) {
35 const pathFile = NodePath.join(electronDir, "path.txt");
36 const currentPath = NodeFS.existsSync(pathFile)
37 ? NodeFS.readFileSync(pathFile, "utf8")
38 : undefined;
39
40 if (currentPath !== platformPath) {
41 NodeFS.writeFileSync(pathFile, platformPath);
42 }
43}
44
45function getRequiredRuntimePaths(electronDir, platformPath) {
46 const paths = [NodePath.join(electronDir, "dist", platformPath)];
47
48 if (hostPlatform === "darwin") {
49 paths.push(
50 NodePath.join(electronDir, "dist", "Electron.app", "Contents", "Info.plist"),
51 NodePath.join(
52 electronDir,
53 "dist",
54 "Electron.app",
55 "Contents",
56 "Frameworks",
57 "Electron Framework.framework",
58 "Electron Framework",
59 ),
60 );
61 }
62
63 return paths;
64}
65
66function isMachO(filePath) {
67 if (hostPlatform !== "darwin") {
68 return true;
69 }
70
71 const result = NodeChildProcess.spawnSync("file", ["-b", filePath], {
72 encoding: "utf8",
73 });
74
75 return result.status === 0 && result.stdout.includes("Mach-O");
76}
77
78function missingRuntimePaths(electronDir, platformPath) {
79 return getRequiredRuntimePaths(electronDir, platformPath).filter((runtimePath) => {
80 return !NodeFS.existsSync(runtimePath);
81 });
82}
83
84function invalidRuntimePaths(electronDir, platformPath) {
85 if (hostPlatform !== "darwin") {
86 return [];
87 }
88
89 return [
90 NodePath.join(electronDir, "dist", platformPath),
91 NodePath.join(
92 electronDir,
93 "dist",
94 "Electron.app",
95 "Contents",
96 "Frameworks",
97 "Electron Framework.framework",
98 "Electron Framework",
99 ),
100 ].filter((runtimePath) => NodeFS.existsSync(runtimePath) && !isMachO(runtimePath));
101}
102
103function runChecked(command, args) {
104 const result = NodeChildProcess.spawnSync(command, args, {
105 encoding: "utf8",
106 stdio: "inherit",
107 });
108
109 if (result.status === 0) {
110 return;
111 }
112
113 throw new Error(
114 `${command} ${args.join(" ")} failed with exit code ${result.status ?? "unknown"}`,
115 );
116}
117
118function installElectronRuntime(electronDir, version) {
119 const tempDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-electron-"));
120 const zipPath = NodePath.join(tempDir, `electron-v${version}-${hostPlatform}-${hostArch}.zip`);
121
122 try {
123 runChecked("curl", [
124 "-fsSL",
125 `https://github.com/electron/electron/releases/download/v${version}/electron-v${version}-${hostPlatform}-${hostArch}.zip`,
126 "-o",
127 zipPath,
128 ]);
129 if (hostPlatform === "darwin") {
130 runChecked("ditto", ["-x", "-k", zipPath, NodePath.join(electronDir, "dist")]);
131 } else {
132 runChecked("python3", [
133 "-c",
134 "import os, sys, zipfile; os.makedirs(sys.argv[2], exist_ok=True); zipfile.ZipFile(sys.argv[1]).extractall(sys.argv[2])",
135 zipPath,
136 NodePath.join(electronDir, "dist"),
137 ]);
138 }
139 } finally {
140 NodeFS.rmSync(tempDir, { recursive: true, force: true });
141 }
142}
143
144export function ensureElectronRuntime() {
145 const electronPackageJsonPath = require.resolve("electron/package.json");
146 const electronPackageJson = JSON.parse(NodeFS.readFileSync(electronPackageJsonPath, "utf8"));
147 const electronDir = NodePath.dirname(electronPackageJsonPath);
148 const platformPath = getPlatformPath();
149 const electronPath = NodePath.join(electronDir, "dist", platformPath);
150 const missingBeforeInstall = missingRuntimePaths(electronDir, platformPath);
151 const invalidBeforeInstall = invalidRuntimePaths(electronDir, platformPath);
152
153 if (missingBeforeInstall.length > 0 || invalidBeforeInstall.length > 0) {
154 if (NodeFS.existsSync(NodePath.join(electronDir, "dist"))) {
155 NodeFS.rmSync(NodePath.join(electronDir, "dist"), { recursive: true, force: true });
156 }
157 NodeFS.rmSync(NodePath.join(electronDir, "path.txt"), { force: true });
158 installElectronRuntime(electronDir, electronPackageJson.version);
159 }
160
161 const missingAfterInstall = missingRuntimePaths(electronDir, platformPath);
162 const invalidAfterInstall = invalidRuntimePaths(electronDir, platformPath);
163 if (missingAfterInstall.length > 0 || invalidAfterInstall.length > 0) {
164 throw new Error(
165 `Electron runtime is incomplete after install.\nMissing:\n${missingAfterInstall
166 .map((runtimePath) => `- ${runtimePath}`)
167 .join("\n")}\nInvalid:\n${invalidAfterInstall
168 .map((runtimePath) => `- ${runtimePath}`)
169 .join("\n")}`,
170 );
171 }
172
173 ensureExecutable(electronPath);
174 repairPathFile(electronDir, platformPath);
175
176 return electronPath;
177}
178
179if (import.meta.url === `file://${process.argv[1]}`) {
180 const electronPath = ensureElectronRuntime();
181 process.stdout.write(`${electronPath}\n`);
182}