Open History to see the last commit
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 readdirSync,
12 rmSync,
13 statSync,
14 writeFileSync,
15} from "node:fs";
16import { createRequire } from "node:module";
17import { dirname, join, resolve } from "node:path";
18import { fileURLToPath } from "node:url";
19
20const isDevelopment = Boolean(process.env.VITE_DEV_SERVER_URL);
21const APP_DISPLAY_NAME = isDevelopment ? "T3 Code (Dev)" : "T3 Code (Alpha)";
22const APP_BUNDLE_ID = isDevelopment ? "com.t3tools.t3code.dev" : "com.t3tools.t3code";
23const LAUNCHER_VERSION = 1;
24
25const __dirname = dirname(fileURLToPath(import.meta.url));
26export const desktopDir = resolve(__dirname, "..");
27const repoRoot = resolve(desktopDir, "..", "..");
28const defaultIconPath = join(desktopDir, "resources", "icon.icns");
29const developmentMacIconPngPath = join(repoRoot, "assets", "dev", "blueprint-macos-1024.png");
30
31function setPlistString(plistPath, key, value) {
32 const replaceResult = spawnSync("plutil", ["-replace", key, "-string", value, plistPath], {
33 encoding: "utf8",
34 });
35 if (replaceResult.status === 0) {
36 return;
37 }
38
39 const insertResult = spawnSync("plutil", ["-insert", key, "-string", value, plistPath], {
40 encoding: "utf8",
41 });
42 if (insertResult.status === 0) {
43 return;
44 }
45
46 const details = [replaceResult.stderr, insertResult.stderr].filter(Boolean).join("\n");
47 throw new Error(`Failed to update plist key "${key}" at ${plistPath}: ${details}`.trim());
48}
49
50function runChecked(command, args) {
51 const result = spawnSync(command, args, { encoding: "utf8" });
52 if (result.status === 0) {
53 return;
54 }
55
56 const details = [result.stdout, result.stderr].filter(Boolean).join("\n");
57 throw new Error(`Failed to run ${command} ${args.join(" ")}: ${details}`.trim());
58}
59
60function ensureDevelopmentIconIcns(runtimeDir) {
61 const generatedIconPath = join(runtimeDir, "icon-dev.icns");
62 mkdirSync(runtimeDir, { recursive: true });
63
64 if (!existsSync(developmentMacIconPngPath)) {
65 return defaultIconPath;
66 }
67
68 const sourceMtimeMs = statSync(developmentMacIconPngPath).mtimeMs;
69 if (existsSync(generatedIconPath) && statSync(generatedIconPath).mtimeMs >= sourceMtimeMs) {
70 return generatedIconPath;
71 }
72
73 const iconsetRoot = mkdtempSync(join(runtimeDir, "dev-iconset-"));
74 const iconsetDir = join(iconsetRoot, "icon.iconset");
75 mkdirSync(iconsetDir, { recursive: true });
76
77 try {
78 for (const size of [16, 32, 128, 256, 512]) {
79 runChecked("sips", [
80 "-z",
81 String(size),
82 String(size),
83 developmentMacIconPngPath,
84 "--out",
85 join(iconsetDir, `icon_${size}x${size}.png`),
86 ]);
87
88 const retinaSize = size * 2;
89 runChecked("sips", [
90 "-z",
91 String(retinaSize),
92 String(retinaSize),
93 developmentMacIconPngPath,
94 "--out",
95 join(iconsetDir, `icon_${size}x${size}@2x.png`),
96 ]);
97 }
98
99 runChecked("iconutil", ["-c", "icns", iconsetDir, "-o", generatedIconPath]);
100 return generatedIconPath;
101 } catch (error) {
102 console.warn(
103 "[desktop-launcher] Failed to generate dev macOS icon, falling back to default icon.",
104 error,
105 );
106 return defaultIconPath;
107 } finally {
108 rmSync(iconsetRoot, { recursive: true, force: true });
109 }
110}
111
112function patchMainBundleInfoPlist(appBundlePath, iconPath) {
113 const infoPlistPath = join(appBundlePath, "Contents", "Info.plist");
114 setPlistString(infoPlistPath, "CFBundleDisplayName", APP_DISPLAY_NAME);
115 setPlistString(infoPlistPath, "CFBundleName", APP_DISPLAY_NAME);
116 setPlistString(infoPlistPath, "CFBundleIdentifier", APP_BUNDLE_ID);
117 setPlistString(infoPlistPath, "CFBundleIconFile", "icon.icns");
118
119 const resourcesDir = join(appBundlePath, "Contents", "Resources");
120 copyFileSync(iconPath, join(resourcesDir, "icon.icns"));
121 copyFileSync(iconPath, join(resourcesDir, "electron.icns"));
122}
123
124function patchHelperBundleInfoPlists(appBundlePath) {
125 const frameworksDir = join(appBundlePath, "Contents", "Frameworks");
126 if (!existsSync(frameworksDir)) {
127 return;
128 }
129
130 for (const entry of readdirSync(frameworksDir, { withFileTypes: true })) {
131 if (!entry.isDirectory() || !entry.name.endsWith(".app")) {
132 continue;
133 }
134 if (!entry.name.startsWith("Electron Helper")) {
135 continue;
136 }
137
138 const helperPlistPath = join(frameworksDir, entry.name, "Contents", "Info.plist");
139 if (!existsSync(helperPlistPath)) {
140 continue;
141 }
142
143 const suffix = entry.name.replace("Electron Helper", "").replace(".app", "").trim();
144 const helperName = suffix
145 ? `${APP_DISPLAY_NAME} Helper ${suffix}`
146 : `${APP_DISPLAY_NAME} Helper`;
147 const helperIdSuffix = suffix.replace(/[()]/g, "").trim().toLowerCase().replace(/\s+/g, "-");
148 const helperBundleId = helperIdSuffix
149 ? `${APP_BUNDLE_ID}.helper.${helperIdSuffix}`
150 : `${APP_BUNDLE_ID}.helper`;
151
152 setPlistString(helperPlistPath, "CFBundleDisplayName", helperName);
153 setPlistString(helperPlistPath, "CFBundleName", helperName);
154 setPlistString(helperPlistPath, "CFBundleIdentifier", helperBundleId);
155 }
156}
157
158function readJson(path) {
159 try {
160 return JSON.parse(readFileSync(path, "utf8"));
161 } catch {
162 return null;
163 }
164}
165
166function buildMacLauncher(electronBinaryPath) {
167 const sourceAppBundlePath = resolve(electronBinaryPath, "../../..");
168 const runtimeDir = join(desktopDir, ".electron-runtime");
169 const targetAppBundlePath = join(runtimeDir, `${APP_DISPLAY_NAME}.app`);
170 const targetBinaryPath = join(targetAppBundlePath, "Contents", "MacOS", "Electron");
171 const iconPath = isDevelopment ? ensureDevelopmentIconIcns(runtimeDir) : defaultIconPath;
172 const metadataPath = join(runtimeDir, "metadata.json");
173
174 mkdirSync(runtimeDir, { recursive: true });
175
176 const expectedMetadata = {
177 launcherVersion: LAUNCHER_VERSION,
178 sourceAppBundlePath,
179 sourceAppMtimeMs: statSync(sourceAppBundlePath).mtimeMs,
180 iconMtimeMs: statSync(iconPath).mtimeMs,
181 };
182
183 const currentMetadata = readJson(metadataPath);
184 if (
185 existsSync(targetBinaryPath) &&
186 currentMetadata &&
187 JSON.stringify(currentMetadata) === JSON.stringify(expectedMetadata)
188 ) {
189 return targetBinaryPath;
190 }
191
192 rmSync(targetAppBundlePath, { recursive: true, force: true });
193 cpSync(sourceAppBundlePath, targetAppBundlePath, { recursive: true });
194 patchMainBundleInfoPlist(targetAppBundlePath, iconPath);
195 patchHelperBundleInfoPlists(targetAppBundlePath);
196 writeFileSync(metadataPath, `${JSON.stringify(expectedMetadata, null, 2)}\n`);
197
198 return targetBinaryPath;
199}
200
201export function resolveElectronPath() {
202 const require = createRequire(import.meta.url);
203 const electronBinaryPath = require("electron");
204
205 if (process.platform !== "darwin") {
206 return electronBinaryPath;
207 }
208
209 return buildMacLauncher(electronBinaryPath);
210}