Open History to see the last commit
1#!/usr/bin/env node
2
3import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
4import * as NodeServices from "@effect/platform-node/NodeServices";
5import * as Console from "effect/Console";
6import * as Config from "effect/Config";
7import * as Effect from "effect/Effect";
8import * as FileSystem from "effect/FileSystem";
9import * as Option from "effect/Option";
10import * as Path from "effect/Path";
11import * as Schema from "effect/Schema";
12import { Command, Flag } from "effect/unstable/cli";
13
14export interface NightlyReleaseMetadata {
15 readonly baseVersion: string;
16 readonly version: string;
17 readonly tag: string;
18 readonly name: string;
19 readonly shortSha: string;
20}
21
22const DateSchema = Schema.String.check(Schema.isPattern(/^\d{8}$/));
23const RunNumberSchema = Schema.FiniteFromString.check(
24 Schema.isInt(),
25 Schema.isGreaterThanOrEqualTo(1),
26);
27const ShaSchema = Schema.String.check(Schema.isPattern(/^[0-9a-f]{7,40}$/i));
28const DesktopPackageJsonSchema = Schema.Struct({
29 version: Schema.NonEmptyString,
30});
31
32export class InvalidDesktopPackageVersionError extends Schema.TaggedErrorClass<InvalidDesktopPackageVersionError>()(
33 "InvalidDesktopPackageVersionError",
34 {
35 version: Schema.String,
36 },
37) {
38 override get message(): string {
39 return `Invalid desktop package version '${this.version}'.`;
40 }
41}
42
43export class NightlyReleaseDesktopPackageError extends Schema.TaggedErrorClass<NightlyReleaseDesktopPackageError>()(
44 "NightlyReleaseDesktopPackageError",
45 {
46 operation: Schema.Literals(["read", "decode"]),
47 packageJsonPath: Schema.String,
48 cause: Schema.Defect(),
49 },
50) {
51 override get message(): string {
52 return `Failed to ${this.operation} desktop package metadata at ${this.packageJsonPath}.`;
53 }
54}
55
56export class NightlyReleaseGitHubOutputConfigError extends Schema.TaggedErrorClass<NightlyReleaseGitHubOutputConfigError>()(
57 "NightlyReleaseGitHubOutputConfigError",
58 {
59 cause: Schema.Defect(),
60 },
61) {
62 override get message(): string {
63 return "Failed to resolve the GITHUB_OUTPUT path for nightly release metadata.";
64 }
65}
66
67export class NightlyReleaseGitHubOutputAppendError extends Schema.TaggedErrorClass<NightlyReleaseGitHubOutputAppendError>()(
68 "NightlyReleaseGitHubOutputAppendError",
69 {
70 outputPath: Schema.String,
71 cause: Schema.Defect(),
72 },
73) {
74 override get message(): string {
75 return `Failed to append nightly release metadata to ${this.outputPath}.`;
76 }
77}
78
79const RepoRoot = Effect.service(Path.Path).pipe(
80 Effect.flatMap((path) => path.fromFileUrl(new URL("..", import.meta.url))),
81);
82const decodeDesktopPackageJson = Schema.decodeUnknownEffect(
83 Schema.fromJsonString(DesktopPackageJsonSchema),
84);
85
86export const resolveNightlyBaseVersion = (version: string) => version.replace(/[-+].*$/, "");
87
88export const resolveNightlyTargetVersion = (version: string) => {
89 const stableCore = resolveNightlyBaseVersion(version);
90 const match = /^(\d+)\.(\d+)\.(\d+)$/.exec(stableCore);
91 if (!match) {
92 return Effect.fail(new InvalidDesktopPackageVersionError({ version }));
93 }
94
95 const [, major, minor, patch] = match;
96 return Effect.succeed(`${major}.${minor}.${Number(patch) + 1}`);
97};
98
99export const resolveNightlyReleaseMetadata = (
100 baseVersion: string,
101 date: string,
102 runNumber: number,
103 sha: string,
104) => {
105 const shortSha = sha.slice(0, 12);
106 const version = `${baseVersion}-nightly.${date}.${runNumber}`;
107 return {
108 baseVersion,
109 version,
110 tag: `v${version}`,
111 name: `V12 Nightly ${version} (${shortSha})`,
112 shortSha,
113 };
114};
115
116export const readDesktopBaseVersion = Effect.fn("readDesktopBaseVersion")(function* (
117 rootDir: string | undefined,
118) {
119 const fs = yield* FileSystem.FileSystem;
120 const path = yield* Path.Path;
121 const workspaceRoot = rootDir ? path.resolve(rootDir) : yield* RepoRoot;
122 const packageJsonPath = path.join(workspaceRoot, "apps/desktop/package.json");
123 const packageJsonSource = yield* fs.readFileString(packageJsonPath).pipe(
124 Effect.mapError(
125 (cause) =>
126 new NightlyReleaseDesktopPackageError({
127 operation: "read",
128 packageJsonPath,
129 cause,
130 }),
131 ),
132 );
133 const packageJson = yield* decodeDesktopPackageJson(packageJsonSource).pipe(
134 Effect.mapError(
135 (cause) =>
136 new NightlyReleaseDesktopPackageError({
137 operation: "decode",
138 packageJsonPath,
139 cause,
140 }),
141 ),
142 );
143 return yield* resolveNightlyTargetVersion(packageJson.version);
144});
145
146export const writeNightlyReleaseOutput = Effect.fn("writeNightlyReleaseOutput")(function* (
147 metadata: NightlyReleaseMetadata,
148 writeGithubOutput: boolean,
149) {
150 const fs = yield* FileSystem.FileSystem;
151
152 const entries = [
153 ["base_version", metadata.baseVersion],
154 ["version", metadata.version],
155 ["tag", metadata.tag],
156 ["name", metadata.name],
157 ["short_sha", metadata.shortSha],
158 ] as const;
159
160 if (writeGithubOutput) {
161 const githubOutputPath = yield* Config.nonEmptyString("GITHUB_OUTPUT").pipe(
162 Effect.mapError(
163 (cause) =>
164 new NightlyReleaseGitHubOutputConfigError({
165 cause,
166 }),
167 ),
168 );
169 const serialized = entries.map(([key, value]) => `${key}=${value}\n`).join("");
170 yield* fs.writeFileString(githubOutputPath, serialized, { flag: "a" }).pipe(
171 Effect.mapError(
172 (cause) =>
173 new NightlyReleaseGitHubOutputAppendError({
174 outputPath: githubOutputPath,
175 cause,
176 }),
177 ),
178 );
179 } else {
180 for (const [key, value] of entries) {
181 yield* Console.log(`${key}=${value}`);
182 }
183 }
184});
185
186const command = Command.make(
187 "resolve-nightly-release",
188 {
189 date: Flag.string("date").pipe(
190 Flag.withSchema(DateSchema),
191 Flag.withDescription("Nightly build date in YYYYMMDD."),
192 ),
193 runNumber: Flag.string("run-number").pipe(
194 Flag.withSchema(RunNumberSchema),
195 Flag.withDescription("GitHub Actions run number."),
196 ),
197 sha: Flag.string("sha").pipe(
198 Flag.withSchema(ShaSchema),
199 Flag.withDescription("Commit sha for the nightly build."),
200 ),
201 githubOutput: Flag.boolean("github-output").pipe(
202 Flag.withDescription("Write values to GITHUB_OUTPUT instead of stdout."),
203 Flag.withDefault(false),
204 ),
205 root: Flag.string("root").pipe(
206 Flag.withDescription("Workspace root used to resolve apps/desktop/package.json."),
207 Flag.optional,
208 ),
209 },
210 ({ date, runNumber, sha, githubOutput, root }) =>
211 readDesktopBaseVersion(Option.getOrUndefined(root)).pipe(
212 Effect.map((baseVersion) => resolveNightlyReleaseMetadata(baseVersion, date, runNumber, sha)),
213 Effect.flatMap((metadata) => writeNightlyReleaseOutput(metadata, githubOutput)),
214 ),
215).pipe(Command.withDescription("Resolve nightly release version metadata."));
216
217if (import.meta.main) {
218 Command.run(command, { version: "0.0.0" }).pipe(
219 Effect.provide(NodeServices.layer),
220 NodeRuntime.runMain,
221 );
222}