34 lines
1.2 KiB
JavaScript
34 lines
1.2 KiB
JavaScript
// Print one version's section of CHANGELOG.md, for use as release notes.
|
|
//
|
|
// node changelog.mjs <tag> [CHANGELOG.md]
|
|
//
|
|
// The section starts at a "## v0.1.0" heading (the "v" is optional) and runs
|
|
// to the next "## " heading. Exits non-zero if it's missing or empty, so a
|
|
// release can't go out without notes.
|
|
import { readFileSync } from "node:fs";
|
|
|
|
function fail(msg) {
|
|
console.error(`::error::${msg}`);
|
|
process.exit(1);
|
|
}
|
|
|
|
const [tag, path = "CHANGELOG.md"] = process.argv.slice(2);
|
|
if (!tag) fail("usage: changelog.mjs <tag> [CHANGELOG.md]");
|
|
|
|
const version = tag.replace(/^v/, "").replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
const heading = new RegExp(`^##\\s+v?${version}(\\s|$)`);
|
|
const lines = readFileSync(path, "utf8").split(/\r?\n/);
|
|
|
|
const start = lines.findIndex((l) => heading.test(l));
|
|
if (start < 0) {
|
|
fail(
|
|
`${path} has no "## ${tag}" section. Add one, commit it, then move the tag to that commit and push it again.`,
|
|
);
|
|
}
|
|
let end = lines.findIndex((l, i) => i > start && /^##\s/.test(l));
|
|
if (end < 0) end = lines.length;
|
|
|
|
const notes = lines.slice(start + 1, end).join("\n").trim();
|
|
if (!notes) fail(`The "## ${tag}" section of ${path} is empty.`);
|
|
process.stdout.write(`${notes}\n`);
|