Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

release-notes.ts

67 lines2.4 KB

1// Prepends the release notes for a tag to CHANGELOG.md. Idempotent: a tag
2// that already has a heading in CHANGELOG.md is skipped.
3//
4// The notes cover the commits since the previous tag, up to the tag if it
5// exists yet, otherwise HEAD. Reads `ALCHEMY_REPO` (`<owner>/<repo>`) for the
6// changelog's links.
7//
8// Usage: node scripts/release/release-notes.ts <tag>
9import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
10import * as NodeServices from "@effect/platform-node/NodeServices";
11import { Argument, Command } from "effect/cli";
12import * as Console from "effect/Console";
13import * as Effect from "effect/Effect";
14import * as FileSystem from "effect/FileSystem";
15import * as Path from "effect/Path";
16import { ChildProcess } from "effect/process";
17import { ChildProcessSpawner } from "effect/process/ChildProcessSpawner";
18import { generate } from "./changelog.ts";
19import { repository } from "./config.ts";
20​
21/** Whether `tag` exists locally. */
22const tagExists = Effect.fn(function* (tag: string) {
23 const spawner = yield* ChildProcessSpawner;
24 const exitCode = yield* Effect.scoped(
25 Effect.gen(function* () {
26 const handle = yield* spawner.spawn(
27 ChildProcess.make("git", ["rev-parse", "--verify", `refs/tags/${tag}`], {
28 stdout: "ignore",
29 stderr: "ignore",
30 }),
31 );
32 return yield* handle.exitCode;
33 }),
34 );
35 return Number(exitCode) === 0;
36});
37​
38const command = Command.make(
39 "release-notes",
40 {
41 tag: Argument.String("tag").pipe(Argument.withDescription("Release tag, e.g. v2.0.0-beta.13")),
42 },
43 Effect.fn(function* ({ tag }) {
44 const fs = yield* FileSystem.FileSystem;
45 const path = yield* Path.Path;
46 const changelogPath = path.resolve("CHANGELOG.md");
47​
48 const existing = (yield* fs.exists(changelogPath))
49 ? yield* fs.readFileString(changelogPath)
50 : "";
51 if (existing.includes(`## ${tag}\n`)) {
52 yield* Console.log(`${tag} already in CHANGELOG.md, skipping`);
53 return;
54 }
55​
56 const to = (yield* tagExists(tag)) ? tag : "HEAD";
57 yield* Console.log(`Generating release notes for ${tag} (using ${to})`);
58 const { md } = yield* generate({ to, repo: yield* repository });
59​
60 yield* fs.writeFileString(changelogPath, `## ${tag}\n\n${md}\n\n---\n\n${existing}`);
61 }),
62).pipe(Command.withDescription("Prepend the release notes for a tag to CHANGELOG.md"));
63​
64Command.run(command, { version: "0.0.0" }).pipe(
65 Effect.provide(NodeServices.layer),
66 NodeRuntime.runMain,
67);