basalt-ui 2.14.0 → 2.15.0
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/CHANGELOG.md +40 -0
- package/MIGRATING.md +19 -13
- package/agent/agents/basalt-design-reviewer.md +83 -0
- package/agent/rules/basalt-batteries.md +1 -2
- package/agent/rules/basalt-charts.md +1 -1
- package/agent/rules/basalt-controls.md +3 -3
- package/agent/rules/basalt-mantine.md +1 -2
- package/agent/rules/basalt-state.md +1 -2
- package/agent/rules/basalt-tokens.md +1 -3
- package/agent/skills/basalt-design/SKILL.md +26 -8
- package/agent/templates/AGENTS-block.md.tpl +7 -0
- package/configs/oxlint-plugin.js +39 -2
- package/configs/oxlint-plugin.test.ts +48 -0
- package/dist/charts/legend.d.ts +4 -4
- package/dist/cli/board-freshness.d.ts +25 -0
- package/dist/cli/board-freshness.d.ts.map +1 -0
- package/dist/cli/board-freshness.js +88 -0
- package/dist/cli/doctor.d.ts +2 -1
- package/dist/cli/doctor.d.ts.map +1 -1
- package/dist/cli/doctor.js +3 -1
- package/dist/cli/help.d.ts.map +1 -1
- package/dist/cli/help.js +9 -3
- package/dist/cli/index.d.ts +15 -1
- package/dist/cli/index.d.ts.map +1 -1
- package/dist/cli/index.js +57 -7
- package/dist/cli/init.d.ts.map +1 -1
- package/dist/cli/init.js +6 -1
- package/dist/cli/shot/board.d.ts +5 -0
- package/dist/cli/shot/board.d.ts.map +1 -0
- package/dist/cli/shot/board.js +96 -0
- package/dist/cli/shot/capture.d.ts +59 -0
- package/dist/cli/shot/capture.d.ts.map +1 -0
- package/dist/cli/shot/capture.js +473 -0
- package/dist/cli/shot/index.d.ts +45 -0
- package/dist/cli/shot/index.d.ts.map +1 -0
- package/dist/cli/shot/index.js +289 -0
- package/dist/cli/shot/measure.d.ts +28 -0
- package/dist/cli/shot/measure.d.ts.map +1 -0
- package/dist/cli/shot/measure.js +176 -0
- package/dist/cli/shot/playwright.d.ts +101 -0
- package/dist/cli/shot/playwright.d.ts.map +1 -0
- package/dist/cli/shot/playwright.js +60 -0
- package/dist/cli/shot/serve.d.ts +6 -0
- package/dist/cli/shot/serve.d.ts.map +1 -0
- package/dist/cli/shot/serve.js +33 -0
- package/dist/cli/shot/types.d.ts +97 -0
- package/dist/cli/shot/types.d.ts.map +1 -0
- package/dist/cli/shot/types.js +17 -0
- package/dist/cli/sync.d.ts.map +1 -1
- package/dist/cli/sync.js +2 -1
- package/dist/surfaces.d.ts +2 -0
- package/dist/surfaces.d.ts.map +1 -1
- package/dist/surfaces.js +2 -0
- package/llms.txt +2 -2
- package/package.json +1 -1
- package/src/charts/legend.tsx +4 -4
- package/src/cli/board-freshness.ts +132 -0
- package/src/cli/doctor.ts +10 -1
- package/src/cli/help.ts +14 -2
- package/src/cli/index.ts +80 -10
- package/src/cli/init.ts +8 -1
- package/src/cli/shot/board.ts +154 -0
- package/src/cli/shot/capture.ts +650 -0
- package/src/cli/shot/index.ts +362 -0
- package/src/cli/shot/measure.ts +225 -0
- package/src/cli/shot/playwright.ts +152 -0
- package/src/cli/shot/serve.ts +35 -0
- package/src/cli/shot/types.ts +91 -0
- package/src/cli/sync.ts +2 -1
- package/src/surfaces.ts +5 -0
|
@@ -0,0 +1,473 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { basename, join } from "node:path";
|
|
4
|
+
import { buildSheet } from "./board.js";
|
|
5
|
+
import { finishMeasurement, measureAnchor } from "./measure.js";
|
|
6
|
+
import { PAGE_ANCHOR, SCHEMES, URL_STATES, VIEWPORTS, VIEWPORT_SPECS } from "./types.js";
|
|
7
|
+
const NAV_TIMEOUT_MS = 3e4;
|
|
8
|
+
const HOVER_TIMEOUT_MS = 5e3;
|
|
9
|
+
const TOOLTIP_TIMEOUT_MS = 3e3;
|
|
10
|
+
const TAB_PRESSES = 80;
|
|
11
|
+
const FOCUS_MARGIN_PX = 8;
|
|
12
|
+
const TIP_PROBE_MS = 250;
|
|
13
|
+
const TIP_SETTLE_MS = 60;
|
|
14
|
+
const TIP_SETTLE_READS = 10;
|
|
15
|
+
class NotDeclared extends Error {
|
|
16
|
+
}
|
|
17
|
+
const hash = (text) => createHash("sha1").update(text).digest("hex").slice(0, 6);
|
|
18
|
+
const slug = (text) => text.replace(/[^\w-]+/g, "-").replace(/^-+|-+$/g, "");
|
|
19
|
+
const firstLine = (error) => (error instanceof Error ? error.message : String(error)).split("\n")[0] ?? "";
|
|
20
|
+
const sel = (name) => `[data-shot=${JSON.stringify(name)}]`;
|
|
21
|
+
const anchorOf = (c, name) => c.page.locator(sel(name)).first();
|
|
22
|
+
const fileName = (route, name, scheme, viewport, state) => `${slug(route) || "root"}-${hash(route)}__${slug(name)}-${hash(name)}__${scheme}__${viewport}__${state}.png`;
|
|
23
|
+
function withParam(route, state) {
|
|
24
|
+
const hash2 = route.indexOf("#");
|
|
25
|
+
const [path, fragment] = hash2 < 0 ? [route, ""] : [route.slice(0, hash2), route.slice(hash2)];
|
|
26
|
+
return `${path}${path.includes("?") ? "&" : "?"}shot-state=${state}${fragment}`;
|
|
27
|
+
}
|
|
28
|
+
function unionClip(boxes, margin, viewport) {
|
|
29
|
+
const { width, height } = VIEWPORT_SPECS[viewport];
|
|
30
|
+
const left = Math.max(0, Math.min(...boxes.map((b) => b.x)) - margin);
|
|
31
|
+
const top = Math.max(0, Math.min(...boxes.map((b) => b.y)) - margin);
|
|
32
|
+
const right = Math.min(width, Math.max(...boxes.map((b) => b.x + b.width)) + margin);
|
|
33
|
+
const bottom = Math.min(height, Math.max(...boxes.map((b) => b.y + b.height)) + margin);
|
|
34
|
+
return { x: left, y: top, width: Math.max(1, right - left), height: Math.max(1, bottom - top) };
|
|
35
|
+
}
|
|
36
|
+
const snap = (target, path, extra = {}) => target.screenshot({ path, animations: "disabled", caret: "hide", ...extra });
|
|
37
|
+
function record(c, { name, state, status, file, note }) {
|
|
38
|
+
const { route, scheme, viewport } = c;
|
|
39
|
+
c.shots.push({
|
|
40
|
+
route,
|
|
41
|
+
name,
|
|
42
|
+
scheme,
|
|
43
|
+
viewport,
|
|
44
|
+
state,
|
|
45
|
+
file: file ?? null,
|
|
46
|
+
status,
|
|
47
|
+
...note ? { note } : {}
|
|
48
|
+
});
|
|
49
|
+
if (status === "error")
|
|
50
|
+
c.input.log(`[shot] ${route} ${name} ${scheme}/${viewport}/${state}: ${note}`);
|
|
51
|
+
}
|
|
52
|
+
async function load(page, url) {
|
|
53
|
+
await page.goto(url, { waitUntil: "networkidle" });
|
|
54
|
+
await page.evaluate("document.fonts.ready.then(() => undefined)");
|
|
55
|
+
}
|
|
56
|
+
const readFacts = (page) => page.evaluate(`(() => ({
|
|
57
|
+
anchors: [...new Set([...document.querySelectorAll('[data-shot]')].map((e) => e.getAttribute('data-shot')).filter(Boolean))],
|
|
58
|
+
declared: [...document.querySelectorAll('[data-shot-states]')].flatMap((e) => (e.getAttribute('data-shot-states') ?? '').split(/\\s+/)).filter(Boolean),
|
|
59
|
+
}))()`);
|
|
60
|
+
async function reveal(anchor) {
|
|
61
|
+
if (await anchor.count() === 0) throw new Error("anchor not found");
|
|
62
|
+
await anchor.scrollIntoViewIfNeeded();
|
|
63
|
+
if (!await anchor.isVisible()) throw new Error("anchor is not visible at this viewport");
|
|
64
|
+
}
|
|
65
|
+
async function attempt(c, name, state, run, note) {
|
|
66
|
+
const file = fileName(c.route, name, c.scheme, c.viewport, state);
|
|
67
|
+
try {
|
|
68
|
+
await run(join(c.input.pngDir, file));
|
|
69
|
+
} catch (error) {
|
|
70
|
+
const status = error instanceof NotDeclared ? "not-declared" : "error";
|
|
71
|
+
record(c, { name, state, status, note: firstLine(error) });
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
let notes = note ? [note] : [];
|
|
75
|
+
if (name !== PAGE_ANCHOR && !c.measured.has(name)) {
|
|
76
|
+
try {
|
|
77
|
+
const measure = c.input.measure ?? ((a) => a.evaluate(measureAnchor).then(finishMeasurement));
|
|
78
|
+
const m = await measure(anchorOf(c, name));
|
|
79
|
+
c.anchors.push({ route: c.route, name, scheme: c.scheme, viewport: c.viewport, ...m });
|
|
80
|
+
c.measured.add(name);
|
|
81
|
+
} catch (error) {
|
|
82
|
+
notes = [...notes, `metrics unavailable: ${firstLine(error)}`];
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
record(c, {
|
|
86
|
+
name,
|
|
87
|
+
state,
|
|
88
|
+
status: "ok",
|
|
89
|
+
file: `png/${file}`,
|
|
90
|
+
...notes.length ? { note: notes.join("; ") } : {}
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
const skipOnMobile = (c, name, state) => {
|
|
94
|
+
if (c.viewport === "desktop") return false;
|
|
95
|
+
record(c, { name, state, status: "skipped", note: "desktop only" });
|
|
96
|
+
return true;
|
|
97
|
+
};
|
|
98
|
+
const restCrop = async (c, name, path) => {
|
|
99
|
+
const anchor = anchorOf(c, name);
|
|
100
|
+
await reveal(anchor);
|
|
101
|
+
await snap(anchor, path);
|
|
102
|
+
};
|
|
103
|
+
async function captureRest(c, name) {
|
|
104
|
+
await attempt(c, name, "rest", (path) => restCrop(c, name, path));
|
|
105
|
+
}
|
|
106
|
+
function markVisibleTooltips() {
|
|
107
|
+
for (const el of Array.from(document.querySelectorAll("[data-shot-seen]")))
|
|
108
|
+
el.removeAttribute("data-shot-seen");
|
|
109
|
+
for (const el of Array.from(document.querySelectorAll('[role="tooltip"]'))) {
|
|
110
|
+
const rect = el.getBoundingClientRect();
|
|
111
|
+
if (rect.width > 0 && rect.height > 0 && getComputedStyle(el).visibility !== "hidden")
|
|
112
|
+
el.setAttribute("data-shot-seen", "");
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
function describedByOf(el) {
|
|
116
|
+
const named = el?.matches("[aria-describedby]") ? el : el?.querySelector("[aria-describedby]");
|
|
117
|
+
return named?.getAttribute("aria-describedby")?.split(/\s+/)[0] ?? "";
|
|
118
|
+
}
|
|
119
|
+
const tooltipSelector = (describedBy) => describedBy ? `[role="tooltip"][id=${JSON.stringify(describedBy)}]` : '[role="tooltip"]:not([data-shot-seen])';
|
|
120
|
+
async function openTooltipBox(c, describedBy, timeout = TIP_PROBE_MS) {
|
|
121
|
+
const tip = c.page.locator(tooltipSelector(describedBy)).first();
|
|
122
|
+
try {
|
|
123
|
+
await tip.waitFor({ state: "visible", timeout });
|
|
124
|
+
} catch {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
let box = await tip.boundingBox();
|
|
128
|
+
for (let i = 0; i < TIP_SETTLE_READS; i++) {
|
|
129
|
+
await new Promise((done) => setTimeout(done, TIP_SETTLE_MS));
|
|
130
|
+
const next = await tip.boundingBox();
|
|
131
|
+
const settled = JSON.stringify(next) === JSON.stringify(box);
|
|
132
|
+
box = next;
|
|
133
|
+
if (settled) break;
|
|
134
|
+
}
|
|
135
|
+
return box;
|
|
136
|
+
}
|
|
137
|
+
const idOf = (value) => typeof value === "string" ? value : "";
|
|
138
|
+
async function captureHover(c, name) {
|
|
139
|
+
if (skipOnMobile(c, name, "hover")) return;
|
|
140
|
+
await attempt(c, name, "hover", async (path) => {
|
|
141
|
+
const anchor = anchorOf(c, name);
|
|
142
|
+
await reveal(anchor);
|
|
143
|
+
const inner = c.page.locator(`${sel(name)} [data-shot-hover]`).first();
|
|
144
|
+
const target = await inner.count() > 0 ? inner : anchor;
|
|
145
|
+
await c.page.evaluate(markVisibleTooltips, void 0);
|
|
146
|
+
await target.hover({ timeout: HOVER_TIMEOUT_MS });
|
|
147
|
+
const describedBy = idOf(await target.evaluate(describedByOf));
|
|
148
|
+
const [box, tip] = [await anchor.boundingBox(), await openTooltipBox(c, describedBy)];
|
|
149
|
+
if (!box || !tip) return void await snap(anchor, path);
|
|
150
|
+
await snap(c.page, path, { clip: unionClip([box, tip], FOCUS_MARGIN_PX, c.viewport) });
|
|
151
|
+
});
|
|
152
|
+
await c.page.mouse.move(0, 0);
|
|
153
|
+
}
|
|
154
|
+
async function tabInto(page, name) {
|
|
155
|
+
await page.evaluate(
|
|
156
|
+
"(() => { document.activeElement?.blur(); window.scrollTo(0, 0); getSelection()?.collapse(document.body, 0) })()"
|
|
157
|
+
);
|
|
158
|
+
const landed = `(() => { const a = document.activeElement; return a != null && a.matches(':focus-visible') && a.closest(${JSON.stringify(sel(name))}) != null })()`;
|
|
159
|
+
for (let i = 0; i < TAB_PRESSES; i++) {
|
|
160
|
+
await page.keyboard.press("Tab");
|
|
161
|
+
if (await page.evaluate(landed)) return;
|
|
162
|
+
}
|
|
163
|
+
throw new Error(`:focus-visible never reached the anchor in ${TAB_PRESSES} Tab presses`);
|
|
164
|
+
}
|
|
165
|
+
function hasFocusTarget(root) {
|
|
166
|
+
const candidates = 'a[href], button, input:not([type="hidden"]), select, textarea, summary, [tabindex], [contenteditable=""], [contenteditable="true"]';
|
|
167
|
+
return [root, ...Array.from(root.querySelectorAll(candidates))].some(
|
|
168
|
+
(el) => el.matches(candidates) && !el.matches(":disabled") && !el.getAttribute("tabindex")?.startsWith("-") && el.closest("[hidden], [inert]") === null && (typeof el.checkVisibility === "function" ? el.checkVisibility() : el.getClientRects().length > 0) && getComputedStyle(el).visibility !== "hidden"
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
async function captureFocus(c, name) {
|
|
172
|
+
await attempt(c, name, "focus", async (path) => {
|
|
173
|
+
const anchor = anchorOf(c, name);
|
|
174
|
+
if (!await anchor.evaluate(hasFocusTarget))
|
|
175
|
+
throw new NotDeclared("no focus target in the anchor");
|
|
176
|
+
await reveal(anchor);
|
|
177
|
+
await c.page.evaluate(markVisibleTooltips, void 0);
|
|
178
|
+
await tabInto(c.page, name);
|
|
179
|
+
const box = await anchor.boundingBox();
|
|
180
|
+
if (!box) throw new Error("anchor has no box");
|
|
181
|
+
const focused = idOf(
|
|
182
|
+
await c.page.evaluate(
|
|
183
|
+
() => document.activeElement?.getAttribute("aria-describedby")?.split(/\s+/)[0] ?? "",
|
|
184
|
+
void 0
|
|
185
|
+
)
|
|
186
|
+
);
|
|
187
|
+
const tip = await openTooltipBox(c, focused);
|
|
188
|
+
await snap(c.page, path, {
|
|
189
|
+
clip: unionClip(tip ? [box, tip] : [box], FOCUS_MARGIN_PX, c.viewport)
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
await c.page.evaluate("document.activeElement?.blur()");
|
|
193
|
+
}
|
|
194
|
+
async function captureTooltip(c, name) {
|
|
195
|
+
if (skipOnMobile(c, name, "tooltip")) return;
|
|
196
|
+
const trigger = c.page.locator(`${sel(name)} [data-shot-tooltip]`).first();
|
|
197
|
+
await attempt(c, name, "tooltip", async (path) => {
|
|
198
|
+
if (await trigger.count() === 0) throw new NotDeclared("no [data-shot-tooltip] in the anchor");
|
|
199
|
+
const anchor = anchorOf(c, name);
|
|
200
|
+
await reveal(anchor);
|
|
201
|
+
await c.page.evaluate(markVisibleTooltips, void 0);
|
|
202
|
+
await trigger.hover({ timeout: HOVER_TIMEOUT_MS });
|
|
203
|
+
const describedBy = idOf(await trigger.evaluate(describedByOf));
|
|
204
|
+
const t = await openTooltipBox(c, describedBy, TOOLTIP_TIMEOUT_MS);
|
|
205
|
+
if (!t) throw new Error('no visible [role="tooltip"] after hover');
|
|
206
|
+
const a = await anchor.boundingBox();
|
|
207
|
+
if (!a) throw new Error("tooltip or anchor has no box");
|
|
208
|
+
await snap(c.page, path, { clip: unionClip([a, t], FOCUS_MARGIN_PX, c.viewport) });
|
|
209
|
+
});
|
|
210
|
+
await c.page.mouse.move(0, 0);
|
|
211
|
+
}
|
|
212
|
+
const POINTER_STATES = {
|
|
213
|
+
rest: captureRest,
|
|
214
|
+
hover: captureHover,
|
|
215
|
+
focus: captureFocus,
|
|
216
|
+
tooltip: captureTooltip
|
|
217
|
+
};
|
|
218
|
+
async function captureUrlState(c, state) {
|
|
219
|
+
const restNames = c.facts.anchors;
|
|
220
|
+
if (!c.facts.declared.includes(state)) {
|
|
221
|
+
for (const name of restNames) {
|
|
222
|
+
record(c, { name, state, status: "not-declared", note: "page does not declare this state" });
|
|
223
|
+
}
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
let here;
|
|
227
|
+
try {
|
|
228
|
+
await load(c.page, c.input.base + withParam(c.route, state));
|
|
229
|
+
here = await readFacts(c.page);
|
|
230
|
+
} catch (error) {
|
|
231
|
+
for (const name of restNames)
|
|
232
|
+
record(c, { name, state, status: "error", note: `load failed: ${firstLine(error)}` });
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
for (const name of /* @__PURE__ */ new Set([...restNames, ...here.anchors])) {
|
|
236
|
+
if (!here.anchors.includes(name)) {
|
|
237
|
+
record(c, { name, state, status: "error", note: "anchor absent in this state" });
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
const note = restNames.includes(name) ? void 0 : "anchor only present in this state";
|
|
241
|
+
await attempt(c, name, state, (path) => restCrop(c, name, path), note);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
const STEPS = ["xs", "sm", "md", "lg", "xl", "default"];
|
|
245
|
+
function readScaleInPage(steps) {
|
|
246
|
+
const probe = document.createElement("div");
|
|
247
|
+
probe.style.cssText = "position:absolute;visibility:hidden";
|
|
248
|
+
document.body.append(probe);
|
|
249
|
+
const root = getComputedStyle(document.documentElement);
|
|
250
|
+
const px = (width) => {
|
|
251
|
+
probe.style.width = "";
|
|
252
|
+
probe.style.width = width;
|
|
253
|
+
const value = parseFloat(getComputedStyle(probe).width);
|
|
254
|
+
return Number.isFinite(value) ? Math.round(value * 100) / 100 : void 0;
|
|
255
|
+
};
|
|
256
|
+
const read = (prefix) => {
|
|
257
|
+
const out = {};
|
|
258
|
+
for (const key of steps) {
|
|
259
|
+
const name = prefix + key;
|
|
260
|
+
const value = root.getPropertyValue(name).trim() ? px(`var(${name})`) : void 0;
|
|
261
|
+
if (value !== void 0) out[key] = value;
|
|
262
|
+
}
|
|
263
|
+
return out;
|
|
264
|
+
};
|
|
265
|
+
const declared = /* @__PURE__ */ new Map();
|
|
266
|
+
const walk = (rules) => {
|
|
267
|
+
for (const rule of Array.from(rules)) {
|
|
268
|
+
if ("cssRules" in rule) walk(rule.cssRules);
|
|
269
|
+
const style = rule.style;
|
|
270
|
+
if (!style) continue;
|
|
271
|
+
for (const name of Array.from(style)) {
|
|
272
|
+
if (/^--vx-(space|text|radius)-/.test(name))
|
|
273
|
+
declared.set(name, style.getPropertyValue(name).trim());
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
try {
|
|
278
|
+
for (const sheet of Array.from(document.styleSheets)) {
|
|
279
|
+
try {
|
|
280
|
+
walk(sheet.cssRules);
|
|
281
|
+
} catch {
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
const vx = {};
|
|
285
|
+
for (const [name, value] of declared) {
|
|
286
|
+
const resolved = root.getPropertyValue(name).trim() ? px(`var(${name})`) : px(value);
|
|
287
|
+
if (resolved !== void 0) vx[name] = resolved;
|
|
288
|
+
}
|
|
289
|
+
return {
|
|
290
|
+
mantine: {
|
|
291
|
+
spacing: read("--mantine-spacing-"),
|
|
292
|
+
radius: read("--mantine-radius-"),
|
|
293
|
+
fontSizes: read("--mantine-font-size-")
|
|
294
|
+
},
|
|
295
|
+
vx
|
|
296
|
+
};
|
|
297
|
+
} finally {
|
|
298
|
+
probe.remove();
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
function groupScale({ mantine, vx }) {
|
|
302
|
+
const pick = (kind) => Object.fromEntries(
|
|
303
|
+
Object.entries(vx).filter(([name, value]) => name.startsWith(`--vx-${kind}-`) && value > 0).map(([name, value]) => [name.slice(2), value])
|
|
304
|
+
);
|
|
305
|
+
return {
|
|
306
|
+
spacing: { ...mantine.spacing, ...pick("space") },
|
|
307
|
+
radius: { ...mantine.radius, ...pick("radius") },
|
|
308
|
+
fontSizes: { ...mantine.fontSizes, ...pick("text") }
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
async function capturePage(args) {
|
|
312
|
+
const { wantScale, ...rest } = args;
|
|
313
|
+
const c = {
|
|
314
|
+
...rest,
|
|
315
|
+
facts: { anchors: [], declared: [] },
|
|
316
|
+
anchors: [],
|
|
317
|
+
shots: [],
|
|
318
|
+
measured: /* @__PURE__ */ new Set()
|
|
319
|
+
};
|
|
320
|
+
const { input, route } = c;
|
|
321
|
+
try {
|
|
322
|
+
await load(c.page, input.base + route);
|
|
323
|
+
c.facts = await readFacts(c.page);
|
|
324
|
+
} catch (error) {
|
|
325
|
+
record(c, {
|
|
326
|
+
name: PAGE_ANCHOR,
|
|
327
|
+
state: "rest",
|
|
328
|
+
status: "error",
|
|
329
|
+
note: `load failed: ${firstLine(error)}`
|
|
330
|
+
});
|
|
331
|
+
return c;
|
|
332
|
+
}
|
|
333
|
+
if (wantScale) {
|
|
334
|
+
try {
|
|
335
|
+
c.scale = groupScale(await c.page.evaluate(readScaleInPage, STEPS));
|
|
336
|
+
} catch (error) {
|
|
337
|
+
c.scaleError = firstLine(error);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
const usable = c.facts.anchors.filter((n) => slug(n) !== "" && n !== PAGE_ANCHOR);
|
|
341
|
+
for (const name of c.facts.anchors.filter((n) => !usable.includes(n))) {
|
|
342
|
+
record(c, { name, state: "rest", status: "error", note: "unusable data-shot name" });
|
|
343
|
+
}
|
|
344
|
+
c.facts.anchors = usable;
|
|
345
|
+
if (usable.length === 0 && c.scheme === "light" && c.viewport === "desktop") {
|
|
346
|
+
input.log(`[shot] no data-shot anchors on ${route}`);
|
|
347
|
+
}
|
|
348
|
+
if (input.states.includes("rest")) {
|
|
349
|
+
await attempt(
|
|
350
|
+
c,
|
|
351
|
+
PAGE_ANCHOR,
|
|
352
|
+
"rest",
|
|
353
|
+
(path) => snap(c.page, path, { fullPage: true }).then(() => void 0)
|
|
354
|
+
);
|
|
355
|
+
}
|
|
356
|
+
for (const name of usable) {
|
|
357
|
+
for (const [state, capture2] of Object.entries(POINTER_STATES)) {
|
|
358
|
+
if (input.states.includes(state)) await capture2(c, name);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
for (const state of URL_STATES) if (input.states.includes(state)) await captureUrlState(c, state);
|
|
362
|
+
return c;
|
|
363
|
+
}
|
|
364
|
+
async function openContext(browser, scheme, viewport) {
|
|
365
|
+
const spec = VIEWPORT_SPECS[viewport];
|
|
366
|
+
const context = await browser.newContext({
|
|
367
|
+
viewport: { width: spec.width, height: spec.height },
|
|
368
|
+
deviceScaleFactor: 1,
|
|
369
|
+
isMobile: spec.mobile,
|
|
370
|
+
hasTouch: spec.mobile,
|
|
371
|
+
colorScheme: scheme,
|
|
372
|
+
reducedMotion: "reduce"
|
|
373
|
+
});
|
|
374
|
+
await context.addInitScript(`localStorage.setItem('mantine-color-scheme-value', '${scheme}')`);
|
|
375
|
+
return context;
|
|
376
|
+
}
|
|
377
|
+
const SHEET_WIDTH_PX = 2e3;
|
|
378
|
+
const sheetName = (route, name) => `${slug(route) || "root"}-${hash(route)}__${slug(name)}-${hash(name)}__sheet.png`;
|
|
379
|
+
const dataUri = (path) => `data:image/png;base64,${readFileSync(path).toString("base64")}`;
|
|
380
|
+
async function writeSheets(input, out) {
|
|
381
|
+
const targets = /* @__PURE__ */ new Map();
|
|
382
|
+
for (const s of out.shots) {
|
|
383
|
+
if (s.name !== PAGE_ANCHOR)
|
|
384
|
+
targets.set(`${s.route}\0${s.name}`, { route: s.route, name: s.name });
|
|
385
|
+
}
|
|
386
|
+
if (targets.size === 0) return;
|
|
387
|
+
const context = await input.browser.newContext({
|
|
388
|
+
viewport: { width: SHEET_WIDTH_PX, height: 1e3 },
|
|
389
|
+
deviceScaleFactor: 1,
|
|
390
|
+
isMobile: false,
|
|
391
|
+
hasTouch: false,
|
|
392
|
+
colorScheme: "light",
|
|
393
|
+
reducedMotion: "reduce"
|
|
394
|
+
});
|
|
395
|
+
try {
|
|
396
|
+
const page = await context.newPage();
|
|
397
|
+
for (const { route, name } of targets.values()) {
|
|
398
|
+
try {
|
|
399
|
+
const shots = out.shots.filter((s) => s.route === route && s.name === name).map(
|
|
400
|
+
(s) => s.status === "ok" && s.file ? { ...s, file: dataUri(join(input.pngDir, basename(s.file))) } : s
|
|
401
|
+
);
|
|
402
|
+
await page.setContent(buildSheet(route, name, input.states, shots), { waitUntil: "load" });
|
|
403
|
+
const file = sheetName(route, name);
|
|
404
|
+
await snap(page, join(input.pngDir, file), { fullPage: true });
|
|
405
|
+
for (const a of out.anchors) {
|
|
406
|
+
if (a.route === route && a.name === name) a.sheet = `png/${file}`;
|
|
407
|
+
}
|
|
408
|
+
} catch (error) {
|
|
409
|
+
input.log(`[shot] ${route} ${name}: sheet not written: ${firstLine(error)}`);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
} finally {
|
|
413
|
+
await context.close().catch(() => void 0);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
async function capture(input) {
|
|
417
|
+
const out = {
|
|
418
|
+
anchors: [],
|
|
419
|
+
shots: [],
|
|
420
|
+
scale: [],
|
|
421
|
+
scaleError: void 0
|
|
422
|
+
};
|
|
423
|
+
for (const scheme of SCHEMES) {
|
|
424
|
+
for (const viewport of VIEWPORTS) {
|
|
425
|
+
const context = await openContext(input.browser, scheme, viewport);
|
|
426
|
+
try {
|
|
427
|
+
const page = await context.newPage();
|
|
428
|
+
page.setDefaultTimeout(NAV_TIMEOUT_MS);
|
|
429
|
+
let scaled = false;
|
|
430
|
+
for (const route of input.routes) {
|
|
431
|
+
const done = await capturePage({
|
|
432
|
+
page,
|
|
433
|
+
input,
|
|
434
|
+
route,
|
|
435
|
+
scheme,
|
|
436
|
+
viewport,
|
|
437
|
+
wantScale: !scaled
|
|
438
|
+
});
|
|
439
|
+
if (done.scale) {
|
|
440
|
+
out.scale.push({ scheme, viewport, ...done.scale });
|
|
441
|
+
scaled = true;
|
|
442
|
+
}
|
|
443
|
+
if (done.scaleError) out.scaleError = out.scaleError ?? done.scaleError;
|
|
444
|
+
out.anchors.push(...done.anchors);
|
|
445
|
+
out.shots.push(...done.shots);
|
|
446
|
+
}
|
|
447
|
+
} finally {
|
|
448
|
+
await context.close().catch(() => void 0);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
await writeSheets(input, out).catch(
|
|
453
|
+
(error) => input.log(`[shot] contact sheets not written: ${firstLine(error)}`)
|
|
454
|
+
);
|
|
455
|
+
const { anchors, shots, scale } = out;
|
|
456
|
+
if (scale.length > 0 || out.scaleError === void 0) return { anchors, shots, scale };
|
|
457
|
+
input.log(`[shot] scale unavailable: ${out.scaleError}`);
|
|
458
|
+
return { anchors, shots, scale, scaleError: out.scaleError };
|
|
459
|
+
}
|
|
460
|
+
export {
|
|
461
|
+
capture,
|
|
462
|
+
describedByOf,
|
|
463
|
+
fileName,
|
|
464
|
+
groupScale,
|
|
465
|
+
hasFocusTarget,
|
|
466
|
+
markVisibleTooltips,
|
|
467
|
+
openContext,
|
|
468
|
+
readScaleInPage,
|
|
469
|
+
sheetName,
|
|
470
|
+
tooltipSelector,
|
|
471
|
+
unionClip,
|
|
472
|
+
withParam
|
|
473
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { capture } from './capture.js';
|
|
2
|
+
import type { PlaywrightResolver } from './playwright.js';
|
|
3
|
+
import type { ShotState } from './types.js';
|
|
4
|
+
export type ShotDeps = {
|
|
5
|
+
resolve?: PlaywrightResolver;
|
|
6
|
+
/** Replaces the browser run (tests): returns what `capture` would. */
|
|
7
|
+
capture?: typeof capture;
|
|
8
|
+
now?: () => Date;
|
|
9
|
+
log?: (line: string) => void;
|
|
10
|
+
};
|
|
11
|
+
export type ShotArgs = {
|
|
12
|
+
routes: string[];
|
|
13
|
+
base?: string;
|
|
14
|
+
dist?: string;
|
|
15
|
+
out: string;
|
|
16
|
+
states: ShotState[];
|
|
17
|
+
};
|
|
18
|
+
/** Parses argv; throws `ShotError` with the one-line reason. */
|
|
19
|
+
export declare function parseShotArgs(flags: readonly string[]): ShotArgs;
|
|
20
|
+
/**
|
|
21
|
+
* Validates `out` WITHOUT touching it and returns its absolute path. A shot only ever empties what
|
|
22
|
+
* it wrote: the dir must be absent, empty, or hold a previous run's `metrics.json` (`version: 1`).
|
|
23
|
+
*/
|
|
24
|
+
export declare function planOut(cwd: string, out: string): string;
|
|
25
|
+
/** `--base` as recorded: origin and path only, never credentials, query or fragment. */
|
|
26
|
+
export declare function sanitizeBase(base: string): string;
|
|
27
|
+
/**
|
|
28
|
+
* Removes `<home>/tmp-<pid>-*` render dirs a killed run left behind: older than an hour AND owned
|
|
29
|
+
* by a pid that is gone (a name without a pid, from an older run, goes by age alone). Never follows
|
|
30
|
+
* a symlink, and an entry that vanishes, dangles or is a plain file is skipped, never fatal.
|
|
31
|
+
*/
|
|
32
|
+
export declare function sweepStaleTmp(home: string, now: Date, alive?: (pid: number) => boolean): void;
|
|
33
|
+
/** A swap killed between its two renames leaves `<out>.prev` and no `<out>`: put it back. */
|
|
34
|
+
export declare function recoverOut(dir: string): void;
|
|
35
|
+
/**
|
|
36
|
+
* Swaps the finished `tmp` render in for `out` (re-validated): old aside, new in, aside removed —
|
|
37
|
+
* a failure between the renames restores the old render. `last.json` is written last.
|
|
38
|
+
*/
|
|
39
|
+
export declare function commitOut(cwd: string, out: string, tmp: string, routes: readonly string[], now: Date): void;
|
|
40
|
+
export declare function shot(flags: readonly string[], cwd: string, deps?: ShotDeps): Promise<number>;
|
|
41
|
+
export { openContext } from './capture.js';
|
|
42
|
+
export { INSTALL_LINE, resolvePlaywright, withBrowser } from './playwright.js';
|
|
43
|
+
export { serveDist } from './serve.js';
|
|
44
|
+
export { VIEWPORT_SPECS } from './types.js';
|
|
45
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/cli/shot/index.ts"],"names":[],"mappings":"AAoBA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AAEnC,OAAO,KAAK,EAAc,kBAAkB,EAAE,MAAM,cAAc,CAAA;AAGlE,OAAO,KAAK,EAAe,SAAS,EAAE,MAAM,SAAS,CAAA;AAQrD,MAAM,MAAM,QAAQ,GAAG;IACrB,OAAO,CAAC,EAAE,kBAAkB,CAAA;IAC5B,sEAAsE;IACtE,OAAO,CAAC,EAAE,OAAO,OAAO,CAAA;IACxB,GAAG,CAAC,EAAE,MAAM,IAAI,CAAA;IAChB,GAAG,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAA;CAC7B,CAAA;AAED,MAAM,MAAM,QAAQ,GAAG;IACrB,MAAM,EAAE,MAAM,EAAE,CAAA;IAChB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,GAAG,EAAE,MAAM,CAAA;IACX,MAAM,EAAE,SAAS,EAAE,CAAA;CACpB,CAAA;AAED,gEAAgE;AAChE,wBAAgB,aAAa,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,QAAQ,CAuChE;AAmCD;;;GAGG;AACH,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAgCxD;AAED,wFAAwF;AACxF,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAOjD;AAcD;;;;GAIG;AACH,wBAAgB,aAAa,CAC3B,IAAI,EAAE,MAAM,EACZ,GAAG,EAAE,IAAI,EACT,KAAK,GAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAkB,GACzC,IAAI,CAoBN;AAKD,6FAA6F;AAC7F,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAE5C;AAED;;;GAGG;AACH,wBAAgB,SAAS,CACvB,GAAG,EAAE,MAAM,EACX,GAAG,EAAE,MAAM,EACX,GAAG,EAAE,MAAM,EACX,MAAM,EAAE,SAAS,MAAM,EAAE,EACzB,GAAG,EAAE,IAAI,GACR,IAAI,CA0BN;AAkBD,wBAAsB,IAAI,CACxB,KAAK,EAAE,SAAS,MAAM,EAAE,EACxB,GAAG,EAAE,MAAM,EACX,IAAI,GAAE,QAAa,GAClB,OAAO,CAAC,MAAM,CAAC,CA2EjB;AAED,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AACvC,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,cAAc,CAAA;AAC3E,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AACnC,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA"}
|