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 NodePath from "node:path";
4import * as Context from "effect/Context";
5import * as Effect from "effect/Effect";
6import * as Layer from "effect/Layer";
7
8import * as Electron from "electron";
9import * as HostProcess from "@t3tools/shared/HostProcess";
10
11import * as DesktopEarlyElectronStartup from "./DesktopEarlyElectronStartup.ts";
12import { resolveDesktopAppBranding } from "./DesktopEnvironment.ts";
13import { renderUrlHandlerDesktopEntry } from "./DesktopLinuxUrlHandler.ts";
14import * as ElectronProtocol from "../electron/ElectronProtocol.ts";
15
16export interface DesktopPreReadyCommandLineReader {
17 readonly hasSwitch: (switchName: string) => boolean;
18 readonly getSwitchValue: (switchName: string) => string;
19}
20
21function readCommandLineSwitchValue(
22 commandLine: DesktopPreReadyCommandLineReader,
23 switchName: string,
24): string | null {
25 if (!commandLine.hasSwitch(switchName)) {
26 return null;
27 }
28
29 const value = commandLine.getSwitchValue(switchName).trim();
30 return value.length > 0 ? value : null;
31}
32
33export const resolveEarlyLinuxElectronOptionsFromProcess = (
34 homeDirectory: string,
35): DesktopEarlyElectronStartup.EarlyLinuxElectronOptions =>
36 DesktopEarlyElectronStartup.resolveEarlyLinuxElectronOptions({
37 env: process.env,
38 homeDirectory,
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* HostProcess.Platform;
54 const homeDirectory = yield* HostProcess.HomeDirectory;
55 return yield* Effect.sync((): DesktopPreReadyElectronOptions["Service"] => {
56 const linuxPasswordStoreCommandLine =
57 platform === "linux"
58 ? readCommandLineSwitchValue(Electron.app.commandLine, "password-store")
59 : null;
60 const linux =
61 platform === "linux" ? resolveEarlyLinuxElectronOptionsFromProcess(homeDirectory) : null;
62
63 if (linux !== null) {
64 // The portal also requires a valid desktop entry. An AppImage update may
65 // have removed the executable referenced by the previous launch's entry.
66 try {
67 const applicationsDir = NodePath.posix.join(
68 process.env.XDG_DATA_HOME?.trim() ||
69 NodePath.posix.join(homeDirectory, ".local", "share"),
70 "applications",
71 );
72 NodeFS.mkdirSync(applicationsDir, { recursive: true });
73 const iconPath = Electron.app.isPackaged
74 ? NodePath.posix.join(
75 applicationsDir,
76 "..",
77 "icons",
78 `${linux.linuxDesktopEntryName}.png`,
79 )
80 : undefined;
81 if (iconPath !== undefined) {
82 try {
83 NodeFS.mkdirSync(NodePath.posix.dirname(iconPath), { recursive: true });
84 NodeFS.copyFileSync(
85 NodePath.posix.join(
86 Electron.app.getAppPath(),
87 "apps/desktop/prod-resources/icon.png",
88 ),
89 iconPath,
90 );
91 } catch {
92 // Icon installation is optional; registration retries after readiness.
93 }
94 }
95 NodeFS.writeFileSync(
96 NodePath.posix.join(applicationsDir, linux.linuxDesktopEntryName),
97 renderUrlHandlerDesktopEntry({
98 displayName: resolveDesktopAppBranding({
99 isDevelopment: linux.isDevelopment,
100 appVersion: Electron.app.getVersion(),
101 }).displayName,
102 execTarget: process.env.APPIMAGE?.trim() || process.execPath,
103 scheme: ElectronProtocol.getDesktopScheme(linux.isDevelopment),
104 ...(iconPath === undefined ? {} : { iconPath }),
105 }),
106 "utf8",
107 );
108 } catch {
109 // The URL handler retries with the full environment and logs failures.
110 }
111 // Chromium caches its portal registration during startup. Set the identity
112 // before any asynchronous work can initialize it with Electron's default.
113 Electron.app.setDesktopName(linux.linuxDesktopEntryName);
114 Electron.app.commandLine.appendSwitch("class", linux.linuxWmClass);
115 if (linux.passwordStore !== null && linuxPasswordStoreCommandLine === null) {
116 Electron.app.commandLine.appendSwitch("password-store", linux.passwordStore);
117 }
118 }
119
120 return { linux, linuxPasswordStoreCommandLine };
121 });
122}).pipe(Effect.withSpan("desktop.electron.configureBeforeReady"));
123
124// Keep Electron's strict pre-ready setup isolated so later runtime layers cannot
125// observe app readiness before scheme privileges and command-line switches exist.
126export const layer = Layer.mergeAll(
127 ElectronProtocol.layerSchemePrivileges,
128 Layer.effect(DesktopPreReadyElectronOptions, make),
129);