Skip to content
Myr

release.ts

This file

Path

scripts/release.ts

285 lines · 8.6 KiB · TypeScript

Last changed by

Drop the private build plan's citations from the code, and fail verify if one comes backoscabrielbefore adoptiona29cef6

History of this file

Take it

Name the swarm, seed and release scripts, and record every throwaway resource a live run makes in one ledgeroscabrielbefore adoptiond7064cb

line 1why, one step back
1/**

Merge branch 'build/715-release'oscabrielbefore adoption918a2c1

lines 2–3why, one step back
2 * release: builds the CLI into its npm package and two single-file
3 * binaries, uploads a release to a stage's own release storage, and

Drop the private build plan's citations from the code, and fail verify if one comes backoscabrielbefore adoptiona29cef6

line 4why, one step back
4 * publishes the package only when a person runs it.

Name the swarm, seed and release scripts, and record every throwaway resource a live run makes in one ledgeroscabrielbefore adoptiond7064cb

line 5why, one step back
5 *

Merge branch 'build/715-release'oscabrielbefore adoption918a2c1

lines 6–9why, one step back
6 * bun run release -- --build [--version <semver>] [--stage <s> | --origin <url>]
7 * bun run release -- --upload --stage dev-oscabriel|demo --version <semver>8 * bun run release -- --publish --version <semver>9 *

Drop the private build plan's citations from the code, and fail verify if one comes backoscabrielbefore adoptiona29cef6

line 10why, one step back
10 * `--build` runs the CLI's build with both targets, stages the npm package in

Merge branch 'build/715-release'oscabrielbefore adoption918a2c1

lines 11–19why, one step back
11 * `apps/cli/release/npm/` (its `package.json` generated from `Brand`, the
12 * platform's skill, a README and the licence), copies the binaries to13 * `apps/cli/release/<slug>-<target>`, and writes `install.sh` and14 * `SHA256SUMS` beside them. The install script fetches from `--origin`, or15 * the stage's platform origin, or the local platform (`wrangler dev`).16 *17 * `--upload --stage <s>` puts the version's files into the stage's18 * `releases-<stage>` bucket with Wrangler's own login, and19 * `releases/manifest.json` last; the platform then serves `<origin>/install.sh`

Drop the private build plan's citations from the code, and fail verify if one comes backoscabrielbefore adoptiona29cef6

line 20why, one step back
20 * and `<origin>/releases/…`. Nothing goes to GitHub.

Merge branch 'build/715-release'oscabrielbefore adoption918a2c1

lines 21–26why, one step back
21 *
22 * `--publish` runs `npm publish` only when a person runs it: under any23 * agent's env (`@repo/protocol/audit`'s table) the run still does every24 * other step it names, checks the staged package as a publish would, prints25 * the command for a person to run, and exits 0 (H2). The release's core26 * lives in `lib/release.ts`, its steps in `lib/release/steps.ts`.

Name the swarm, seed and release scripts, and record every throwaway resource a live run makes in one ledgeroscabrielbefore adoptiond7064cb

line 27why, one step back
27 */

Merge branch 'build/715-release'oscabrielbefore adoption918a2c1

lines 28–38why, one step back
28import { BunRuntime, BunServices } from "@effect/platform-bun";
29import {30 Console,31 Effect,32 FileSystem,33 Option,34 Path,35 Runtime,36 Schema,37 Stdio,38} from "effect";

Name the swarm, seed and release scripts, and record every throwaway resource a live run makes in one ledgeroscabrielbefore adoptiond7064cb

line 39why, one step back
39

Merge branch 'build/715-release'oscabrielbefore adoption918a2c1

lines 40–78why, one step back
40import {
41 declaredStage,42 flagValue,43 platformOrigin,44 repoRoot,45 subdomainFile,46 WorkersDev,47} from "../infra/stages.ts";48import type { Stage } from "../infra/stages.ts";49import { releaseRun, releaseVersion } from "./lib/release.ts";50import {51 layoutOf,52 missingFromPackage,53 publishable,54 publishCommand,55 ReleaseFailed,56 runStep,57} from "./lib/release/steps.ts";58import type { Layout, Release } from "./lib/release/steps.ts";5960/** The flags the command takes; any other is a typo, refused. */61const FLAGS = new Set([62 "--build",63 "--origin",64 "--publish",65 "--stage",66 "--upload",67 "--version",68]);6970const USAGE =71 "bun run release -- [--build] [--upload --stage <dev-…|demo>] [--publish] [--version <semver>] [--origin <url>]";7273/**74 * The release stopped, and it has said why in one line: what failed and75 * what to do (a step's failure, a refusal of the flags or the plan). The76 * runtime then exits 1 without printing it again.77 */78class Reported extends Schema.TaggedError<Reported>()("Reported", {}) {

Name the swarm, seed and release scripts, and record every throwaway resource a live run makes in one ledgeroscabrielbefore adoptiond7064cb

lines 79–81why, one step back
79 override readonly [Runtime.errorReported] = false;
80}81

Merge branch 'build/715-release'oscabrielbefore adoption918a2c1

lines 82–83why, one step back
82/** The local platform, as `wrangler dev` serves it. */
83const LOCAL_ORIGIN = "http://localhost:8787";

Name the swarm, seed and release scripts, and record every throwaway resource a live run makes in one ledgeroscabrielbefore adoptiond7064cb

line 84why, one step back
84

Merge branch 'build/715-release'oscabrielbefore adoption918a2c1

lines 85–93why, one step back
85/** What `bun run release` was asked to do. */
86interface Options {87 readonly build: boolean;88 readonly upload: boolean;89 readonly publish: boolean;90 readonly stage?: string;91 readonly version?: string;92 readonly origin?: string;93}

Name the swarm, seed and release scripts, and record every throwaway resource a live run makes in one ledgeroscabrielbefore adoptiond7064cb

line 94why, one step back
94

Merge branch 'build/715-release'oscabrielbefore adoption918a2c1

lines 95–285why, one step back
95/** The command's flags, read; an unknown flag or no step at all is refused. */
96const optionsOf = (argv: readonly string[]) =>97 Effect.gen(function* () {98 const unknown = argv.filter(99 (arg) => arg.startsWith("--") && !FLAGS.has(arg.split("=")[0] ?? arg)100 );101102 if (unknown.length > 0) {103 return yield* new ReleaseFailed({104 reason: `unknown ${unknown.join(", ")}; usage: ${USAGE}`,105 step: "flags",106 });107 }108109 const options: Options = {110 build: argv.includes("--build"),111 publish: argv.includes("--publish"),112 upload: argv.includes("--upload"),113 ...Option.match(flagValue(argv, "--stage"), {114 onNone: () => ({}),115 onSome: (stage) => ({ stage }),116 }),117 ...Option.match(flagValue(argv, "--version"), {118 onNone: () => ({}),119 onSome: (version) => ({ version }),120 }),121 ...Option.match(flagValue(argv, "--origin"), {122 onNone: () => ({}),123 onSome: (origin) => ({ origin: origin.replace(/\/+$/u, "") }),124 }),125 };126127 if (!options.build && !options.upload && !options.publish) {128 return yield* new ReleaseFailed({129 reason: `name a step; usage: ${USAGE}`,130 step: "flags",131 });132 }133134 return options;135 });136137/** The stage's platform origin, on the workers.dev subdomain bootstrap recorded. */138const stageOrigin = (root: string, stage: Stage) =>139 Effect.gen(function* () {140 const fs = yield* FileSystem.FileSystem;141 const path = yield* Path.Path;142143 const { subdomain } = yield* fs144 .readFileString(path.join(root, subdomainFile))145 .pipe(146 Effect.flatMap(Schema.decodeEffect(Schema.fromJsonString(WorkersDev))),147 Effect.mapError(148 () =>149 new ReleaseFailed({150 reason: `${subdomainFile} names no workers.dev subdomain: run bootstrap, or pass --origin`,151 step: "installScript",152 })153 )154 );155156 return platformOrigin(stage, subdomain);157 });158159/** Sizes of the release folder's files, and the staged package's files, for the record. */160const report = (layout: Layout, version: string) =>161 Effect.gen(function* () {162 const fs = yield* FileSystem.FileSystem;163 const path = yield* Path.Path;164165 if (!(yield* fs.exists(layout.release))) {166 return;167 }168169 for (const name of (yield* fs.readDirectory(layout.release)).toSorted()) {170 const info = yield* fs.stat(path.join(layout.release, name));171172 if (info.type === "File") {173 yield* Console.log(174 ` ${path.relative(layout.root, path.join(layout.release, name))} ${Number(info.size).toLocaleString("en-US")} bytes`175 );176 }177 }178179 if (yield* fs.exists(layout.npm)) {180 const files = (yield* fs.readDirectory(layout.npm, {181 recursive: true,182 })).toSorted();183184 yield* Console.log(185 ` ${path.relative(layout.root, layout.npm)}/: ${files.join(", ")}`186 );187188 const missing = yield* missingFromPackage(layout, version);189190 if (missing.length > 0) {191 yield* Console.log(192 ` the package lacks ${missing.join(", ")}, which its manifest names: --publish refuses it until the build makes ${missing.length === 1 ? "that file" : "them"}`193 );194 }195 }196 });197198/**199 * Under an agent, the publish is a person's (H2): once the staged package200 * is whole and of this version, say what they run, and end well, since the201 * agent has done all it may.202 */203const handOver = (204 layout: Layout,205 release: Release,206 markers: readonly string[]207) =>208 Effect.gen(function* () {209 const path = yield* Path.Path;210211 yield* publishable(layout, release);212213 yield* Console.log(214 [215 `release: an agent runs this (${markers.join(", ")}), so it doesn't publish (H2).`,216 `A person publishes the staged package (${release.version}), after \`npm login\`, from the repo root:`,217 "",218 ` ${publishCommand(layout, path)}`,219 ].join("\n")220 );221 });222223export const main = (224 argv: readonly string[],225 env: Readonly<Record<string, string | undefined>>226) =>227 Effect.gen(function* () {228 const options = yield* optionsOf(argv);229 const root = yield* repoRoot;230 const layout = yield* layoutOf(root);231232 const { handOver: markers, steps } = yield* Effect.fromResult(233 releaseRun({ ...options, env })234 );235236 const version = yield* Effect.fromResult(releaseVersion(options));237238 const stage = Option.flatMap(239 Option.fromNullishOr(options.stage),240 declaredStage241 );242243 const origin =244 options.origin ??245 (yield* Option.match(stage, {246 onNone: () => Effect.succeed(LOCAL_ORIGIN),247 onSome: (named) => stageOrigin(root, named),248 }));249250 const release: Release = { origin, stage, version };251252 const planned = [253 ...steps,254 ...(markers.length > 0 ? ["publish (by a person)"] : []),255 ];256257 yield* Console.log(258 `release ${version}: ${planned.join(", ")} (the install script fetches from ${origin})`259 );260261 for (const step of steps) {262 yield* Console.log(`release: ${step}`);263 yield* runStep(step, layout, release);264 }265266 yield* report(layout, version);267268 if (markers.length > 0) {269 yield* handOver(layout, release, markers);270 }271 }).pipe(272 Effect.tapError((failure) => Console.error(`release: ${failure.message}`)),273 Effect.mapError(() => new Reported())274 );275276if (import.meta.main) {277 BunRuntime.runMain(278 Effect.gen(function* () {279 const args = yield* (yield* Stdio.Stdio).args;280281 // The env as this process got it: an agent's harness marks it there.282 return yield* main(args, process.env);283 }).pipe(Effect.provide(BunServices.layer))284 );285}