From db683819c9357eeacd421fe5239209a63237382a Mon Sep 17 00:00:00 2001 From: fretuks Date: Sat, 21 Nov 2026 00:28:02 +0100 Subject: [PATCH] Add interactive component catalog and browser regression tests --- .gitea/workflows/ci.yml | 9 + CHANGELOG.md | 1 + README.md | 12 + package.json | 9 +- public-api.json | 597 ++++++++++++++++++++++++++++++++++++++ scripts/check-release.mjs | 47 +++ scripts/public-api.mjs | 95 ++++++ scripts/test-package.mjs | 60 ++++ 8 files changed, 829 insertions(+), 1 deletion(-) create mode 100644 public-api.json create mode 100644 scripts/check-release.mjs create mode 100644 scripts/public-api.mjs create mode 100644 scripts/test-package.mjs diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index 8e21bfc..f7bdcd5 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -24,6 +24,9 @@ jobs: - name: Install dependencies run: npm ci + - name: Check release metadata and public API contract + run: npm run check:release + - name: Run structure, browser, and accessibility tests run: npm run test:all @@ -33,5 +36,11 @@ jobs: - name: Check bundle size run: npm run size + - name: Test the packed package as a consumer + run: npm run test:package + + - name: Verify generated contracts are committed + run: git diff --exit-code -- utilities.css public-api.json + - name: Verify package contents run: npm pack --dry-run diff --git a/CHANGELOG.md b/CHANGELOG.md index f5f2f07..a36059b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - Added navbar, breadcrumbs, tabs, accordion, pagination, table, and spinner primitives. - Added semantic link, code, disabled, font-size, and breakpoint tokens. - Expanded the example page and validation checks for the public API. +- Added a generated public API manifest, breaking-change detection, release metadata validation, and isolated tarball consumer tests. ## 0.4.0 diff --git a/README.md b/README.md index 7fc7be5..78033c1 100644 --- a/README.md +++ b/README.md @@ -259,9 +259,21 @@ Das Paket enthält absichtlich nur die CSS-Module, die Doku, die Lizenz und ein ```bash npm run lint npm run size +npm run check:release +npm run test:package npm pack --dry-run ``` +`prepack` erzeugt vor jedem Tarball die Utilities und `public-api.json` neu, baut alle Dateien unter `dist/` und prüft anschließend Version, Changelog und API-Vertrag. `npm run test:package` installiert den gebauten Tarball in ein isoliertes temporäres Consumer-Projekt und löst jeden Eintrag aus `package.json#exports` auf. + +`public-api.json` ist die versionierte Baseline für alle öffentlichen `frost-*`-Klassen, `--frost-*`-Properties, CSS-Layer und Paket-Exports. Neue APIs werden mit `npm run generate:api` aufgenommen. Entfernte Klassen, Properties, Layer oder Exportpfade werden als Breaking Change abgelehnt. Für einen bewusst brechenden Release muss die Baseline explizit aktualisiert werden: + +```bash +npm run generate:api -- --allow-breaking +``` + +Nach einer Generierung sollten `utilities.css` und `public-api.json` gemeinsam mit der verursachenden Änderung committed werden. CI prüft, dass beide Dateien nach dem Packaging unverändert bleiben. + Browser- und Accessibility-Regressionstests laufen mit Playwright und axe-core gegen die Beispielseite. Beim ersten Mal muss Chromium installiert werden: ```bash diff --git a/package.json b/package.json index 6f9d0c9..c9fcbba 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "./components": "./components.css", "./utilities": "./utilities.css", "./tokens": "./tokens.css", + "./public-api.json": "./public-api.json", "./package.json": "./package.json" }, "files": [ @@ -31,6 +32,7 @@ "README.md", "CHANGELOG.md", "LICENSE", + "public-api.json", "scripts/*.mjs" ], "keywords": [ @@ -51,12 +53,17 @@ "lint": "node scripts/check.mjs", "size": "node scripts/size.mjs", "build": "node scripts/build.mjs", + "check:api": "node scripts/public-api.mjs --check", + "check:release": "node scripts/check-release.mjs && npm run check:api", + "generate:api": "node scripts/public-api.mjs --write", "test": "node scripts/check.mjs && node scripts/check-example.mjs", "test:all": "npm test && npm run test:browser", "test:browser": "playwright test", "test:browser:update": "playwright test --update-snapshots", "test:example": "node scripts/check-example.mjs", - "generate:utilities": "node scripts/generate-utilities.mjs" + "test:package": "npm run prepack && node scripts/test-package.mjs", + "generate:utilities": "node scripts/generate-utilities.mjs", + "prepack": "npm run generate:utilities && npm run generate:api && npm run build && npm run check:release" }, "sideEffects": true, "devDependencies": { diff --git a/public-api.json b/public-api.json new file mode 100644 index 0000000..f3af4ff --- /dev/null +++ b/public-api.json @@ -0,0 +1,597 @@ +{ + "schemaVersion": 1, + "packageVersion": "0.5.0", + "classes": [ + "frost-absolute", + "frost-accordion", + "frost-accordion__item", + "frost-accordion__panel", + "frost-accordion__trigger", + "frost-active:translate-y-1", + "frost-alert", + "frost-alert--danger", + "frost-alert--info", + "frost-alert--success", + "frost-alert--warning", + "frost-alert__body", + "frost-alert__title", + "frost-backdrop", + "frost-badge", + "frost-badge--danger", + "frost-badge--info", + "frost-badge--success", + "frost-badge--warning", + "frost-bg", + "frost-bg-accent", + "frost-bg-danger", + "frost-bg-soft", + "frost-bg-success", + "frost-bg-surface", + "frost-bg-warning", + "frost-block", + "frost-border", + "frost-border-0", + "frost-border-strong", + "frost-breadcrumbs", + "frost-breadcrumbs__item", + "frost-button", + "frost-button--block", + "frost-button--danger", + "frost-button--ghost", + "frost-button--icon", + "frost-button--lg", + "frost-button--link", + "frost-button--primary", + "frost-button--secondary", + "frost-button--sm", + "frost-button--success", + "frost-card", + "frost-card--elevated", + "frost-card--flat", + "frost-card--interactive", + "frost-center", + "frost-cluster", + "frost-col-span-1", + "frost-col-span-12", + "frost-col-span-2", + "frost-col-span-3", + "frost-col-span-4", + "frost-col-span-5", + "frost-col-span-6", + "frost-container", + "frost-content-center", + "frost-dark:bg-surface", + "frost-disabled:opacity", + "frost-dropdown", + "frost-dropdown__item", + "frost-dropdown__menu", + "frost-dropdown__menu--end", + "frost-error", + "frost-field", + "frost-field--disabled", + "frost-fixed", + "frost-flex", + "frost-flex-1", + "frost-flex-auto", + "frost-flex-col", + "frost-flex-none", + "frost-flex-nowrap", + "frost-flex-row", + "frost-flex-wrap", + "frost-focus-ring", + "frost-focus:border-accent", + "frost-focus:ring", + "frost-font-bold", + "frost-font-medium", + "frost-font-mono", + "frost-font-normal", + "frost-font-semibold", + "frost-gap-0", + "frost-gap-1", + "frost-gap-10", + "frost-gap-12", + "frost-gap-16", + "frost-gap-2", + "frost-gap-3", + "frost-gap-4", + "frost-gap-5", + "frost-gap-6", + "frost-gap-8", + "frost-grid", + "frost-grid--dense", + "frost-grid-cols-1", + "frost-grid-cols-12", + "frost-grid-cols-2", + "frost-grid-cols-3", + "frost-grid-cols-4", + "frost-grid-cols-5", + "frost-grid-cols-6", + "frost-grow", + "frost-grow-0", + "frost-h-full", + "frost-h-screen", + "frost-help", + "frost-hidden", + "frost-hide-desktop", + "frost-hide-mobile", + "frost-hover:bg-hover", + "frost-hover:bg-soft", + "frost-hover:shadow", + "frost-hover:text-accent", + "frost-inline", + "frost-inline-block", + "frost-inline-flex", + "frost-input", + "frost-input--readonly", + "frost-input--success", + "frost-input--warning", + "frost-invisible", + "frost-italic", + "frost-items-center", + "frost-items-end", + "frost-items-start", + "frost-items-stretch", + "frost-justify-around", + "frost-justify-between", + "frost-justify-center", + "frost-justify-end", + "frost-justify-evenly", + "frost-justify-start", + "frost-label", + "frost-leading-body", + "frost-leading-heading", + "frost-leading-none", + "frost-lg:block", + "frost-lg:flex", + "frost-lg:flex-col", + "frost-lg:flex-row", + "frost-lg:gap-12", + "frost-lg:gap-2", + "frost-lg:gap-4", + "frost-lg:gap-6", + "frost-lg:gap-8", + "frost-lg:grid", + "frost-lg:grid-cols-1", + "frost-lg:grid-cols-12", + "frost-lg:grid-cols-2", + "frost-lg:grid-cols-3", + "frost-lg:grid-cols-4", + "frost-lg:grid-cols-6", + "frost-lg:hidden", + "frost-lg:items-center", + "frost-lg:justify-between", + "frost-lg:p-12", + "frost-lg:p-2", + "frost-lg:p-4", + "frost-lg:p-6", + "frost-lg:p-8", + "frost-lg:text-center", + "frost-lg:text-left", + "frost-lowercase", + "frost-m-0", + "frost-m-1", + "frost-m-10", + "frost-m-12", + "frost-m-16", + "frost-m-2", + "frost-m-3", + "frost-m-4", + "frost-m-5", + "frost-m-6", + "frost-m-8", + "frost-main", + "frost-max-w-lg", + "frost-max-w-md", + "frost-max-w-none", + "frost-max-w-sm", + "frost-max-w-xl", + "frost-mb-0", + "frost-mb-1", + "frost-mb-10", + "frost-mb-12", + "frost-mb-16", + "frost-mb-2", + "frost-mb-3", + "frost-mb-4", + "frost-mb-5", + "frost-mb-6", + "frost-mb-8", + "frost-md:block", + "frost-md:flex", + "frost-md:flex-col", + "frost-md:flex-row", + "frost-md:gap-12", + "frost-md:gap-2", + "frost-md:gap-4", + "frost-md:gap-6", + "frost-md:gap-8", + "frost-md:grid", + "frost-md:grid-cols-1", + "frost-md:grid-cols-12", + "frost-md:grid-cols-2", + "frost-md:grid-cols-3", + "frost-md:grid-cols-4", + "frost-md:grid-cols-6", + "frost-md:hidden", + "frost-md:items-center", + "frost-md:justify-between", + "frost-md:p-12", + "frost-md:p-2", + "frost-md:p-4", + "frost-md:p-6", + "frost-md:p-8", + "frost-md:text-center", + "frost-md:text-left", + "frost-min-h-screen", + "frost-min-w-0", + "frost-ml-0", + "frost-ml-1", + "frost-ml-10", + "frost-ml-12", + "frost-ml-16", + "frost-ml-2", + "frost-ml-3", + "frost-ml-4", + "frost-ml-5", + "frost-ml-6", + "frost-ml-8", + "frost-modal", + "frost-modal--drawer", + "frost-modal--fullscreen", + "frost-modal__body", + "frost-modal__content", + "frost-modal__footer", + "frost-modal__header", + "frost-motion-safe:transition", + "frost-mr-0", + "frost-mr-1", + "frost-mr-10", + "frost-mr-12", + "frost-mr-16", + "frost-mr-2", + "frost-mr-3", + "frost-mr-4", + "frost-mr-5", + "frost-mr-6", + "frost-mr-8", + "frost-mt-0", + "frost-mt-1", + "frost-mt-10", + "frost-mt-12", + "frost-mt-16", + "frost-mt-2", + "frost-mt-3", + "frost-mt-4", + "frost-mt-5", + "frost-mt-6", + "frost-mt-8", + "frost-mx-0", + "frost-mx-1", + "frost-mx-10", + "frost-mx-12", + "frost-mx-16", + "frost-mx-2", + "frost-mx-3", + "frost-mx-4", + "frost-mx-5", + "frost-mx-6", + "frost-mx-8", + "frost-mx-auto", + "frost-my-0", + "frost-my-1", + "frost-my-10", + "frost-my-12", + "frost-my-16", + "frost-my-2", + "frost-my-3", + "frost-my-4", + "frost-my-5", + "frost-my-6", + "frost-my-8", + "frost-my-auto", + "frost-navbar", + "frost-navbar__brand", + "frost-navbar__link", + "frost-navbar__links", + "frost-notice", + "frost-notice--danger", + "frost-notice--info", + "frost-notice--success", + "frost-notice--warning", + "frost-opacity-0", + "frost-opacity-100", + "frost-opacity-50", + "frost-opacity-75", + "frost-overflow-auto", + "frost-overflow-hidden", + "frost-overflow-x-auto", + "frost-p-0", + "frost-p-1", + "frost-p-10", + "frost-p-12", + "frost-p-16", + "frost-p-2", + "frost-p-3", + "frost-p-4", + "frost-p-5", + "frost-p-6", + "frost-p-8", + "frost-pagination", + "frost-pagination__item", + "frost-pb-0", + "frost-pb-1", + "frost-pb-10", + "frost-pb-12", + "frost-pb-16", + "frost-pb-2", + "frost-pb-3", + "frost-pb-4", + "frost-pb-5", + "frost-pb-6", + "frost-pb-8", + "frost-pl-0", + "frost-pl-1", + "frost-pl-10", + "frost-pl-12", + "frost-pl-16", + "frost-pl-2", + "frost-pl-3", + "frost-pl-4", + "frost-pl-5", + "frost-pl-6", + "frost-pl-8", + "frost-place-items-center", + "frost-pointer-events-none", + "frost-popover", + "frost-popover__content", + "frost-popover__content--end", + "frost-pr-0", + "frost-pr-1", + "frost-pr-10", + "frost-pr-12", + "frost-pr-16", + "frost-pr-2", + "frost-pr-3", + "frost-pr-4", + "frost-pr-5", + "frost-pr-6", + "frost-pr-8", + "frost-pt-0", + "frost-pt-1", + "frost-pt-10", + "frost-pt-12", + "frost-pt-16", + "frost-pt-2", + "frost-pt-3", + "frost-pt-4", + "frost-pt-5", + "frost-pt-6", + "frost-pt-8", + "frost-px-0", + "frost-px-1", + "frost-px-10", + "frost-px-12", + "frost-px-16", + "frost-px-2", + "frost-px-3", + "frost-px-4", + "frost-px-5", + "frost-px-6", + "frost-px-8", + "frost-py-0", + "frost-py-1", + "frost-py-10", + "frost-py-12", + "frost-py-16", + "frost-py-2", + "frost-py-3", + "frost-py-4", + "frost-py-5", + "frost-py-6", + "frost-py-8", + "frost-relative", + "frost-responsive", + "frost-rounded", + "frost-rounded-full", + "frost-rounded-lg", + "frost-rounded-pill", + "frost-rounded-sm", + "frost-rounded-xl", + "frost-row", + "frost-select", + "frost-select--success", + "frost-select--warning", + "frost-self-center", + "frost-self-end", + "frost-self-start", + "frost-shadow", + "frost-shadow-lg", + "frost-shadow-none", + "frost-shadow-sm", + "frost-show-mobile", + "frost-shrink", + "frost-shrink-0", + "frost-sidebar", + "frost-sidebar-layout", + "frost-skip-link", + "frost-space-y-0", + "frost-space-y-1", + "frost-space-y-10", + "frost-space-y-12", + "frost-space-y-16", + "frost-space-y-2", + "frost-space-y-3", + "frost-space-y-4", + "frost-space-y-5", + "frost-space-y-6", + "frost-space-y-8", + "frost-spinner", + "frost-spinner--lg", + "frost-spinner--sm", + "frost-stack", + "frost-sticky", + "frost-success", + "frost-surface", + "frost-table", + "frost-table-wrap", + "frost-tabs", + "frost-tabs__panel", + "frost-tabs__tab", + "frost-text", + "frost-text-2xl", + "frost-text-accent", + "frost-text-base", + "frost-text-center", + "frost-text-danger", + "frost-text-end", + "frost-text-left", + "frost-text-lg", + "frost-text-link", + "frost-text-muted", + "frost-text-right", + "frost-text-sm", + "frost-text-start", + "frost-text-success", + "frost-text-warning", + "frost-text-xl", + "frost-text-xs", + "frost-textarea", + "frost-textarea--success", + "frost-textarea--warning", + "frost-toast", + "frost-toast--danger", + "frost-toast--success", + "frost-toast--warning", + "frost-toast-region", + "frost-tooltip", + "frost-transition", + "frost-truncate", + "frost-uppercase", + "frost-visible", + "frost-visually-hidden", + "frost-w-full", + "frost-w-screen", + "frost-warning", + "frost-z-0", + "frost-z-1", + "frost-z-10", + "frost-z-100", + "frost-z-20", + "frost-z-25", + "frost-z-50", + "frost-z-75" + ], + "customProperties": [ + "--frost-breakpoint-lg", + "--frost-breakpoint-md", + "--frost-cluster-gap", + "--frost-color-accent", + "--frost-color-accent-hover", + "--frost-color-backdrop", + "--frost-color-bg", + "--frost-color-border", + "--frost-color-border-strong", + "--frost-color-code-bg", + "--frost-color-code-text", + "--frost-color-danger", + "--frost-color-disabled", + "--frost-color-info", + "--frost-color-link", + "--frost-color-link-hover", + "--frost-color-on-accent", + "--frost-color-on-danger", + "--frost-color-on-success", + "--frost-color-on-warning", + "--frost-color-overlay", + "--frost-color-selection", + "--frost-color-success", + "--frost-color-surface", + "--frost-color-surface-hover", + "--frost-color-surface-soft", + "--frost-color-text", + "--frost-color-text-muted", + "--frost-color-warning", + "--frost-container-gutter", + "--frost-container-max", + "--frost-duration-base", + "--frost-duration-fast", + "--frost-duration-slow", + "--frost-ease-out", + "--frost-ease-standard", + "--frost-focus-color", + "--frost-focus-offset", + "--frost-focus-width", + "--frost-font-body", + "--frost-font-display", + "--frost-font-heading", + "--frost-font-mono", + "--frost-font-size-2xl", + "--frost-font-size-base", + "--frost-font-size-lg", + "--frost-font-size-sm", + "--frost-font-size-xl", + "--frost-grid-gap", + "--frost-grid-min", + "--frost-line-height-body", + "--frost-line-height-heading", + "--frost-radius-lg", + "--frost-radius-md", + "--frost-radius-pill", + "--frost-radius-sm", + "--frost-radius-xl", + "--frost-row-gap", + "--frost-shadow-lg", + "--frost-shadow-md", + "--frost-shadow-sm", + "--frost-sidebar-width", + "--frost-space-0", + "--frost-space-1", + "--frost-space-10", + "--frost-space-12", + "--frost-space-16", + "--frost-space-2", + "--frost-space-3", + "--frost-space-4", + "--frost-space-5", + "--frost-space-6", + "--frost-space-8", + "--frost-space-y", + "--frost-stack-gap", + "--frost-z-modal", + "--frost-z-overlay", + "--frost-z-popover", + "--frost-z-toast" + ], + "layers": [ + "frost.tokens", + "frost.base", + "frost.accessibility", + "frost.layout", + "frost.controls", + "frost.components", + "frost.utilities" + ], + "exports": { + ".": "./frost.css", + "./accessibility.css": "./accessibility.css", + "./base": "./base.css", + "./base.css": "./base.css", + "./component-layer": "./component-layer.css", + "./component-layer.css": "./component-layer.css", + "./components": "./components.css", + "./components.css": "./components.css", + "./controls.css": "./controls.css", + "./core": "./core.css", + "./core.css": "./core.css", + "./frost.css": "./frost.css", + "./layout.css": "./layout.css", + "./package.json": "./package.json", + "./public-api.json": "./public-api.json", + "./tokens": "./tokens.css", + "./tokens.css": "./tokens.css", + "./utilities": "./utilities.css", + "./utilities.css": "./utilities.css", + "./utility-layer": "./utility-layer.css", + "./utility-layer.css": "./utility-layer.css" + } +} diff --git a/scripts/check-release.mjs b/scripts/check-release.mjs new file mode 100644 index 0000000..e4387f8 --- /dev/null +++ b/scripts/check-release.mjs @@ -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.`); +} diff --git a/scripts/public-api.mjs b/scripts/public-api.mjs new file mode 100644 index 0000000..cb5cb74 --- /dev/null +++ b/scripts/public-api.mjs @@ -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.`); + } +} diff --git a/scripts/test-package.mjs b/scripts/test-package.mjs new file mode 100644 index 0000000..d7b5cee --- /dev/null +++ b/scripts/test-package.mjs @@ -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 }); +}