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

main
ts 137 lines 4.67 KB
Raw
1 #!/usr/bin/env node
2
3 import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
4 import * as NodeServices from "@effect/platform-node/NodeServices";
5 import * as Effect from "effect/Effect";
6 import * as FileSystem from "effect/FileSystem";
7 import * as Path from "effect/Path";
8 import { Argument, Command } from "effect/cli";
9
10 import { mergeUpdateManifestFiles, type UpdateManifestPlatform } from "./merge-update-manifests.ts";
11
12 interface ReleaseUpdateManifestMerge {
13 readonly platform: UpdateManifestPlatform;
14 readonly primary: string;
15 readonly secondary: string;
16 readonly output: string;
17 }
18
19 function pairArchitectures(
20 platformLabel: string,
21 arm64: ReadonlyMap<string, string>,
22 x64: ReadonlyMap<string, string>,
23 ): ReadonlyArray<{ readonly channel: string; readonly arm64: string; readonly x64: string }> {
24 const channels = new Set([...arm64.keys(), ...x64.keys()]);
25 if (channels.size === 0) {
26 throw new Error(
27 `Incomplete ${platformLabel} updater manifests: no architecture manifests found.`,
28 );
29 }
30 return [...channels].toSorted().map((channel) => {
31 const arm64Manifest = arm64.get(channel);
32 const x64Manifest = x64.get(channel);
33 if (arm64Manifest === undefined || x64Manifest === undefined) {
34 const missing = arm64Manifest === undefined ? "arm64" : "x64";
35 throw new Error(
36 `Incomplete ${platformLabel} updater manifests: ${channel} is missing its ${missing} manifest.`,
37 );
38 }
39 return { channel, arm64: arm64Manifest, x64: x64Manifest };
40 });
41 }
42
43 function planReleaseUpdateManifestMerges(
44 fileNames: ReadonlyArray<string>,
45 ): ReadonlyArray<ReleaseUpdateManifestMerge> {
46 const macArm64 = new Map<string, string>();
47 const macX64 = new Map<string, string>();
48 const winArm64 = new Map<string, string>();
49 const winX64 = new Map<string, string>();
50
51 for (const fileName of fileNames) {
52 if (fileName.startsWith("builder-debug")) continue;
53
54 const macArm64Match = /^(.+)-mac\.yml$/u.exec(fileName);
55 if (macArm64Match?.[1] !== undefined) {
56 macArm64.set(macArm64Match[1], fileName);
57 continue;
58 }
59 const macX64Match = /^(.+)-mac-x64\.yml$/u.exec(fileName);
60 if (macX64Match?.[1] !== undefined) {
61 macX64.set(macX64Match[1], fileName);
62 continue;
63 }
64 const winMatch = /^(.+)-win-(arm64|x64)\.yml$/u.exec(fileName);
65 if (winMatch?.[1] !== undefined) {
66 (winMatch[2] === "arm64" ? winArm64 : winX64).set(winMatch[1], fileName);
67 }
68 }
69
70 // electron-builder names the macOS arm64 manifest without an arch suffix,
71 // so the merged macOS feed keeps that name; Windows gets `<channel>.yml`.
72 return [
73 ...pairArchitectures("macOS", macArm64, macX64).map(
74 ({ arm64, x64 }): ReleaseUpdateManifestMerge => ({
75 platform: "mac",
76 primary: arm64,
77 secondary: x64,
78 output: arm64,
79 }),
80 ),
81 ...pairArchitectures("Windows", winArm64, winX64).map(
82 ({ channel, arm64, x64 }): ReleaseUpdateManifestMerge => ({
83 platform: "win",
84 primary: x64,
85 secondary: arm64,
86 output: `${channel}.yml`,
87 }),
88 ),
89 ];
90 }
91
92 export const mergeReleaseUpdateManifestFiles = Effect.fn("mergeReleaseUpdateManifestFiles")(
93 function* (releaseAssetsDirArg: string) {
94 const fs = yield* FileSystem.FileSystem;
95 const path = yield* Path.Path;
96 const releaseAssetsDir = path.resolve(releaseAssetsDirArg);
97 const plans = planReleaseUpdateManifestMerges(yield* fs.readDirectory(releaseAssetsDir));
98
99 yield* Effect.forEach(
100 plans,
101 (plan) => {
102 const primary = path.join(releaseAssetsDir, plan.primary);
103 const secondary = path.join(releaseAssetsDir, plan.secondary);
104 const output = path.join(releaseAssetsDir, plan.output);
105 return mergeUpdateManifestFiles(plan.platform, primary, secondary, output).pipe(
106 Effect.andThen(
107 Effect.forEach(
108 [primary, secondary].filter((source) => source !== output),
109 (source) => fs.remove(source),
110 { discard: true },
111 ),
112 ),
113 );
114 },
115 { discard: true },
116 );
117 },
118 );
119
120 export const mergeReleaseUpdateManifestsCommand = Command.make(
121 "merge-release-update-manifests",
122 {
123 releaseAssetsDir: Argument.String("release-assets-dir").pipe(
124 Argument.withDescription("Directory containing every desktop release artifact."),
125 ),
126 },
127 ({ releaseAssetsDir }) => mergeReleaseUpdateManifestFiles(releaseAssetsDir),
128 ).pipe(
129 Command.withDescription("Require and merge complete arm64/x64 desktop updater manifest pairs."),
130 );
131
132 if (import.meta.main) {
133 Command.run(mergeReleaseUpdateManifestsCommand, { version: "0.0.0" }).pipe(
134 Effect.provide(NodeServices.layer),
135 NodeRuntime.runMain,
136 );
137 }