The best way to control Claude Code, Codex and more from any computer or phone
| 1 | // Picks the release version and stamps it on every publishable package. |
| 2 | // |
| 3 | // The packages' `package.json` version is the source of truth: every package |
| 4 | // must carry the same one (anything else fails), and the spec bumps from it. |
| 5 | // npm and git tags are never consulted. |
| 6 | // |
| 7 | // - `beta` (default), `rc` or `alpha`: the next candidate on that channel. |
| 8 | // `2.0.0-beta.81` becomes `2.0.0-beta.82`; `2.0.0-beta.81` with `rc` |
| 9 | // becomes `2.0.0-rc.1`. Moving to an earlier channel, or starting one from |
| 10 | // a stable version, needs an explicit version. |
| 11 | // - `beta.N`, `rc.N`, `alpha.N`: that candidate of the current version. |
| 12 | // - `patch` / `minor` / `major`: the semver bump, so a prerelease graduates |
| 13 | // (`2.0.0-beta.81` with `major` becomes `2.0.0`). |
| 14 | // - `x.y.z` or `x.y.z-<channel>.N`: exactly that version. |
| 15 | // - anything else: `0.0.0-<spec>`, a tag release that is published but never |
| 16 | // committed, so it does not move the version later releases bump from. |
| 17 | // |
| 18 | // Every version except a tag release must be newer than the current one. |
| 19 | // |
| 20 | // Writes the version into each `packages/*/package.json`, records the |
| 21 | // packages in `release-packages.json`, refreshes the lockfile and validates |
| 22 | // the result (`scripts/validate-publish-packages.ts`). stdout |
| 23 | // carries only `version=` and `channel=` lines for `$GITHUB_OUTPUT`; |
| 24 | // everything else goes to stderr. |
| 25 | // |
| 26 | // Usage: node scripts/release/prepare.ts [spec] |
| 27 | import * as NodeRuntime from "@effect/platform-node/NodeRuntime"; |
| 28 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 29 | import * as Cause from "effect/Cause"; |
| 30 | import { Argument, Command } from "effect/cli"; |
| 31 | import * as Console from "effect/Console"; |
| 32 | import * as Data from "effect/Data"; |
| 33 | import * as Effect from "effect/Effect"; |
| 34 | import * as FileSystem from "effect/FileSystem"; |
| 35 | import * as Option from "effect/Option"; |
| 36 | import * as Path from "effect/Path"; |
| 37 | import { ChildProcess } from "effect/process"; |
| 38 | import { ChildProcessSpawner } from "effect/process/ChildProcessSpawner"; |
| 39 | import * as SemVer from "effect/SemVer"; |
| 40 | import * as Stream from "effect/Stream"; |
| 41 | import { publishablePackages, sharedVersion } from "../package-manifest.ts"; |
| 42 | |
| 43 | const PRERELEASE_CHANNELS = ["alpha", "beta", "rc"] as const; |
| 44 | type PrereleaseChannel = (typeof PRERELEASE_CHANNELS)[number]; |
| 45 | type Channel = "release" | PrereleaseChannel | "tag"; |
| 46 | |
| 47 | export class InvalidReleaseSpec extends Data.TaggedError("InvalidReleaseSpec")<{ |
| 48 | readonly message: string; |
| 49 | readonly spec: string; |
| 50 | }> {} |
| 51 | |
| 52 | export class InvalidCurrentVersion extends Data.TaggedError("InvalidCurrentVersion")<{ |
| 53 | readonly message: string; |
| 54 | readonly version: string; |
| 55 | }> {} |
| 56 | |
| 57 | export class VersionNotNewer extends Data.TaggedError("VersionNotNewer")<{ |
| 58 | readonly message: string; |
| 59 | readonly current: string; |
| 60 | readonly next: string; |
| 61 | }> {} |
| 62 | |
| 63 | export class CommandFailed extends Data.TaggedError("CommandFailed")<{ |
| 64 | readonly message: string; |
| 65 | readonly command: string; |
| 66 | readonly exitCode: number; |
| 67 | }> {} |
| 68 | |
| 69 | /** `x.y.z` with an optional `-<channel>.N`. */ |
| 70 | interface Version { |
| 71 | readonly core: readonly [number, number, number]; |
| 72 | readonly pre?: { readonly channel: PrereleaseChannel; readonly n: number }; |
| 73 | } |
| 74 | |
| 75 | const VERSION = /^(\d+)\.(\d+)\.(\d+)(?:-(alpha|beta|rc)\.(\d+))?$/; |
| 76 | |
| 77 | const parse = (text: string): Version | undefined => { |
| 78 | const match = text.match(VERSION); |
| 79 | if (!match) return undefined; |
| 80 | const [, major, minor, patch, channel, n] = match; |
| 81 | return { |
| 82 | core: [Number(major), Number(minor), Number(patch)], |
| 83 | pre: channel ? { channel: channel as PrereleaseChannel, n: Number(n) } : undefined, |
| 84 | }; |
| 85 | }; |
| 86 | |
| 87 | const format = ({ core, pre }: Version) => |
| 88 | `${core.join(".")}${pre ? `-${pre.channel}.${pre.n}` : ""}`; |
| 89 | |
| 90 | /** The version `spec` selects, bumped from `current`. */ |
| 91 | const nextVersion = Effect.fn(function* ( |
| 92 | current: Version, |
| 93 | spec: string, |
| 94 | ): Effect.fn.Return<Version, InvalidReleaseSpec> { |
| 95 | const invalid = (reason: string) => |
| 96 | new InvalidReleaseSpec({ message: `Cannot release ${spec}: ${reason}`, spec }); |
| 97 | |
| 98 | const prerelease = spec.match(/^(alpha|beta|rc)(?:\.(\d+))?$/); |
| 99 | if (prerelease) { |
| 100 | const channel = prerelease[1] as PrereleaseChannel; |
| 101 | if (prerelease[2] !== undefined) { |
| 102 | return { core: current.core, pre: { channel, n: Number(prerelease[2]) } }; |
| 103 | } |
| 104 | if (!current.pre) { |
| 105 | return yield* invalid(`${format(current)} is stable; pass an explicit x.y.z-${channel}.N`); |
| 106 | } |
| 107 | return { |
| 108 | core: current.core, |
| 109 | pre: { channel, n: current.pre.channel === channel ? current.pre.n + 1 : 1 }, |
| 110 | }; |
| 111 | } |
| 112 | |
| 113 | if (spec === "patch" || spec === "minor" || spec === "major") { |
| 114 | const [major, minor, patch] = current.core; |
| 115 | // A prerelease graduates to its own version when that already is the bump. |
| 116 | if (spec === "major") { |
| 117 | return { core: current.pre && minor === 0 && patch === 0 ? current.core : [major + 1, 0, 0] }; |
| 118 | } |
| 119 | if (spec === "minor") { |
| 120 | return { core: current.pre && patch === 0 ? current.core : [major, minor + 1, 0] }; |
| 121 | } |
| 122 | return { core: current.pre ? current.core : [major, minor, patch + 1] }; |
| 123 | } |
| 124 | |
| 125 | const exact = parse(spec); |
| 126 | if (exact) return exact; |
| 127 | return yield* invalid("expected rc, beta, alpha, <channel>.N, patch, minor, major or x.y.z"); |
| 128 | }); |
| 129 | |
| 130 | const resolveVersion = Effect.fn(function* (currentText: string, spec: string) { |
| 131 | // Anything that is not a version bump is a tag release. |
| 132 | if ( |
| 133 | /^[A-Za-z][A-Za-z0-9.-]*$/.test(spec) && |
| 134 | !/^(alpha|beta|rc)(\.\d+)?$|^(patch|minor|major)$/.test(spec) |
| 135 | ) { |
| 136 | return { channel: "tag" as Channel, version: `0.0.0-${spec}` }; |
| 137 | } |
| 138 | |
| 139 | const current = parse(currentText); |
| 140 | if (!current) { |
| 141 | return yield* new InvalidCurrentVersion({ |
| 142 | message: `The packages' version ${currentText} is not x.y.z or x.y.z-<alpha|beta|rc>.N`, |
| 143 | version: currentText, |
| 144 | }); |
| 145 | } |
| 146 | const next = yield* nextVersion(current, spec); |
| 147 | if ( |
| 148 | SemVer.isLessThanOrEqualTo(SemVer.parseUnsafe(format(next)), SemVer.parseUnsafe(currentText)) |
| 149 | ) { |
| 150 | return yield* new VersionNotNewer({ |
| 151 | message: `${format(next)} is not newer than the current ${currentText}`, |
| 152 | current: currentText, |
| 153 | next: format(next), |
| 154 | }); |
| 155 | } |
| 156 | return { channel: (next.pre?.channel ?? "release") as Channel, version: format(next) }; |
| 157 | }); |
| 158 | |
| 159 | /** Runs a command; stdout is captured (and echoed to stderr), stderr inherited. */ |
| 160 | const runOrFail = Effect.fn(function* (command: string, args: ReadonlyArray<string>, cwd: string) { |
| 161 | const spawner = yield* ChildProcessSpawner; |
| 162 | const { stdout, exitCode } = yield* Effect.scoped( |
| 163 | Effect.gen(function* () { |
| 164 | const handle = yield* spawner.spawn( |
| 165 | ChildProcess.make(command, [...args], { cwd, stdout: "pipe", stderr: "inherit" }), |
| 166 | ); |
| 167 | const [stdout, exitCode] = yield* Effect.all( |
| 168 | [handle.stdout.pipe(Stream.decodeText(), Stream.mkString), handle.exitCode], |
| 169 | { concurrency: "unbounded" }, |
| 170 | ); |
| 171 | return { stdout: stdout.trim(), exitCode: Number(exitCode) }; |
| 172 | }), |
| 173 | ); |
| 174 | if (stdout) yield* Console.error(stdout); |
| 175 | if (exitCode !== 0) { |
| 176 | const line = [command, ...args].join(" "); |
| 177 | return yield* new CommandFailed({ |
| 178 | message: `${line} exited with code ${exitCode}`, |
| 179 | command: line, |
| 180 | exitCode, |
| 181 | }); |
| 182 | } |
| 183 | }); |
| 184 | |
| 185 | const command = Command.make( |
| 186 | "prepare", |
| 187 | { |
| 188 | spec: Argument.String("spec").pipe( |
| 189 | Argument.withDescription("Release spec (see the header of this file); defaults to beta"), |
| 190 | Argument.optional, |
| 191 | ), |
| 192 | }, |
| 193 | Effect.fn(function* ({ spec }) { |
| 194 | const fs = yield* FileSystem.FileSystem; |
| 195 | const path = yield* Path.Path; |
| 196 | const root = path.resolve(import.meta.dirname, "../.."); |
| 197 | |
| 198 | const packages = yield* publishablePackages(root, "packages"); |
| 199 | const current = yield* sharedVersion(packages); |
| 200 | |
| 201 | const { channel, version } = yield* resolveVersion( |
| 202 | current, |
| 203 | Option.getOrElse(spec, () => "").trim() || "beta", |
| 204 | ); |
| 205 | yield* Console.error( |
| 206 | `Releasing ${packages.length} packages at ${version} (${channel}), from ${current}`, |
| 207 | ); |
| 208 | |
| 209 | yield* Effect.forEach(packages, (pkg) => |
| 210 | fs.writeFileString( |
| 211 | path.join(root, pkg.dir, "package.json"), |
| 212 | `${JSON.stringify({ ...pkg.raw, version }, null, 2)}\n`, |
| 213 | ), |
| 214 | ); |
| 215 | yield* fs.writeFileString( |
| 216 | path.join(root, "release-packages.json"), |
| 217 | `${JSON.stringify( |
| 218 | packages.map(({ dir, manifest }) => ({ dir, name: manifest.name })), |
| 219 | null, |
| 220 | 2, |
| 221 | )}\n`, |
| 222 | ); |
| 223 | |
| 224 | yield* runOrFail("pnpm", ["install", "--lockfile-only"], root); |
| 225 | yield* runOrFail("node", ["scripts/validate-publish-packages.ts"], root); |
| 226 | |
| 227 | yield* Console.log(`version=${version}`); |
| 228 | yield* Console.log(`channel=${channel}`); |
| 229 | }), |
| 230 | ).pipe(Command.withDescription("Bump every package from the version they share")); |
| 231 | |
| 232 | Command.run(command, { version: "0.0.0" }).pipe( |
| 233 | Effect.provide(NodeServices.layer), |
| 234 | // Report failures on stderr: stdout is reserved for `$GITHUB_OUTPUT`. |
| 235 | Effect.catchCause((cause) => |
| 236 | cause.pipe( |
| 237 | Cause.pretty, |
| 238 | Console.error, |
| 239 | Effect.andThen(Effect.sync(() => (process.exitCode = 1))), |
| 240 | ), |
| 241 | ), |
| 242 | NodeRuntime.runMain, |
| 243 | ); |