Open History to see the last commit
1// Creates the GitHub release for a tag, marked Latest to match npm.
2//
3// `scripts/release/publish.ts` publishes every release, prereleases included,
4// under npm's `latest` dist-tag, so every release is a plain (non-prerelease)
5// GitHub release and the newest one is Latest; GitHub cannot mark a
6// prerelease Latest. release.yml does not run this for tag releases. A tag
7// that already has a release is skipped, so a failed run can be re-run.
8//
9// The notes cover the commits since the previous tag. Reads `ALCHEMY_REPO`
10// (`<owner>/<repo>`) for the changelog's links.
11//
12// Usage: node scripts/release/github-release.ts <tag> <release|beta|alpha|rc>
13import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
14import * as NodeServices from "@effect/platform-node/NodeServices";
15import { Argument, Command } from "effect/cli";
16import * as Console from "effect/Console";
17import * as Data from "effect/Data";
18import * as Effect from "effect/Effect";
19import { ChildProcess } from "effect/process";
20import { ChildProcessSpawner } from "effect/process/ChildProcessSpawner";
21import * as Stream from "effect/Stream";
22import { generate } from "./changelog.ts";
23import { repository } from "./config.ts";
24
25export class CommandFailed extends Data.TaggedError("CommandFailed")<{
26 readonly message: string;
27 readonly command: string;
28 readonly exitCode: number;
29}> {}
30
31/** Runs a command, returning its exit code and captured stdout. */
32const run = Effect.fn(function* (command: string, args: ReadonlyArray<string>) {
33 const spawner = yield* ChildProcessSpawner;
34 return yield* Effect.scoped(
35 Effect.gen(function* () {
36 const handle = yield* spawner.spawn(
37 ChildProcess.make(command, [...args], { stdout: "pipe", stderr: "ignore" }),
38 );
39 const [stdout, exitCode] = yield* Effect.all(
40 [handle.stdout.pipe(Stream.decodeText(), Stream.mkString), handle.exitCode],
41 { concurrency: "unbounded" },
42 );
43 return { exitCode: Number(exitCode), stdout: stdout.trim() };
44 }),
45 );
46});
47
48/** Runs a command with its output inherited, failing on a non-zero exit. */
49const runOrFail = Effect.fn(function* (command: string, args: ReadonlyArray<string>) {
50 const spawner = yield* ChildProcessSpawner;
51 const exitCode = yield* Effect.scoped(
52 Effect.gen(function* () {
53 const handle = yield* spawner.spawn(
54 ChildProcess.make(command, [...args], { stdout: "inherit", stderr: "inherit" }),
55 );
56 return Number(yield* handle.exitCode);
57 }),
58 );
59 if (exitCode !== 0) {
60 return yield* new CommandFailed({
61 message: `${command} ${args[0] ?? ""} exited with code ${exitCode}`,
62 command,
63 exitCode,
64 });
65 }
66});
67
68const command = Command.make(
69 "github-release",
70 {
71 tag: Argument.String("tag").pipe(Argument.withDescription("Release tag, e.g. v1.0.0-rc.14")),
72 channel: Argument.Literals("channel", ["release", "beta", "alpha", "rc"]).pipe(
73 Argument.withDescription("Release channel"),
74 ),
75 },
76 Effect.fn(function* ({ tag, channel }) {
77 if ((yield* run("gh", ["release", "view", tag])).exitCode === 0) {
78 yield* Console.log(`Release ${tag} already exists on GitHub, skipping`);
79 return;
80 }
81
82 const previous = yield* run("git", ["describe", "--tags", "--abbrev=0", `${tag}^`]);
83 const from = previous.exitCode === 0 ? previous.stdout : undefined;
84
85 yield* Console.log(
86 `Generating ${channel} release notes for ${tag}${from ? ` from ${from}` : ""}`,
87 );
88 const { md } = yield* generate({ from, to: tag, repo: yield* repository });
89
90 yield* runOrFail("gh", ["release", "create", tag, "--title", tag, "--notes", md, "--latest"]);
91 }),
92).pipe(Command.withDescription("Create the GitHub release for a tag, marked Latest"));
93
94Command.run(command, { version: "0.0.0" }).pipe(
95 Effect.provide(NodeServices.layer),
96 NodeRuntime.runMain,
97);