Add interactive component catalog and browser regression tests
CI / Test and package (push) Failing after 4s
CI / Test and package (push) Failing after 4s
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import { readFile } from "node:fs/promises";
|
||||
import { resolve } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const root = fileURLToPath(new URL("..", import.meta.url));
|
||||
const packageJson = JSON.parse(await readFile(resolve(root, "package.json"), "utf8"));
|
||||
const packageLock = JSON.parse(await readFile(resolve(root, "package-lock.json"), "utf8"));
|
||||
const changelog = await readFile(resolve(root, "CHANGELOG.md"), "utf8");
|
||||
const errors = [];
|
||||
const semverPattern = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/;
|
||||
|
||||
if (!semverPattern.test(packageJson.version)) errors.push(`package.json: invalid semantic version ${packageJson.version}`);
|
||||
if (packageLock.name !== packageJson.name) errors.push(`package-lock.json: package name ${packageLock.name} does not match ${packageJson.name}`);
|
||||
if (packageLock.version !== packageJson.version) errors.push(`package-lock.json: version ${packageLock.version} does not match ${packageJson.version}`);
|
||||
if (packageLock.packages?.[""]?.name !== packageJson.name) errors.push("package-lock.json: root package name is stale");
|
||||
if (packageLock.packages?.[""]?.version !== packageJson.version) errors.push("package-lock.json: root package version is stale");
|
||||
|
||||
const releases = [...changelog.matchAll(/^##\s+\[?(\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?)\]?\s*$/gm)].map((match) => ({ version: match[1], index: match.index }));
|
||||
if (!releases.length) errors.push("CHANGELOG.md: no release headings found");
|
||||
else if (releases[0].version !== packageJson.version) errors.push(`CHANGELOG.md: latest release ${releases[0].version} does not match package version ${packageJson.version}`);
|
||||
|
||||
const seenVersions = new Set();
|
||||
const compareVersions = (left, right) => {
|
||||
const leftParts = left.split("-")[0].split(".").map(Number);
|
||||
const rightParts = right.split("-")[0].split(".").map(Number);
|
||||
for (let index = 0; index < 3; index += 1) {
|
||||
if (leftParts[index] !== rightParts[index]) return leftParts[index] - rightParts[index];
|
||||
}
|
||||
return left.includes("-") === right.includes("-") ? left.localeCompare(right) : left.includes("-") ? -1 : 1;
|
||||
};
|
||||
for (let index = 0; index < releases.length; index += 1) {
|
||||
const release = releases[index];
|
||||
if (seenVersions.has(release.version)) errors.push(`CHANGELOG.md: duplicate release ${release.version}`);
|
||||
seenVersions.add(release.version);
|
||||
const sectionEnd = releases[index + 1]?.index ?? changelog.length;
|
||||
const section = changelog.slice(release.index, sectionEnd);
|
||||
if (!/^\s*-\s+\S/m.test(section)) errors.push(`CHANGELOG.md: release ${release.version} has no change entries`);
|
||||
const nextRelease = releases[index + 1];
|
||||
if (nextRelease && compareVersions(release.version, nextRelease.version) <= 0) errors.push(`CHANGELOG.md: ${release.version} must appear after a newer version, not before ${nextRelease.version}`);
|
||||
}
|
||||
|
||||
if (errors.length) {
|
||||
console.error(errors.join("\n"));
|
||||
process.exitCode = 1;
|
||||
} else {
|
||||
console.log(`Release metadata is consistent for ${packageJson.name}@${packageJson.version}; ${releases.length} changelog releases checked.`);
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
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", "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 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: 1,
|
||||
packageVersion: packageJson.version,
|
||||
classes: sortLexically(classes),
|
||||
customProperties: sortLexically(customProperties),
|
||||
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 [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, 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}`),
|
||||
...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, ${Object.keys(currentManifest.exports).length} exports.`);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
import { execFile } from "node:child_process";
|
||||
import { mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { basename, join, resolve, sep } from "node:path";
|
||||
import { createRequire } from "node:module";
|
||||
import { promisify } from "node:util";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const execFileAsync = promisify(execFile);
|
||||
const root = fileURLToPath(new URL("..", import.meta.url));
|
||||
const packageJson = JSON.parse(await readFile(resolve(root, "package.json"), "utf8"));
|
||||
const temporaryRoot = await mkdtemp(join(tmpdir(), "frost-consumer-"));
|
||||
const fixture = join(temporaryRoot, "fixture");
|
||||
const npmCli = process.env.npm_execpath;
|
||||
|
||||
const runNpm = async (arguments_, options = {}) => {
|
||||
if (npmCli) return execFileAsync(process.execPath, [npmCli, ...arguments_], { maxBuffer: 10 * 1024 * 1024, ...options });
|
||||
const command = process.platform === "win32" ? "npm.cmd" : "npm";
|
||||
return execFileAsync(command, arguments_, { maxBuffer: 10 * 1024 * 1024, ...options });
|
||||
};
|
||||
|
||||
try {
|
||||
const { stdout } = await runNpm(["pack", "--json", "--ignore-scripts", "--pack-destination", temporaryRoot], { cwd: root });
|
||||
const packResult = JSON.parse(stdout)[0];
|
||||
const tarball = join(temporaryRoot, packResult.filename);
|
||||
const packedPaths = new Set(packResult.files.map((file) => file.path.replaceAll("\\", "/")));
|
||||
|
||||
for (const target of Object.values(packageJson.exports)) {
|
||||
const path = target.replace(/^\.\//, "");
|
||||
if (!packedPaths.has(path)) throw new Error(`Tarball is missing export target ${target}`);
|
||||
}
|
||||
for (const required of ["public-api.json", "dist/frost.css", "dist/frost.min.css", "dist/utility-layer.css", "dist/component-layer.css"]) {
|
||||
if (!packedPaths.has(required)) throw new Error(`Tarball is missing required file ${required}`);
|
||||
}
|
||||
const forbidden = [...packedPaths].filter((path) => /^(?:examples|tests|\.gitea|node_modules)\//.test(path));
|
||||
if (forbidden.length) throw new Error(`Tarball contains repository-only files: ${forbidden.join(", ")}`);
|
||||
|
||||
await writeFile(join(temporaryRoot, "package.json"), `${JSON.stringify({ private: true, name: "frost-consumer-fixture", version: "1.0.0" }, null, 2)}\n`);
|
||||
await runNpm(["install", "--ignore-scripts", "--no-audit", "--no-fund", "--no-package-lock", tarball], { cwd: temporaryRoot });
|
||||
|
||||
const installedRoot = join(temporaryRoot, "node_modules", ...packageJson.name.split("/"));
|
||||
const installedPackage = JSON.parse(await readFile(join(installedRoot, "package.json"), "utf8"));
|
||||
if (installedPackage.version !== packageJson.version) throw new Error(`Installed version ${installedPackage.version} does not match ${packageJson.version}`);
|
||||
|
||||
const requireFromFixture = createRequire(join(temporaryRoot, "consumer.cjs"));
|
||||
for (const [exportPath, target] of Object.entries(packageJson.exports)) {
|
||||
const specifier = exportPath === "." ? packageJson.name : `${packageJson.name}${exportPath.slice(1)}`;
|
||||
const resolved = requireFromFixture.resolve(specifier);
|
||||
const expected = resolve(installedRoot, target);
|
||||
if (resolve(resolved) !== expected) throw new Error(`${specifier} resolved to ${resolved}, expected ${expected}`);
|
||||
const metadata = await stat(resolved);
|
||||
if (!metadata.isFile() || metadata.size === 0) throw new Error(`${specifier} resolved to an empty or non-file target`);
|
||||
}
|
||||
|
||||
const tarballMetadata = await stat(tarball);
|
||||
console.log(`Installed ${basename(tarball)} (${tarballMetadata.size} bytes) and resolved all ${Object.keys(packageJson.exports).length} package exports from an isolated consumer.`);
|
||||
} finally {
|
||||
if (!resolve(temporaryRoot).startsWith(resolve(tmpdir()) + sep)) throw new Error("Refusing to remove a temporary directory outside the system temp root");
|
||||
await rm(temporaryRoot, { recursive: true, force: true });
|
||||
}
|
||||
Reference in New Issue
Block a user