The best way to control Claude Code, Codex and more from any computer or phone
| 1 | #!/usr/bin/env node |
| 2 | |
| 3 | import * as NodeRuntime from "@effect/platform-node/NodeRuntime"; |
| 4 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 5 | import * as Config from "effect/Config"; |
| 6 | import * as Console from "effect/Console"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as FileSystem from "effect/FileSystem"; |
| 9 | import * as Option from "effect/Option"; |
| 10 | import * as Path from "effect/Path"; |
| 11 | import * as Schema from "effect/Schema"; |
| 12 | import { Argument, Command, Flag } from "effect/cli"; |
| 13 | import { fromJsonStringPretty } from "@t3tools/shared/schemaJson"; |
| 14 | |
| 15 | import { releasePackageFiles } from "./lib/tagged-source-version.ts"; |
| 16 | |
| 17 | export class ReleasePackageManifestError extends Schema.TaggedError<ReleasePackageManifestError>()( |
| 18 | "ReleasePackageManifestError", |
| 19 | { |
| 20 | operation: Schema.Literals(["read", "decode", "encode", "write"]), |
| 21 | filePath: Schema.String, |
| 22 | cause: Schema.Defect(), |
| 23 | }, |
| 24 | ) { |
| 25 | override get message(): string { |
| 26 | return `Failed to ${this.operation} release package manifest '${this.filePath}'.`; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | export class ReleaseGitHubOutputConfigurationError extends Schema.TaggedError<ReleaseGitHubOutputConfigurationError>()( |
| 31 | "ReleaseGitHubOutputConfigurationError", |
| 32 | { cause: Schema.Defect() }, |
| 33 | ) { |
| 34 | override get message(): string { |
| 35 | return "Failed to resolve GITHUB_OUTPUT for release package version output."; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | export class ReleaseGitHubOutputWriteError extends Schema.TaggedError<ReleaseGitHubOutputWriteError>()( |
| 40 | "ReleaseGitHubOutputWriteError", |
| 41 | { |
| 42 | filePath: Schema.String, |
| 43 | cause: Schema.Defect(), |
| 44 | }, |
| 45 | ) { |
| 46 | override get message(): string { |
| 47 | return `Failed to append release package version output to '${this.filePath}'.`; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | export { releasePackageFiles }; |
| 52 | |
| 53 | interface UpdateReleasePackageVersionsOptions { |
| 54 | readonly rootDir?: string | undefined; |
| 55 | } |
| 56 | |
| 57 | const PackageJsonSchema = Schema.Record(Schema.String, Schema.Unknown); |
| 58 | const PackageJsonPrettyJson = fromJsonStringPretty(PackageJsonSchema); |
| 59 | const decodePackageJson = Schema.decodeUnknownEffect(PackageJsonPrettyJson); |
| 60 | const encodePackageJson = Schema.encodeEffect(PackageJsonPrettyJson); |
| 61 | |
| 62 | export const updateReleasePackageVersions = Effect.fn("updateReleasePackageVersions")(function* ( |
| 63 | version: string, |
| 64 | options: UpdateReleasePackageVersionsOptions = {}, |
| 65 | ) { |
| 66 | const fs = yield* FileSystem.FileSystem; |
| 67 | const path = yield* Path.Path; |
| 68 | const rootDir = path.resolve(options.rootDir ?? process.cwd()); |
| 69 | let changed = false; |
| 70 | |
| 71 | for (const relativePath of releasePackageFiles) { |
| 72 | const filePath = path.join(rootDir, relativePath); |
| 73 | const packageJsonText = yield* fs.readFileString(filePath).pipe( |
| 74 | Effect.mapError( |
| 75 | (cause) => |
| 76 | new ReleasePackageManifestError({ |
| 77 | operation: "read", |
| 78 | filePath, |
| 79 | cause, |
| 80 | }), |
| 81 | ), |
| 82 | ); |
| 83 | const packageJson = yield* decodePackageJson(packageJsonText).pipe( |
| 84 | Effect.mapError( |
| 85 | (cause) => |
| 86 | new ReleasePackageManifestError({ |
| 87 | operation: "decode", |
| 88 | filePath, |
| 89 | cause, |
| 90 | }), |
| 91 | ), |
| 92 | ); |
| 93 | if (packageJson.version === version) { |
| 94 | continue; |
| 95 | } |
| 96 | |
| 97 | const packageJsonString = yield* encodePackageJson({ ...packageJson, version }).pipe( |
| 98 | Effect.mapError( |
| 99 | (cause) => |
| 100 | new ReleasePackageManifestError({ |
| 101 | operation: "encode", |
| 102 | filePath, |
| 103 | cause, |
| 104 | }), |
| 105 | ), |
| 106 | ); |
| 107 | yield* fs.writeFileString(filePath, `${packageJsonString}\n`).pipe( |
| 108 | Effect.mapError( |
| 109 | (cause) => |
| 110 | new ReleasePackageManifestError({ |
| 111 | operation: "write", |
| 112 | filePath, |
| 113 | cause, |
| 114 | }), |
| 115 | ), |
| 116 | ); |
| 117 | changed = true; |
| 118 | } |
| 119 | |
| 120 | return { changed }; |
| 121 | }); |
| 122 | |
| 123 | const writeGithubOutput = Effect.fn("writeGithubOutput")(function* (changed: boolean) { |
| 124 | const fs = yield* FileSystem.FileSystem; |
| 125 | const githubOutputPath = yield* Config.NonEmptyString("GITHUB_OUTPUT").pipe( |
| 126 | Effect.mapError( |
| 127 | (cause) => |
| 128 | new ReleaseGitHubOutputConfigurationError({ |
| 129 | cause, |
| 130 | }), |
| 131 | ), |
| 132 | ); |
| 133 | yield* fs.writeFileString(githubOutputPath, `changed=${changed}\n`, { flag: "a" }).pipe( |
| 134 | Effect.mapError( |
| 135 | (cause) => |
| 136 | new ReleaseGitHubOutputWriteError({ |
| 137 | filePath: githubOutputPath, |
| 138 | cause, |
| 139 | }), |
| 140 | ), |
| 141 | ); |
| 142 | }); |
| 143 | |
| 144 | export const updateReleasePackageVersionsCommand = Command.make( |
| 145 | "update-release-package-versions", |
| 146 | { |
| 147 | version: Argument.String("version").pipe( |
| 148 | Argument.withDescription("Release version to write into each releasable package.json."), |
| 149 | ), |
| 150 | root: Flag.String("root").pipe( |
| 151 | Flag.withDescription("Workspace root used to resolve the release package manifests."), |
| 152 | Flag.optional, |
| 153 | ), |
| 154 | githubOutput: Flag.Boolean("github-output").pipe( |
| 155 | Flag.withDescription("Append changed=<boolean> to GITHUB_OUTPUT."), |
| 156 | Flag.withDefault(false), |
| 157 | ), |
| 158 | }, |
| 159 | ({ version, root, githubOutput }) => |
| 160 | updateReleasePackageVersions(version, { |
| 161 | rootDir: Option.getOrUndefined(root), |
| 162 | }).pipe( |
| 163 | Effect.tap(({ changed }) => |
| 164 | changed |
| 165 | ? Effect.void |
| 166 | : Console.log("All package.json versions already match release version."), |
| 167 | ), |
| 168 | Effect.tap(({ changed }) => (githubOutput ? writeGithubOutput(changed) : Effect.void)), |
| 169 | ), |
| 170 | ).pipe(Command.withDescription("Update release package versions across the workspace.")); |
| 171 | |
| 172 | if (import.meta.main) { |
| 173 | Command.run(updateReleasePackageVersionsCommand, { version: "0.0.0" }).pipe( |
| 174 | Effect.provide(NodeServices.layer), |
| 175 | NodeRuntime.runMain, |
| 176 | ); |
| 177 | } |