// Creates the GitHub release for a tag, marked Latest to match npm. // // `scripts/release/publish.ts` publishes every release, prereleases included, // under npm's `latest` dist-tag, so every release is a plain (non-prerelease) // GitHub release and the newest one is Latest; GitHub cannot mark a // prerelease Latest. release.yml does not run this for tag releases. A tag // that already has a release is skipped, so a failed run can be re-run. // // The notes cover the commits since the previous tag. Reads `ALCHEMY_REPO` // (`/`) for the changelog's links. // // Usage: node scripts/release/github-release.ts import * as NodeRuntime from "@effect/platform-node/NodeRuntime"; import * as NodeServices from "@effect/platform-node/NodeServices"; import { Argument, Command } from "effect/cli"; import * as Config from "effect/Config"; import * as Console from "effect/Console"; import * as Data from "effect/Data"; import * as Effect from "effect/Effect"; import { ChildProcess } from "effect/process"; import { ChildProcessSpawner } from "effect/process/ChildProcessSpawner"; import * as Stream from "effect/Stream"; import { generate } from "./changelog.ts"; export class CommandFailed extends Data.TaggedError("CommandFailed")<{ readonly message: string; readonly command: string; readonly exitCode: number; }> {} export class InvalidRepo extends Data.TaggedError("InvalidRepo")<{ readonly message: string; }> {} export class ChangelogFailed extends Data.TaggedError("ChangelogFailed")<{ readonly message: string; readonly cause: unknown; }> {} /** Runs a command, returning its exit code and captured stdout. */ const run = Effect.fn(function* (command: string, args: ReadonlyArray) { const spawner = yield* ChildProcessSpawner; return yield* Effect.scoped( Effect.gen(function* () { const handle = yield* spawner.spawn( ChildProcess.make(command, [...args], { stdout: "pipe", stderr: "ignore" }), ); const [stdout, exitCode] = yield* Effect.all( [handle.stdout.pipe(Stream.decodeText(), Stream.mkString), handle.exitCode], { concurrency: "unbounded" }, ); return { exitCode: Number(exitCode), stdout: stdout.trim() }; }), ); }); /** Runs a command with its output inherited, failing on a non-zero exit. */ const runOrFail = Effect.fn(function* (command: string, args: ReadonlyArray) { const spawner = yield* ChildProcessSpawner; const exitCode = yield* Effect.scoped( Effect.gen(function* () { const handle = yield* spawner.spawn( ChildProcess.make(command, [...args], { stdout: "inherit", stderr: "inherit" }), ); return Number(yield* handle.exitCode); }), ); if (exitCode !== 0) { return yield* new CommandFailed({ message: `${command} ${args[0] ?? ""} exited with code ${exitCode}`, command, exitCode, }); } }); /** `ALCHEMY_REPO`, as `/`. */ const repository = Effect.gen(function* () { const repo = (yield* Config.String("ALCHEMY_REPO")).trim(); if (!/^[\w.-]+\/[\w.-]+$/.test(repo)) { return yield* new InvalidRepo({ message: `ALCHEMY_REPO must be / (got: ${repo})`, }); } return repo; }); const command = Command.make( "github-release", { tag: Argument.String("tag").pipe(Argument.withDescription("Release tag, e.g. v1.0.0-rc.14")), channel: Argument.Literals("channel", ["release", "beta", "alpha", "rc"]).pipe( Argument.withDescription("Release channel"), ), }, Effect.fn(function* ({ tag, channel }) { if ((yield* run("gh", ["release", "view", tag])).exitCode === 0) { yield* Console.log(`Release ${tag} already exists on GitHub, skipping`); return; } const previous = yield* run("git", ["describe", "--tags", "--abbrev=0", `${tag}^`]); const from = previous.exitCode === 0 ? previous.stdout : undefined; yield* Console.log( `Generating ${channel} release notes for ${tag}${from ? ` from ${from}` : ""}`, ); const repo = yield* repository; const { md } = yield* Effect.tryPromise({ try: () => generate({ from, to: tag, emoji: true, contributors: true, repo }), catch: (cause) => new ChangelogFailed({ message: `Cannot generate release notes for ${tag}`, cause }), }); yield* runOrFail("gh", ["release", "create", tag, "--title", tag, "--notes", md, "--latest"]); }), ).pipe(Command.withDescription("Create the GitHub release for a tag, marked Latest")); Command.run(command, { version: "0.0.0" }).pipe( Effect.provide(NodeServices.layer), NodeRuntime.runMain, );