Add a prepare release script

scripts/prepare_release.mts takes a version, sets it in the package.json
files under packages/ and plugins/, adds the pr_to_changelog.mts output to
the top of CHANGELOG.md, then commits "Prepare <version>" and tags it
v<version>. It does not push.

pr_to_changelog.mts now exports generateReleaseNotes() so the output can
be reused. The template comment in CHANGELOG.md is removed so the first
"## " heading is always the latest release.

Closes #1240

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
bbassieandClaude Opus 5.5 committed 2026-11-21 02:28:33 +00:00
1 parent 8692ea3941
commit 8e01a96df9
5 files changed
+173 -72

No files matched your search

+32 -20
View File
@@ -258,7 +258,7 @@ async function fetchStatistics(lastRelease: Release, pullRequests: Issue[]): Pro
};
}
function printMarkdown(
function formatMarkdown(
changelog: Changelog,
issues: Issue[],
users: Issue["user"][],
@@ -266,12 +266,13 @@ function printMarkdown(
refText: (issue: Issue) => string,
userText: (user: Issue["user"]) => string,
) {
const lines: string[] = [];
const sections = new Set([...changelogSections, ...Object.keys(changelog)]);
for (const section of sections) {
if (section in changelog) {
console.log(`### ${section}\n`);
lines.push(`### ${section}`, "");
const entries = changelog[section];
console.log(entries.map(text => (
lines.push(...entries.map(text => (
`- ${text.replaceAll(/#(\d+)/g, (ref, idAsText) => {
const id = Number.parseInt(idAsText, 10);
if (!Number.isFinite(id)) {
@@ -283,27 +284,29 @@ function printMarkdown(
}
return refText(issue);
})}`
)).join("\n"));
console.log();
)));
lines.push("");
}
}
console.log("### Contributors\n");
console.log(`Many thanks to the following for contributing to this release:`);
console.log(users.map(userText).join(", "));
lines.push("### Contributors", "");
lines.push("Many thanks to the following for contributing to this release:");
lines.push(users.map(userText).join(", "));
const format = (value: number) => value.toLocaleString("en-US");
console.log("\n### Statistics\n");
console.log([
lines.push("", "### Statistics", "");
lines.push([
`Commits: ${format(statistics.commits)}`,
`Pull Requests: ${format(statistics.pullRequests)}`,
`Files Changed: ${format(statistics.filesChanged)}`,
`Additions: +${format(statistics.lineAdditions)}`,
`Deletions: -${format(statistics.lineDeletions)}`,
].join(" "));
return lines.join("\n");
}
async function main() {
/** Builds the release notes for the changes merged since the last release. */
export async function generateReleaseNotes() {
const lastRelease = await fetchLastRelease();
const issues = await fetchIssuesUpdatedSince(lastRelease.created_at);
@@ -314,22 +317,31 @@ async function main() {
));
const changelog = changelogFromPullRequests(pullRequests, issues);
const users = [...new Map(pullRequests.map(issue => [issue.user.login, issue.user])).values()]
const users = [...new Map(pullRequests.map(issue => [issue.user.login, issue.user])).values()];
const statistics = await fetchStatistics(lastRelease, pullRequests);
return {
github: formatMarkdown(changelog, issues, users, statistics,
issue => `${repository}#${issue.number}`,
user => `@${user.login}`,
),
changelog: formatMarkdown(changelog, issues, users, statistics,
issue => `[#${issue.number}](<${issue.html_url}>)`,
user => `[@${user.login}](<${user.html_url}>)`
),
};
}
async function main() {
const notes = await generateReleaseNotes();
console.log();
console.log("=== Github Release Markdown ===");
printMarkdown(changelog, issues, users, statistics,
issue => `${repository}#${issue.number}`,
user => `@${user.login}`,
);
console.log(notes.github);
console.log();
console.log("=== Discord / Changelog Markdown ===");
printMarkdown(changelog, issues, users, statistics,
issue => `[#${issue.number}](<${issue.html_url}>)`,
user => `[@${user.login}](<${user.html_url}>)`
);
console.log(notes.changelog);
}
if (import.meta.main) {
+91
View File
@@ -0,0 +1,91 @@
// Bumps the package versions, adds the release notes to CHANGELOG.md, then commits and tags the release.
// Usage: node scripts/prepare_release.mts <version>
import { execFile } from "node:child_process";
import fs from "node:fs/promises";
import path from "node:path";
import { promisify } from "node:util";
import { generateReleaseNotes } from "./pr_to_changelog.mts";
const root = path.resolve(import.meta.dirname, "..");
const versionPattern = /^\d+\.\d+\.\d+(-[0-9A-Za-z.-]+)?$/;
async function git(...args: string[]) {
return (await promisify(execFile)("git", args, { cwd: root, encoding: "utf8" })).stdout;
}
export function setPackageVersion(text: string, version: string) {
const pattern = /^([ \t]*"version":[ \t]*)"[^"]*"/m;
if (!pattern.test(text)) {
throw new Error("No version field found");
}
return text.replace(pattern, `$1"${version}"`);
}
export function addChangelogEntry(text: string, version: string, notes: string) {
if (new RegExp(`^## Version ${version.replaceAll(".", "\\.")}$`, "m").test(text)) {
throw new Error(`CHANGELOG.md already has an entry for ${version}`);
}
const firstEntry = text.search(/^## /m);
if (firstEntry === -1) {
throw new Error("No existing entry found in CHANGELOG.md");
}
return `${text.slice(0, firstEntry)}## Version ${version}\n\n${notes.trim()}\n\n\n${text.slice(firstEntry)}`;
}
async function packageFiles() {
const files: string[] = [];
for (const parent of ["packages", "plugins"]) {
for (const entry of await fs.readdir(path.join(root, parent), { withFileTypes: true })) {
const file = path.join(parent, entry.name, "package.json");
if (entry.isDirectory() && await fs.stat(path.join(root, file)).catch(() => null)) {
files.push(file);
}
}
}
return files;
}
async function main() {
const version = process.argv[2];
if (!version || !versionPattern.test(version)) {
console.error("Usage: node scripts/prepare_release.mts <version>, e.g. 2.1.2");
process.exitCode = 1;
return;
}
if (await git("status", "--porcelain", "--untracked-files=no")) {
throw new Error("Working tree has uncommitted changes");
}
if (await git("tag", "--list", `v${version}`)) {
throw new Error(`Tag v${version} already exists`);
}
// Done first so a GitHub API failure leaves the working tree untouched.
const notes = await generateReleaseNotes();
const changelogPath = path.join(root, "CHANGELOG.md");
const changelog = addChangelogEntry(await fs.readFile(changelogPath, "utf8"), version, notes.changelog);
const files = await packageFiles();
for (const file of files) {
const filePath = path.join(root, file);
await fs.writeFile(filePath, setPackageVersion(await fs.readFile(filePath, "utf8"), version));
}
await fs.writeFile(changelogPath, changelog);
await git("add", "CHANGELOG.md", ...files);
await git("commit", "--quiet", "-m", `Prepare ${version}`);
await git("tag", `v${version}`);
console.log();
console.log(`Committed "Prepare ${version}" and tagged it v${version}. Nothing was pushed.`);
console.log("Review it with: git show --stat HEAD");
console.log();
console.log("=== Github Release Markdown ===");
console.log(notes.github);
}
if (import.meta.main) {
main().catch(err => {
console.error(err.message ?? err);
process.exitCode = 1;
});
}