Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it } from "@effect/vitest";
3import * as ConfigProvider from "effect/ConfigProvider";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Layer from "effect/Layer";
7import * as Path from "effect/Path";
8import * as Schema from "effect/Schema";
9import { Command, CliError } from "effect/unstable/cli";
10import * as TestConsole from "effect/testing/TestConsole";
11import { fromJsonStringPretty } from "@t3tools/shared/schemaJson";
12
13import {
14 releasePackageFiles,
15 updateReleasePackageVersions,
16 updateReleasePackageVersionsCommand,
17} from "./update-release-package-versions.ts";
18
19const ScriptTestLayer = Layer.mergeAll(NodeServices.layer, TestConsole.layer);
20const runCli = Command.runWith(updateReleasePackageVersionsCommand, { version: "0.0.0" });
21const PackageJsonSchema = Schema.Record(Schema.String, Schema.Unknown);
22const PackageJsonPrettyJson = fromJsonStringPretty(PackageJsonSchema);
23const decodePackageJson = Schema.decodeEffect(PackageJsonPrettyJson);
24const encodePackageJson = Schema.encodeEffect(PackageJsonPrettyJson);
25
26const writePackageJsonFixtures = Effect.fn("writePackageJsonFixtures")(function* (
27 rootDir: string,
28 version: string,
29) {
30 const fs = yield* FileSystem.FileSystem;
31 const path = yield* Path.Path;
32
33 for (const relativePath of releasePackageFiles) {
34 const filePath = path.join(rootDir, relativePath);
35 yield* fs.makeDirectory(path.dirname(filePath), { recursive: true });
36 yield* fs.writeFileString(
37 filePath,
38 `${yield* encodePackageJson({
39 name: relativePath,
40 version,
41 private: true,
42 })}\n`,
43 );
44 }
45});
46
47const readReleaseVersions = Effect.fn("readReleaseVersions")(function* (rootDir: string) {
48 const fs = yield* FileSystem.FileSystem;
49 const path = yield* Path.Path;
50 const versions = new Map<string, string>();
51
52 for (const relativePath of releasePackageFiles) {
53 const filePath = path.join(rootDir, relativePath);
54 const packageJson = yield* fs.readFileString(filePath).pipe(Effect.flatMap(decodePackageJson));
55 versions.set(relativePath, String(packageJson.version));
56 }
57
58 return versions;
59});
60
61const captureLogs = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
62 Effect.gen(function* () {
63 const result = yield* effect;
64 const logs = (yield* TestConsole.logLines).filter(
65 (line): line is string => typeof line === "string",
66 );
67 return { result, logs };
68 });
69
70it.layer(ScriptTestLayer)("update-release-package-versions", (it) => {
71 it.effect("updates all release package versions under the provided root", () =>
72 Effect.gen(function* () {
73 const fs = yield* FileSystem.FileSystem;
74 const baseDir = yield* fs.makeTempDirectoryScoped({
75 prefix: "update-release-package-versions-",
76 });
77
78 yield* writePackageJsonFixtures(baseDir, "0.0.1");
79
80 const result = yield* updateReleasePackageVersions("1.2.3", { rootDir: baseDir });
81 const versions = yield* readReleaseVersions(baseDir);
82
83 assert.deepStrictEqual(result, { changed: true });
84 assert.deepStrictEqual(
85 Array.from(versions.entries()),
86 releasePackageFiles.map((relativePath) => [relativePath, "1.2.3"]),
87 );
88 }),
89 );
90
91 it.effect("returns changed=false when all versions already match", () =>
92 Effect.gen(function* () {
93 const fs = yield* FileSystem.FileSystem;
94 const baseDir = yield* fs.makeTempDirectoryScoped({
95 prefix: "update-release-package-versions-unchanged-",
96 });
97
98 yield* writePackageJsonFixtures(baseDir, "1.2.3");
99
100 const result = yield* updateReleasePackageVersions("1.2.3", { rootDir: baseDir });
101
102 assert.deepStrictEqual(result, { changed: false });
103 }),
104 );
105
106 it.effect("accepts flags before the version positional and appends changed output", () =>
107 Effect.gen(function* () {
108 const fs = yield* FileSystem.FileSystem;
109 const path = yield* Path.Path;
110 const baseDir = yield* fs.makeTempDirectoryScoped({
111 prefix: "update-release-package-versions-cli-",
112 });
113 const githubOutputPath = path.join(baseDir, "github-output.txt");
114
115 yield* writePackageJsonFixtures(baseDir, "0.0.1");
116
117 yield* runCli(["--github-output", "--root", baseDir, "2.0.0"]).pipe(
118 Effect.provide(
119 ConfigProvider.layer(
120 ConfigProvider.fromEnv({
121 env: {
122 GITHUB_OUTPUT: githubOutputPath,
123 },
124 }),
125 ),
126 ),
127 );
128
129 const githubOutput = yield* fs.readFileString(githubOutputPath);
130 assert.equal(githubOutput, "changed=true\n");
131 }),
132 );
133
134 it.effect("logs when nothing changed", () =>
135 captureLogs(
136 Effect.gen(function* () {
137 const fs = yield* FileSystem.FileSystem;
138 const baseDir = yield* fs.makeTempDirectoryScoped({
139 prefix: "update-release-package-versions-cli-log-",
140 });
141
142 yield* writePackageJsonFixtures(baseDir, "3.0.0");
143 yield* runCli(["3.0.0", "--root", baseDir]);
144 }),
145 ).pipe(
146 Effect.tap(({ logs }) => {
147 assert.deepStrictEqual(logs, ["All package.json versions already match release version."]);
148 return Effect.void;
149 }),
150 ),
151 );
152
153 it.effect("requires GITHUB_OUTPUT when --github-output is set", () =>
154 Effect.gen(function* () {
155 const fs = yield* FileSystem.FileSystem;
156 const baseDir = yield* fs.makeTempDirectoryScoped({
157 prefix: "update-release-package-versions-cli-missing-output-",
158 });
159
160 yield* writePackageJsonFixtures(baseDir, "0.0.1");
161
162 const error = yield* runCli(["4.0.0", "--root", baseDir, "--github-output"]).pipe(
163 Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} }))),
164 Effect.flip,
165 );
166
167 assert.equal(
168 error.message,
169 'SchemaError(Expected string, got undefined\n at ["GITHUB_OUTPUT"])',
170 );
171 }),
172 );
173
174 it.effect("rejects unknown flags during cli parsing", () =>
175 Effect.gen(function* () {
176 const error = yield* runCli(["1.2.3", "--unknown"]).pipe(Effect.flip);
177
178 if (!CliError.isCliError(error)) {
179 assert.fail(`Expected CliError, got ${String(error)}`);
180 }
181
182 const optionError =
183 error._tag === "ShowHelp" ? (error.errors[0] as CliError.CliError | undefined) : error;
184
185 if (!optionError || optionError._tag !== "UnrecognizedOption") {
186 assert.fail(`Expected UnrecognizedOption, got ${String(optionError?._tag)}`);
187 }
188
189 assert.equal(optionError.option, "--unknown");
190 }),
191 );
192
193 it.effect("rejects a missing version positional during cli parsing", () =>
194 Effect.gen(function* () {
195 const error = yield* runCli(["--github-output"]).pipe(Effect.flip);
196
197 if (!CliError.isCliError(error)) {
198 assert.fail(`Expected CliError, got ${String(error)}`);
199 }
200
201 const versionError =
202 error._tag === "ShowHelp" ? (error.errors[0] as CliError.CliError | undefined) : error;
203
204 if (!versionError || versionError._tag !== "MissingArgument") {
205 assert.fail(`Expected MissingArgument, got ${String(versionError?._tag)}`);
206 }
207
208 assert.equal(versionError.argument, "version");
209 }),
210 );
211});