@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 194 lines 7.92 KB
Raw
1 // @effect-diagnostics nodeBuiltinImport:off - pre-ready Electron setup reads settings, prepares the Linux desktop entry, and asks Hyprland for its version synchronously before app services are available.
2 import * as NodeChildProcess from "node:child_process";
3 import * as NodeFS from "node:fs";
4 import * as NodePath from "node:path";
5 import * as Config from "effect/Config";
6 import * as Context from "effect/Context";
7 import * as Effect from "effect/Effect";
8 import * as Layer from "effect/Layer";
9 import * as Option from "effect/Option";
10
11 import * as Electron from "electron";
12 import * as HostProcess from "@t3tools/shared/HostProcess";
13
14 import * as DesktopEarlyElectronStartup from "./DesktopEarlyElectronStartup.ts";
15 import { trimmedString } from "./DesktopConfig.ts";
16 import { resolveDesktopAppBranding } from "./DesktopEnvironment.ts";
17 import {
18 renderUrlHandlerDesktopEntry,
19 replacedVersionedAppImagePath,
20 } from "./DesktopLinuxUrlHandler.ts";
21 import * as ElectronProtocol from "../electron/ElectronProtocol.ts";
22
23 export interface DesktopPreReadyCommandLineReader {
24 readonly hasSwitch: (switchName: string) => boolean;
25 readonly getSwitchValue: (switchName: string) => string;
26 }
27
28 function readCommandLineSwitchValue(
29 commandLine: DesktopPreReadyCommandLineReader,
30 switchName: string,
31 ): string | null {
32 if (!commandLine.hasSwitch(switchName)) {
33 return null;
34 }
35
36 const value = commandLine.getSwitchValue(switchName).trim();
37 return value.length > 0 ? value : null;
38 }
39
40 export const resolveEarlyLinuxElectronOptionsFromProcess = (
41 homeDirectory: string,
42 ): DesktopEarlyElectronStartup.EarlyLinuxElectronOptions =>
43 DesktopEarlyElectronStartup.resolveEarlyLinuxElectronOptions({
44 env: process.env,
45 homeDirectory,
46 joinPath: NodePath.posix.join,
47 readFileString: (path) => NodeFS.readFileSync(path, "utf8"),
48 });
49
50 export class DesktopPreReadyElectronOptions extends Context.Service<
51 DesktopPreReadyElectronOptions,
52 {
53 readonly linux: DesktopEarlyElectronStartup.EarlyLinuxElectronOptions | null;
54 readonly linuxPasswordStoreCommandLine: string | null;
55 }
56 >()("@t3tools/desktop/app/DesktopPreReadyPlatform/DesktopPreReadyElectronOptions") {}
57
58 const LinuxDesktopEntryConfig = Config.all({
59 xdgDataHome: trimmedString("XDG_DATA_HOME"),
60 launcherPath: trimmedString("T3CODE_DESKTOP_LAUNCHER_PATH"),
61 appImagePath: trimmedString("APPIMAGE"),
62 // electron-updater sets this on the AppImage it relaunches after installing an update.
63 relaunchedByUpdater: trimmedString("APPIMAGE_SILENT_INSTALL"),
64 });
65
66 /** @public */
67 export const make = Effect.gen(function* () {
68 const platform = yield* HostProcess.Platform;
69 const homeDirectory = yield* HostProcess.HomeDirectory;
70 const linuxDesktopEntryConfig = yield* LinuxDesktopEntryConfig;
71 return yield* Effect.sync((): DesktopPreReadyElectronOptions["Service"] => {
72 const linuxPasswordStoreCommandLine =
73 platform === "linux"
74 ? readCommandLineSwitchValue(Electron.app.commandLine, "password-store")
75 : null;
76 const linux =
77 platform === "linux" ? resolveEarlyLinuxElectronOptionsFromProcess(homeDirectory) : null;
78
79 if (linux !== null) {
80 DesktopEarlyElectronStartup.restoreEarlyLinuxDeviceScaleFactor({
81 env: process.env,
82 homeDirectory,
83 joinPath: NodePath.posix.join,
84 commandLine: Electron.app.commandLine,
85 readFileString: (path) => NodeFS.readFileSync(path, "utf8"),
86 writeFileString: (path, value) => {
87 NodeFS.mkdirSync(NodePath.posix.dirname(path), { recursive: true });
88 NodeFS.writeFileSync(path, value, "utf8");
89 },
90 });
91 // The portal also requires a valid desktop entry. An AppImage update may
92 // have removed the executable referenced by the previous launch's entry.
93 try {
94 const applicationsDir = NodePath.posix.join(
95 Option.getOrElse(linuxDesktopEntryConfig.xdgDataHome, () =>
96 NodePath.posix.join(homeDirectory, ".local", "share"),
97 ),
98 "applications",
99 );
100 NodeFS.mkdirSync(applicationsDir, { recursive: true });
101 const iconPath = Electron.app.isPackaged
102 ? NodePath.posix.join(
103 applicationsDir,
104 "..",
105 "icons",
106 `${linux.linuxDesktopEntryName}.png`,
107 )
108 : undefined;
109 if (iconPath !== undefined) {
110 try {
111 NodeFS.mkdirSync(NodePath.posix.dirname(iconPath), { recursive: true });
112 // Electron's copyFileSync from app.asar extracts to a temp file it never deletes.
113 NodeFS.writeFileSync(
114 iconPath,
115 NodeFS.readFileSync(
116 NodePath.posix.join(
117 Electron.app.getAppPath(),
118 "apps/desktop/prod-resources/icon.png",
119 ),
120 ),
121 );
122 } catch {}
123 }
124 const desktopEntryPath = NodePath.posix.join(applicationsDir, linux.linuxDesktopEntryName);
125 const { appImagePath } = linuxDesktopEntryConfig;
126 if (
127 Option.isSome(appImagePath) &&
128 Option.isNone(linuxDesktopEntryConfig.launcherPath) &&
129 Option.contains(linuxDesktopEntryConfig.relaunchedByUpdater, "true")
130 ) {
131 try {
132 const replacedPath = replacedVersionedAppImagePath({
133 previousEntry: NodeFS.readFileSync(desktopEntryPath, "utf8"),
134 appImagePath: appImagePath.value,
135 });
136 // Relative, so the pair survives a moved folder; symlink never overwrites a file.
137 if (replacedPath !== null) {
138 NodeFS.symlinkSync(NodePath.posix.basename(appImagePath.value), replacedPath);
139 }
140 } catch {}
141 }
142 NodeFS.writeFileSync(
143 desktopEntryPath,
144 renderUrlHandlerDesktopEntry({
145 displayName: resolveDesktopAppBranding({
146 isDevelopment: linux.isDevelopment,
147 appVersion: Electron.app.getVersion(),
148 }).displayName,
149 execTarget:
150 Option.getOrUndefined(
151 Option.orElse(
152 linuxDesktopEntryConfig.launcherPath,
153 () => linuxDesktopEntryConfig.appImagePath,
154 ),
155 ) ?? process.execPath,
156 scheme: ElectronProtocol.getDesktopScheme(linux.isDevelopment),
157 ...(iconPath === undefined ? {} : { iconPath }),
158 }),
159 "utf8",
160 );
161 } catch {}
162 // Chromium caches portal identity; set it before any asynchronous initialization.
163 Electron.app.setDesktopName(linux.linuxDesktopEntryName);
164 if (linux.passwordStore !== null && linuxPasswordStoreCommandLine === null) {
165 Electron.app.commandLine.appendSwitch("password-store", linux.passwordStore);
166 }
167 // Chromium keeps only the last --disable-features value, so pass the merged list.
168 const disableFeatures = DesktopEarlyElectronStartup.resolveLinuxDisableFeaturesSwitch({
169 env: process.env,
170 ozonePlatform: readCommandLineSwitchValue(Electron.app.commandLine, "ozone-platform"),
171 enableFeatures: readCommandLineSwitchValue(Electron.app.commandLine, "enable-features"),
172 disableFeatures: readCommandLineSwitchValue(Electron.app.commandLine, "disable-features"),
173 readHyprctlVersionJson: () =>
174 NodeChildProcess.execFileSync("hyprctl", ["version", "-j"], {
175 encoding: "utf8",
176 stdio: ["ignore", "pipe", "ignore"],
177 timeout: 1000,
178 maxBuffer: 64 * 1024,
179 }),
180 });
181 if (disableFeatures !== null) {
182 Electron.app.commandLine.appendSwitch("disable-features", disableFeatures);
183 }
184 }
185
186 return { linux, linuxPasswordStoreCommandLine };
187 });
188 }).pipe(Effect.withSpan("desktop.electron.configureBeforeReady"));
189
190 // Complete scheme privileges and switches before later layers can observe app readiness.
191 export const layer = Layer.mergeAll(
192 ElectronProtocol.layerSchemePrivileges,
193 Layer.effect(DesktopPreReadyElectronOptions, make),
194 );