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 Context from "effect/Context";
6import * as Effect from "effect/Effect";
7import * as Layer from "effect/Layer";
8
9import * as Electron from "electron";
10import * as HostProcess from "@t3tools/shared/HostProcess";
11
12import * as DesktopEarlyElectronStartup from "./DesktopEarlyElectronStartup.ts";
13import { resolveDesktopAppBranding } from "./DesktopEnvironment.ts";
14import { renderUrlHandlerDesktopEntry } from "./DesktopLinuxUrlHandler.ts";
15import * as ElectronProtocol from "../electron/ElectronProtocol.ts";
16
17export interface DesktopPreReadyCommandLineReader {
18 readonly hasSwitch: (switchName: string) => boolean;
19 readonly getSwitchValue: (switchName: string) => string;
20}
21
22function readCommandLineSwitchValue(
23 commandLine: DesktopPreReadyCommandLineReader,
24 switchName: string,
25): string | null {
26 if (!commandLine.hasSwitch(switchName)) {
27 return null;
28 }
29
30 const value = commandLine.getSwitchValue(switchName).trim();
31 return value.length > 0 ? value : null;
32}
33
34export const resolveEarlyLinuxElectronOptionsFromProcess = (
35 homeDirectory: string,
36): DesktopEarlyElectronStartup.EarlyLinuxElectronOptions =>
37 DesktopEarlyElectronStartup.resolveEarlyLinuxElectronOptions({
38 env: process.env,
39 homeDirectory,
40 joinPath: NodePath.posix.join,
41 readFileString: (path) => NodeFS.readFileSync(path, "utf8"),
42 });
43
44export class DesktopPreReadyElectronOptions extends Context.Service<
45 DesktopPreReadyElectronOptions,
46 {
47 readonly linux: DesktopEarlyElectronStartup.EarlyLinuxElectronOptions | null;
48 readonly linuxPasswordStoreCommandLine: string | null;
49 }
50>()("@t3tools/desktop/app/DesktopPreReadyPlatform/DesktopPreReadyElectronOptions") {}
51
52/** @public Service construction is part of the canonical Effect module API. */
53export const make = Effect.gen(function* () {
54 const platform = yield* HostProcess.Platform;
55 const homeDirectory = yield* HostProcess.HomeDirectory;
56 return yield* Effect.sync((): DesktopPreReadyElectronOptions["Service"] => {
57 const linuxPasswordStoreCommandLine =
58 platform === "linux"
59 ? readCommandLineSwitchValue(Electron.app.commandLine, "password-store")
60 : null;
61 const linux =
62 platform === "linux" ? resolveEarlyLinuxElectronOptionsFromProcess(homeDirectory) : null;
63
64 if (linux !== null) {
65 // The portal also requires a valid desktop entry. An AppImage update may
66 // have removed the executable referenced by the previous launch's entry.
67 try {
68 const applicationsDir = NodePath.posix.join(
69 process.env.XDG_DATA_HOME?.trim() ||
70 NodePath.posix.join(homeDirectory, ".local", "share"),
71 "applications",
72 );
73 NodeFS.mkdirSync(applicationsDir, { recursive: true });
74 const iconPath = Electron.app.isPackaged
75 ? NodePath.posix.join(
76 applicationsDir,
77 "..",
78 "icons",
79 `${linux.linuxDesktopEntryName}.png`,
80 )
81 : undefined;
82 if (iconPath !== undefined) {
83 try {
84 NodeFS.mkdirSync(NodePath.posix.dirname(iconPath), { recursive: true });
85 NodeFS.copyFileSync(
86 NodePath.posix.join(
87 Electron.app.getAppPath(),
88 "apps/desktop/prod-resources/icon.png",
89 ),
90 iconPath,
91 );
92 } catch {
93 // Icon installation is optional; registration retries after readiness.
94 }
95 }
96 NodeFS.writeFileSync(
97 NodePath.posix.join(applicationsDir, linux.linuxDesktopEntryName),
98 renderUrlHandlerDesktopEntry({
99 displayName: resolveDesktopAppBranding({
100 isDevelopment: linux.isDevelopment,
101 appVersion: Electron.app.getVersion(),
102 }).displayName,
103 execTarget: process.env.APPIMAGE?.trim() || process.execPath,
104 scheme: ElectronProtocol.getDesktopScheme(linux.isDevelopment),
105 ...(iconPath === undefined ? {} : { iconPath }),
106 }),
107 "utf8",
108 );
109 } catch {
110 // The URL handler retries with the full environment and logs failures.
111 }
112 // Chromium caches its portal registration during startup. Set the identity
113 // before any asynchronous work can initialize it with Electron's default.
114 Electron.app.setDesktopName(linux.linuxDesktopEntryName);
115 Electron.app.commandLine.appendSwitch("class", linux.linuxWmClass);
116 if (linux.passwordStore !== null && linuxPasswordStoreCommandLine === null) {
117 Electron.app.commandLine.appendSwitch("password-store", linux.passwordStore);
118 }
119 // Chromium keeps only the last --disable-features value, so pass the merged list.
120 const disableFeatures = DesktopEarlyElectronStartup.resolveLinuxDisableFeaturesSwitch({
121 env: process.env,
122 ozonePlatform: readCommandLineSwitchValue(Electron.app.commandLine, "ozone-platform"),
123 enableFeatures: readCommandLineSwitchValue(Electron.app.commandLine, "enable-features"),
124 disableFeatures: readCommandLineSwitchValue(Electron.app.commandLine, "disable-features"),
125 // Runs only on Hyprland Wayland sessions without an explicit feature override.
126 readHyprctlVersionJson: () =>
127 NodeChildProcess.execFileSync("hyprctl", ["version", "-j"], {
128 encoding: "utf8",
129 stdio: ["ignore", "pipe", "ignore"],
130 timeout: 1000,
131 maxBuffer: 64 * 1024,
132 }),
133 });
134 if (disableFeatures !== null) {
135 Electron.app.commandLine.appendSwitch("disable-features", disableFeatures);
136 }
137 }
138
139 return { linux, linuxPasswordStoreCommandLine };
140 });
141}).pipe(Effect.withSpan("desktop.electron.configureBeforeReady"));
142
143// Keep Electron's strict pre-ready setup isolated so later runtime layers cannot
144// observe app readiness before scheme privileges and command-line switches exist.
145export const layer = Layer.mergeAll(
146 ElectronProtocol.layerSchemePrivileges,
147 Layer.effect(DesktopPreReadyElectronOptions, make),
148);