| 1 | #!/usr/bin/env bun |
| 2 | /** |
| 3 | * Prepend release notes for a tag to CHANGELOG.md. Idempotent: if the tag |
| 4 | * already appears as a heading in CHANGELOG.md, does nothing. |
| 5 | * |
| 6 | * Usage: bun release-notes.ts v2.0.0-beta.13 |
| 7 | * |
| 8 | * Reads ALCHEMY_REPO for the GitHub repo to query commits/authors from. |
| 9 | */ |
| 10 | import { $ } from "bun"; |
| 11 | import { generate } from "./changelog.ts"; |
| 12 | import { readFile, writeFile } from "node:fs/promises"; |
| 13 | import { join } from "node:path"; |
| 14 | import { repo } from "./config.ts"; |
| 15 | import { renderMarkdown } from "./render.ts"; |
| 16 | |
| 17 | const tag = process.argv[2]; |
| 18 | if (!tag) { |
| 19 | console.error("Usage: bun release-notes.ts <tag>"); |
| 20 | process.exit(1); |
| 21 | } |
| 22 | |
| 23 | const changelogPath = join(process.cwd(), "CHANGELOG.md"); |
| 24 | const existing = await readFile(changelogPath, "utf-8").catch((e) => { |
| 25 | if ((e as NodeJS.ErrnoException).code === "ENOENT") return ""; |
| 26 | throw e; |
| 27 | }); |
| 28 | if (existing.includes(`## ${tag}\n`)) { |
| 29 | console.log(`${tag} already in CHANGELOG.md, skipping`); |
| 30 | process.exit(0); |
| 31 | } |
| 32 | |
| 33 | const tagExists = |
| 34 | (await $`git rev-parse --verify ${`refs/tags/${tag}`}`.nothrow().quiet()) |
| 35 | .exitCode === 0; |
| 36 | const toRev = tagExists ? tag : "HEAD"; |
| 37 | |
| 38 | console.log(`Generating release notes for ${tag} (using ${toRev})`); |
| 39 | const { commits, config } = await generate({ |
| 40 | to: toRev, |
| 41 | emoji: true, |
| 42 | contributors: true, |
| 43 | repo: repo(), |
| 44 | }); |
| 45 | |
| 46 | const md = renderMarkdown(commits, config); |
| 47 | |
| 48 | await writeFile(changelogPath, `## ${tag}\n\n${md}\n\n---\n\n${existing}`); |