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 readFileSync,
10 readdirSync,
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 = "com.t3tools.t3code";
22const LAUNCHER_VERSION = 1;
23
24const __dirname = dirname(fileURLToPath(import.meta.url));
25export const desktopDir = resolve(__dirname, "..");
26
27function setPlistString(plistPath, key, value) {
28 const replaceResult = spawnSync("plutil", ["-replace", key, "-string", value, plistPath], {
29 encoding: "utf8",
30 });
31 if (replaceResult.status === 0) {
32 return;
33 }
34
35 const insertResult = spawnSync("plutil", ["-insert", key, "-string", value, plistPath], {
36 encoding: "utf8",
37 });
38 if (insertResult.status === 0) {
39 return;
40 }
41
42 const details = [replaceResult.stderr, insertResult.stderr].filter(Boolean).join("\n");
43 throw new Error(`Failed to update plist key "${key}" at ${plistPath}: ${details}`.trim());
44}
45
46function patchMainBundleInfoPlist(appBundlePath, iconPath) {
47 const infoPlistPath = join(appBundlePath, "Contents", "Info.plist");
48 setPlistString(infoPlistPath, "CFBundleDisplayName", APP_DISPLAY_NAME);
49 setPlistString(infoPlistPath, "CFBundleName", APP_DISPLAY_NAME);
50 setPlistString(infoPlistPath, "CFBundleIdentifier", APP_BUNDLE_ID);
51 setPlistString(infoPlistPath, "CFBundleIconFile", "icon.icns");
52
53 const resourcesDir = join(appBundlePath, "Contents", "Resources");
54 copyFileSync(iconPath, join(resourcesDir, "icon.icns"));
55 copyFileSync(iconPath, join(resourcesDir, "electron.icns"));
56}
57
58function patchHelperBundleInfoPlists(appBundlePath) {
59 const frameworksDir = join(appBundlePath, "Contents", "Frameworks");
60 if (!existsSync(frameworksDir)) {
61 return;
62 }
63
64 for (const entry of readdirSync(frameworksDir, { withFileTypes: true })) {
65 if (!entry.isDirectory() || !entry.name.endsWith(".app")) {
66 continue;
67 }
68 if (!entry.name.startsWith("Electron Helper")) {
69 continue;
70 }
71
72 const helperPlistPath = join(frameworksDir, entry.name, "Contents", "Info.plist");
73 if (!existsSync(helperPlistPath)) {
74 continue;
75 }
76
77 const suffix = entry.name.replace("Electron Helper", "").replace(".app", "").trim();
78 const helperName = suffix
79 ? `${APP_DISPLAY_NAME} Helper ${suffix}`
80 : `${APP_DISPLAY_NAME} Helper`;
81 const helperIdSuffix = suffix.replace(/[()]/g, "").trim().toLowerCase().replace(/\s+/g, "-");
82 const helperBundleId = helperIdSuffix
83 ? `${APP_BUNDLE_ID}.helper.${helperIdSuffix}`
84 : `${APP_BUNDLE_ID}.helper`;
85
86 setPlistString(helperPlistPath, "CFBundleDisplayName", helperName);
87 setPlistString(helperPlistPath, "CFBundleName", helperName);
88 setPlistString(helperPlistPath, "CFBundleIdentifier", helperBundleId);
89 }
90}
91
92function readJson(path) {
93 try {
94 return JSON.parse(readFileSync(path, "utf8"));
95 } catch {
96 return null;
97 }
98}
99
100function buildMacLauncher(electronBinaryPath) {
101 const sourceAppBundlePath = resolve(electronBinaryPath, "../../..");
102 const runtimeDir = join(desktopDir, ".electron-runtime");
103 const targetAppBundlePath = join(runtimeDir, `${APP_DISPLAY_NAME}.app`);
104 const targetBinaryPath = join(targetAppBundlePath, "Contents", "MacOS", "Electron");
105 const iconPath = join(desktopDir, "resources", "icon.icns");
106 const metadataPath = join(runtimeDir, "metadata.json");
107
108 mkdirSync(runtimeDir, { recursive: true });
109
110 const expectedMetadata = {
111 launcherVersion: LAUNCHER_VERSION,
112 sourceAppBundlePath,
113 sourceAppMtimeMs: statSync(sourceAppBundlePath).mtimeMs,
114 iconMtimeMs: statSync(iconPath).mtimeMs,
115 };
116
117 const currentMetadata = readJson(metadataPath);
118 if (
119 existsSync(targetBinaryPath) &&
120 currentMetadata &&
121 JSON.stringify(currentMetadata) === JSON.stringify(expectedMetadata)
122 ) {
123 return targetBinaryPath;
124 }
125
126 rmSync(targetAppBundlePath, { recursive: true, force: true });
127 cpSync(sourceAppBundlePath, targetAppBundlePath, { recursive: true });
128 patchMainBundleInfoPlist(targetAppBundlePath, iconPath);
129 patchHelperBundleInfoPlists(targetAppBundlePath);
130 writeFileSync(metadataPath, `${JSON.stringify(expectedMetadata, null, 2)}\n`);
131
132 return targetBinaryPath;
133}
134
135export function resolveElectronPath() {
136 const require = createRequire(import.meta.url);
137 const electronBinaryPath = require("electron");
138
139 if (process.platform !== "darwin") {
140 return electronBinaryPath;
141 }
142
143 return buildMacLauncher(electronBinaryPath);
144}