@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 207 lines 7.8 KB
Raw
1 import * as Context from "effect/Context";
2 import * as Effect from "effect/Effect";
3 import * as Layer from "effect/Layer";
4 import * as Schema from "effect/Schema";
5 import * as Scope from "effect/Scope";
6
7 import { AppImageUpdater, autoUpdater } from "electron-updater";
8
9 type AutoUpdater = typeof autoUpdater;
10 type SpawnLog = (cmd: string, args?: Array<string>, ...rest: Array<unknown>) => Promise<boolean>;
11
12 // Electron holds the AppImage runtime's keepalive pipe and some files without O_CLOEXEC; a
13 // relaunched AppImage inherits them, so old runtimes and FUSE mounts never exit. bash (AppRun
14 // already needs it; dash cannot close multi-digit fds) closes them before exec'ing the AppImage.
15 // -p skips BASH_ENV and exported functions in the helper only; the AppImage still receives both.
16 const CLOSE_INHERITED_FDS_THEN_EXEC =
17 'for fd in /proc/self/fd/*; do fd=${fd##*/}; case $fd in *[!0-9]*|[012]) ;; *) eval "exec $fd>&-" ;; esac; done; exec "$@"';
18
19 const relaunchWithoutInheritedDescriptors = (updater: AppImageUpdater) => {
20 const spawnLog = (AppImageUpdater.prototype as unknown as { spawnLog: SpawnLog }).spawnLog;
21 (updater as unknown as { spawnLog: SpawnLog }).spawnLog = function (cmd, args = [], ...rest) {
22 return spawnLog.call(
23 this,
24 "bash",
25 ["-p", "-c", CLOSE_INHERITED_FDS_THEN_EXEC, cmd, cmd, ...args],
26 ...rest,
27 );
28 };
29 };
30
31 export type ElectronUpdaterFeedUrl = Parameters<AutoUpdater["setFeedURL"]>[0];
32
33 export class ElectronUpdaterCheckForUpdatesError extends Schema.TaggedError<ElectronUpdaterCheckForUpdatesError>()(
34 "ElectronUpdaterCheckForUpdatesError",
35 {
36 channel: Schema.NullOr(Schema.String),
37 cause: Schema.Defect(),
38 },
39 ) {
40 override get message(): string {
41 return `Electron updater failed to check for updates on channel ${this.channel ?? "default"}.`;
42 }
43 }
44
45 export class ElectronUpdaterDownloadUpdateError extends Schema.TaggedError<ElectronUpdaterDownloadUpdateError>()(
46 "ElectronUpdaterDownloadUpdateError",
47 {
48 channel: Schema.NullOr(Schema.String),
49 cause: Schema.Defect(),
50 },
51 ) {
52 override get message(): string {
53 return `Electron updater failed to download the update on channel ${this.channel ?? "default"}.`;
54 }
55 }
56
57 export class ElectronUpdaterQuitAndInstallError extends Schema.TaggedError<ElectronUpdaterQuitAndInstallError>()(
58 "ElectronUpdaterQuitAndInstallError",
59 {
60 channel: Schema.NullOr(Schema.String),
61 isSilent: Schema.Boolean,
62 isForceRunAfter: Schema.Boolean,
63 cause: Schema.Defect(),
64 },
65 ) {
66 override get message(): string {
67 return `Electron updater failed to quit and install the update on channel ${this.channel ?? "default"} (silent: ${this.isSilent}, force run after: ${this.isForceRunAfter}).`;
68 }
69 }
70
71 export const ElectronUpdaterError = Schema.Union([
72 ElectronUpdaterCheckForUpdatesError,
73 ElectronUpdaterDownloadUpdateError,
74 ElectronUpdaterQuitAndInstallError,
75 ]);
76 export type ElectronUpdaterError = typeof ElectronUpdaterError.Type;
77
78 export class ElectronUpdater extends Context.Service<
79 ElectronUpdater,
80 {
81 readonly setFeedURL: (options: ElectronUpdaterFeedUrl) => Effect.Effect<void>;
82 readonly setAutoDownload: (value: boolean) => Effect.Effect<void>;
83 readonly setAutoInstallOnAppQuit: (value: boolean) => Effect.Effect<void>;
84 readonly setChannel: (channel: string) => Effect.Effect<void>;
85 readonly setAllowPrerelease: (value: boolean) => Effect.Effect<void>;
86 readonly allowDowngrade: Effect.Effect<boolean>;
87 readonly setAllowDowngrade: (value: boolean) => Effect.Effect<void>;
88 readonly setFullChangelog: (value: boolean) => Effect.Effect<void>;
89 readonly setDisableDifferentialDownload: (value: boolean) => Effect.Effect<void>;
90 readonly checkForUpdates: Effect.Effect<void, ElectronUpdaterCheckForUpdatesError>;
91 readonly downloadUpdate: Effect.Effect<void, ElectronUpdaterDownloadUpdateError>;
92 readonly quitAndInstall: (options: {
93 readonly isSilent: boolean;
94 readonly isForceRunAfter: boolean;
95 }) => Effect.Effect<void, ElectronUpdaterQuitAndInstallError>;
96 readonly on: <Args extends ReadonlyArray<unknown>>(
97 eventName: string,
98 listener: (...args: Args) => void,
99 ) => Effect.Effect<void, never, Scope.Scope>;
100 }
101 >()("@t3tools/desktop/electron/ElectronUpdater") {}
102
103 /** @public */
104 export const make = ElectronUpdater.of({
105 setFeedURL: Effect.fn("ElectronUpdater.setFeedURL")(function (options) {
106 return Effect.suspend(() => {
107 autoUpdater.setFeedURL(options);
108 return Effect.void;
109 });
110 }),
111 setAutoDownload: Effect.fn("ElectronUpdater.setAutoDownload")(function (value) {
112 return Effect.suspend(() => {
113 autoUpdater.autoDownload = value;
114 return Effect.void;
115 });
116 }),
117 setAutoInstallOnAppQuit: Effect.fn("ElectronUpdater.setAutoInstallOnAppQuit")(function (value) {
118 return Effect.suspend(() => {
119 autoUpdater.autoInstallOnAppQuit = value;
120 return Effect.void;
121 });
122 }),
123 setChannel: Effect.fn("ElectronUpdater.setChannel")(function (channel) {
124 return Effect.suspend(() => {
125 autoUpdater.channel = channel;
126 return Effect.void;
127 });
128 }),
129 setAllowPrerelease: Effect.fn("ElectronUpdater.setAllowPrerelease")(function (value) {
130 return Effect.suspend(() => {
131 autoUpdater.allowPrerelease = value;
132 return Effect.void;
133 });
134 }),
135 allowDowngrade: Effect.sync(() => autoUpdater.allowDowngrade),
136 setAllowDowngrade: Effect.fn("ElectronUpdater.setAllowDowngrade")(function (value) {
137 return Effect.suspend(() => {
138 autoUpdater.allowDowngrade = value;
139 return Effect.void;
140 });
141 }),
142 setFullChangelog: Effect.fn("ElectronUpdater.setFullChangelog")(function (value) {
143 return Effect.suspend(() => {
144 autoUpdater.fullChangelog = value;
145 return Effect.void;
146 });
147 }),
148 setDisableDifferentialDownload: Effect.fn("ElectronUpdater.setDisableDifferentialDownload")(
149 function (value) {
150 return Effect.suspend(() => {
151 autoUpdater.disableDifferentialDownload = value;
152 return Effect.void;
153 });
154 },
155 ),
156 checkForUpdates: Effect.suspend(() => {
157 const channel = autoUpdater.channel;
158 return Effect.tryPromise({
159 try: () => autoUpdater.checkForUpdates(),
160 catch: (cause) => new ElectronUpdaterCheckForUpdatesError({ channel, cause }),
161 }).pipe(Effect.asVoid);
162 }),
163 downloadUpdate: Effect.suspend(() => {
164 const channel = autoUpdater.channel;
165 return Effect.tryPromise({
166 try: () => autoUpdater.downloadUpdate(),
167 catch: (cause) => new ElectronUpdaterDownloadUpdateError({ channel, cause }),
168 }).pipe(Effect.asVoid);
169 }),
170 quitAndInstall: Effect.fn("ElectronUpdater.quitAndInstall")(function ({
171 isSilent,
172 isForceRunAfter,
173 }) {
174 return Effect.suspend(() => {
175 const channel = autoUpdater.channel;
176 if (autoUpdater instanceof AppImageUpdater) relaunchWithoutInheritedDescriptors(autoUpdater);
177 return Effect.try({
178 try: () => autoUpdater.quitAndInstall(isSilent, isForceRunAfter),
179 catch: (cause) =>
180 new ElectronUpdaterQuitAndInstallError({
181 channel,
182 isSilent,
183 isForceRunAfter,
184 cause,
185 }),
186 });
187 });
188 }),
189 on: Effect.fn("ElectronUpdater.on")(function (eventName, listener) {
190 const eventTarget = autoUpdater as unknown as {
191 on: (eventName: string, listener: (...args: Array<unknown>) => void) => void;
192 removeListener: (eventName: string, listener: (...args: Array<unknown>) => void) => void;
193 };
194 const untypedListener = listener as unknown as (...args: Array<unknown>) => void;
195 return Effect.acquireRelease(
196 Effect.sync(() => {
197 eventTarget.on(eventName, untypedListener);
198 }),
199 () =>
200 Effect.sync(() => {
201 eventTarget.removeListener(eventName, untypedListener);
202 }),
203 ).pipe(Effect.asVoid);
204 }),
205 });
206
207 export const layer = Layer.succeed(ElectronUpdater, make);