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 Config from "effect/Config";
6import * as Context from "effect/Context";
7import * as Effect from "effect/Effect";
8import * as Layer from "effect/Layer";
9import * as Option from "effect/Option";
10
11import * as Electron from "electron";
12import * as HostProcess from "@t3tools/shared/HostProcess";
13
14import * as DesktopEarlyElectronStartup from "./DesktopEarlyElectronStartup.ts";
15import { trimmedString } from "./DesktopConfig.ts";
16import { resolveDesktopAppBranding } from "./DesktopEnvironment.ts";
17import { renderUrlHandlerDesktopEntry } from "./DesktopLinuxUrlHandler.ts";
18import * as ElectronProtocol from "../electron/ElectronProtocol.ts";
19
20export interface DesktopPreReadyCommandLineReader {
21 readonly hasSwitch: (switchName: string) => boolean;
22 readonly getSwitchValue: (switchName: string) => string;
23}
24
25function readCommandLineSwitchValue(
26 commandLine: DesktopPreReadyCommandLineReader,
27 switchName: string,
28): string | null {
29 if (!commandLine.hasSwitch(switchName)) {
30 return null;
31 }
32
33 const value = commandLine.getSwitchValue(switchName).trim();
34 return value.length > 0 ? value : null;
35}
36
37export const resolveEarlyLinuxElectronOptionsFromProcess = (
38 homeDirectory: string,
39): DesktopEarlyElectronStartup.EarlyLinuxElectronOptions =>
40 DesktopEarlyElectronStartup.resolveEarlyLinuxElectronOptions({
41 env: process.env,
42 homeDirectory,
43 joinPath: NodePath.posix.join,
44 readFileString: (path) => NodeFS.readFileSync(path, "utf8"),
45 });
46
47export class DesktopPreReadyElectronOptions extends Context.Service<
48 DesktopPreReadyElectronOptions,
49 {
50 readonly linux: DesktopEarlyElectronStartup.EarlyLinuxElectronOptions | null;
51 readonly linuxPasswordStoreCommandLine: string | null;
52 }
53>()("@t3tools/desktop/app/DesktopPreReadyPlatform/DesktopPreReadyElectronOptions") {}
54
55const LinuxDesktopEntryConfig = Config.all({
56 xdgDataHome: trimmedString("XDG_DATA_HOME"),
57 launcherPath: trimmedString("T3CODE_DESKTOP_LAUNCHER_PATH"),
58 appImagePath: trimmedString("APPIMAGE"),
59});
60
61/** @public */
62export const make = Effect.gen(function* () {
63 const platform = yield* HostProcess.Platform;
64 const homeDirectory = yield* HostProcess.HomeDirectory;
65 const linuxDesktopEntryConfig = yield* LinuxDesktopEntryConfig;
66 return yield* Effect.sync((): DesktopPreReadyElectronOptions["Service"] => {
67 const linuxPasswordStoreCommandLine =
68 platform === "linux"
69 ? readCommandLineSwitchValue(Electron.app.commandLine, "password-store")
70 : null;
71 const linux =
72 platform === "linux" ? resolveEarlyLinuxElectronOptionsFromProcess(homeDirectory) : null;
73
74 if (linux !== null) {
75 DesktopEarlyElectronStartup.restoreEarlyLinuxDeviceScaleFactor({
76 env: process.env,
77 homeDirectory,
78 joinPath: NodePath.posix.join,
79 commandLine: Electron.app.commandLine,
80 readFileString: (path) => NodeFS.readFileSync(path, "utf8"),
81 writeFileString: (path, value) => {
82 NodeFS.mkdirSync(NodePath.posix.dirname(path), { recursive: true });
83 NodeFS.writeFileSync(path, value, "utf8");
84 },
85 });
86 // The portal also requires a valid desktop entry. An AppImage update may
87 // have removed the executable referenced by the previous launch's entry.
88 try {
89 const applicationsDir = NodePath.posix.join(
90 Option.getOrElse(linuxDesktopEntryConfig.xdgDataHome, () =>
91 NodePath.posix.join(homeDirectory, ".local", "share"),
92 ),
93 "applications",
94 );
95 NodeFS.mkdirSync(applicationsDir, { recursive: true });
96 const iconPath = Electron.app.isPackaged
97 ? NodePath.posix.join(
98 applicationsDir,
99 "..",
100 "icons",
101 `${linux.linuxDesktopEntryName}.png`,
102 )
103 : undefined;
104 if (iconPath !== undefined) {
105 try {
106 NodeFS.mkdirSync(NodePath.posix.dirname(iconPath), { recursive: true });
107 // Electron's copyFileSync from app.asar extracts to a temp file it never deletes.
108 NodeFS.writeFileSync(
109 iconPath,
110 NodeFS.readFileSync(
111 NodePath.posix.join(
112 Electron.app.getAppPath(),
113 "apps/desktop/prod-resources/icon.png",
114 ),
115 ),
116 );
117 } catch {}
118 }
119 NodeFS.writeFileSync(
120 NodePath.posix.join(applicationsDir, linux.linuxDesktopEntryName),
121 renderUrlHandlerDesktopEntry({
122 displayName: resolveDesktopAppBranding({
123 isDevelopment: linux.isDevelopment,
124 appVersion: Electron.app.getVersion(),
125 }).displayName,
126 execTarget:
127 Option.getOrUndefined(
128 Option.orElse(
129 linuxDesktopEntryConfig.launcherPath,
130 () => linuxDesktopEntryConfig.appImagePath,
131 ),
132 ) ?? process.execPath,
133 scheme: ElectronProtocol.getDesktopScheme(linux.isDevelopment),
134 ...(iconPath === undefined ? {} : { iconPath }),
135 }),
136 "utf8",
137 );
138 } catch {}
139 // Chromium caches portal identity; set it before any asynchronous initialization.
140 Electron.app.setDesktopName(linux.linuxDesktopEntryName);
141 if (linux.passwordStore !== null && linuxPasswordStoreCommandLine === null) {
142 Electron.app.commandLine.appendSwitch("password-store", linux.passwordStore);
143 }
144 // Chromium keeps only the last --disable-features value, so pass the merged list.
145 const disableFeatures = DesktopEarlyElectronStartup.resolveLinuxDisableFeaturesSwitch({
146 env: process.env,
147 ozonePlatform: readCommandLineSwitchValue(Electron.app.commandLine, "ozone-platform"),
148 enableFeatures: readCommandLineSwitchValue(Electron.app.commandLine, "enable-features"),
149 disableFeatures: readCommandLineSwitchValue(Electron.app.commandLine, "disable-features"),
150 readHyprctlVersionJson: () =>
151 NodeChildProcess.execFileSync("hyprctl", ["version", "-j"], {
152 encoding: "utf8",
153 stdio: ["ignore", "pipe", "ignore"],
154 timeout: 1000,
155 maxBuffer: 64 * 1024,
156 }),
157 });
158 if (disableFeatures !== null) {
159 Electron.app.commandLine.appendSwitch("disable-features", disableFeatures);
160 }
161 }
162
163 return { linux, linuxPasswordStoreCommandLine };
164 });
165}).pipe(Effect.withSpan("desktop.electron.configureBeforeReady"));
166
167// Complete scheme privileges and switches before later layers can observe app readiness.
168export const layer = Layer.mergeAll(
169 ElectronProtocol.layerSchemePrivileges,
170 Layer.effect(DesktopPreReadyElectronOptions, make),
171);