Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ElectronUpdater.ts

172 lines6.0 KB

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 { autoUpdater } from "electron-updater";
8​
9type AutoUpdater = typeof autoUpdater;
10​
11export type ElectronUpdaterFeedUrl = Parameters<AutoUpdater["setFeedURL"]>[0];
12​
13export class ElectronUpdaterCheckForUpdatesError extends Schema.TaggedError<ElectronUpdaterCheckForUpdatesError>()(
14 "ElectronUpdaterCheckForUpdatesError",
15 {
16 channel: Schema.NullOr(Schema.String),
17 cause: Schema.Defect(),
18 },
19) {
20 override get message(): string {
21 return `Electron updater failed to check for updates on channel ${this.channel ?? "default"}.`;
22 }
24​
25export class ElectronUpdaterDownloadUpdateError extends Schema.TaggedError<ElectronUpdaterDownloadUpdateError>()(
26 "ElectronUpdaterDownloadUpdateError",
27 {
28 channel: Schema.NullOr(Schema.String),
29 cause: Schema.Defect(),
30 },
31) {
32 override get message(): string {
33 return `Electron updater failed to download the update on channel ${this.channel ?? "default"}.`;
34 }
36​
37export class ElectronUpdaterQuitAndInstallError extends Schema.TaggedError<ElectronUpdaterQuitAndInstallError>()(
38 "ElectronUpdaterQuitAndInstallError",
39 {
40 channel: Schema.NullOr(Schema.String),
41 isSilent: Schema.Boolean,
42 isForceRunAfter: Schema.Boolean,
43 cause: Schema.Defect(),
44 },
45) {
46 override get message(): string {
47 return `Electron updater failed to quit and install the update on channel ${this.channel ?? "default"} (silent: ${this.isSilent}, force run after: ${this.isForceRunAfter}).`;
48 }
50​
51export const ElectronUpdaterError = Schema.Union([
52 ElectronUpdaterCheckForUpdatesError,
53 ElectronUpdaterDownloadUpdateError,
54 ElectronUpdaterQuitAndInstallError,
55]);
56export type ElectronUpdaterError = typeof ElectronUpdaterError.Type;
57​
58export class ElectronUpdater extends Context.Service<
59 ElectronUpdater,
60 {
61 readonly setFeedURL: (options: ElectronUpdaterFeedUrl) => Effect.Effect<void>;
62 readonly setAutoDownload: (value: boolean) => Effect.Effect<void>;
63 readonly setAutoInstallOnAppQuit: (value: boolean) => Effect.Effect<void>;
64 readonly setChannel: (channel: string) => Effect.Effect<void>;
65 readonly setAllowPrerelease: (value: boolean) => Effect.Effect<void>;
66 readonly allowDowngrade: Effect.Effect<boolean>;
67 readonly setAllowDowngrade: (value: boolean) => Effect.Effect<void>;
68 readonly setFullChangelog: (value: boolean) => Effect.Effect<void>;
69 readonly setDisableDifferentialDownload: (value: boolean) => Effect.Effect<void>;
70 readonly checkForUpdates: Effect.Effect<void, ElectronUpdaterCheckForUpdatesError>;
71 readonly downloadUpdate: Effect.Effect<void, ElectronUpdaterDownloadUpdateError>;
72 readonly quitAndInstall: (options: {
73 readonly isSilent: boolean;
74 readonly isForceRunAfter: boolean;
75 }) => Effect.Effect<void, ElectronUpdaterQuitAndInstallError>;
76 readonly on: <Args extends ReadonlyArray<unknown>>(
77 eventName: string,
78 listener: (...args: Args) => void,
79 ) => Effect.Effect<void, never, Scope.Scope>;
80 }
81>()("@t3tools/desktop/electron/ElectronUpdater") {}
82​
83/** @public */
84export const make = ElectronUpdater.of({
85 setFeedURL: (options) =>
86 Effect.suspend(() => {
87 autoUpdater.setFeedURL(options);
88 return Effect.void;
89 }),
90 setAutoDownload: (value) =>
91 Effect.suspend(() => {
92 autoUpdater.autoDownload = value;
93 return Effect.void;
94 }),
95 setAutoInstallOnAppQuit: (value) =>
96 Effect.suspend(() => {
97 autoUpdater.autoInstallOnAppQuit = value;
98 return Effect.void;
99 }),
100 setChannel: (channel) =>
101 Effect.suspend(() => {
102 autoUpdater.channel = channel;
103 return Effect.void;
104 }),
105 setAllowPrerelease: (value) =>
106 Effect.suspend(() => {
107 autoUpdater.allowPrerelease = value;
108 return Effect.void;
109 }),
110 allowDowngrade: Effect.sync(() => autoUpdater.allowDowngrade),
111 setAllowDowngrade: (value) =>
112 Effect.suspend(() => {
113 autoUpdater.allowDowngrade = value;
114 return Effect.void;
115 }),
116 setFullChangelog: (value) =>
117 Effect.suspend(() => {
118 autoUpdater.fullChangelog = value;
119 return Effect.void;
120 }),
121 setDisableDifferentialDownload: (value) =>
122 Effect.suspend(() => {
123 autoUpdater.disableDifferentialDownload = value;
124 return Effect.void;
125 }),
126 checkForUpdates: Effect.suspend(() => {
127 const channel = autoUpdater.channel;
128 return Effect.tryPromise({
129 try: () => autoUpdater.checkForUpdates(),
130 catch: (cause) => new ElectronUpdaterCheckForUpdatesError({ channel, cause }),
131 }).pipe(Effect.asVoid);
132 }),
133 downloadUpdate: Effect.suspend(() => {
134 const channel = autoUpdater.channel;
135 return Effect.tryPromise({
136 try: () => autoUpdater.downloadUpdate(),
137 catch: (cause) => new ElectronUpdaterDownloadUpdateError({ channel, cause }),
138 }).pipe(Effect.asVoid);
139 }),
140 quitAndInstall: ({ isSilent, isForceRunAfter }) =>
141 Effect.suspend(() => {
142 const channel = autoUpdater.channel;
143 return Effect.try({
144 try: () => autoUpdater.quitAndInstall(isSilent, isForceRunAfter),
145 catch: (cause) =>
146 new ElectronUpdaterQuitAndInstallError({
147 channel,
148 isSilent,
149 isForceRunAfter,
150 cause,
151 }),
152 });
153 }),
154 on: (eventName, listener) => {
155 const eventTarget = autoUpdater as unknown as {
156 on: (eventName: string, listener: (...args: Array<unknown>) => void) => void;
157 removeListener: (eventName: string, listener: (...args: Array<unknown>) => void) => void;
158 };
159 const untypedListener = listener as unknown as (...args: Array<unknown>) => void;
160 return Effect.acquireRelease(
161 Effect.sync(() => {
162 eventTarget.on(eventName, untypedListener);
163 }),
164 () =>
165 Effect.sync(() => {
166 eventTarget.removeListener(eventName, untypedListener);
167 }),
168 ).pipe(Effect.asVoid);
169 },
170});
171​
172export const layer = Layer.succeed(ElectronUpdater, make);