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

ElectronUpdater.ts

186 lines6.6 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: Effect.fn("ElectronUpdater.setFeedURL")(function (options) {
86 return Effect.suspend(() => {
87 autoUpdater.setFeedURL(options);
88 return Effect.void;
89 });
90 }),
91 setAutoDownload: Effect.fn("ElectronUpdater.setAutoDownload")(function (value) {
92 return Effect.suspend(() => {
93 autoUpdater.autoDownload = value;
94 return Effect.void;
95 });
96 }),
97 setAutoInstallOnAppQuit: Effect.fn("ElectronUpdater.setAutoInstallOnAppQuit")(function (value) {
98 return Effect.suspend(() => {
99 autoUpdater.autoInstallOnAppQuit = value;
100 return Effect.void;
101 });
102 }),
103 setChannel: Effect.fn("ElectronUpdater.setChannel")(function (channel) {
104 return Effect.suspend(() => {
105 autoUpdater.channel = channel;
106 return Effect.void;
107 });
108 }),
109 setAllowPrerelease: Effect.fn("ElectronUpdater.setAllowPrerelease")(function (value) {
110 return Effect.suspend(() => {
111 autoUpdater.allowPrerelease = value;
112 return Effect.void;
113 });
114 }),
115 allowDowngrade: Effect.sync(() => autoUpdater.allowDowngrade),
116 setAllowDowngrade: Effect.fn("ElectronUpdater.setAllowDowngrade")(function (value) {
117 return Effect.suspend(() => {
118 autoUpdater.allowDowngrade = value;
119 return Effect.void;
120 });
121 }),
122 setFullChangelog: Effect.fn("ElectronUpdater.setFullChangelog")(function (value) {
123 return Effect.suspend(() => {
124 autoUpdater.fullChangelog = value;
125 return Effect.void;
126 });
127 }),
128 setDisableDifferentialDownload: Effect.fn("ElectronUpdater.setDisableDifferentialDownload")(
129 function (value) {
130 return Effect.suspend(() => {
131 autoUpdater.disableDifferentialDownload = value;
132 return Effect.void;
133 });
134 },
135 ),
136 checkForUpdates: Effect.suspend(() => {
137 const channel = autoUpdater.channel;
138 return Effect.tryPromise({
139 try: () => autoUpdater.checkForUpdates(),
140 catch: (cause) => new ElectronUpdaterCheckForUpdatesError({ channel, cause }),
141 }).pipe(Effect.asVoid);
142 }),
143 downloadUpdate: Effect.suspend(() => {
144 const channel = autoUpdater.channel;
145 return Effect.tryPromise({
146 try: () => autoUpdater.downloadUpdate(),
147 catch: (cause) => new ElectronUpdaterDownloadUpdateError({ channel, cause }),
148 }).pipe(Effect.asVoid);
149 }),
150 quitAndInstall: Effect.fn("ElectronUpdater.quitAndInstall")(function ({
151 isSilent,
152 isForceRunAfter,
153 }) {
154 return Effect.suspend(() => {
155 const channel = autoUpdater.channel;
156 return Effect.try({
157 try: () => autoUpdater.quitAndInstall(isSilent, isForceRunAfter),
158 catch: (cause) =>
159 new ElectronUpdaterQuitAndInstallError({
160 channel,
161 isSilent,
162 isForceRunAfter,
163 cause,
164 }),
165 });
166 });
167 }),
168 on: Effect.fn("ElectronUpdater.on")(function (eventName, listener) {
169 const eventTarget = autoUpdater as unknown as {
170 on: (eventName: string, listener: (...args: Array<unknown>) => void) => void;
171 removeListener: (eventName: string, listener: (...args: Array<unknown>) => void) => void;
172 };
173 const untypedListener = listener as unknown as (...args: Array<unknown>) => void;
174 return Effect.acquireRelease(
175 Effect.sync(() => {
176 eventTarget.on(eventName, untypedListener);
177 }),
178 () =>
179 Effect.sync(() => {
180 eventTarget.removeListener(eventName, untypedListener);
181 }),
182 ).pipe(Effect.asVoid);
183 }),
184});
185​
186export const layer = Layer.succeed(ElectronUpdater, make);