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