gram/check-changesets.ts
abiwab fc68b86dc3
All checks were successful
CI / Build, Test & Publish (pull_request) Successful in 3m14s
chore(deps): upgrade changesets packages to v3
2026-09-28 23:06:53 +02:00

86 lines
3 KiB
TypeScript

#!/usr/bin/env bun
// Read-only pre-release checklist: previews how the pending .changeset/*.md
// files will land in CHANGELOG.md — bump type, cleaned title, and any likely
// duplicates against each other or recent release history — without writing
// anything or touching versions. Run this before `bun run version-packages`,
// fix whatever it flags, then release.
import { readChangesets } from "@changesets/read";
import { readConfig } from "@changesets/config";
import { getPackages } from "@manypkg/get-packages";
import { readFile } from "node:fs/promises";
import {
parseChangesetSummary,
findLikelyDuplicates,
extractRecentChangelogLines,
filterUnreleasedChangesets,
} from "./changeset-utils";
async function main() {
const cwd = process.cwd();
const rawPackages = await getPackages(cwd);
const packages = {
...rawPackages,
root: rawPackages.root || rawPackages.rootPackage || { dir: cwd },
};
const allChangesets = await readChangesets(cwd);
// validates .changeset/config.json exists and is well-formed
const { errors: configErrors } = await readConfig(cwd, packages);
if (configErrors) {
throw new Error(`Invalid .changeset/config.json: ${configErrors.join("; ")}`);
}
let preState: { changesets: string[]; tag?: string } | undefined;
try {
preState = JSON.parse(await readFile(`${cwd}/.changeset/pre.json`, "utf-8"));
} catch (e) {
// not in prerelease mode — nothing to filter out
}
const changesets = filterUnreleasedChangesets(allChangesets, preState);
if (changesets.length === 0) {
console.log("No pending changesets.");
return;
}
console.log(`${changesets.length} pending changeset${changesets.length === 1 ? "" : "s"}${preState ? ` (${allChangesets.length - changesets.length} already released in a previous ${preState.tag ?? "pre"}-release, excluded)` : ""}:\n`);
const titles: string[] = [];
for (const cs of changesets) {
const bumpType = cs.releases.some((r) => r.type === "major")
? "major"
: cs.releases.some((r) => r.type === "minor")
? "minor"
: "patch";
const { head, sections } = parseChangesetSummary(cs.summary);
console.log(`[${bumpType}] ${cs.id}`);
console.log(` ${head || "(no title parsed — check formatting)"}`);
for (const [sectionName, items] of Object.entries(sections)) {
console.log(` ${sectionName}: ${items.length} item${items.length === 1 ? "" : "s"}`);
}
console.log("");
if (head) titles.push(head);
}
let existingContent = "";
try {
existingContent = await readFile("./CHANGELOG.md", "utf-8");
} catch (e) {
// no changelog yet — nothing to compare against
}
const warnings = findLikelyDuplicates(titles, extractRecentChangelogLines(existingContent));
if (warnings.length === 0) {
console.log("No likely duplicates detected.");
} else {
console.log(`⚠️ ${warnings.length} possible duplicate${warnings.length === 1 ? "" : "s"}:\n`);
for (const w of warnings) {
console.log(` - "${w.title}"\n ~ "${w.matchedAgainst}" (${Math.round(w.similarity * 100)}% word overlap)\n`);
}
}
}
main().catch(console.error);