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
20export function 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
49export const make = Effect.gen(function* () {
50 const platform = yield* HostProcessPlatform;
51 return yield* Effect.sync((): DesktopPreReadyElectronOptions["Service"] => {
52 const linuxPasswordStoreCommandLine =
53 platform === "linux"
54 ? readCommandLineSwitchValue(Electron.app.commandLine, "password-store")
55 : null;
56 const linux = platform === "linux" ? resolveEarlyLinuxElectronOptionsFromProcess() : null;
57
58 if (linux !== null) {
59 Electron.app.commandLine.appendSwitch("class", linux.linuxWmClass);
60 if (linux.passwordStore !== null && linuxPasswordStoreCommandLine === null) {
61 Electron.app.commandLine.appendSwitch("password-store", linux.passwordStore);
62 }
63 }
64
65 return { linux, linuxPasswordStoreCommandLine };
66 });
67}).pipe(Effect.withSpan("desktop.electron.configureBeforeReady"));
68
69// Keep Electron's strict pre-ready setup isolated so later runtime layers cannot
70// observe app readiness before scheme privileges and command-line switches exist.
71export const layer = Layer.mergeAll(
72 ElectronProtocol.layerSchemePrivileges,
73 Layer.effect(DesktopPreReadyElectronOptions, make),
74);