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

ElectronUpdater.ts

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