create-ailk 0.47.0 → 0.48.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +192 -43
- package/component-catalog.json +223 -285
- package/component-catalog.md +1 -1
- package/dist/apply-design.d.ts +26 -68
- package/dist/apply-design.js +150 -322
- package/dist/cli.js +152 -17
- package/dist/compare-chrome.d.ts +108 -0
- package/dist/compare-chrome.js +481 -0
- package/dist/compare-look.d.ts +94 -0
- package/dist/compare-look.js +378 -0
- package/dist/copy.js +1 -1
- package/dist/design-compile.d.ts +17 -21
- package/dist/design-compile.js +32 -56
- package/dist/design-files.d.ts +93 -0
- package/dist/design-files.js +312 -0
- package/dist/design-home.d.ts +4 -2
- package/dist/design-home.js +88 -6
- package/dist/design-names.d.ts +13 -0
- package/dist/design-names.js +76 -0
- package/dist/design-package.d.ts +46 -56
- package/dist/design-package.js +285 -288
- package/dist/design-presets.d.ts +41 -0
- package/dist/design-presets.js +83 -4
- package/dist/design-scaffold.d.ts +12 -2
- package/dist/design-scaffold.js +72 -34
- package/dist/generated/design-settings.d.ts +56 -0
- package/dist/generated/design-settings.js +237 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -2
- package/dist/inventory.d.ts +3 -2
- package/dist/inventory.js +10 -4
- package/dist/lib/design-checks.d.ts +46 -0
- package/dist/lib/design-checks.js +79 -1
- package/dist/lib/env-catalog.js +1 -1
- package/dist/lib/module-license-gate.d.ts +1 -1
- package/dist/lib/module-license-gate.js +1 -1
- package/dist/lib/render-verdict.d.ts +13 -1
- package/dist/lib/render-verdict.js +41 -1
- package/dist/look-hub.d.ts +44 -0
- package/dist/look-hub.js +130 -0
- package/dist/look-record.d.ts +35 -0
- package/dist/look-record.js +85 -0
- package/dist/look-samples.d.ts +66 -0
- package/dist/look-samples.js +456 -0
- package/dist/look-settings.d.ts +53 -0
- package/dist/look-settings.js +807 -0
- package/dist/module-architecture.js +12 -0
- package/dist/parse-args.d.ts +10 -6
- package/dist/parse-args.js +26 -13
- package/dist/programmatic.d.ts +1 -19
- package/dist/programmatic.js +65 -103
- package/dist/retired-design-bodies.d.ts +29 -0
- package/dist/retired-design-bodies.js +51 -0
- package/dist/seed-flows.d.ts +1 -1
- package/dist/seed-flows.js +12 -14
- package/dist/seed-workspace.d.ts +4 -1
- package/dist/seed-workspace.js +139 -17
- package/dist/site-chrome.d.ts +42 -41
- package/dist/site-chrome.js +51 -70
- package/dist/site-fonts.d.ts +14 -14
- package/dist/site-fonts.js +9 -4
- package/dist/starter-grid.d.ts +2 -2
- package/dist/strip-look-copies.d.ts +67 -0
- package/dist/strip-look-copies.js +591 -0
- package/dist/template-manifest.d.ts +6 -14
- package/dist/template-manifest.js +0 -9
- package/dist/template-record.d.ts +3 -1
- package/dist/template-record.js +3 -1
- package/dist/topology.d.ts +15 -0
- package/dist/topology.js +60 -0
- package/dist/update.d.ts +57 -1
- package/dist/update.js +742 -13
- package/package.json +3 -4
- package/site-templates/aliases/hub.TEMPLATE-MANIFEST.json +2 -31
- package/site-templates/designs/minimalism/DESIGN.md +163 -297
- package/site-templates/designs/minimalism/LOOK.md +42 -0
- package/site-templates/designs/minimalism/design.yaml +167 -0
- package/site-templates/designs/standard/DESIGN.md +145 -149
- package/site-templates/designs/standard/LOOK.md +42 -0
- package/site-templates/designs/standard/design.yaml +72 -0
- package/site-templates/marketing-site/TEMPLATE-MANIFEST.json +2 -31
- package/site-templates/marketing-site/TEMPLATE-MANIFEST.md +1 -25
- package/site-templates/marketing-site-free/TEMPLATE-MANIFEST.json +2 -31
- package/site-templates/marketing-site-free/TEMPLATE-MANIFEST.md +1 -25
- package/site-templates/shapes/local-business-service/TEMPLATE-MANIFEST.json +2 -31
- package/site-templates/shapes/local-business-service/TEMPLATE-MANIFEST.md +1 -25
- package/site-templates/shapes/organization-service/TEMPLATE-MANIFEST.json +2 -31
- package/site-templates/shapes/organization-service/TEMPLATE-MANIFEST.md +1 -25
- package/site-templates/shapes/person-service/TEMPLATE-MANIFEST.json +2 -31
- package/site-templates/shapes/person-service/TEMPLATE-MANIFEST.md +1 -25
- package/site-templates/shapes/person-service/content/_site.mdx +0 -26
- package/templates/.claude/rules/extending.md +3 -2
- package/templates/.env.example +11 -4
- package/templates/CLAUDE.md +1 -1
- package/templates/DESIGN.md +145 -149
- package/templates/README.md +15 -16
- package/templates/apps/api/.env.example +11 -4
- package/templates/apps/api/CLAUDE.md +6 -4
- package/templates/apps/api/src/lib/__mocks__/prisma.ts +15 -5
- package/templates/apps/mcp/CLAUDE.md +4 -2
- package/templates/apps/web/.env.example +10 -3
- package/templates/apps/web/CLAUDE.md +14 -3
- package/templates/apps/web/app/[locale]/layout.tsx +53 -50
- package/templates/apps/web/app/not-found.tsx +11 -11
- package/templates/apps/web/auth.ts +48 -35
- package/templates/apps/web/jest.config.cjs +5 -0
- package/templates/apps/web/lib/__tests__/hub-site-config.test.ts +179 -31
- package/templates/apps/web/lib/__tests__/library-fonts.test.ts +35 -0
- package/templates/apps/web/lib/__tests__/page-chrome-navigation.test.ts +71 -32
- package/templates/apps/web/lib/__tests__/page-chrome.test.ts +123 -26
- package/templates/apps/web/lib/__tests__/section-look.test.ts +150 -0
- package/templates/apps/web/lib/__tests__/site-theme.test.ts +81 -289
- package/templates/apps/web/lib/deck-page.ts +3 -10
- package/templates/apps/web/lib/deploy-fonts.ts +12 -0
- package/templates/apps/web/lib/hub-site-config.ts +97 -14
- package/templates/apps/web/lib/library-fonts.ts +41 -0
- package/templates/apps/web/lib/mcp-oauth.ts +11 -4
- package/templates/apps/web/lib/page-chrome.ts +52 -34
- package/templates/apps/web/lib/section-look.ts +28 -0
- package/templates/apps/web/lib/site-design.ts +205 -0
- package/templates/apps/web/lib/site-theme-head.ts +98 -13
- package/templates/apps/web/lib/site-theme.ts +40 -62
- package/templates/apps/web/messages/en.json +0 -6
- package/templates/apps/web/next.config.mjs +195 -6
- package/templates/apps/web/package.json +2 -0
- package/templates/apps/web/site-pages.config.tsx +4 -2
- package/templates/content/_site.mdx +0 -26
- package/templates/design.yaml +75 -0
- package/templates/project.yaml +14 -0
- package/versions.json +52 -51
- package/site-templates/marketing-site/DESIGN.md +0 -259
- package/site-templates/marketing-site-free/DESIGN.md +0 -259
- package/site-templates/saas-product/DESIGN.md +0 -259
- package/templates/apps/web/lib/__tests__/deck-page.test.ts +0 -288
- package/templates/apps/web/lib/__tests__/site-theme-head.test.ts +0 -70
- package/templates/apps/web/lib/site-chrome.ts +0 -46
- package/templates/apps/web/lib/site-fonts.ts +0 -15
|
@@ -0,0 +1,481 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `create-ailk compare-chrome` (#12403 D6, spec
|
|
3
|
+
* docs/specs/12403-import-each-live-site-s-spec.md): the before-and-after
|
|
4
|
+
* evidence for a change to a site's header and footer — the navigation
|
|
5
|
+
* import first, any later chrome change after it.
|
|
6
|
+
*
|
|
7
|
+
* `compare-renders` reads `main`'s text and takes its pictures with every
|
|
8
|
+
* menu closed, so a changed menu link passes it. This opens the menus. For
|
|
9
|
+
* each path, at each of `VIEWPORTS`, in a fresh signed-out context, it
|
|
10
|
+
* records five parts — `header`, each desktop `menu-<n>`, the phone's
|
|
11
|
+
* `mobile-menu` and `footer` — each as its links (`{ text, href }`, in
|
|
12
|
+
* document order) and a screenshot. `--capture` writes them; `--against`
|
|
13
|
+
* captures again and compares: links entry by entry, pixels by
|
|
14
|
+
* `diffPixels` at `--tolerance` (default 0). The header's render stamp
|
|
15
|
+
* (#12401: the `<head>` meta, else the header wrapper's attributes) is recorded, reported, and
|
|
16
|
+
* — with `--expect-source record --expect-version <n>` — required.
|
|
17
|
+
*
|
|
18
|
+
* Pure module: no `import.meta.url`, so jest imports it directly; the browser
|
|
19
|
+
* is injected, and the page is reached only through the `ChromePage` slice.
|
|
20
|
+
*/
|
|
21
|
+
import { createHash } from "node:crypto";
|
|
22
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
23
|
+
import { join } from "node:path";
|
|
24
|
+
import { parseArgs } from "node:util";
|
|
25
|
+
import { decodePng, diffPixels, PLAYWRIGHT_INSTALL_HINT, VIEWPORTS } from "./lib/render-comparison.js";
|
|
26
|
+
import { ParseError } from "./parse-args.js";
|
|
27
|
+
/** A refusal before anything is rendered: exit 2. */
|
|
28
|
+
export class CompareChromeRefusedError extends Error {
|
|
29
|
+
name = "CompareChromeRefusedError";
|
|
30
|
+
}
|
|
31
|
+
const NAVIGATION_TIMEOUT_MS = 60_000;
|
|
32
|
+
/** How long an opened menu may take to show its panel. */
|
|
33
|
+
const OPEN_WAIT_MS = 2_000;
|
|
34
|
+
/** The most disclosures the mobile menu is opened through — a bound, never a real limit. */
|
|
35
|
+
const MAX_DISCLOSURES = 24;
|
|
36
|
+
const NAV = '[data-testid="nav"]';
|
|
37
|
+
const FOOTER = '[data-testid="footer"]';
|
|
38
|
+
const MENU_TRIGGER = '[data-testid="section-nav"] button[aria-expanded="false"]:visible';
|
|
39
|
+
const FLYOUT_PANEL = '[data-testid="flyout-panel"]';
|
|
40
|
+
const HAMBURGER = '[data-testid="nav-hamburger"]:visible';
|
|
41
|
+
const MOBILE_MENU = '[data-testid="nav-mobile-menu"]';
|
|
42
|
+
const MOBILE_DISCLOSURE = '[data-testid="nav-mobile-menu"] button[aria-expanded="false"]:visible';
|
|
43
|
+
/** The first visible element matching `selector`, as a browser expression. */
|
|
44
|
+
const visible = (selector) => `Array.from(document.querySelectorAll(${JSON.stringify(selector)})).find((el) => el.checkVisibility())`;
|
|
45
|
+
/** Every `a[href]` inside the first visible `selector`, or null when none is visible. */
|
|
46
|
+
export const linksExpression = (selector) => `(() => { const root = ${visible(selector)}; if (!root) return null; ` +
|
|
47
|
+
`return Array.from(root.querySelectorAll("a[href]"), (a) => ({ ` +
|
|
48
|
+
`text: (a.textContent ?? "").replace(/\\s+/g, " ").trim(), href: a.getAttribute("href") ?? "" })); })()`;
|
|
49
|
+
/**
|
|
50
|
+
* The page's render stamp (#12401): the `<head>` meta every page carries
|
|
51
|
+
* whatever its header style, else the attributes on the header's wrapper.
|
|
52
|
+
*/
|
|
53
|
+
export const STAMP_EXPRESSION = '(() => { const meta = document.querySelector(\'meta[name="ailk-nav-source"]\'); ' +
|
|
54
|
+
'if (meta) return { source: meta.getAttribute("content"), version: meta.getAttribute("data-version") }; ' +
|
|
55
|
+
'const el = document.querySelector("[data-nav-source]"); ' +
|
|
56
|
+
'return { source: el ? el.getAttribute("data-nav-source") : null, ' +
|
|
57
|
+
'version: el ? el.getAttribute("data-nav-version") : null }; })()';
|
|
58
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
59
|
+
function asLinks(value) {
|
|
60
|
+
if (!Array.isArray(value))
|
|
61
|
+
return null;
|
|
62
|
+
return value.map((entry) => ({
|
|
63
|
+
text: String(entry?.text ?? ""),
|
|
64
|
+
href: String(entry?.href ?? ""),
|
|
65
|
+
}));
|
|
66
|
+
}
|
|
67
|
+
function asStamp(value) {
|
|
68
|
+
const stamp = (value ?? {});
|
|
69
|
+
return {
|
|
70
|
+
source: typeof stamp.source === "string" ? stamp.source : null,
|
|
71
|
+
version: typeof stamp.version === "string" ? stamp.version : null,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Whether `path` is a path on any origin it is joined to: `/`-rooted, not
|
|
76
|
+
* `//`, and resolving to itself — the rule `compare-renders` checks a plan's
|
|
77
|
+
* path by.
|
|
78
|
+
*/
|
|
79
|
+
function isSitePath(path) {
|
|
80
|
+
if (!/^\/(?!\/)/.test(path))
|
|
81
|
+
return false;
|
|
82
|
+
const probe = "https://origin.invalid";
|
|
83
|
+
try {
|
|
84
|
+
const url = new URL(path, probe);
|
|
85
|
+
return url.origin === probe && url.pathname === path;
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/** A part's file stem: readable, and unique per path by its hash. */
|
|
92
|
+
function stem(path, viewport, part) {
|
|
93
|
+
const slug = path.replace(/^\/+/, "").replace(/[^a-z0-9-]+/gi, "_").slice(0, 60) || "home";
|
|
94
|
+
const hash = createHash("sha256").update(path).digest("hex").slice(0, 8);
|
|
95
|
+
return `${slug}-${hash}--${viewport}--${part}`;
|
|
96
|
+
}
|
|
97
|
+
/** Captures one path at one viewport, writing each part's PNG to `dir`. */
|
|
98
|
+
async function captureViewport(page, origin, path, viewport, dir) {
|
|
99
|
+
const result = {
|
|
100
|
+
path,
|
|
101
|
+
viewport: viewport.name,
|
|
102
|
+
loaded: true,
|
|
103
|
+
stamp: { source: null, version: null },
|
|
104
|
+
parts: [],
|
|
105
|
+
failures: [],
|
|
106
|
+
};
|
|
107
|
+
await page.setViewportSize({ width: viewport.width, height: viewport.height });
|
|
108
|
+
let status;
|
|
109
|
+
try {
|
|
110
|
+
status = (await page.goto(origin + path, { waitUntil: "load", timeout: NAVIGATION_TIMEOUT_MS }))?.status();
|
|
111
|
+
}
|
|
112
|
+
catch (err) {
|
|
113
|
+
result.loaded = `navigation failed: ${err instanceof Error ? (err.message.split("\n")[0] ?? "") : String(err)}`;
|
|
114
|
+
return result;
|
|
115
|
+
}
|
|
116
|
+
if (status !== 200) {
|
|
117
|
+
result.loaded = status === undefined ? "no response" : `status ${status}, expected 200`;
|
|
118
|
+
return result;
|
|
119
|
+
}
|
|
120
|
+
const settled = new URL(page.url()).pathname;
|
|
121
|
+
if (settled !== path) {
|
|
122
|
+
result.loaded = `requested "${path}" but the page settled on "${settled}"`;
|
|
123
|
+
return result;
|
|
124
|
+
}
|
|
125
|
+
result.stamp = asStamp(await page.evaluate(STAMP_EXPRESSION));
|
|
126
|
+
const record = async (part, selector, shot) => {
|
|
127
|
+
const links = asLinks(await page.evaluate(linksExpression(selector)));
|
|
128
|
+
if (links === null)
|
|
129
|
+
return;
|
|
130
|
+
const name = `${stem(path, viewport.name, part)}.png`;
|
|
131
|
+
writeFileSync(join(dir, name), await shot());
|
|
132
|
+
result.parts.push({ path, viewport: viewport.name, part, links, shot: name });
|
|
133
|
+
};
|
|
134
|
+
const elementShot = (selector) => () => page.locator(`${selector}:visible`).nth(0).screenshot({ animations: "disabled" });
|
|
135
|
+
const viewportShot = () => page.screenshot({ animations: "disabled" });
|
|
136
|
+
/** Waits until `selector` is visible (or, `false`, until it is not); false when it never is. */
|
|
137
|
+
const shown = async (selector, wanted = true) => {
|
|
138
|
+
for (let waited = 0; waited <= OPEN_WAIT_MS; waited += 100) {
|
|
139
|
+
if ((await page.locator(`${selector}:visible`).count()) > 0 === wanted)
|
|
140
|
+
return true;
|
|
141
|
+
await sleep(100);
|
|
142
|
+
}
|
|
143
|
+
return false;
|
|
144
|
+
};
|
|
145
|
+
await record("header", NAV, elementShot(NAV));
|
|
146
|
+
if (viewport.name === "desktop") {
|
|
147
|
+
// Each visible menu trigger in the desktop row, opened in turn. After its
|
|
148
|
+
// Escape every trigger is collapsed again, so the n-th stays the n-th.
|
|
149
|
+
// A menu that opens nothing, or does not close before the next, fails the
|
|
150
|
+
// capture: skipped, it would compare equal by omission.
|
|
151
|
+
const triggers = await page.locator(MENU_TRIGGER).count();
|
|
152
|
+
for (let n = 0; n < triggers; n += 1) {
|
|
153
|
+
await page.locator(MENU_TRIGGER).nth(n).click();
|
|
154
|
+
if (await shown(FLYOUT_PANEL))
|
|
155
|
+
await record(`menu-${n + 1}`, FLYOUT_PANEL, viewportShot);
|
|
156
|
+
else
|
|
157
|
+
result.failures.push(`menu-${n + 1} did not open`);
|
|
158
|
+
await page.keyboard.press("Escape");
|
|
159
|
+
if (!(await shown(FLYOUT_PANEL, false))) {
|
|
160
|
+
result.failures.push(`menu-${n + 1} did not close`);
|
|
161
|
+
break;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
else if ((await page.locator(HAMBURGER).count()) > 0) {
|
|
166
|
+
// No menu button (a mark-only, bare or absent header) records no part:
|
|
167
|
+
// equal when neither side has one, `only before` when one is lost.
|
|
168
|
+
await page.locator(HAMBURGER).nth(0).click();
|
|
169
|
+
if (await shown(MOBILE_MENU)) {
|
|
170
|
+
for (let opened = 0; opened < MAX_DISCLOSURES; opened += 1) {
|
|
171
|
+
if ((await page.locator(MOBILE_DISCLOSURE).count()) === 0)
|
|
172
|
+
break;
|
|
173
|
+
await page.locator(MOBILE_DISCLOSURE).nth(0).click();
|
|
174
|
+
}
|
|
175
|
+
await record("mobile-menu", MOBILE_MENU, viewportShot);
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
result.failures.push("mobile-menu did not open");
|
|
179
|
+
}
|
|
180
|
+
await page.keyboard.press("Escape");
|
|
181
|
+
}
|
|
182
|
+
await record("footer", FOOTER, elementShot(FOOTER));
|
|
183
|
+
return result;
|
|
184
|
+
}
|
|
185
|
+
/** Every path at every viewport, each in a fresh signed-out context. */
|
|
186
|
+
async function captureAll(chromium, origin, paths, dir) {
|
|
187
|
+
mkdirSync(dir, { recursive: true });
|
|
188
|
+
const browser = await chromium.launch();
|
|
189
|
+
const captures = [];
|
|
190
|
+
try {
|
|
191
|
+
for (const path of paths) {
|
|
192
|
+
for (const viewport of VIEWPORTS) {
|
|
193
|
+
const context = await browser.newContext();
|
|
194
|
+
try {
|
|
195
|
+
captures.push(await captureViewport(await context.newPage(), origin, path, viewport, dir));
|
|
196
|
+
}
|
|
197
|
+
finally {
|
|
198
|
+
await context.close();
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
finally {
|
|
204
|
+
await browser.close();
|
|
205
|
+
}
|
|
206
|
+
for (const capture of captures) {
|
|
207
|
+
writeFileSync(join(dir, `${stem(capture.path, capture.viewport, "capture")}.json`), `${JSON.stringify(capture, null, 2)}\n`);
|
|
208
|
+
}
|
|
209
|
+
return captures;
|
|
210
|
+
}
|
|
211
|
+
function linkDifference(before, after) {
|
|
212
|
+
for (let i = 0; i < Math.max(before.length, after.length); i += 1) {
|
|
213
|
+
const a = before[i];
|
|
214
|
+
const b = after[i];
|
|
215
|
+
if (a?.text === b?.text && a?.href === b?.href)
|
|
216
|
+
continue;
|
|
217
|
+
const show = (link) => (link === undefined ? "(none)" : `"${link.text}" ${link.href}`);
|
|
218
|
+
return `link ${i + 1}: before ${show(a)} · after ${show(b)}`;
|
|
219
|
+
}
|
|
220
|
+
return null;
|
|
221
|
+
}
|
|
222
|
+
function pixelDifference(before, after, tolerance) {
|
|
223
|
+
const a = decodePng(before);
|
|
224
|
+
const b = decodePng(after);
|
|
225
|
+
if (!a.ok || !b.ok)
|
|
226
|
+
return "a screenshot could not be read";
|
|
227
|
+
const differing = diffPixels(a.image, b.image);
|
|
228
|
+
if (differing === "size-differs") {
|
|
229
|
+
return `screenshot size ${a.image.width}×${a.image.height} → ${b.image.width}×${b.image.height}`;
|
|
230
|
+
}
|
|
231
|
+
const share = differing / (a.image.width * a.image.height);
|
|
232
|
+
return share > tolerance ? `${differing} pixels differ (${(share * 100).toFixed(4)}%)` : null;
|
|
233
|
+
}
|
|
234
|
+
/** Captures or compares (D6); answers the exit code: 0 all equal, 1 a difference or a page that did not load. */
|
|
235
|
+
export async function compareChrome(o) {
|
|
236
|
+
const line = o.onLine ?? (() => { });
|
|
237
|
+
if ((o.capture === undefined) === (o.against === undefined)) {
|
|
238
|
+
throw new CompareChromeRefusedError("compare-chrome: pass exactly one of --capture <dir> and --against <dir>");
|
|
239
|
+
}
|
|
240
|
+
let origin;
|
|
241
|
+
try {
|
|
242
|
+
origin = new URL(o.origin).origin;
|
|
243
|
+
}
|
|
244
|
+
catch {
|
|
245
|
+
throw new CompareChromeRefusedError(`compare-chrome: --origin ${o.origin} is not a URL`);
|
|
246
|
+
}
|
|
247
|
+
if (!/^https?:$/.test(new URL(origin).protocol)) {
|
|
248
|
+
throw new CompareChromeRefusedError("compare-chrome: --origin must be an http(s) URL");
|
|
249
|
+
}
|
|
250
|
+
if (o.paths.length === 0)
|
|
251
|
+
throw new CompareChromeRefusedError("compare-chrome: pass at least one --path");
|
|
252
|
+
for (const path of o.paths) {
|
|
253
|
+
if (!isSitePath(path))
|
|
254
|
+
throw new CompareChromeRefusedError(`compare-chrome: --path ${path} is not a site path`);
|
|
255
|
+
}
|
|
256
|
+
if (o.against !== undefined) {
|
|
257
|
+
// The comparison is of the capture's own site and paths: a path left out,
|
|
258
|
+
// or another origin, would be a part never compared.
|
|
259
|
+
const captureFile = join(o.against, "capture.json");
|
|
260
|
+
if (!existsSync(captureFile))
|
|
261
|
+
throw new CompareChromeRefusedError(`compare-chrome: no capture at ${o.against}`);
|
|
262
|
+
let captured;
|
|
263
|
+
try {
|
|
264
|
+
captured = JSON.parse(readFileSync(captureFile, "utf8"));
|
|
265
|
+
}
|
|
266
|
+
catch {
|
|
267
|
+
throw new CompareChromeRefusedError(`compare-chrome: ${captureFile} is not a capture`);
|
|
268
|
+
}
|
|
269
|
+
if (captured.origin !== origin) {
|
|
270
|
+
throw new CompareChromeRefusedError(`compare-chrome: the capture is of ${String(captured.origin)}, not ${origin}`);
|
|
271
|
+
}
|
|
272
|
+
if (JSON.stringify(captured.paths) !== JSON.stringify(o.paths)) {
|
|
273
|
+
throw new CompareChromeRefusedError(`compare-chrome: pass the capture's paths, in its order: ${JSON.stringify(captured.paths)}`);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
if (o.chromium === undefined) {
|
|
277
|
+
throw new CompareChromeRefusedError(`compare-chrome: Playwright's Chromium was not found in the site; install it: ${PLAYWRIGHT_INSTALL_HINT}`);
|
|
278
|
+
}
|
|
279
|
+
if (o.capture !== undefined) {
|
|
280
|
+
const captures = await captureAll(o.chromium, origin, o.paths, o.capture);
|
|
281
|
+
writeFileSync(join(o.capture, "capture.json"), `${JSON.stringify({ origin, paths: o.paths, at: (o.now ?? (() => new Date()))().toISOString() }, null, 2)}\n`);
|
|
282
|
+
let failed = 0;
|
|
283
|
+
for (const capture of captures) {
|
|
284
|
+
const ok = capture.loaded === true && capture.failures.length === 0;
|
|
285
|
+
if (!ok)
|
|
286
|
+
failed += 1;
|
|
287
|
+
line(`${ok ? "captured" : "NOT CAPTURED"} ${capture.path} @ ${capture.viewport} — ` +
|
|
288
|
+
(capture.loaded !== true
|
|
289
|
+
? capture.loaded
|
|
290
|
+
: [capture.parts.map((p) => p.part).join(", "), ...capture.failures].join("; ")));
|
|
291
|
+
}
|
|
292
|
+
return { exitCode: failed === 0 ? 0 : 1 };
|
|
293
|
+
}
|
|
294
|
+
const beforeDir = o.against;
|
|
295
|
+
const out = o.out ?? join(beforeDir, "comparison");
|
|
296
|
+
const afterDir = join(out, "after");
|
|
297
|
+
const shots = join(out, "shots");
|
|
298
|
+
mkdirSync(shots, { recursive: true });
|
|
299
|
+
const after = await captureAll(o.chromium, origin, o.paths, afterDir);
|
|
300
|
+
const tolerance = o.tolerance ?? 0;
|
|
301
|
+
const differences = [];
|
|
302
|
+
const stamps = [];
|
|
303
|
+
for (const now of after) {
|
|
304
|
+
const file = join(beforeDir, `${stem(now.path, now.viewport, "capture")}.json`);
|
|
305
|
+
let then;
|
|
306
|
+
try {
|
|
307
|
+
then = existsSync(file) ? JSON.parse(readFileSync(file, "utf8")) : undefined;
|
|
308
|
+
}
|
|
309
|
+
catch {
|
|
310
|
+
then = undefined;
|
|
311
|
+
}
|
|
312
|
+
const at = { path: now.path, viewport: now.viewport };
|
|
313
|
+
stamps.push({ ...at, before: then?.stamp ?? null, after: now.stamp });
|
|
314
|
+
if (then === undefined) {
|
|
315
|
+
differences.push({ ...at, part: "page", detail: "not in the capture, or unreadable" });
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
318
|
+
if (then.loaded !== true || now.loaded !== true) {
|
|
319
|
+
differences.push({
|
|
320
|
+
...at,
|
|
321
|
+
part: "page",
|
|
322
|
+
detail: `did not load: ${then.loaded !== true ? `before — ${then.loaded}` : `after — ${String(now.loaded)}`}`,
|
|
323
|
+
});
|
|
324
|
+
continue;
|
|
325
|
+
}
|
|
326
|
+
for (const [side, failures] of [["before", then.failures ?? []], ["after", now.failures]]) {
|
|
327
|
+
for (const failure of failures)
|
|
328
|
+
differences.push({ ...at, part: "capture", detail: `${side}: ${failure}` });
|
|
329
|
+
}
|
|
330
|
+
if (o.expect !== undefined && (now.stamp.source !== o.expect.source || now.stamp.version !== o.expect.version)) {
|
|
331
|
+
differences.push({
|
|
332
|
+
...at,
|
|
333
|
+
part: "stamp",
|
|
334
|
+
detail: `expected source ${o.expect.source} version ${o.expect.version}, rendered source ${now.stamp.source ?? "(missing)"} version ${now.stamp.version ?? "(missing)"}`,
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
const names = [...new Set([...then.parts.map((p) => p.part), ...now.parts.map((p) => p.part)])];
|
|
338
|
+
for (const part of names) {
|
|
339
|
+
const a = then.parts.find((p) => p.part === part);
|
|
340
|
+
const b = now.parts.find((p) => p.part === part);
|
|
341
|
+
if (a === undefined || b === undefined) {
|
|
342
|
+
differences.push({ ...at, part, detail: a === undefined ? "only after" : "only before" });
|
|
343
|
+
continue;
|
|
344
|
+
}
|
|
345
|
+
// Both screenshots are named as this run names them, never by a name the
|
|
346
|
+
// capture's JSON holds: `part` is one this run recorded, so it is our own.
|
|
347
|
+
const name = stem(now.path, now.viewport, part);
|
|
348
|
+
let beforePng;
|
|
349
|
+
try {
|
|
350
|
+
beforePng = readFileSync(join(beforeDir, `${name}.png`));
|
|
351
|
+
}
|
|
352
|
+
catch {
|
|
353
|
+
differences.push({ ...at, part, detail: "the capture's screenshot is missing" });
|
|
354
|
+
continue;
|
|
355
|
+
}
|
|
356
|
+
const afterPng = readFileSync(join(afterDir, `${name}.png`));
|
|
357
|
+
const detail = linkDifference(a.links, b.links) ?? pixelDifference(beforePng, afterPng, tolerance);
|
|
358
|
+
if (detail === null)
|
|
359
|
+
continue;
|
|
360
|
+
differences.push({ ...at, part, detail });
|
|
361
|
+
writeFileSync(join(shots, `${name}--before.png`), beforePng);
|
|
362
|
+
writeFileSync(join(shots, `${name}--after.png`), afterPng);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
const pass = differences.length === 0;
|
|
366
|
+
const report = { origin, against: beforeDir, tolerance, paths: o.paths, pass, differences, stamps };
|
|
367
|
+
writeFileSync(join(out, "comparison.json"), `${JSON.stringify(report, null, 2)}\n`);
|
|
368
|
+
writeFileSync(join(out, "comparison.md"), comparisonMarkdown(report));
|
|
369
|
+
for (const d of differences)
|
|
370
|
+
line(`DIFFERS ${d.path} @ ${d.viewport} ${d.part} — ${d.detail}`);
|
|
371
|
+
line(`${pass ? "PASS" : "FAIL"}: ${differences.length} difference(s); evidence in ${out}`);
|
|
372
|
+
return { exitCode: pass ? 0 : 1 };
|
|
373
|
+
}
|
|
374
|
+
function comparisonMarkdown(report) {
|
|
375
|
+
const stamp = (s) => (s === null ? "—" : `${s.source ?? "(missing)"} v${s.version ?? "(missing)"}`);
|
|
376
|
+
return [
|
|
377
|
+
`# Chrome comparison — ${report.origin}`,
|
|
378
|
+
"",
|
|
379
|
+
`**${report.pass ? "PASS" : "FAIL"}** — ${report.differences.length} difference(s) at tolerance ${report.tolerance}.`,
|
|
380
|
+
"",
|
|
381
|
+
"## Differences",
|
|
382
|
+
"",
|
|
383
|
+
...(report.differences.length === 0
|
|
384
|
+
? ["None."]
|
|
385
|
+
: report.differences.map((d) => `- \`${d.path}\` · ${d.viewport} · ${d.part} — ${d.detail}`)),
|
|
386
|
+
"",
|
|
387
|
+
"## Render stamp",
|
|
388
|
+
"",
|
|
389
|
+
"| Path | Viewport | Before | After |",
|
|
390
|
+
"| --- | --- | --- | --- |",
|
|
391
|
+
...report.stamps.map((s) => `| \`${s.path}\` | ${s.viewport} | ${stamp(s.before)} | ${stamp(s.after)} |`),
|
|
392
|
+
"",
|
|
393
|
+
].join("\n");
|
|
394
|
+
}
|
|
395
|
+
export const COMPARE_CHROME_HELP = `create-ailk compare-chrome — before-and-after captures of a site's header, open menus and footer.
|
|
396
|
+
|
|
397
|
+
Usage:
|
|
398
|
+
create-ailk compare-chrome [<dir>] --origin <url> --path <path> [--path <path> …] --capture <dir>
|
|
399
|
+
create-ailk compare-chrome [<dir>] --origin <url> --path <path> [--path <path> …] --against <dir> [--out <dir>] [--tolerance <share>] [--expect-source record --expect-version <n>]
|
|
400
|
+
|
|
401
|
+
Arguments:
|
|
402
|
+
<dir> The site, whose Playwright runs the browser. Defaults to the current dir.
|
|
403
|
+
|
|
404
|
+
Options:
|
|
405
|
+
--origin <url> The live site.
|
|
406
|
+
--path <path> A site path to capture; repeat for each.
|
|
407
|
+
--capture <dir> Record the header, each open desktop menu, the open phone menu and the footer.
|
|
408
|
+
--against <dir> Capture again and compare with that capture.
|
|
409
|
+
--out <dir> Where comparison.json, comparison.md, after/ and shots/ go. Default <against>/comparison.
|
|
410
|
+
--tolerance <share> The share of a screenshot's pixels that may differ. Default 0.
|
|
411
|
+
--expect-source <src> With --expect-version: every page must render its header from <src> (record or files).
|
|
412
|
+
--expect-version <n> The record version every page must render.
|
|
413
|
+
-h, --help Show this help.
|
|
414
|
+
|
|
415
|
+
Links (text, address, order) and screenshots are compared at 1440 and 390 wide.
|
|
416
|
+
Needs Playwright's Chromium in the site: ${PLAYWRIGHT_INSTALL_HINT}.
|
|
417
|
+
The runbook is docs/internal/runbooks/site-navigation-import.md.
|
|
418
|
+
Exit 0 when every part is the same, 1 when one is not or a page did not load,
|
|
419
|
+
2 on a refusal.
|
|
420
|
+
`;
|
|
421
|
+
export function parseCompareChromeArgs(argv) {
|
|
422
|
+
let parsed;
|
|
423
|
+
try {
|
|
424
|
+
parsed = parseArgs({
|
|
425
|
+
args: argv,
|
|
426
|
+
strict: true,
|
|
427
|
+
allowPositionals: true,
|
|
428
|
+
options: {
|
|
429
|
+
origin: { type: "string" },
|
|
430
|
+
path: { type: "string", multiple: true },
|
|
431
|
+
capture: { type: "string" },
|
|
432
|
+
against: { type: "string" },
|
|
433
|
+
out: { type: "string" },
|
|
434
|
+
tolerance: { type: "string" },
|
|
435
|
+
"expect-source": { type: "string" },
|
|
436
|
+
"expect-version": { type: "string" },
|
|
437
|
+
help: { type: "boolean", default: false, short: "h" },
|
|
438
|
+
},
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
catch (err) {
|
|
442
|
+
throw new ParseError(`compare-chrome: ${err.message}`);
|
|
443
|
+
}
|
|
444
|
+
const [target, ...rest] = parsed.positionals;
|
|
445
|
+
if (rest.length > 0) {
|
|
446
|
+
throw new ParseError(`compare-chrome takes at most one site directory, got ${parsed.positionals.length} arguments`);
|
|
447
|
+
}
|
|
448
|
+
const raw = parsed.values.tolerance;
|
|
449
|
+
const tolerance = raw === undefined ? undefined : Number(raw);
|
|
450
|
+
if (tolerance !== undefined && (raw.trim() === "" || Number.isNaN(tolerance) || tolerance < 0 || tolerance > 1)) {
|
|
451
|
+
throw new ParseError("compare-chrome: --tolerance must be a number from 0 to 1");
|
|
452
|
+
}
|
|
453
|
+
const source = parsed.values["expect-source"];
|
|
454
|
+
const version = parsed.values["expect-version"];
|
|
455
|
+
if ((source === undefined) !== (version === undefined)) {
|
|
456
|
+
throw new ParseError("compare-chrome: --expect-source and --expect-version go together");
|
|
457
|
+
}
|
|
458
|
+
if (source !== undefined && source !== "record" && source !== "files") {
|
|
459
|
+
throw new ParseError("compare-chrome: --expect-source is record or files");
|
|
460
|
+
}
|
|
461
|
+
if (version !== undefined && !/^[1-9][0-9]{0,9}$/.test(version)) {
|
|
462
|
+
throw new ParseError("compare-chrome: --expect-version must be a version number");
|
|
463
|
+
}
|
|
464
|
+
if (source !== undefined && parsed.values.capture !== undefined) {
|
|
465
|
+
throw new ParseError("compare-chrome: --expect-source and --expect-version go with --against");
|
|
466
|
+
}
|
|
467
|
+
const help = parsed.values.help === true;
|
|
468
|
+
if (!help && parsed.values.origin === undefined)
|
|
469
|
+
throw new ParseError("compare-chrome: --origin is required");
|
|
470
|
+
return {
|
|
471
|
+
target,
|
|
472
|
+
origin: parsed.values.origin,
|
|
473
|
+
paths: parsed.values.path ?? [],
|
|
474
|
+
capture: parsed.values.capture,
|
|
475
|
+
against: parsed.values.against,
|
|
476
|
+
out: parsed.values.out,
|
|
477
|
+
tolerance,
|
|
478
|
+
expect: source !== undefined && version !== undefined ? { source, version } : undefined,
|
|
479
|
+
help,
|
|
480
|
+
};
|
|
481
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/** A refusal before anything is rendered: exit 2. */
|
|
2
|
+
export declare class CompareLookRefusedError extends Error {
|
|
3
|
+
name: string;
|
|
4
|
+
}
|
|
5
|
+
export type ColorScheme = "light" | "dark";
|
|
6
|
+
/** The two colour schemes every page is captured in, light first. */
|
|
7
|
+
export declare const COLOR_SCHEMES: readonly ColorScheme[];
|
|
8
|
+
/** The slice of a Playwright `Page` the capture uses. */
|
|
9
|
+
export interface LookPage {
|
|
10
|
+
setViewportSize(size: {
|
|
11
|
+
width: number;
|
|
12
|
+
height: number;
|
|
13
|
+
}): Promise<void>;
|
|
14
|
+
goto(url: string, options: {
|
|
15
|
+
waitUntil: "networkidle";
|
|
16
|
+
timeout: number;
|
|
17
|
+
}): Promise<{
|
|
18
|
+
status(): number;
|
|
19
|
+
url(): string;
|
|
20
|
+
} | null>;
|
|
21
|
+
evaluate(expression: string): Promise<unknown>;
|
|
22
|
+
screenshot(options: {
|
|
23
|
+
fullPage: true;
|
|
24
|
+
animations: "disabled";
|
|
25
|
+
}): Promise<Buffer>;
|
|
26
|
+
}
|
|
27
|
+
/** The slice of Playwright's `chromium` the capture launches; each context takes its colour scheme. */
|
|
28
|
+
export interface LookChromium {
|
|
29
|
+
launch(): Promise<{
|
|
30
|
+
newContext(options: {
|
|
31
|
+
colorScheme: ColorScheme;
|
|
32
|
+
}): Promise<{
|
|
33
|
+
newPage(): Promise<LookPage>;
|
|
34
|
+
close(): Promise<void>;
|
|
35
|
+
}>;
|
|
36
|
+
close(): Promise<void>;
|
|
37
|
+
}>;
|
|
38
|
+
}
|
|
39
|
+
/** The design render stamp (D7); null where missing. */
|
|
40
|
+
export interface DesignStamp {
|
|
41
|
+
source: string | null;
|
|
42
|
+
version: string | null;
|
|
43
|
+
}
|
|
44
|
+
/** The page's design render stamp (D7): the `<head>` meta, or nulls. */
|
|
45
|
+
export declare const DESIGN_STAMP_EXPRESSION: string;
|
|
46
|
+
/** Resolves once every face the page uses is loaded, or after `FONTS_TIMEOUT_MS`. */
|
|
47
|
+
export declare const FONTS_READY_EXPRESSION = "Promise.race([document.fonts.ready, new Promise((r) => setTimeout(r, 15000))]).then(() => true)";
|
|
48
|
+
export interface LookDifference {
|
|
49
|
+
path: string;
|
|
50
|
+
viewport: string;
|
|
51
|
+
scheme: string;
|
|
52
|
+
/** What differs: the statuses, the pixel share, a size, an unreadable PNG, a side missing, the stamp. */
|
|
53
|
+
detail: string;
|
|
54
|
+
}
|
|
55
|
+
export interface CompareLookOptions {
|
|
56
|
+
origin: string;
|
|
57
|
+
paths: readonly string[];
|
|
58
|
+
/** `--capture <dir>`: record the before capture. */
|
|
59
|
+
capture?: string;
|
|
60
|
+
/** `--against <dir>`: capture again and compare with that one. */
|
|
61
|
+
against?: string;
|
|
62
|
+
/** Where the comparison's evidence goes; default `<against>/comparison`. */
|
|
63
|
+
out?: string;
|
|
64
|
+
tolerance?: number;
|
|
65
|
+
/** `--expect-source` (and, for `record`, `--expect-version`). */
|
|
66
|
+
expect?: {
|
|
67
|
+
source: "record" | "files";
|
|
68
|
+
version?: string;
|
|
69
|
+
};
|
|
70
|
+
/** Playwright's `chromium`; `undefined` refuses. */
|
|
71
|
+
chromium: LookChromium | undefined;
|
|
72
|
+
onLine?: (line: string) => void;
|
|
73
|
+
now?: () => Date;
|
|
74
|
+
}
|
|
75
|
+
/** Captures or compares (D6); answers the exit code: 0 all equal, 1 a difference or a page that did not load. */
|
|
76
|
+
export declare function compareLook(o: CompareLookOptions): Promise<{
|
|
77
|
+
exitCode: number;
|
|
78
|
+
}>;
|
|
79
|
+
export interface CompareLookArgs {
|
|
80
|
+
target: string | undefined;
|
|
81
|
+
origin: string | undefined;
|
|
82
|
+
paths: string[];
|
|
83
|
+
capture: string | undefined;
|
|
84
|
+
against: string | undefined;
|
|
85
|
+
out: string | undefined;
|
|
86
|
+
tolerance: number | undefined;
|
|
87
|
+
expect: {
|
|
88
|
+
source: "record" | "files";
|
|
89
|
+
version?: string;
|
|
90
|
+
} | undefined;
|
|
91
|
+
help: boolean;
|
|
92
|
+
}
|
|
93
|
+
export declare const COMPARE_LOOK_HELP = "create-ailk compare-look \u2014 before-and-after captures of whole pages, light and dark.\n\nUsage:\n create-ailk compare-look [<dir>] --origin <url> --path <path> [--path <path> \u2026] --capture <dir>\n create-ailk compare-look [<dir>] --origin <url> --path <path> [--path <path> \u2026] --against <dir> [--out <dir>] [--tolerance <share>] [--expect-source record --expect-version <n> | --expect-source files]\n\nArguments:\n <dir> A checkout whose Playwright runs the browser. Defaults to the current dir.\n\nOptions:\n --origin <url> The live site.\n --path <path> A site path to capture; repeat for each.\n --capture <dir> Record each whole page at 1440 and 390 wide, light and dark.\n --against <dir> Capture again and compare with that capture.\n --out <dir> Where comparison.json, comparison.md, after/ and shots/ go. Default <against>/comparison.\n --tolerance <share> The share of a screenshot's pixels that may differ. Default 0.\n --expect-source <src> Every page must render its look from <src>: record (with --expect-version) or files.\n --expect-version <n> The design record version every page must render.\n -h, --help Show this help.\n\nOnly a 200 page is captured; full-page screenshots are compared, after the page's fonts are ready.\nNeeds Playwright's Chromium: pnpm add -D @playwright/test && pnpm exec playwright install chromium.\nThe runbook is docs/internal/runbooks/site-design-import.md.\nExit 0 when every shot is the same, 1 when one is not or a page did not load,\n2 on a refusal.\n";
|
|
94
|
+
export declare function parseCompareLookArgs(argv: string[]): CompareLookArgs;
|