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