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 */
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 DesktopEarlyElectronStartup.restoreEarlyLinuxDeviceScaleFactor({
66 env: process.env,
67 homeDirectory,
68 joinPath: NodePath.posix.join,
69 commandLine: Electron.app.commandLine,
70 readFileString: (path) => NodeFS.readFileSync(path, "utf8"),
71 writeFileString: (path, value) => {
72 NodeFS.mkdirSync(NodePath.posix.dirname(path), { recursive: true });
73 NodeFS.writeFileSync(path, value, "utf8");
74 },
75 });
76 // The portal also requires a valid desktop entry. An AppImage update may
77 // have removed the executable referenced by the previous launch's entry.
78 try {
79 const applicationsDir = NodePath.posix.join(
80 process.env.XDG_DATA_HOME?.trim() ||
81 NodePath.posix.join(homeDirectory, ".local", "share"),
82 "applications",
83 );
84 NodeFS.mkdirSync(applicationsDir, { recursive: true });
85 const iconPath = Electron.app.isPackaged
86 ? NodePath.posix.join(
87 applicationsDir,
88 "..",
89 "icons",
90 `${linux.linuxDesktopEntryName}.png`,
91 )
92 : undefined;
93 if (iconPath !== undefined) {
94 try {
95 NodeFS.mkdirSync(NodePath.posix.dirname(iconPath), { recursive: true });
96 NodeFS.copyFileSync(
97 NodePath.posix.join(
98 Electron.app.getAppPath(),
99 "apps/desktop/prod-resources/icon.png",
100 ),
101 iconPath,
102 );
103 } catch {}
104 }
105 NodeFS.writeFileSync(
106 NodePath.posix.join(applicationsDir, linux.linuxDesktopEntryName),
107 renderUrlHandlerDesktopEntry({
108 displayName: resolveDesktopAppBranding({
109 isDevelopment: linux.isDevelopment,
110 appVersion: Electron.app.getVersion(),
111 }).displayName,
112 execTarget:
113 process.env.T3CODE_DESKTOP_LAUNCHER_PATH?.trim() ||
114 process.env.APPIMAGE?.trim() ||
115 process.execPath,
116 scheme: ElectronProtocol.getDesktopScheme(linux.isDevelopment),
117 ...(iconPath === undefined ? {} : { iconPath }),
118 }),
119 "utf8",
120 );
121 } catch {}
122 // Chromium caches portal identity; set it before any asynchronous initialization.
123 Electron.app.setDesktopName(linux.linuxDesktopEntryName);
124 if (linux.passwordStore !== null && linuxPasswordStoreCommandLine === null) {
125 Electron.app.commandLine.appendSwitch("password-store", linux.passwordStore);
126 }
127 // Chromium keeps only the last --disable-features value, so pass the merged list.
128 const disableFeatures = DesktopEarlyElectronStartup.resolveLinuxDisableFeaturesSwitch({
129 env: process.env,
130 ozonePlatform: readCommandLineSwitchValue(Electron.app.commandLine, "ozone-platform"),
131 enableFeatures: readCommandLineSwitchValue(Electron.app.commandLine, "enable-features"),
132 disableFeatures: readCommandLineSwitchValue(Electron.app.commandLine, "disable-features"),
133 readHyprctlVersionJson: () =>
134 NodeChildProcess.execFileSync("hyprctl", ["version", "-j"], {
135 encoding: "utf8",
136 stdio: ["ignore", "pipe", "ignore"],
137 timeout: 1000,
138 maxBuffer: 64 * 1024,
139 }),
140 });
141 if (disableFeatures !== null) {
142 Electron.app.commandLine.appendSwitch("disable-features", disableFeatures);
143 }
144 }
145
146 return { linux, linuxPasswordStoreCommandLine };
147 });
148}).pipe(Effect.withSpan("desktop.electron.configureBeforeReady"));
149
150// Complete scheme privileges and switches before later layers can observe app readiness.
151export const layer = Layer.mergeAll(
152 ElectronProtocol.layerSchemePrivileges,
153 Layer.effect(DesktopPreReadyElectronOptions, make),
154);