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 Config from "effect/Config";
6import * as Console from "effect/Console";
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 { Argument, Command, Flag } from "effect/unstable/cli";
13import { fromJsonStringPretty } from "@t3tools/shared/schemaJson";
14
15export const releasePackageFiles = [
16 "apps/server/package.json",
17 "apps/desktop/package.json",
18 "apps/web/package.json",
19 "packages/contracts/package.json",
20] as const;
21
22interface UpdateReleasePackageVersionsOptions {
23 readonly rootDir?: string | undefined;
24}
25
26const PackageJsonSchema = Schema.Record(Schema.String, Schema.Unknown);
27const PackageJsonPrettyJson = fromJsonStringPretty(PackageJsonSchema);
28const decodePackageJson = Schema.decodeUnknownEffect(PackageJsonPrettyJson);
29const encodePackageJson = Schema.encodeEffect(PackageJsonPrettyJson);
30
31export const updateReleasePackageVersions = Effect.fn("updateReleasePackageVersions")(function* (
32 version: string,
33 options: UpdateReleasePackageVersionsOptions = {},
34) {
35 const fs = yield* FileSystem.FileSystem;
36 const path = yield* Path.Path;
37 const rootDir = path.resolve(options.rootDir ?? process.cwd());
38 let changed = false;
39
40 for (const relativePath of releasePackageFiles) {
41 const filePath = path.join(rootDir, relativePath);
42 const packageJson = yield* fs.readFileString(filePath).pipe(Effect.flatMap(decodePackageJson));
43 if (packageJson.version === version) {
44 continue;
45 }
46
47 const packageJsonString = yield* encodePackageJson({ ...packageJson, version });
48 yield* fs.writeFileString(filePath, `${packageJsonString}\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}