Open History to see the last commit
1import * as Context from "effect/Context";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Schema from "effect/Schema";
5import * as Scope from "effect/Scope";
6
7import * as Electron from "electron";
8
9export interface ElectronAppMetadata {
10 readonly appVersion: string;
11 readonly appPath: string;
12 readonly isPackaged: boolean;
13 readonly resourcesPath: string;
14 readonly runningUnderArm64Translation: boolean;
15}
16
17export class ElectronAppMetadataReadError extends Schema.TaggedError<ElectronAppMetadataReadError>()(
18 "ElectronAppMetadataReadError",
19 {
20 property: Schema.Literals(["app-version", "app-path"]),
21 cause: Schema.Defect(),
22 },
23) {
24 override get message(): string {
25 return `Failed to read Electron app metadata property "${this.property}".`;
26 }
27}
28
29export class ElectronAppWhenReadyError extends Schema.TaggedError<ElectronAppWhenReadyError>()(
30 "ElectronAppWhenReadyError",
31 {
32 isPackaged: Schema.Boolean,
33 cause: Schema.Defect(),
34 },
35) {
36 override get message(): string {
37 return `Failed to wait for the Electron app to become ready (packaged: ${this.isPackaged}).`;
38 }
39}
40
41export class ElectronApp extends Context.Service<
42 ElectronApp,
43 {
44 readonly metadata: Effect.Effect<ElectronAppMetadata, ElectronAppMetadataReadError>;
45 readonly name: Effect.Effect<string>;
46 /** Packaged builds ship only en-US; app.getLocale() and Intl defaults do not reflect the OS locale. */
47 readonly systemLocale: Effect.Effect<string>;
48 readonly whenReady: Effect.Effect<void, ElectronAppWhenReadyError>;
49 readonly requestSingleInstanceLock: Effect.Effect<boolean>;
50 readonly quit: Effect.Effect<void>;
51 readonly exit: (code: number) => Effect.Effect<void>;
52 readonly relaunch: (options: Electron.RelaunchOptions) => Effect.Effect<void>;
53 readonly setPath: (
54 name: Parameters<Electron.App["setPath"]>[0],
55 path: string,
56 ) => Effect.Effect<void>;
57 readonly setName: (name: string) => Effect.Effect<void>;
58 readonly setAboutPanelOptions: (
59 options: Electron.AboutPanelOptionsOptions,
60 ) => Effect.Effect<void>;
61 readonly setAppUserModelId: (id: string) => Effect.Effect<void>;
62 readonly getAppMetrics: Effect.Effect<ReadonlyArray<Electron.ProcessMetric>>;
63 readonly setAsDefaultProtocolClient: (
64 protocol: string,
65 path?: string,
66 args?: readonly string[],
67 ) => Effect.Effect<boolean>;
68 readonly setDesktopName: (desktopName: string) => Effect.Effect<void>;
69 readonly setDockIcon: (iconPath: string) => Effect.Effect<void>;
70 readonly appendCommandLineSwitch: (switchName: string, value?: string) => Effect.Effect<void>;
71 readonly onBeforeQuitForUpdate: (
72 listener: () => void,
73 ) => Effect.Effect<void, never, Scope.Scope>;
74 readonly removeCommandLineSwitch: (switchName: string) => Effect.Effect<void>;
75 readonly on: <Args extends ReadonlyArray<unknown>>(
76 eventName: string,
77 listener: (...args: Args) => void,
78 ) => Effect.Effect<void, never, Scope.Scope>;
79 }
80>()("@t3tools/desktop/electron/ElectronApp") {}
81
82const addScopedAppListener = <Args extends ReadonlyArray<unknown>>(
83 eventName: string,
84 listener: (...args: Args) => void,
85): Effect.Effect<void, never, Scope.Scope> =>
86 Effect.acquireRelease(
87 Effect.sync(() => {
88 Electron.app.on(eventName as any, listener as any);
89 }),
90 () =>
91 Effect.sync(() => {
92 Electron.app.removeListener(eventName as any, listener as any);
93 }),
94 ).pipe(Effect.asVoid);
95
96/** @public */
97export const make = ElectronApp.of({
98 metadata: Effect.gen(function* () {
99 const appVersion = yield* Effect.try({
100 try: () => Electron.app.getVersion(),
101 catch: (cause) =>
102 new ElectronAppMetadataReadError({
103 property: "app-version",
104 cause,
105 }),
106 });
107 const appPath = yield* Effect.try({
108 try: () => Electron.app.getAppPath(),
109 catch: (cause) =>
110 new ElectronAppMetadataReadError({
111 property: "app-path",
112 cause,
113 }),
114 });
115
116 return {
117 appVersion,
118 appPath,
119 isPackaged: Electron.app.isPackaged,
120 resourcesPath: process.resourcesPath,
121 runningUnderArm64Translation: Electron.app.runningUnderARM64Translation === true,
122 };
123 }),
124 name: Effect.sync(() => Electron.app.name),
125 // NSLocale returns en_GB-style tags; Intl requires hyphens.
126 systemLocale: Effect.sync(() => Electron.app.getSystemLocale().replace(/_/g, "-")),
127 whenReady: Effect.gen(function* () {
128 const isPackaged = Electron.app.isPackaged;
129 yield* Effect.tryPromise({
130 try: () => Electron.app.whenReady(),
131 catch: (cause) => new ElectronAppWhenReadyError({ isPackaged, cause }),
132 });
133 }),
134 requestSingleInstanceLock: Effect.sync(() => Electron.app.requestSingleInstanceLock()),
135 quit: Effect.sync(() => {
136 Electron.app.quit();
137 }),
138 exit: (code) =>
139 Effect.sync(() => {
140 Electron.app.exit(code);
141 }),
142 relaunch: (options) =>
143 Effect.sync(() => {
144 Electron.app.relaunch(options);
145 }),
146 setPath: (name, path) =>
147 Effect.sync(() => {
148 Electron.app.setPath(name, path);
149 }),
150 setName: (name) =>
151 Effect.sync(() => {
152 Electron.app.setName(name);
153 }),
154 setAboutPanelOptions: (options) =>
155 Effect.sync(() => {
156 Electron.app.setAboutPanelOptions(options);
157 }),
158 setAppUserModelId: (id) =>
159 Effect.sync(() => {
160 Electron.app.setAppUserModelId(id);
161 }),
162 getAppMetrics: Effect.sync(() => Electron.app.getAppMetrics()),
163 setAsDefaultProtocolClient: (protocol, path, args) =>
164 Effect.sync(() => {
165 if (path === undefined) {
166 return Electron.app.setAsDefaultProtocolClient(protocol);
167 }
168 return Electron.app.setAsDefaultProtocolClient(protocol, path, [...(args ?? [])]);
169 }),
170 setDesktopName: (desktopName) =>
171 Effect.sync(() => {
172 const linuxApp = Electron.app as Electron.App & {
173 setDesktopName?: (desktopName: string) => void;
174 };
175 linuxApp.setDesktopName?.(desktopName);
176 }),
177 setDockIcon: (iconPath) =>
178 Effect.sync(() => {
179 Electron.app.dock?.setIcon(iconPath);
180 }),
181 appendCommandLineSwitch: (switchName, value) =>
182 Effect.sync(() => {
183 if (value === undefined) {
184 Electron.app.commandLine.appendSwitch(switchName);
185 return;
186 }
187 Electron.app.commandLine.appendSwitch(switchName, value);
188 }),
189 onBeforeQuitForUpdate: (listener) =>
190 Effect.acquireRelease(
191 Effect.sync(() => {
192 Electron.autoUpdater.on("before-quit-for-update", listener);
193 }),
194 () =>
195 Effect.sync(() => {
196 Electron.autoUpdater.removeListener("before-quit-for-update", listener);
197 }),
198 ).pipe(Effect.asVoid),
199 removeCommandLineSwitch: (switchName) =>
200 Effect.sync(() => {
201 Electron.app.commandLine.removeSwitch(switchName);
202 }),
203 on: addScopedAppListener,
204});
205
206export const layer = Layer.succeed(ElectronApp, make);