Skip to content
Myr

release.ts

This file

Path

scripts/release.ts

244 lines · 6.5 KiB · TypeScript

Last changed by

Merge examples/cli-dev-dependency into mainoscabrielbefore adoption83ea953

History of this file

Take it

The walk stopped after 200 commits: lines older than 10c8b54 point at it.

Merge branch 'build/906-platform-root-page'oscabrielbefore adoption10c8b54

lines 1–42why, one step back
1import { BunRuntime, BunServices } from "@effect/platform-bun";
2import {3 Console,4 Effect,5 FileSystem,6 Option,7 Path,8 Runtime,9 Schema,10 Stdio,11} from "effect";1213import {14 declaredStage,15 flagValue,16 platformOrigin,17 repoRoot,18 subdomainFile,19 WorkersDev,20} from "../infra/stages.ts";21import type { Stage } from "../infra/stages.ts";22import { releaseRun, releaseVersion } from "./lib/release.ts";23import {24 layoutOf,25 missingFromPackage,26 publishable,27 publishCommand,28 ReleaseFailed,29 runStep,30} from "./lib/release/steps.ts";31import type { Layout, Release } from "./lib/release/steps.ts";3233const FLAGS = new Set([34 "--build",35 "--origin",36 "--publish",37 "--stage",38 "--upload",39 "--version",40]);4142const USAGE =

Merge examples/cli-dev-dependency into mainoscabrielbefore adoption83ea953

line 43why, one step back
43 "bun run release -- [--build] [--upload] [--publish] [--stage <dev-…|prod>] [--version <semver>] [--origin <url>]";

Merge branch 'build/906-platform-root-page'oscabrielbefore adoption10c8b54

line 44why, one step back
44

Merge build/comments-ops into mainoscabrielbefore adoptione4c407e

line 45why, one step back
45// Already said in one line, so the runtime exits 1 without printing it again.

Merge branch 'build/906-platform-root-page'oscabrielbefore adoption10c8b54

lines 46–160why, one step back
46class Reported extends Schema.TaggedError<Reported>()("Reported", {}) {
47 override readonly [Runtime.errorReported] = false;48}4950const LOCAL_ORIGIN = "http://localhost:8787";5152interface Options {53 readonly build: boolean;54 readonly upload: boolean;55 readonly publish: boolean;56 readonly stage?: string;57 readonly version?: string;58 readonly origin?: string;59}6061const optionsOf = (argv: readonly string[]) =>62 Effect.gen(function* () {63 const unknown = argv.filter(64 (arg) => arg.startsWith("--") && !FLAGS.has(arg.split("=")[0] ?? arg)65 );6667 if (unknown.length > 0) {68 return yield* new ReleaseFailed({69 reason: `unknown ${unknown.join(", ")}; usage: ${USAGE}`,70 step: "flags",71 });72 }7374 const options: Options = {75 build: argv.includes("--build"),76 publish: argv.includes("--publish"),77 upload: argv.includes("--upload"),78 ...Option.match(flagValue(argv, "--stage"), {79 onNone: () => ({}),80 onSome: (stage) => ({ stage }),81 }),82 ...Option.match(flagValue(argv, "--version"), {83 onNone: () => ({}),84 onSome: (version) => ({ version }),85 }),86 ...Option.match(flagValue(argv, "--origin"), {87 onNone: () => ({}),88 onSome: (origin) => ({ origin: origin.replace(/\/+$/u, "") }),89 }),90 };9192 if (!options.build && !options.upload && !options.publish) {93 return yield* new ReleaseFailed({94 reason: `name a step; usage: ${USAGE}`,95 step: "flags",96 });97 }9899 return options;100 });101102const stageOrigin = (root: string, stage: Stage) =>103 Effect.gen(function* () {104 const fs = yield* FileSystem.FileSystem;105 const path = yield* Path.Path;106107 const { subdomain } = yield* fs108 .readFileString(path.join(root, subdomainFile))109 .pipe(110 Effect.flatMap(Schema.decodeEffect(Schema.fromJsonString(WorkersDev))),111 Effect.mapError(112 () =>113 new ReleaseFailed({114 reason: `${subdomainFile} names no workers.dev subdomain: run bootstrap, or pass --origin`,115 step: "installScript",116 })117 )118 );119120 return platformOrigin(stage, subdomain);121 });122123const report = (layout: Layout, version: string) =>124 Effect.gen(function* () {125 const fs = yield* FileSystem.FileSystem;126 const path = yield* Path.Path;127128 if (!(yield* fs.exists(layout.release))) {129 return;130 }131132 for (const name of (yield* fs.readDirectory(layout.release)).toSorted()) {133 const info = yield* fs.stat(path.join(layout.release, name));134135 if (info.type === "File") {136 yield* Console.log(137 ` ${path.relative(layout.root, path.join(layout.release, name))} ${Number(info.size).toLocaleString("en-US")} bytes`138 );139 }140 }141142 if (yield* fs.exists(layout.npm)) {143 const files = (yield* fs.readDirectory(layout.npm, {144 recursive: true,145 })).toSorted();146147 yield* Console.log(148 ` ${path.relative(layout.root, layout.npm)}/: ${files.join(", ")}`149 );150151 const missing = yield* missingFromPackage(layout, version);152153 if (missing.length > 0) {154 yield* Console.log(155 ` the package lacks ${missing.join(", ")}, which its manifest names: --publish refuses it until the build makes ${missing.length === 1 ? "that file" : "them"}`156 );157 }158 }159 });160

Merge build/comments-ops into mainoscabrielbefore adoptione4c407e

line 161why, one step back
161// Under an agent, the publish is a person's: say what they run, and end well.

Merge branch 'build/906-platform-root-page'oscabrielbefore adoption10c8b54

lines 162–244why, one step back
162const handOver = (
163 layout: Layout,164 release: Release,165 markers: readonly string[]166) =>167 Effect.gen(function* () {168 const path = yield* Path.Path;169170 yield* publishable(layout, release);171172 yield* Console.log(173 [174 `release: an agent runs this (${markers.join(", ")}), so it doesn't publish (H2).`,175 `A person publishes the staged package (${release.version}), after \`npm login\`, from the repo root:`,176 "",177 ` ${publishCommand(layout, path)}`,178 ].join("\n")179 );180 });181182export const main = (183 argv: readonly string[],184 env: Readonly<Record<string, string | undefined>>185) =>186 Effect.gen(function* () {187 const options = yield* optionsOf(argv);188 const root = yield* repoRoot;189 const layout = yield* layoutOf(root);190191 const { handOver: markers, steps } = yield* Effect.fromResult(192 releaseRun({ ...options, env })193 );194195 const version = yield* Effect.fromResult(releaseVersion(options));196197 const stage = Option.flatMap(198 Option.fromNullishOr(options.stage),199 declaredStage200 );201202 const origin =203 options.origin ??204 (yield* Option.match(stage, {205 onNone: () => Effect.succeed(LOCAL_ORIGIN),206 onSome: (named) => stageOrigin(root, named),207 }));208209 const release: Release = { origin, stage, version };210211 const planned = [212 ...steps,213 ...(markers.length > 0 ? ["publish (by a person)"] : []),214 ];215216 yield* Console.log(217 `release ${version}: ${planned.join(", ")} (the install script fetches from ${origin})`218 );219220 for (const step of steps) {221 yield* Console.log(`release: ${step}`);222 yield* runStep(step, layout, release);223 }224225 yield* report(layout, version);226227 if (markers.length > 0) {228 yield* handOver(layout, release, markers);229 }230 }).pipe(231 Effect.tapError((failure) => Console.error(`release: ${failure.message}`)),232 Effect.mapError(() => new Reported())233 );234235if (import.meta.main) {236 BunRuntime.runMain(237 Effect.gen(function* () {238 const args = yield* (yield* Stdio.Stdio).args;239240 // The env as this process got it: an agent's harness marks it there.241 return yield* main(args, process.env);242 }).pipe(Effect.provide(BunServices.layer))243 );244}