Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off - pre-ready Electron setup reads persisted settings 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 * as ElectronProtocol from "../electron/ElectronProtocol.ts";
14
15export interface DesktopPreReadyCommandLineReader {
16 readonly hasSwitch: (switchName: string) => boolean;
17 readonly getSwitchValue: (switchName: string) => string;
18}
19
20function readCommandLineSwitchValue(
21 commandLine: DesktopPreReadyCommandLineReader,
22 switchName: string,
23): string | null {
24 if (!commandLine.hasSwitch(switchName)) {
25 return null;
26 }
27
28 const value = commandLine.getSwitchValue(switchName).trim();
29 return value.length > 0 ? value : null;
30}
31
32export const resolveEarlyLinuxElectronOptionsFromProcess =
33 (): DesktopEarlyElectronStartup.EarlyLinuxElectronOptions =>
34 DesktopEarlyElectronStartup.resolveEarlyLinuxElectronOptions({
35 env: process.env,
36 homeDirectory: NodeOS.homedir(),
37 joinPath: NodePath.posix.join,
38 readFileString: (path) => NodeFS.readFileSync(path, "utf8"),
39 });
40
41export class DesktopPreReadyElectronOptions extends Context.Service<
42 DesktopPreReadyElectronOptions,
43 {
44 readonly linux: DesktopEarlyElectronStartup.EarlyLinuxElectronOptions | null;
45 readonly linuxPasswordStoreCommandLine: string | null;
46 }
47>()("@t3tools/desktop/app/DesktopPreReadyPlatform/DesktopPreReadyElectronOptions") {}
48
49/** @public Service construction is part of the canonical Effect module API. */
50export const make = Effect.gen(function* () {
51 const platform = yield* HostProcessPlatform;
52 return yield* Effect.sync((): DesktopPreReadyElectronOptions["Service"] => {
53 const linuxPasswordStoreCommandLine =
54 platform === "linux"
55 ? readCommandLineSwitchValue(Electron.app.commandLine, "password-store")
56 : null;
57 const linux = platform === "linux" ? resolveEarlyLinuxElectronOptionsFromProcess() : null;
58
59 if (linux !== null) {
60 Electron.app.commandLine.appendSwitch("class", linux.linuxWmClass);
61 if (linux.passwordStore !== null && linuxPasswordStoreCommandLine === null) {
62 Electron.app.commandLine.appendSwitch("password-store", linux.passwordStore);
63 }
64 }
65
66 return { linux, linuxPasswordStoreCommandLine };
67 });
68}).pipe(Effect.withSpan("desktop.electron.configureBeforeReady"));
69
70// Keep Electron's strict pre-ready setup isolated so later runtime layers cannot
71// observe app readiness before scheme privileges and command-line switches exist.
72export const layer = Layer.mergeAll(
73 ElectronProtocol.layerSchemePrivileges,
74 Layer.effect(DesktopPreReadyElectronOptions, make),
75);