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, Console, Effect, FileSystem, Option, Path, Schema, SchemaGetter } from "effect";
6import { Argument, Command, Flag } from "effect/unstable/cli";
7
8export const releasePackageFiles = [
9 "apps/server/package.json",
10 "apps/desktop/package.json",
11 "apps/web/package.json",
12 "packages/contracts/package.json",
13] as const;
14
15interface UpdateReleasePackageVersionsOptions {
16 readonly rootDir?: string | undefined;
17}
18
19const PackageJsonSchema = Schema.Record(Schema.String, Schema.Unknown);
20const PrettyJsonString = SchemaGetter.parseJson<string>().compose(
21 SchemaGetter.stringifyJson({ space: 2 }),
22);
23const PackageJsonPrettyJson = Schema.fromJsonString(PackageJsonSchema).pipe(
24 Schema.encode({
25 decode: PrettyJsonString,
26 encode: PrettyJsonString,
27 }),
28);
29const decodePackageJson = Schema.decodeUnknownEffect(PackageJsonPrettyJson);
30const encodePackageJson = Schema.encodeSync(PackageJsonPrettyJson);
31
32export const updateReleasePackageVersions = Effect.fn("updateReleasePackageVersions")(function* (
33 version: string,
34 options: UpdateReleasePackageVersionsOptions = {},
35) {
36 const fs = yield* FileSystem.FileSystem;
37 const path = yield* Path.Path;
38 const rootDir = path.resolve(options.rootDir ?? process.cwd());
39 let changed = false;
40
41 for (const relativePath of releasePackageFiles) {
42 const filePath = path.join(rootDir, relativePath);
43 const packageJson = yield* fs.readFileString(filePath).pipe(Effect.flatMap(decodePackageJson));
44 if (packageJson.version === version) {
45 continue;
46 }
47
48 yield* fs.writeFileString(filePath, `${encodePackageJson({ ...packageJson, version })}\n`);
49 changed = true;
50 }
51
52 return { changed };
53});
54
55const writeGithubOutput = Effect.fn("writeGithubOutput")(function* (changed: boolean) {
56 const fs = yield* FileSystem.FileSystem;
57 const githubOutputPath = yield* Config.nonEmptyString("GITHUB_OUTPUT");
58 yield* fs.writeFileString(githubOutputPath, `changed=${changed}\n`, { flag: "a" });
59});
60
61export const updateReleasePackageVersionsCommand = Command.make(
62 "update-release-package-versions",
63 {
64 version: Argument.string("version").pipe(
65 Argument.withDescription("Release version to write into each releasable package.json."),
66 ),
67 root: Flag.string("root").pipe(
68 Flag.withDescription("Workspace root used to resolve the release package manifests."),
69 Flag.optional,
70 ),
71 githubOutput: Flag.boolean("github-output").pipe(
72 Flag.withDescription("Append changed=<boolean> to GITHUB_OUTPUT."),
73 Flag.withDefault(false),
74 ),
75 },
76 ({ version, root, githubOutput }) =>
77 updateReleasePackageVersions(version, {
78 rootDir: Option.getOrUndefined(root),
79 }).pipe(
80 Effect.tap(({ changed }) =>
81 changed
82 ? Effect.void
83 : Console.log("All package.json versions already match release version."),
84 ),
85 Effect.tap(({ changed }) => (githubOutput ? writeGithubOutput(changed) : Effect.void)),
86 ),
87).pipe(Command.withDescription("Update release package versions across the workspace."));
88
89if (import.meta.main) {
90 Command.run(updateReleasePackageVersionsCommand, { version: "0.0.0" }).pipe(
91 Effect.provide(NodeServices.layer),
92 NodeRuntime.runMain,
93 );
94}