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

ElectronUpdater.ts

166 lines5.8 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;
57export const isElectronUpdaterError = Schema.is(ElectronUpdaterError);
58​
59export class ElectronUpdater extends Context.Service<
60 ElectronUpdater,
61 {
62 readonly setFeedURL: (options: ElectronUpdaterFeedUrl) => Effect.Effect<void>;
63 readonly setAutoDownload: (value: boolean) => Effect.Effect<void>;
64 readonly setAutoInstallOnAppQuit: (value: boolean) => Effect.Effect<void>;
65 readonly setChannel: (channel: string) => Effect.Effect<void>;
66 readonly setAllowPrerelease: (value: boolean) => Effect.Effect<void>;
67 readonly allowDowngrade: Effect.Effect<boolean>;
68 readonly setAllowDowngrade: (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 setDisableDifferentialDownload: (value) =>
116 Effect.suspend(() => {
117 autoUpdater.disableDifferentialDownload = value;
118 return Effect.void;
119 }),
120 checkForUpdates: Effect.suspend(() => {
121 const channel = autoUpdater.channel;
122 return Effect.tryPromise({
123 try: () => autoUpdater.checkForUpdates(),
124 catch: (cause) => new ElectronUpdaterCheckForUpdatesError({ channel, cause }),
125 }).pipe(Effect.asVoid);
126 }),
127 downloadUpdate: Effect.suspend(() => {
128 const channel = autoUpdater.channel;
129 return Effect.tryPromise({
130 try: () => autoUpdater.downloadUpdate(),
131 catch: (cause) => new ElectronUpdaterDownloadUpdateError({ channel, cause }),
132 }).pipe(Effect.asVoid);
133 }),
134 quitAndInstall: ({ isSilent, isForceRunAfter }) =>
135 Effect.suspend(() => {
136 const channel = autoUpdater.channel;
137 return Effect.try({
138 try: () => autoUpdater.quitAndInstall(isSilent, isForceRunAfter),
139 catch: (cause) =>
140 new ElectronUpdaterQuitAndInstallError({
141 channel,
142 isSilent,
143 isForceRunAfter,
144 cause,
145 }),
146 });
147 }),
148 on: (eventName, listener) => {
149 const eventTarget = autoUpdater as unknown as {
150 on: (eventName: string, listener: (...args: Array<unknown>) => void) => void;
151 removeListener: (eventName: string, listener: (...args: Array<unknown>) => void) => void;
152 };
153 const untypedListener = listener as unknown as (...args: Array<unknown>) => void;
154 return Effect.acquireRelease(
155 Effect.sync(() => {
156 eventTarget.on(eventName, untypedListener);
157 }),
158 () =>
159 Effect.sync(() => {
160 eventTarget.removeListener(eventName, untypedListener);
161 }),
162 ).pipe(Effect.asVoid);
163 },
164});
165​
166export const layer = Layer.succeed(ElectronUpdater, make);