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

merge-update-manifests.ts

108 lines3.4 KB

1#!/usr/bin/env node
2​
3import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
4import * as NodeServices from "@effect/platform-node/NodeServices";
5import { Effect, FileSystem, Option, Path, Schema } from "effect";
6import { Argument, Command, Flag } from "effect/unstable/cli";
7​
8import {
9 mergeUpdateManifests,
10 parseUpdateManifest,
11 serializeUpdateManifest,
12 type UpdateManifest,
13} from "./lib/update-manifest.ts";
14​
15const UpdateManifestPlatform = Schema.Literals(["mac", "win"]);
16export type UpdateManifestPlatform = typeof UpdateManifestPlatform.Type;
17​
18function getPlatformLabel(platform: UpdateManifestPlatform): string {
19 return platform === "mac" ? "macOS" : "Windows";
21​
22export function parsePlatformUpdateManifest(
23 platform: UpdateManifestPlatform,
24 raw: string,
25 sourcePath: string,
26): UpdateManifest {
27 return parseUpdateManifest(raw, sourcePath, getPlatformLabel(platform));
29​
30export function mergePlatformUpdateManifests(
31 platform: UpdateManifestPlatform,
32 primary: UpdateManifest,
33 secondary: UpdateManifest,
34): UpdateManifest {
35 return mergeUpdateManifests(primary, secondary, getPlatformLabel(platform));
37​
38export function serializePlatformUpdateManifest(
39 platform: UpdateManifestPlatform,
40 manifest: UpdateManifest,
41): string {
42 return serializeUpdateManifest(manifest, {
43 platformLabel: getPlatformLabel(platform),
44 });
46​
47export const mergeUpdateManifestFiles = Effect.fn("mergeUpdateManifestFiles")(function* (
48 platform: UpdateManifestPlatform,
49 primaryPathArg: string,
50 secondaryPathArg: string,
51 outputPathArg: string | undefined,
52) {
53 const fs = yield* FileSystem.FileSystem;
54 const path = yield* Path.Path;
55​
56 const primaryPath = path.resolve(primaryPathArg);
57 const secondaryPath = path.resolve(secondaryPathArg);
58 const outputPath = path.resolve(outputPathArg ?? primaryPathArg);
59​
60 const primaryManifest = parsePlatformUpdateManifest(
61 platform,
62 yield* fs.readFileString(primaryPath),
63 primaryPath,
64 );
65 const secondaryManifest = parsePlatformUpdateManifest(
66 platform,
67 yield* fs.readFileString(secondaryPath),
68 secondaryPath,
69 );
70 const merged = mergePlatformUpdateManifests(platform, primaryManifest, secondaryManifest);
71​
72 yield* fs.writeFileString(outputPath, serializePlatformUpdateManifest(platform, merged));
73});
74​
75export const mergeUpdateManifestsCommand = Command.make(
76 "merge-update-manifests",
77 {
78 platform: Flag.choice("platform", UpdateManifestPlatform.literals).pipe(
79 Flag.withDescription("Update manifest platform."),
80 ),
81 primaryPath: Argument.string("primary-path").pipe(
82 Argument.withDescription("Primary update manifest path. Defaults to the output path."),
83 ),
84 secondaryPath: Argument.string("secondary-path").pipe(
85 Argument.withDescription(
86 "Secondary update manifest path to merge into the primary manifest.",
87 ),
88 ),
89 outputPath: Argument.string("output-path").pipe(
90 Argument.withDescription("Optional output path for the merged manifest."),
91 Argument.optional,
92 ),
93 },
94 ({ platform, primaryPath, secondaryPath, outputPath }) =>
95 mergeUpdateManifestFiles(
96 platform,
97 primaryPath,
98 secondaryPath,
99 Option.getOrUndefined(outputPath),
100 ),
101).pipe(Command.withDescription("Merge two Electron updater manifests into a multi-arch manifest."));
102​
103if (import.meta.main) {
104 Command.run(mergeUpdateManifestsCommand, { version: "0.0.0" }).pipe(
105 Effect.provide(NodeServices.layer),
106 NodeRuntime.runMain,
107 );