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
14interface 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
32const RepoRoot = Effect.service(Path.Path).pipe(
33 Effect.flatMap((path) => path.fromFileUrl(new URL("..", import.meta.url))),
34);
35const decodeDesktopPackageJson = Schema.decodeUnknownEffect(
36 Schema.fromJsonString(DesktopPackageJsonSchema),
37);
38
39export const resolveNightlyBaseVersion = (version: string) => version.replace(/[-+].*$/, "");
40
41export const resolveNightlyTargetVersion = (version: string) => {
42 const stableCore = resolveNightlyBaseVersion(version);
43 const match = /^(\d+)\.(\d+)\.(\d+)$/.exec(stableCore);
44 if (!match) {
45 throw new Error(`Invalid desktop package version '${version}'.`);
46 }
47
48 const [, major, minor, patch] = match;
49 return `${major}.${minor}.${Number(patch) + 1}`;
50};
51
52export const resolveNightlyReleaseMetadata = (
53 baseVersion: string,
54 date: string,
55 runNumber: number,
56 sha: string,
57) => {
58 const shortSha = sha.slice(0, 12);
59 const version = `${baseVersion}-nightly.${date}.${runNumber}`;
60 return {
61 baseVersion,
62 version,
63 tag: `v${version}`,
64 name: `T3 Code Nightly ${version} (${shortSha})`,
65 shortSha,
66 };
67};
68
69const readDesktopBaseVersion = Effect.fn("readDesktopBaseVersion")(function* (
70 rootDir: string | undefined,
71) {
72 const fs = yield* FileSystem.FileSystem;
73 const path = yield* Path.Path;
74 const workspaceRoot = rootDir ? path.resolve(rootDir) : yield* RepoRoot;
75 const packageJsonPath = path.join(workspaceRoot, "apps/desktop/package.json");
76 const packageJson = yield* fs
77 .readFileString(packageJsonPath)
78 .pipe(Effect.flatMap(decodeDesktopPackageJson));
79 return resolveNightlyTargetVersion(packageJson.version);
80});
81
82const writeOutput = Effect.fn("writeOutput")(function* (
83 metadata: NightlyReleaseMetadata,
84 writeGithubOutput: boolean,
85) {
86 const fs = yield* FileSystem.FileSystem;
87
88 const entries = [
89 ["base_version", metadata.baseVersion],
90 ["version", metadata.version],
91 ["tag", metadata.tag],
92 ["name", metadata.name],
93 ["short_sha", metadata.shortSha],
94 ] as const;
95
96 if (writeGithubOutput) {
97 const githubOutputPath = yield* Config.nonEmptyString("GITHUB_OUTPUT");
98 const serialized = entries.map(([key, value]) => `${key}=${value}\n`).join("");
99 yield* fs.writeFileString(githubOutputPath, serialized, { flag: "a" });
100 } else {
101 for (const [key, value] of entries) {
102 yield* Console.log(`${key}=${value}`);
103 }
104 }
105});
106
107const command = Command.make(
108 "resolve-nightly-release",
109 {
110 date: Flag.string("date").pipe(
111 Flag.withSchema(DateSchema),
112 Flag.withDescription("Nightly build date in YYYYMMDD."),
113 ),
114 runNumber: Flag.string("run-number").pipe(
115 Flag.withSchema(RunNumberSchema),
116 Flag.withDescription("GitHub Actions run number."),
117 ),
118 sha: Flag.string("sha").pipe(
119 Flag.withSchema(ShaSchema),
120 Flag.withDescription("Commit sha for the nightly build."),
121 ),
122 githubOutput: Flag.boolean("github-output").pipe(
123 Flag.withDescription("Write values to GITHUB_OUTPUT instead of stdout."),
124 Flag.withDefault(false),
125 ),
126 root: Flag.string("root").pipe(
127 Flag.withDescription("Workspace root used to resolve apps/desktop/package.json."),
128 Flag.optional,
129 ),
130 },
131 ({ date, runNumber, sha, githubOutput, root }) =>
132 readDesktopBaseVersion(Option.getOrUndefined(root)).pipe(
133 Effect.map((baseVersion) => resolveNightlyReleaseMetadata(baseVersion, date, runNumber, sha)),
134 Effect.flatMap((metadata) => writeOutput(metadata, githubOutput)),
135 ),
136).pipe(Command.withDescription("Resolve nightly release version metadata."));
137
138if (import.meta.main) {
139 Command.run(command, { version: "0.0.0" }).pipe(
140 Effect.provide(NodeServices.layer),
141 NodeRuntime.runMain,
142 );
143}