Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off - pre-ready Electron setup reads settings and prepares the Linux desktop entry synchronously before app services are available.
2import * as NodeFS from "node:fs";
3import * as NodeOS from "node:os";
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 { HostProcessPlatform } 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 (): DesktopEarlyElectronStartup.EarlyLinuxElectronOptions =>
36 DesktopEarlyElectronStartup.resolveEarlyLinuxElectronOptions({
37 env: process.env,
38 homeDirectory: NodeOS.homedir(),
39 joinPath: NodePath.posix.join,
40 readFileString: (path) => NodeFS.readFileSync(path, "utf8"),
41 });
42
43export class DesktopPreReadyElectronOptions extends Context.Service<
44 DesktopPreReadyElectronOptions,
45 {
46 readonly linux: DesktopEarlyElectronStartup.EarlyLinuxElectronOptions | null;
47 readonly linuxPasswordStoreCommandLine: string | null;
48 }
49>()("@t3tools/desktop/app/DesktopPreReadyPlatform/DesktopPreReadyElectronOptions") {}
50
51/** @public Service construction is part of the canonical Effect module API. */
52export const make = Effect.gen(function* () {
53 const platform = yield* HostProcessPlatform;
54 return yield* Effect.sync((): DesktopPreReadyElectronOptions["Service"] => {
55 const linuxPasswordStoreCommandLine =
56 platform === "linux"
57 ? readCommandLineSwitchValue(Electron.app.commandLine, "password-store")
58 : null;
59 const linux = platform === "linux" ? resolveEarlyLinuxElectronOptionsFromProcess() : null;
60
61 if (linux !== null) {
62 // The portal also requires a valid desktop entry. An AppImage update may
63 // have removed the executable referenced by the previous launch's entry.
64 try {
65 const applicationsDir = NodePath.posix.join(
66 process.env.XDG_DATA_HOME?.trim() ||
67 NodePath.posix.join(NodeOS.homedir(), ".local", "share"),
68 "applications",
69 );
70 NodeFS.mkdirSync(applicationsDir, { recursive: true });
71 const iconPath = Electron.app.isPackaged
72 ? NodePath.posix.join(
73 applicationsDir,
74 "..",
75 "icons",
76 `${linux.linuxDesktopEntryName}.png`,
77 )
78 : undefined;
79 if (iconPath !== undefined) {
80 try {
81 NodeFS.mkdirSync(NodePath.posix.dirname(iconPath), { recursive: true });
82 NodeFS.copyFileSync(
83 NodePath.posix.join(
84 Electron.app.getAppPath(),
85 "apps/desktop/prod-resources/icon.png",
86 ),
87 iconPath,
88 );
89 } catch {
90 // Icon installation is optional; registration retries after readiness.
91 }
92 }
93 NodeFS.writeFileSync(
94 NodePath.posix.join(applicationsDir, linux.linuxDesktopEntryName),
95 renderUrlHandlerDesktopEntry({
96 displayName: resolveDesktopAppBranding({
97 isDevelopment: linux.isDevelopment,
98 appVersion: Electron.app.getVersion(),
99 }).displayName,
100 execTarget: process.env.APPIMAGE?.trim() || process.execPath,
101 scheme: ElectronProtocol.getDesktopScheme(linux.isDevelopment),
102 ...(iconPath === undefined ? {} : { iconPath }),
103 }),
104 "utf8",
105 );
106 } catch {
107 // The URL handler retries with the full environment and logs failures.
108 }
109 // Chromium caches its portal registration during startup. Set the identity
110 // before any asynchronous work can initialize it with Electron's default.
111 Electron.app.setDesktopName(linux.linuxDesktopEntryName);
112 Electron.app.commandLine.appendSwitch("class", linux.linuxWmClass);
113 if (linux.passwordStore !== null && linuxPasswordStoreCommandLine === null) {
114 Electron.app.commandLine.appendSwitch("password-store", linux.passwordStore);
115 }
116 }
117
118 return { linux, linuxPasswordStoreCommandLine };
119 });
120}).pipe(Effect.withSpan("desktop.electron.configureBeforeReady"));
121
122// Keep Electron's strict pre-ready setup isolated so later runtime layers cannot
123// observe app readiness before scheme privileges and command-line switches exist.
124export const layer = Layer.mergeAll(
125 ElectronProtocol.layerSchemePrivileges,
126 Layer.effect(DesktopPreReadyElectronOptions, make),
127);