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

electron-launcher.mjs

180 lines5.7 KB

1// This file mostly exists because we want dev mode to say "T3 Code (Dev)" instead of "electron"
2​
3import { spawnSync } from "node:child_process";
4import {
5 copyFileSync,
6 cpSync,
7 existsSync,
8 mkdirSync,
9 mkdtempSync,
10 readFileSync,
11 rmSync,
12 statSync,
13 writeFileSync,
14} from "node:fs";
15import { createRequire } from "node:module";
16import { dirname, join, resolve } from "node:path";
17import { fileURLToPath } from "node:url";
18​
19const isDevelopment = Boolean(process.env.VITE_DEV_SERVER_URL);
20const APP_DISPLAY_NAME = isDevelopment ? "T3 Code (Dev)" : "T3 Code (Alpha)";
21const APP_BUNDLE_ID = isDevelopment ? "com.t3tools.t3code.dev" : "com.t3tools.t3code";
22const LAUNCHER_VERSION = 2;
23​
24const __dirname = dirname(fileURLToPath(import.meta.url));
25export const desktopDir = resolve(__dirname, "..");
26const repoRoot = resolve(desktopDir, "..", "..");
27const defaultIconPath = join(desktopDir, "resources", "icon.icns");
28const developmentMacIconPngPath = join(repoRoot, "assets", "dev", "blueprint-macos-1024.png");
29​
30function setPlistString(plistPath, key, value) {
31 const replaceResult = spawnSync("plutil", ["-replace", key, "-string", value, plistPath], {
32 encoding: "utf8",
33 });
34 if (replaceResult.status === 0) {
35 return;
36 }
37​
38 const insertResult = spawnSync("plutil", ["-insert", key, "-string", value, plistPath], {
39 encoding: "utf8",
40 });
41 if (insertResult.status === 0) {
42 return;
43 }
44​
45 const details = [replaceResult.stderr, insertResult.stderr].filter(Boolean).join("\n");
46 throw new Error(`Failed to update plist key "${key}" at ${plistPath}: ${details}`.trim());
48​
49function runChecked(command, args) {
50 const result = spawnSync(command, args, { encoding: "utf8" });
51 if (result.status === 0) {
52 return;
53 }
54​
55 const details = [result.stdout, result.stderr].filter(Boolean).join("\n");
56 throw new Error(`Failed to run ${command} ${args.join(" ")}: ${details}`.trim());
58​
59function ensureDevelopmentIconIcns(runtimeDir) {
60 const generatedIconPath = join(runtimeDir, "icon-dev.icns");
61 mkdirSync(runtimeDir, { recursive: true });
62​
63 if (!existsSync(developmentMacIconPngPath)) {
64 return defaultIconPath;
65 }
66​
67 const sourceMtimeMs = statSync(developmentMacIconPngPath).mtimeMs;
68 if (existsSync(generatedIconPath) && statSync(generatedIconPath).mtimeMs >= sourceMtimeMs) {
69 return generatedIconPath;
70 }
71​
72 const iconsetRoot = mkdtempSync(join(runtimeDir, "dev-iconset-"));
73 const iconsetDir = join(iconsetRoot, "icon.iconset");
74 mkdirSync(iconsetDir, { recursive: true });
75​
76 try {
77 for (const size of [16, 32, 128, 256, 512]) {
78 runChecked("sips", [
79 "-z",
80 String(size),
81 String(size),
82 developmentMacIconPngPath,
83 "--out",
84 join(iconsetDir, `icon_${size}x${size}.png`),
85 ]);
86​
87 const retinaSize = size * 2;
88 runChecked("sips", [
89 "-z",
90 String(retinaSize),
91 String(retinaSize),
92 developmentMacIconPngPath,
93 "--out",
94 join(iconsetDir, `icon_${size}x${size}@2x.png`),
95 ]);
96 }
97​
98 runChecked("iconutil", ["-c", "icns", iconsetDir, "-o", generatedIconPath]);
99 return generatedIconPath;
100 } catch (error) {
101 console.warn(
102 "[desktop-launcher] Failed to generate dev macOS icon, falling back to default icon.",
103 error,
104 );
105 return defaultIconPath;
106 } finally {
107 rmSync(iconsetRoot, { recursive: true, force: true });
108 }
110​
111function patchMainBundleInfoPlist(appBundlePath, iconPath) {
112 const infoPlistPath = join(appBundlePath, "Contents", "Info.plist");
113 setPlistString(infoPlistPath, "CFBundleDisplayName", APP_DISPLAY_NAME);
114 setPlistString(infoPlistPath, "CFBundleName", APP_DISPLAY_NAME);
115 setPlistString(infoPlistPath, "CFBundleIdentifier", APP_BUNDLE_ID);
116 setPlistString(infoPlistPath, "CFBundleIconFile", "icon.icns");
117​
118 const resourcesDir = join(appBundlePath, "Contents", "Resources");
119 copyFileSync(iconPath, join(resourcesDir, "icon.icns"));
120 copyFileSync(iconPath, join(resourcesDir, "electron.icns"));
122​
123function readJson(path) {
124 try {
125 return JSON.parse(readFileSync(path, "utf8"));
126 } catch {
127 return null;
128 }
130​
131function buildMacLauncher(electronBinaryPath) {
132 const sourceAppBundlePath = resolve(electronBinaryPath, "../../..");
133 const runtimeDir = join(desktopDir, ".electron-runtime");
134 const targetAppBundlePath = join(runtimeDir, `${APP_DISPLAY_NAME}.app`);
135 const targetBinaryPath = join(targetAppBundlePath, "Contents", "MacOS", "Electron");
136 const iconPath = isDevelopment ? ensureDevelopmentIconIcns(runtimeDir) : defaultIconPath;
137 const metadataPath = join(runtimeDir, "metadata.json");
138​
139 mkdirSync(runtimeDir, { recursive: true });
140​
141 const expectedMetadata = {
142 launcherVersion: LAUNCHER_VERSION,
143 sourceAppBundlePath,
144 sourceAppMtimeMs: statSync(sourceAppBundlePath).mtimeMs,
145 iconMtimeMs: statSync(iconPath).mtimeMs,
146 };
147​
148 const currentMetadata = readJson(metadataPath);
149 if (
150 existsSync(targetBinaryPath) &&
151 currentMetadata &&
152 JSON.stringify(currentMetadata) === JSON.stringify(expectedMetadata)
153 ) {
154 return targetBinaryPath;
155 }
156​
157 rmSync(targetAppBundlePath, { recursive: true, force: true });
158 cpSync(sourceAppBundlePath, targetAppBundlePath, { recursive: true });
159 patchMainBundleInfoPlist(targetAppBundlePath, iconPath);
160 writeFileSync(metadataPath, `${JSON.stringify(expectedMetadata, null, 2)}\n`);
161​
162 return targetBinaryPath;
164​
165export function resolveElectronPath() {
166 const require = createRequire(import.meta.url);
167 const electronBinaryPath = require("electron");
168​
169 if (process.platform !== "darwin") {
170 return electronBinaryPath;
171 }
172​
173 // Dev launches do not need a renamed app bundle badly enough to risk breaking
174 // Electron helper resource lookup on macOS.
175 if (isDevelopment) {
176 return electronBinaryPath;
177 }
178​
179 return buildMacLauncher(electronBinaryPath);