chrome-extension / scripts / release.tsmain at c58198b
// A release is one file of this repo: releases/iterate-chrome-extension-<version>.zip, with its row
// in releases/index.json. This script builds dist/ with iterate's key and writes both. The mind
// serves them from this repo's main (README.md, "Release"): the push is the publication.
//
// node scripts/release.ts build, write the ZIP and its row, make it the current one
// node scripts/release.ts --verify build, and compare with the ZIP that the repo holds
//
// A released version is never changed: when the build's files differ from the kept ZIP of that
// version, the script stops. Raise `version` in public/manifest.json and package.json first.
import { createHash } from "node:crypto";
import { existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
import { unzipSync, zipSync } from "fflate";
const root = new URL("../", import.meta.url);
const verify = process.argv.includes("--verify");
// The key is the public half: every released manifest.json holds it. It fixes the extension's id.
process.env.CHROME_EXTENSION_KEY = readFileSync(new URL("extension-key.txt", root), "utf8").trim();
await import("./build.ts");
const dist = new URL("dist/", root);
const { version } = JSON.parse(readFileSync(new URL("manifest.json", dist), "utf8")) as { version: string };
if (!/^\d+(\.\d+){0,3}$/.test(version)) throw new Error(`manifest.json: "${version}" is no version`);
const declared = (JSON.parse(readFileSync(new URL("package.json", root), "utf8")) as { version: string }).version;
if (declared !== version) throw new Error(`package.json says ${declared}, public/manifest.json says ${version}`);
// Top-level files alone, A to Z: dist/ is flat (README.md). One fixed time for every entry, so the
// same commit gives the same bytes.
const built: Record<string, Uint8Array> = {};
for (const name of readdirSync(dist).sort()) built[name] = readFileSync(new URL(name, dist));
const sha256 = (data: Uint8Array) => createHash("sha256").update(data).digest("hex");
const file = `iterate-chrome-extension-${version}.zip`;
const zipPath = new URL(`releases/${file}`, root);
const indexPath = new URL("releases/index.json", root);
type Index = { current: string; releases: Record<string, { file: string; sha256: string; bytes: number }> };
const index: Index = existsSync(indexPath) ? JSON.parse(readFileSync(indexPath, "utf8")) : { current: "", releases: {} };
if (existsSync(zipPath)) {
const kept = unzipSync(readFileSync(zipPath));
const names = [...new Set([...Object.keys(kept), ...Object.keys(built)])].sort();
const differ = names.filter((name) => !kept[name] || !built[name] || sha256(kept[name]) !== sha256(built[name]));
if (differ.length) {
console.error(`${file} is released, and this build differs in: ${differ.join(", ")}`);
console.error("A released version is never changed. Raise the version, or build the release's own commit.");
process.exit(1);
}
console.log(`${file}: this build's ${names.length} files are the released ones.`);
} else if (verify) {
console.error(`${file} is not in releases/: nothing to compare with.`);
process.exit(1);
} else {
mkdirSync(new URL("releases/", root), { recursive: true });
writeFileSync(zipPath, zipSync(built, { mtime: new Date(1980, 0, 1) }));
console.log(`wrote releases/${file}`);
}
if (!verify) {
const data = readFileSync(zipPath);
index.releases[version] = { file, sha256: sha256(data), bytes: data.byteLength };
index.current = version;
writeFileSync(indexPath, JSON.stringify(index, null, 2) + "\n");
console.log(`releases/index.json: current is ${version}, sha256 ${index.releases[version].sha256}`);
}