102 lines
5.5 KiB
JavaScript
102 lines
5.5 KiB
JavaScript
import { readFile, writeFile } from "node:fs/promises";
|
|
import { resolve } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
const root = fileURLToPath(new URL("..", import.meta.url));
|
|
const manifestPath = resolve(root, "public-api.json");
|
|
const cssFiles = ["tokens.css", "base.css", "accessibility.css", "forced-colors.css", "layout.css", "controls.css", "components.css", "utilities.css"];
|
|
const sortLexically = (values) => [...values].sort((left, right) => left < right ? -1 : left > right ? 1 : 0);
|
|
|
|
const packageJson = JSON.parse(await readFile(resolve(root, "package.json"), "utf8"));
|
|
const sources = await Promise.all(cssFiles.map((file) => readFile(resolve(root, file), "utf8")));
|
|
const combinedCss = sources.join("\n");
|
|
|
|
const classes = new Set();
|
|
for (const match of combinedCss.matchAll(/\.(frost-[A-Za-z0-9_-]+(?:\\:[A-Za-z0-9_-]+)*)/g)) {
|
|
classes.add(match[1].replaceAll("\\:", ":"));
|
|
}
|
|
|
|
const customProperties = new Set(combinedCss.match(/--frost-[a-z0-9-]+/g) ?? []);
|
|
const keyframes = new Set([...combinedCss.matchAll(/@keyframes\s+([A-Za-z0-9_-]+)/g)].map((match) => match[1]));
|
|
const entryCss = await readFile(resolve(root, "frost.css"), "utf8");
|
|
const layerDeclaration = entryCss.match(/@layer\s+([^;]+);/);
|
|
const layers = layerDeclaration ? layerDeclaration[1].split(",").map((layer) => layer.trim()) : [];
|
|
const packageExports = Object.fromEntries(sortLexically(Object.keys(packageJson.exports)).map((key) => [key, packageJson.exports[key]]));
|
|
|
|
const currentManifest = {
|
|
schemaVersion: 2,
|
|
packageVersion: packageJson.version,
|
|
classes: sortLexically(classes),
|
|
customProperties: sortLexically(customProperties),
|
|
keyframes: sortLexically(keyframes),
|
|
layers,
|
|
exports: packageExports,
|
|
};
|
|
|
|
const serialize = (value) => `${JSON.stringify(value, null, 2)}\n`;
|
|
const readExistingManifest = async () => {
|
|
try {
|
|
return JSON.parse(await readFile(manifestPath, "utf8"));
|
|
} catch (error) {
|
|
if (error.code === "ENOENT") return null;
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
const findBreakingChanges = (baseline, current) => {
|
|
if (!baseline) return [];
|
|
const changes = [];
|
|
for (const className of baseline.classes ?? []) {
|
|
if (!current.classes.includes(className)) changes.push(`removed class .${className}`);
|
|
}
|
|
for (const property of baseline.customProperties ?? []) {
|
|
if (!current.customProperties.includes(property)) changes.push(`removed custom property ${property}`);
|
|
}
|
|
for (const keyframe of baseline.keyframes ?? []) {
|
|
if (!current.keyframes.includes(keyframe)) changes.push(`removed keyframes ${keyframe}`);
|
|
}
|
|
for (const [key, target] of Object.entries(baseline.exports ?? {})) {
|
|
if (!(key in current.exports)) changes.push(`removed package export ${key}`);
|
|
else if (current.exports[key] !== target) changes.push(`changed package export ${key}: ${target} -> ${current.exports[key]}`);
|
|
}
|
|
for (const layer of baseline.layers ?? []) {
|
|
if (!current.layers.includes(layer)) changes.push(`removed CSS layer ${layer}`);
|
|
}
|
|
return changes;
|
|
};
|
|
|
|
const command = process.argv.includes("--write") ? "write" : process.argv.includes("--check") ? "check" : null;
|
|
if (!command) {
|
|
console.error("Usage: node scripts/public-api.mjs --write [--allow-breaking] | --check");
|
|
process.exitCode = 2;
|
|
} else {
|
|
const existingManifest = await readExistingManifest();
|
|
const breakingChanges = findBreakingChanges(existingManifest, currentManifest);
|
|
|
|
if (command === "write") {
|
|
if (breakingChanges.length && !process.argv.includes("--allow-breaking")) {
|
|
console.error(["Breaking public API changes detected:", ...breakingChanges.map((change) => `- ${change}`), "Run with --allow-breaking only for an intentional breaking release."].join("\n"));
|
|
process.exitCode = 1;
|
|
} else {
|
|
const next = serialize(currentManifest);
|
|
const previous = existingManifest ? serialize(existingManifest) : null;
|
|
if (next !== previous) await writeFile(manifestPath, next);
|
|
console.log(`Public API manifest contains ${currentManifest.classes.length} classes, ${currentManifest.customProperties.length} custom properties, ${currentManifest.keyframes.length} keyframes, and ${Object.keys(currentManifest.exports).length} exports.`);
|
|
}
|
|
} else if (!existingManifest) {
|
|
console.error("public-api.json is missing. Run npm run generate:api.");
|
|
process.exitCode = 1;
|
|
} else if (serialize(existingManifest) !== serialize(currentManifest)) {
|
|
const additions = [
|
|
...currentManifest.classes.filter((value) => !existingManifest.classes.includes(value)).map((value) => `added class .${value}`),
|
|
...currentManifest.customProperties.filter((value) => !existingManifest.customProperties.includes(value)).map((value) => `added custom property ${value}`),
|
|
...currentManifest.keyframes.filter((value) => !(existingManifest.keyframes ?? []).includes(value)).map((value) => `added keyframes ${value}`),
|
|
...Object.keys(currentManifest.exports).filter((value) => !(value in existingManifest.exports)).map((value) => `added package export ${value}`),
|
|
];
|
|
console.error(["public-api.json does not match the current CSS/package surface.", ...breakingChanges.map((change) => `BREAKING: ${change}`), ...additions.map((change) => `NEW: ${change}`), "Run npm run generate:api and commit the result."].join("\n"));
|
|
process.exitCode = 1;
|
|
} else {
|
|
console.log(`Public API contract is current: ${currentManifest.classes.length} classes, ${currentManifest.customProperties.length} custom properties, ${currentManifest.keyframes.length} keyframes, ${Object.keys(currentManifest.exports).length} exports.`);
|
|
}
|
|
}
|