Files
plugins/scripts/release.mjs
T
chrnx 32baf0c592
CI / dco (pull_request) Successful in 4s
CI / build (pull_request) Successful in 15s
CI / runtimes (bun) (pull_request) Successful in 10s
CI / runtimes (edge) (pull_request) Successful in 8s
CI / runtimes (deno) (pull_request) Successful in 12s
ci: publish to JSR from a dated tag, and close the changelogs
Adds the release workflow and the script it leans on, plus the `[Unreleased]`
sections these three packages have carried since they were written.

**The tag is a date, not a version.** `release-26.9.23` says when; the release
body says what moved. A version-shaped tag would be a lie here — three packages
carry three independent versions, so `plugins-v26.9.0` would match none of them
and invite being read as `@green-tea/jwt@26.9.0`.

**Tag-driven, not push-driven.** JSR's own setup page offers an example that
publishes on every push to `main`. For this repository that means every
promotion publishes, and a JSR version can never be replaced or removed, so one
distracted merge would burn a version number permanently. A tag is a decision; a
merge is not.

**Publishing all three at once is safe, and that is measured rather than
assumed.** `deno publish` queries the registry per package and drops the ones
already there (`filter_out_published_packages` in denoland/deno,
cli/tools/publish/mod.rs); skipping logs a warning, and a workspace with nothing
left to do exits 0 with "All packages are already published". So a tag cut after
changing one package publishes that one and leaves the others alone, with no
per-package logic here.

`scripts/release.mjs --check` runs **before** the publish and refuses when a
version about to ship has no changelog entry. That ordering is the point: core's
equivalent runs after npm and JSR have already published, so a missing heading
fails a release that has already shipped. Worth not inheriting.

`--notes` builds the release body from what `deno publish` actually reported
rather than from a prediction of it — predicting means duplicating Deno's
filtering and being wrong about it silently.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
2026-09-23 12:30:18 -05:00

134 lines
5.7 KiB
JavaScript

#!/usr/bin/env node
// Two jobs around a `deno publish`, both about the same thing: a JSR version can never be replaced
// or removed, so anything wrong has to be caught *before* the publish rather than reported after it.
//
// --check Which packages this tag would publish, and whether each has a CHANGELOG entry
// to match. Exits non-zero when one does not. Run this BEFORE publishing.
// --notes <log> The GitHub release body, built from what `deno publish` actually did.
//
// The order matters and is the whole point of splitting it in two. core's equivalent script runs
// after npm and JSR have already published, so a missing heading fails a release that has already
// shipped — a wart worth not inheriting.
import { readFileSync, readdirSync } from 'node:fs';
import { join } from 'node:path';
const ROOT = new URL('..', import.meta.url).pathname;
const PACKAGES = join(ROOT, 'packages');
const REPO = 'Expressive-Tea/green-tea-plugins';
/** Every workspace package, with the version its deno.json declares. */
function readPackages() {
return readdirSync(PACKAGES, { withFileTypes: true })
.filter((entry) => entry.isDirectory())
.map((entry) => {
const deno = JSON.parse(readFileSync(join(PACKAGES, entry.dir ?? entry.name, 'deno.json'), 'utf8'));
return { dir: entry.name, name: deno.name, version: deno.version };
});
}
/** Whether JSR already serves this exact version. A 404 for a package with no versions at all is
* not an error — it is what every one of these looks like until its first publish. */
async function isPublished({ name, version }) {
const [scope, pkg] = name.replace(/^@/, '').split('/');
const url = `https://api.jsr.io/scopes/${scope}/packages/${pkg}/versions/${version}`;
const response = await fetch(url, { headers: { accept: 'application/json' } });
if (response.status === 404) return false;
if (!response.ok) throw new Error(`${name}@${version}: ${url} answered ${response.status}`);
return true;
}
/** One version's section of a package CHANGELOG, heading excluded. */
function changelogSection(dir, version) {
const lines = readFileSync(join(PACKAGES, dir, 'CHANGELOG.md'), 'utf8').split('\n');
const start = lines.findIndex((line) => line.startsWith(`## [${version}]`));
if (start === -1) return null;
const rest = lines.slice(start + 1);
const end = rest.findIndex((line) => line.startsWith('## '));
return (end === -1 ? rest : rest.slice(0, end))
// Link reference definitions belong to the file, not to the entry. Nothing separates them from
// the last section, so without this they ride along into the release body as a stray line.
.filter((line) => !/^\[[^\]]+\]:\s/.test(line))
.join('\n')
.trim();
}
async function check() {
const packages = readPackages();
const published = await Promise.all(packages.map(isPublished));
const pending = packages.filter((_, i) => !published[i]);
if (pending.length === 0) {
console.log('Nothing to publish: every package version is already on JSR.');
return 0;
}
let missing = 0;
for (const pkg of pending) {
const section = changelogSection(pkg.dir, pkg.version);
if (section) {
console.log(` ok ${pkg.name}@${pkg.version}`);
} else {
// Deliberately an error rather than a warning. A version heading is a claim that the release
// exists, and publishing without one leaves a package on JSR that its own changelog says was
// never released — permanently, because the version cannot be replaced.
console.error(` MISSING ${pkg.name}@${pkg.version} has no "## [${pkg.version}]" in packages/${pkg.dir}/CHANGELOG.md`);
missing += 1;
}
}
packages
.filter((_, i) => published[i])
.forEach((pkg) => console.log(` skip ${pkg.name}@${pkg.version} (already on JSR)`));
return missing === 0 ? 0 : 1;
}
/** Parse what `deno publish` reported. Its own output is the only honest source for this: predicting
* it means duplicating the filtering Deno already did, and being wrong about it silently. */
function notes(logPath, tag) {
const log = readFileSync(logPath, 'utf8');
// Colour codes survive when a terminal is attached and vanish in CI. Strip either way.
const plain = log.replace(/\u001b\[[0-9;]*m/g, '');
const shipped = [...plain.matchAll(/Successfully published (@[^@\s]+\/[^@\s]+)@(\S+)/g)]
.map(([, name, version]) => ({ name, version }));
if (shipped.length === 0) {
return `No package versions were published by \`${tag}\` — every version in the workspace was already on JSR.`;
}
const byName = Object.fromEntries(readPackages().map((pkg) => [pkg.name, pkg.dir]));
const body = shipped.map(({ name, version }) => {
const dir = byName[name];
const section = (dir && changelogSection(dir, version)) || '_No changelog entry._';
const link = `https://github.com/${REPO}/blob/${tag}/packages/${dir}/CHANGELOG.md`;
return `## [${name}@${version}](https://jsr.io/${name}@${version})\n\n${section}\n\n[Full changelog](${link})`;
});
const untouched = readPackages().filter((pkg) => !shipped.some((s) => s.name === pkg.name));
const footer = untouched.length
? `\n\n---\n\nUnchanged in this release: ${untouched.map((p) => `\`${p.name}@${p.version}\``).join(', ')}.`
: '';
return `${body.join('\n\n')}${footer}`;
}
const [mode, arg] = process.argv.slice(2);
if (mode === '--check') {
process.exit(await check());
} else if (mode === '--notes') {
if (!arg) {
console.error('usage: release.mjs --notes <publish-log> [tag]');
process.exit(2);
}
console.log(notes(arg, process.argv[4] ?? 'main'));
} else {
console.error('usage: release.mjs --check | --notes <publish-log> [tag]');
process.exit(2);
}