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