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

merge-update-manifests.ts

91 lines2.7 KB

1import { readFileSync, writeFileSync } from "node:fs";
2import { resolve } from "node:path";
3import { fileURLToPath } from "node:url";
4​
5import {
6 mergeUpdateManifests,
7 parseUpdateManifest,
8 serializeUpdateManifest,
9 type UpdateManifest,
10} from "./lib/update-manifest.ts";
11​
12export type UpdateManifestPlatform = "mac" | "win";
13​
14function getPlatformLabel(platform: UpdateManifestPlatform): string {
15 return platform === "mac" ? "macOS" : "Windows";
17​
18export function parsePlatformUpdateManifest(
19 platform: UpdateManifestPlatform,
20 raw: string,
21 sourcePath: string,
22): UpdateManifest {
23 return parseUpdateManifest(raw, sourcePath, getPlatformLabel(platform));
25​
26export function mergePlatformUpdateManifests(
27 platform: UpdateManifestPlatform,
28 primary: UpdateManifest,
29 secondary: UpdateManifest,
30): UpdateManifest {
31 return mergeUpdateManifests(primary, secondary, getPlatformLabel(platform));
33​
34export function serializePlatformUpdateManifest(
35 platform: UpdateManifestPlatform,
36 manifest: UpdateManifest,
37): string {
38 return serializeUpdateManifest(manifest, {
39 platformLabel: getPlatformLabel(platform),
40 });
42​
43function parseArgs(args: ReadonlyArray<string>): {
44 platform: UpdateManifestPlatform;
45 primaryPath: string;
46 secondaryPath: string;
47 outputPath: string;
48} {
49 const [platformFlag, platformValue, primaryPathArg, secondaryPathArg, outputPathArg] = args;
50 if (platformFlag !== "--platform" || (platformValue !== "mac" && platformValue !== "win")) {
51 throw new Error(
52 "Usage: node scripts/merge-update-manifests.ts --platform <mac|win> <primary-path> <secondary-path> [output-path]",
53 );
54 }
55 if (!primaryPathArg || !secondaryPathArg) {
56 throw new Error(
57 "Usage: node scripts/merge-update-manifests.ts --platform <mac|win> <primary-path> <secondary-path> [output-path]",
58 );
59 }
60​
61 const primaryPath = resolve(primaryPathArg);
62 const secondaryPath = resolve(secondaryPathArg);
63 const outputPath = resolve(outputPathArg ?? primaryPathArg);
64​
65 return {
66 platform: platformValue,
67 primaryPath,
68 secondaryPath,
69 outputPath,
70 };
72​
73function main(args: ReadonlyArray<string>): void {
74 const { platform, primaryPath, secondaryPath, outputPath } = parseArgs(args);
75 const primaryManifest = parsePlatformUpdateManifest(
76 platform,
77 readFileSync(primaryPath, "utf8"),
78 primaryPath,
79 );
80 const secondaryManifest = parsePlatformUpdateManifest(
81 platform,
82 readFileSync(secondaryPath, "utf8"),
83 secondaryPath,
84 );
85 const merged = mergePlatformUpdateManifests(platform, primaryManifest, secondaryManifest);
86 writeFileSync(outputPath, serializePlatformUpdateManifest(platform, merged));
88​
89if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
90 main(process.argv.slice(2));