basalt-ui 2.13.3 → 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 +66 -0
- package/MIGRATING.md +42 -12
- 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/agent/chat/thread-feed-row.d.ts.map +1 -1
- package/dist/agent/chat/thread-feed-row.js +2 -2
- package/dist/agent/chat/thread-message.d.ts.map +1 -1
- package/dist/agent/chat/thread-message.js +2 -2
- package/dist/agent/chat/thread-outcome-card.js +2 -2
- package/dist/charts/DualPanel.d.ts.map +1 -1
- package/dist/charts/DualPanel.js +1 -0
- package/dist/charts/compile.d.ts.map +1 -1
- package/dist/charts/compile.js +3 -2
- package/dist/charts/cursor.d.ts +10 -6
- package/dist/charts/cursor.d.ts.map +1 -1
- package/dist/charts/cursor.js +20 -13
- package/dist/charts/legend.d.ts +7 -6
- package/dist/charts/legend.d.ts.map +1 -1
- package/dist/charts/legend.js +3 -3
- package/dist/charts/panel.d.ts +2 -0
- package/dist/charts/panel.d.ts.map +1 -1
- package/dist/charts/panel.js +1 -0
- 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/commands/notifications/center.d.ts.map +1 -1
- package/dist/commands/notifications/center.js +2 -11
- package/dist/controls/control-name.d.ts +27 -0
- package/dist/controls/control-name.d.ts.map +1 -0
- package/dist/controls/control-name.js +11 -0
- package/dist/controls/enum-filter.d.ts +3 -6
- package/dist/controls/enum-filter.d.ts.map +1 -1
- package/dist/controls/enum-filter.js +8 -4
- package/dist/controls/filter-base.d.ts +30 -0
- package/dist/controls/filter-base.d.ts.map +1 -0
- package/dist/controls/filter-base.js +0 -0
- package/dist/controls/filter-pill.d.ts +3 -1
- package/dist/controls/filter-pill.d.ts.map +1 -1
- package/dist/controls/filter-pill.js +2 -0
- package/dist/controls/filter-sheet.d.ts +0 -32
- package/dist/controls/filter-sheet.d.ts.map +1 -1
- package/dist/controls/filter-sheet.js +1 -28
- package/dist/controls/glyphs.d.ts +12 -0
- package/dist/controls/glyphs.d.ts.map +1 -1
- package/dist/controls/glyphs.js +23 -2
- package/dist/controls/index.d.ts +2 -1
- package/dist/controls/index.d.ts.map +1 -1
- package/dist/controls/multi-select-filter.d.ts +8 -3
- package/dist/controls/multi-select-filter.d.ts.map +1 -1
- package/dist/controls/multi-select-filter.js +27 -10
- package/dist/controls/number-filter.d.ts +3 -12
- package/dist/controls/number-filter.d.ts.map +1 -1
- package/dist/controls/number-filter.js +17 -6
- package/dist/controls/panel-row.d.ts +5 -8
- package/dist/controls/panel-row.d.ts.map +1 -1
- package/dist/controls/panel-row.js +3 -0
- package/dist/controls/range-filter.d.ts +14 -14
- package/dist/controls/range-filter.d.ts.map +1 -1
- package/dist/controls/range-filter.js +18 -41
- package/dist/controls/search-filter.d.ts +5 -2
- package/dist/controls/search-filter.d.ts.map +1 -1
- package/dist/controls/search-filter.js +74 -40
- package/dist/controls/select-filter.d.ts +2 -15
- package/dist/controls/select-filter.d.ts.map +1 -1
- package/dist/controls/sync-button.d.ts +0 -6
- package/dist/controls/sync-button.d.ts.map +1 -1
- package/dist/controls/sync-button.js +6 -36
- package/dist/controls/toggle-filter.d.ts +2 -2
- package/dist/controls/toggle-filter.d.ts.map +1 -1
- package/dist/controls/toggle-filter.js +3 -2
- package/dist/controls/view-tabs.d.ts +3 -4
- package/dist/controls/view-tabs.d.ts.map +1 -1
- package/dist/controls/view-tabs.js +24 -5
- package/dist/format/date.d.ts +43 -10
- package/dist/format/date.d.ts.map +1 -1
- package/dist/format/date.js +109 -18
- package/dist/format/index.d.ts +12 -11
- package/dist/format/index.d.ts.map +1 -1
- package/dist/format/index.js +11 -2
- package/dist/format/measure.d.ts +9 -9
- package/dist/format/measure.d.ts.map +1 -1
- package/dist/format/measure.js +19 -8
- package/dist/format/number.d.ts +35 -27
- package/dist/format/number.d.ts.map +1 -1
- package/dist/format/number.js +41 -20
- package/dist/format/shared.d.ts +38 -7
- package/dist/format/shared.d.ts.map +1 -1
- package/dist/format/shared.js +61 -13
- package/dist/provider/index.d.ts +13 -1
- package/dist/provider/index.d.ts.map +1 -1
- package/dist/provider/index.js +8 -0
- package/dist/surfaces.d.ts +2 -0
- package/dist/surfaces.d.ts.map +1 -1
- package/dist/surfaces.js +2 -0
- package/llms.txt +35 -14
- package/package.json +1 -1
- package/src/agent/chat/thread-feed-row.tsx +2 -2
- package/src/agent/chat/thread-message.tsx +2 -2
- package/src/agent/chat/thread-outcome-card.tsx +2 -2
- package/src/charts/DualPanel.tsx +1 -0
- package/src/charts/compile.ts +3 -2
- package/src/charts/cursor.ts +44 -27
- package/src/charts/legend.tsx +11 -8
- package/src/charts/panel.tsx +3 -0
- 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/commands/notifications/center.tsx +2 -14
- package/src/controls/control-name.ts +33 -0
- package/src/controls/enum-filter.tsx +29 -27
- package/src/controls/filter-base.ts +31 -0
- package/src/controls/filter-pill.tsx +4 -0
- package/src/controls/filter-sheet.tsx +0 -56
- package/src/controls/glyphs.tsx +45 -2
- package/src/controls/index.ts +2 -1
- package/src/controls/multi-select-filter.tsx +67 -43
- package/src/controls/number-filter.tsx +47 -47
- package/src/controls/panel-row.tsx +7 -8
- package/src/controls/range-filter.tsx +54 -78
- package/src/controls/search-filter.tsx +107 -54
- package/src/controls/select-filter.tsx +2 -18
- package/src/controls/sync-button.tsx +7 -40
- package/src/controls/toggle-filter.tsx +9 -7
- package/src/controls/view-tabs.tsx +41 -21
- package/src/format/date.ts +189 -36
- package/src/format/index.ts +12 -11
- package/src/format/measure.ts +29 -18
- package/src/format/number.ts +76 -37
- package/src/format/shared.ts +116 -25
- package/src/provider/index.tsx +23 -0
- package/src/surfaces.ts +5 -0
- package/dist/agent/chat/relative-time.d.ts +0 -27
- package/dist/agent/chat/relative-time.d.ts.map +0 -1
- package/dist/agent/chat/relative-time.js +0 -27
- package/src/agent/chat/relative-time.ts +0 -55
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import {
|
|
2
|
+
lstatSync,
|
|
3
|
+
mkdirSync,
|
|
4
|
+
mkdtempSync,
|
|
5
|
+
readFileSync,
|
|
6
|
+
readdirSync,
|
|
7
|
+
realpathSync,
|
|
8
|
+
renameSync,
|
|
9
|
+
rmSync,
|
|
10
|
+
statSync,
|
|
11
|
+
writeFileSync
|
|
12
|
+
} from "node:fs";
|
|
13
|
+
import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
14
|
+
import { parseArgs } from "node:util";
|
|
15
|
+
import { buildBoard } from "./board.js";
|
|
16
|
+
import { capture } from "./capture.js";
|
|
17
|
+
import { INSTALL_LINE, ShotError, resolvePlaywright, withBrowser } from "./playwright.js";
|
|
18
|
+
import { serveDist } from "./serve.js";
|
|
19
|
+
import { SCHEMES, STATES, VIEWPORTS } from "./types.js";
|
|
20
|
+
const HOME = ".basalt/shots";
|
|
21
|
+
const DEFAULT_OUT = `${HOME}/latest`;
|
|
22
|
+
const USAGE_LINE = "usage: basalt-ui shot <route\u2026> (--base <url> | --dist <dir>) [--out <dir>] [--states <list>]";
|
|
23
|
+
function parseShotArgs(flags) {
|
|
24
|
+
let parsed;
|
|
25
|
+
try {
|
|
26
|
+
parsed = parseArgs({
|
|
27
|
+
args: [...flags],
|
|
28
|
+
allowPositionals: true,
|
|
29
|
+
options: {
|
|
30
|
+
base: { type: "string" },
|
|
31
|
+
dist: { type: "string" },
|
|
32
|
+
out: { type: "string" },
|
|
33
|
+
states: { type: "string" }
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
} catch (error) {
|
|
37
|
+
throw new ShotError(`${error instanceof Error ? error.message : String(error)}
|
|
38
|
+
${USAGE_LINE}`);
|
|
39
|
+
}
|
|
40
|
+
const { values, positionals } = parsed;
|
|
41
|
+
if (values.base) sanitizeBase(values.base);
|
|
42
|
+
if (positionals.length === 0) throw new ShotError(`no route given.
|
|
43
|
+
${USAGE_LINE}`);
|
|
44
|
+
const bad = positionals.find((r) => !r.startsWith("/"));
|
|
45
|
+
if (bad) throw new ShotError(`route '${bad}' must start with '/'.
|
|
46
|
+
${USAGE_LINE}`);
|
|
47
|
+
if (Boolean(values.base) === Boolean(values.dist)) {
|
|
48
|
+
throw new ShotError(`pass exactly one of --base <url> or --dist <dir>.
|
|
49
|
+
${USAGE_LINE}`);
|
|
50
|
+
}
|
|
51
|
+
const states = values.states ? values.states.split(",").map((s) => s.trim()).filter(Boolean) : [...STATES];
|
|
52
|
+
const unknown = states.find((s) => !STATES.includes(s));
|
|
53
|
+
if (unknown) throw new ShotError(`unknown state '${unknown}'. Known: ${STATES.join(", ")}`);
|
|
54
|
+
return {
|
|
55
|
+
routes: positionals,
|
|
56
|
+
...values.base ? { base: values.base.replace(/\/+$/, "") } : {},
|
|
57
|
+
...values.dist ? { dist: values.dist } : {},
|
|
58
|
+
out: values.out ?? DEFAULT_OUT,
|
|
59
|
+
states: STATES.filter((s) => states.includes(s))
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
const PROTECTED_SEGMENTS = /* @__PURE__ */ new Set([".git", "node_modules"]);
|
|
63
|
+
const SHOT_OWNED = /* @__PURE__ */ new Set(["png", "board.html", "metrics.json", ".gitignore"]);
|
|
64
|
+
const outside = (rel) => rel === ".." || rel.startsWith(".." + sep) || isAbsolute(rel);
|
|
65
|
+
const lexists = (path) => {
|
|
66
|
+
try {
|
|
67
|
+
lstatSync(path);
|
|
68
|
+
return true;
|
|
69
|
+
} catch {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
const isPreviousShot = (dir) => {
|
|
74
|
+
try {
|
|
75
|
+
const marker = JSON.parse(readFileSync(join(dir, "metrics.json"), "utf8"));
|
|
76
|
+
const png = join(dir, "png");
|
|
77
|
+
return marker.version === 1 && readdirSync(dir).every((e) => SHOT_OWNED.has(e)) && (!lexists(png) || lstatSync(png).isDirectory() && !lstatSync(png).isSymbolicLink());
|
|
78
|
+
} catch {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
function planOut(cwd, out) {
|
|
83
|
+
const dir = resolve(cwd, out);
|
|
84
|
+
const rel = relative(cwd, dir);
|
|
85
|
+
const refuse = (why) => {
|
|
86
|
+
throw new ShotError(`--out ${out} ${why}`);
|
|
87
|
+
};
|
|
88
|
+
if (rel === "" || outside(rel)) refuse("must be a directory inside the project");
|
|
89
|
+
if (relative(join(cwd, HOME), dir) === "")
|
|
90
|
+
refuse("is the shot home (holds last.json); use a subdirectory");
|
|
91
|
+
if (rel === ".basalt" || rel.split(sep).some((part) => PROTECTED_SEGMENTS.has(part))) {
|
|
92
|
+
refuse("is (or sits inside) a protected directory (.git, node_modules, .basalt)");
|
|
93
|
+
}
|
|
94
|
+
let probe = dir;
|
|
95
|
+
while (!lexists(probe)) probe = dirname(probe);
|
|
96
|
+
if (probe === dir && lstatSync(dir).isSymbolicLink())
|
|
97
|
+
refuse("is a symlink; refusing to follow it");
|
|
98
|
+
let real = "";
|
|
99
|
+
try {
|
|
100
|
+
real = realpathSync(probe);
|
|
101
|
+
} catch {
|
|
102
|
+
refuse("sits behind a dangling symlink");
|
|
103
|
+
}
|
|
104
|
+
if (outside(relative(realpathSync(cwd), real))) refuse("resolves outside the project");
|
|
105
|
+
if (probe === dir) {
|
|
106
|
+
if (!statSync(dir).isDirectory()) refuse("exists and is not a directory");
|
|
107
|
+
if (readdirSync(dir).length > 0 && !isPreviousShot(dir)) {
|
|
108
|
+
refuse(
|
|
109
|
+
"exists and is not purely a previous shot output (metrics.json version 1 plus png/, board.html, .gitignore); refusing to empty it"
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return dir;
|
|
114
|
+
}
|
|
115
|
+
function sanitizeBase(base) {
|
|
116
|
+
try {
|
|
117
|
+
const url = new URL(base);
|
|
118
|
+
return `${url.origin}${url.pathname}`.replace(/\/+$/, "");
|
|
119
|
+
} catch {
|
|
120
|
+
throw new ShotError(`--base ${base} is not a URL`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
const STALE_TMP_MS = 60 * 60 * 1e3;
|
|
124
|
+
function pidAlive(pid) {
|
|
125
|
+
try {
|
|
126
|
+
process.kill(pid, 0);
|
|
127
|
+
return true;
|
|
128
|
+
} catch (error) {
|
|
129
|
+
return error.code === "EPERM";
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
function sweepStaleTmp(home, now, alive = pidAlive) {
|
|
133
|
+
let entries;
|
|
134
|
+
try {
|
|
135
|
+
entries = readdirSync(home);
|
|
136
|
+
} catch {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
for (const entry of entries) {
|
|
140
|
+
if (!entry.startsWith("tmp-")) continue;
|
|
141
|
+
const path = join(home, entry);
|
|
142
|
+
try {
|
|
143
|
+
const stat = lstatSync(path);
|
|
144
|
+
if (!stat.isDirectory() || now.getTime() - stat.mtimeMs <= STALE_TMP_MS) continue;
|
|
145
|
+
const pid = /^tmp-(\d+)-/.exec(entry)?.[1];
|
|
146
|
+
if (pid !== void 0 && alive(Number(pid))) continue;
|
|
147
|
+
rmSync(path, { recursive: true, force: true });
|
|
148
|
+
} catch {
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
const asideOf = (dir) => `${dir}.prev`;
|
|
153
|
+
function recoverOut(dir) {
|
|
154
|
+
if (lexists(asideOf(dir)) && !lexists(dir)) renameSync(asideOf(dir), dir);
|
|
155
|
+
}
|
|
156
|
+
function commitOut(cwd, out, tmp, routes, now) {
|
|
157
|
+
const dir = planOut(cwd, out);
|
|
158
|
+
const aside = asideOf(dir);
|
|
159
|
+
if (lexists(aside)) {
|
|
160
|
+
if (!isPreviousShot(aside))
|
|
161
|
+
throw new ShotError(`${relative(cwd, aside)} exists and is not a previous shot output`);
|
|
162
|
+
rmSync(aside, { recursive: true, force: true });
|
|
163
|
+
}
|
|
164
|
+
mkdirSync(dirname(dir), { recursive: true });
|
|
165
|
+
const hadOld = lexists(dir);
|
|
166
|
+
if (hadOld) renameSync(dir, aside);
|
|
167
|
+
try {
|
|
168
|
+
renameSync(tmp, dir);
|
|
169
|
+
} catch (error) {
|
|
170
|
+
if (hadOld) renameSync(aside, dir);
|
|
171
|
+
throw error;
|
|
172
|
+
}
|
|
173
|
+
if (hadOld) rmSync(aside, { recursive: true, force: true });
|
|
174
|
+
const home = join(cwd, HOME);
|
|
175
|
+
mkdirSync(home, { recursive: true });
|
|
176
|
+
writeFileSync(join(home, ".gitignore"), "*\n");
|
|
177
|
+
const rel = relative(cwd, dir).split(sep).join("/");
|
|
178
|
+
writeFileSync(
|
|
179
|
+
join(home, "last.json"),
|
|
180
|
+
JSON.stringify({ version: 1, at: now.toISOString(), out: rel, routes }, null, 2) + "\n"
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
function exitOnSignal(stops) {
|
|
184
|
+
const exitWith = (code) => () => {
|
|
185
|
+
void Promise.allSettled(stops.map((stop) => stop())).finally(() => process.exit(code));
|
|
186
|
+
};
|
|
187
|
+
const [onInt, onTerm] = [exitWith(130), exitWith(143)];
|
|
188
|
+
const events = process;
|
|
189
|
+
events.once("SIGINT", onInt);
|
|
190
|
+
events.once("SIGTERM", onTerm);
|
|
191
|
+
return () => {
|
|
192
|
+
events.removeListener("SIGINT", onInt);
|
|
193
|
+
events.removeListener("SIGTERM", onTerm);
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
async function shot(flags, cwd, deps = {}) {
|
|
197
|
+
const log = deps.log ?? ((line) => console.log(line));
|
|
198
|
+
const stops = [];
|
|
199
|
+
const uninstall = exitOnSignal(stops);
|
|
200
|
+
try {
|
|
201
|
+
const args = parseShotArgs(flags);
|
|
202
|
+
const outDir = planOut(cwd, args.out);
|
|
203
|
+
recoverOut(outDir);
|
|
204
|
+
const pw = (deps.resolve ?? resolvePlaywright)(cwd);
|
|
205
|
+
if (!pw) throw new ShotError(INSTALL_LINE);
|
|
206
|
+
const now = (deps.now ?? (() => /* @__PURE__ */ new Date()))();
|
|
207
|
+
sweepStaleTmp(join(cwd, HOME), now);
|
|
208
|
+
const server = args.dist ? serveDist(resolve(cwd, args.dist)) : null;
|
|
209
|
+
if (server) stops.push(server.stop);
|
|
210
|
+
let tmp = "";
|
|
211
|
+
stops.push(() => tmp && rmSync(tmp, { recursive: true, force: true }));
|
|
212
|
+
const base = server?.origin ?? args.base ?? "";
|
|
213
|
+
try {
|
|
214
|
+
const { anchors, shots, scale, scaleError } = await withBrowser(
|
|
215
|
+
pw,
|
|
216
|
+
(browser) => {
|
|
217
|
+
const home = join(cwd, HOME);
|
|
218
|
+
mkdirSync(home, { recursive: true });
|
|
219
|
+
writeFileSync(join(home, ".gitignore"), "*\n");
|
|
220
|
+
tmp = mkdtempSync(join(home, `tmp-${process.pid}-`));
|
|
221
|
+
mkdirSync(join(tmp, "png"));
|
|
222
|
+
writeFileSync(join(tmp, ".gitignore"), "*\n");
|
|
223
|
+
return (deps.capture ?? capture)({
|
|
224
|
+
browser,
|
|
225
|
+
base,
|
|
226
|
+
routes: args.routes,
|
|
227
|
+
states: args.states,
|
|
228
|
+
pngDir: join(tmp, "png"),
|
|
229
|
+
log
|
|
230
|
+
});
|
|
231
|
+
},
|
|
232
|
+
{ stops }
|
|
233
|
+
);
|
|
234
|
+
const metrics = {
|
|
235
|
+
version: 1,
|
|
236
|
+
generatedAt: now.toISOString(),
|
|
237
|
+
base: sanitizeBase(base),
|
|
238
|
+
routes: args.routes,
|
|
239
|
+
viewports: [...VIEWPORTS],
|
|
240
|
+
schemes: [...SCHEMES],
|
|
241
|
+
states: args.states,
|
|
242
|
+
scale,
|
|
243
|
+
...scaleError ? { scaleError } : {},
|
|
244
|
+
anchors,
|
|
245
|
+
shots
|
|
246
|
+
};
|
|
247
|
+
writeFileSync(join(tmp, "metrics.json"), JSON.stringify(metrics, null, 2) + "\n");
|
|
248
|
+
writeFileSync(join(tmp, "board.html"), buildBoard(metrics));
|
|
249
|
+
commitOut(cwd, args.out, tmp, args.routes, now);
|
|
250
|
+
const count = (status) => shots.filter((s) => s.status === status).length;
|
|
251
|
+
log(
|
|
252
|
+
`[shot] ${count("ok")} shots, ${count("not-declared")} not declared, ${count("skipped")} skipped, ${count("error")} failed \u2192 ${relative(cwd, outDir)}/board.html`
|
|
253
|
+
);
|
|
254
|
+
if (count("error") > 0) {
|
|
255
|
+
console.error(`basalt-ui shot: ${count("error")} shot(s) failed; see board.html`);
|
|
256
|
+
return 1;
|
|
257
|
+
}
|
|
258
|
+
return count("ok") === 0 ? 1 : 0;
|
|
259
|
+
} finally {
|
|
260
|
+
if (tmp) rmSync(tmp, { recursive: true, force: true });
|
|
261
|
+
server?.stop();
|
|
262
|
+
}
|
|
263
|
+
} catch (error) {
|
|
264
|
+
if (!(error instanceof ShotError)) throw error;
|
|
265
|
+
console.error(error.message);
|
|
266
|
+
return 1;
|
|
267
|
+
} finally {
|
|
268
|
+
uninstall();
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
import { openContext } from "./capture.js";
|
|
272
|
+
import { INSTALL_LINE as INSTALL_LINE2, resolvePlaywright as resolvePlaywright2, withBrowser as withBrowser2 } from "./playwright.js";
|
|
273
|
+
import { serveDist as serveDist2 } from "./serve.js";
|
|
274
|
+
import { VIEWPORT_SPECS } from "./types.js";
|
|
275
|
+
export {
|
|
276
|
+
INSTALL_LINE2 as INSTALL_LINE,
|
|
277
|
+
VIEWPORT_SPECS,
|
|
278
|
+
commitOut,
|
|
279
|
+
openContext,
|
|
280
|
+
parseShotArgs,
|
|
281
|
+
planOut,
|
|
282
|
+
recoverOut,
|
|
283
|
+
resolvePlaywright2 as resolvePlaywright,
|
|
284
|
+
sanitizeBase,
|
|
285
|
+
serveDist2 as serveDist,
|
|
286
|
+
shot,
|
|
287
|
+
sweepStaleTmp,
|
|
288
|
+
withBrowser2 as withBrowser
|
|
289
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `measureAnchor` runs in the page (Playwright sends its source: it must close over nothing) and
|
|
3
|
+
* returns raw readings; the contrast maths runs here in Node, where it is unit-testable.
|
|
4
|
+
*/
|
|
5
|
+
import type { Measurement } from './types.js';
|
|
6
|
+
/** WCAG contrast ratio of two sRGB colours (channels 0–255). */
|
|
7
|
+
export declare function contrastRatio(a: readonly number[], b: readonly number[]): number;
|
|
8
|
+
/** 3 for large text (≥ 24px, or ≥ 18.66px bold), else 4.5. */
|
|
9
|
+
export declare function contrastThreshold(fontSize: number, bold: boolean): number;
|
|
10
|
+
export type TextReading = {
|
|
11
|
+
text: string;
|
|
12
|
+
fg: number[];
|
|
13
|
+
bg: number[];
|
|
14
|
+
fontSize: number;
|
|
15
|
+
bold: boolean;
|
|
16
|
+
/** No opaque ancestor background was found: `bg` is white by assumption. */
|
|
17
|
+
assumedWhite: boolean;
|
|
18
|
+
};
|
|
19
|
+
export type Reading = Omit<Measurement, 'contrast'> & {
|
|
20
|
+
texts: TextReading[];
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Contrast per text reading: `min` over all, `failures` below the WCAG AA threshold. The threshold
|
|
24
|
+
* is tested on the UNROUNDED ratio; only the stored value is rounded.
|
|
25
|
+
*/
|
|
26
|
+
export declare function finishMeasurement({ texts, ...rest }: Reading): Measurement;
|
|
27
|
+
export declare function measureAnchor(root: Element): Reading;
|
|
28
|
+
//# sourceMappingURL=measure.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"measure.d.ts","sourceRoot":"","sources":["../../../src/cli/shot/measure.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,OAAO,KAAK,EAAmB,WAAW,EAAE,MAAM,SAAS,CAAA;AAS3D,gEAAgE;AAChE,wBAAgB,aAAa,CAAC,CAAC,EAAE,SAAS,MAAM,EAAE,EAAE,CAAC,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAGhF;AAED,8DAA8D;AAC9D,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,MAAM,CAEzE;AAED,MAAM,MAAM,WAAW,GAAG;IACxB,IAAI,EAAE,MAAM,CAAA;IACZ,EAAE,EAAE,MAAM,EAAE,CAAA;IACZ,EAAE,EAAE,MAAM,EAAE,CAAA;IACZ,QAAQ,EAAE,MAAM,CAAA;IAChB,IAAI,EAAE,OAAO,CAAA;IACb,4EAA4E;IAC5E,YAAY,EAAE,OAAO,CAAA;CACtB,CAAA;AACD,MAAM,MAAM,OAAO,GAAG,IAAI,CAAC,WAAW,EAAE,UAAU,CAAC,GAAG;IAAE,KAAK,EAAE,WAAW,EAAE,CAAA;CAAE,CAAA;AAS9E;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,EAAE,KAAK,EAAE,GAAG,IAAI,EAAE,EAAE,OAAO,GAAG,WAAW,CAa1E;AAGD,wBAAgB,aAAa,CAAC,IAAI,EAAE,OAAO,GAAG,OAAO,CAgKpD"}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
const linear = (v) => {
|
|
2
|
+
const s = (v ?? 0) / 255;
|
|
3
|
+
return s <= 0.03928 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4;
|
|
4
|
+
};
|
|
5
|
+
const luminance = (c) => 0.2126 * linear(c[0]) + 0.7152 * linear(c[1]) + 0.0722 * linear(c[2]);
|
|
6
|
+
function contrastRatio(a, b) {
|
|
7
|
+
const [hi, lo] = [luminance(a), luminance(b)].toSorted((x, y) => y - x);
|
|
8
|
+
return ((hi ?? 0) + 0.05) / ((lo ?? 0) + 0.05);
|
|
9
|
+
}
|
|
10
|
+
function contrastThreshold(fontSize, bold) {
|
|
11
|
+
return fontSize >= 24 || bold && fontSize >= 18.66 ? 3 : 4.5;
|
|
12
|
+
}
|
|
13
|
+
const hex = (c) => "#" + c.slice(0, 3).map((n) => Math.round(n).toString(16).padStart(2, "0")).join("");
|
|
14
|
+
function finishMeasurement({ texts, ...rest }) {
|
|
15
|
+
const failures = [];
|
|
16
|
+
let min = null;
|
|
17
|
+
for (const t of texts) {
|
|
18
|
+
const exact = contrastRatio(t.fg, t.bg);
|
|
19
|
+
const ratio = Math.round(exact * 100) / 100;
|
|
20
|
+
min = min === null ? ratio : Math.min(min, ratio);
|
|
21
|
+
if (exact < contrastThreshold(t.fontSize, t.bold)) {
|
|
22
|
+
failures.push({ text: t.text, ratio, fg: hex(t.fg), bg: hex(t.bg), fontSize: t.fontSize });
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const backdrop = texts.some((t) => t.assumedWhite) ? { backdrop: "assumed-white" } : {};
|
|
26
|
+
return { ...rest, contrast: { min, failures, ...backdrop } };
|
|
27
|
+
}
|
|
28
|
+
function measureAnchor(root) {
|
|
29
|
+
const px = (v) => Math.round(parseFloat(String(v)) * 100) / 100;
|
|
30
|
+
const sides = (s, p) => ({
|
|
31
|
+
top: px(s[`${p}Top`]),
|
|
32
|
+
right: px(s[`${p}Right`]),
|
|
33
|
+
bottom: px(s[`${p}Bottom`]),
|
|
34
|
+
left: px(s[`${p}Left`])
|
|
35
|
+
});
|
|
36
|
+
const ctx = document.createElement("canvas").getContext("2d", { willReadFrequently: true });
|
|
37
|
+
if (!ctx) throw new Error("no 2d canvas context: cannot read colours");
|
|
38
|
+
const parsed = /* @__PURE__ */ new Map();
|
|
39
|
+
const paint = (start, css) => {
|
|
40
|
+
ctx.clearRect(0, 0, 1, 1);
|
|
41
|
+
ctx.fillStyle = start;
|
|
42
|
+
ctx.fillStyle = css;
|
|
43
|
+
ctx.fillRect(0, 0, 1, 1);
|
|
44
|
+
return Array.from(ctx.getImageData(0, 0, 1, 1).data);
|
|
45
|
+
};
|
|
46
|
+
const rgba = (css) => {
|
|
47
|
+
const known = parsed.get(css);
|
|
48
|
+
if (known) return known;
|
|
49
|
+
const onBlack = paint("#000000", css);
|
|
50
|
+
const onWhite = paint("#ffffff", css);
|
|
51
|
+
if (onBlack.some((v, i) => v !== onWhite[i])) throw new Error(`unparseable colour: ${css}`);
|
|
52
|
+
const out = [onBlack[0] ?? 0, onBlack[1] ?? 0, onBlack[2] ?? 0, (onBlack[3] ?? 0) / 255];
|
|
53
|
+
parsed.set(css, out);
|
|
54
|
+
return out;
|
|
55
|
+
};
|
|
56
|
+
const over = (top2, under) => {
|
|
57
|
+
const a = top2[3] ?? 1;
|
|
58
|
+
return [0, 1, 2, 3].map((i) => i === 3 ? 1 : (top2[i] ?? 0) * a + (under[i] ?? 0) * (1 - a));
|
|
59
|
+
};
|
|
60
|
+
const visible = (el) => {
|
|
61
|
+
const s = getComputedStyle(el);
|
|
62
|
+
const r = el.getBoundingClientRect();
|
|
63
|
+
return s.visibility === "hidden" || s.display === "none" || s.opacity === "0" || r.width === 0 ? null : s;
|
|
64
|
+
};
|
|
65
|
+
const backdrop = (el) => {
|
|
66
|
+
const layers = [];
|
|
67
|
+
let opaque = false;
|
|
68
|
+
for (let n = el; n && !opaque; n = n.parentElement) {
|
|
69
|
+
const c = rgba(getComputedStyle(n).backgroundColor);
|
|
70
|
+
if ((c[3] ?? 0) > 0) layers.push(c);
|
|
71
|
+
opaque = (c[3] ?? 0) >= 0.99;
|
|
72
|
+
}
|
|
73
|
+
const color = layers.toReversed().reduce((under, c) => over(c, under), [255, 255, 255, 1]);
|
|
74
|
+
return { color, assumedWhite: !opaque };
|
|
75
|
+
};
|
|
76
|
+
const cs = getComputedStyle(root);
|
|
77
|
+
const rect = root.getBoundingClientRect();
|
|
78
|
+
const widest = Math.max(
|
|
79
|
+
...["Top", "Right", "Bottom", "Left"].map(
|
|
80
|
+
(e) => px(cs.getPropertyValue(`border-${e.toLowerCase()}-width`))
|
|
81
|
+
)
|
|
82
|
+
);
|
|
83
|
+
let gap = { row: 0, column: 0 };
|
|
84
|
+
const queue = [root];
|
|
85
|
+
for (let i = 0; i < queue.length && i < 80; i++) {
|
|
86
|
+
const item = queue[i];
|
|
87
|
+
if (!item) break;
|
|
88
|
+
const s = getComputedStyle(item);
|
|
89
|
+
const row = px(s.rowGap) || 0;
|
|
90
|
+
const column = px(s.columnGap) || 0;
|
|
91
|
+
if ((s.display.includes("flex") || s.display.includes("grid")) && (row > 0 || column > 0)) {
|
|
92
|
+
gap = { row, column };
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
queue.push(...Array.from(item.children));
|
|
96
|
+
}
|
|
97
|
+
const fontSizes = /* @__PURE__ */ new Set();
|
|
98
|
+
const texts = [];
|
|
99
|
+
const painted = [];
|
|
100
|
+
const nested = [];
|
|
101
|
+
const pathOf = (el) => {
|
|
102
|
+
const parts = [];
|
|
103
|
+
for (let n = el; n && n !== root; n = n.parentElement) {
|
|
104
|
+
const cls = n.classList.item(0);
|
|
105
|
+
parts.unshift(n.tagName.toLowerCase() + (cls ? `.${cls}` : ""));
|
|
106
|
+
}
|
|
107
|
+
return parts.join(" > ");
|
|
108
|
+
};
|
|
109
|
+
const range = document.createRange();
|
|
110
|
+
for (const el of [root, ...Array.from(root.querySelectorAll("*"))]) {
|
|
111
|
+
const s = visible(el);
|
|
112
|
+
if (!s) continue;
|
|
113
|
+
const r = el.getBoundingClientRect();
|
|
114
|
+
const hasBg = (rgba(s.backgroundColor)[3] ?? 0) > 0;
|
|
115
|
+
const hasBorder = ["Top", "Right", "Bottom", "Left"].some(
|
|
116
|
+
(e) => px(s.getPropertyValue(`border-${e.toLowerCase()}-width`)) > 0
|
|
117
|
+
);
|
|
118
|
+
if (el !== root && (hasBg || hasBorder) && nested.length < 60) {
|
|
119
|
+
nested.push({
|
|
120
|
+
path: pathOf(el),
|
|
121
|
+
radius: px(s.borderTopLeftRadius),
|
|
122
|
+
padding: sides(s, "padding")
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
if (el !== root && (/^(svg|img|canvas|video|input|textarea)$/i.test(el.tagName) || (hasBg || hasBorder) && r.height < rect.height * 0.9)) {
|
|
126
|
+
painted.push([r.top, r.bottom]);
|
|
127
|
+
}
|
|
128
|
+
const text = Array.from(el.childNodes).filter(
|
|
129
|
+
(n) => n.nodeType === Node.TEXT_NODE && n.textContent?.trim()
|
|
130
|
+
);
|
|
131
|
+
if (text.length === 0) continue;
|
|
132
|
+
const size = px(s.fontSize);
|
|
133
|
+
fontSizes.add(size);
|
|
134
|
+
for (const node of text) {
|
|
135
|
+
range.selectNodeContents(node);
|
|
136
|
+
const tr = range.getBoundingClientRect();
|
|
137
|
+
painted.push([tr.top, tr.bottom]);
|
|
138
|
+
}
|
|
139
|
+
const back = backdrop(el);
|
|
140
|
+
texts.push({
|
|
141
|
+
text: (text[0]?.textContent ?? "").trim().slice(0, 40),
|
|
142
|
+
fg: over(rgba(s.color), back.color),
|
|
143
|
+
bg: back.color,
|
|
144
|
+
assumedWhite: back.assumedWhite,
|
|
145
|
+
fontSize: size,
|
|
146
|
+
bold: parseInt(s.fontWeight, 10) >= 700
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
const top = rect.top;
|
|
150
|
+
const bottom = rect.bottom;
|
|
151
|
+
let cursor = top;
|
|
152
|
+
let longest = 0;
|
|
153
|
+
for (const [a, b] of painted.toSorted((x, y) => x[0] - y[0])) {
|
|
154
|
+
longest = Math.max(longest, Math.min(a, bottom) - cursor);
|
|
155
|
+
cursor = Math.max(cursor, Math.min(b, bottom));
|
|
156
|
+
}
|
|
157
|
+
longest = Math.max(longest, bottom - cursor);
|
|
158
|
+
const emptyRatio = rect.height > 0 ? Math.round(Math.max(longest, 0) / rect.height * 1e3) / 1e3 : 1;
|
|
159
|
+
return {
|
|
160
|
+
box: { width: px(rect.width), height: px(rect.height) },
|
|
161
|
+
padding: sides(cs, "padding"),
|
|
162
|
+
gap,
|
|
163
|
+
radius: px(cs.borderTopLeftRadius),
|
|
164
|
+
borderWidth: widest,
|
|
165
|
+
fontSizes: [...fontSizes].toSorted((a, b) => a - b),
|
|
166
|
+
nested,
|
|
167
|
+
emptyRatio,
|
|
168
|
+
texts
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
export {
|
|
172
|
+
contrastRatio,
|
|
173
|
+
contrastThreshold,
|
|
174
|
+
finishMeasurement,
|
|
175
|
+
measureAnchor
|
|
176
|
+
};
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type { Scheme } from './types.js';
|
|
2
|
+
export declare const INSTALL_LINE = "basalt-ui shot needs Playwright: bun add -d playwright && bunx playwright install chromium";
|
|
3
|
+
export type Box = {
|
|
4
|
+
x: number;
|
|
5
|
+
y: number;
|
|
6
|
+
width: number;
|
|
7
|
+
height: number;
|
|
8
|
+
};
|
|
9
|
+
export type ShotOptions = {
|
|
10
|
+
path: string;
|
|
11
|
+
animations: 'disabled';
|
|
12
|
+
caret: 'hide';
|
|
13
|
+
fullPage?: boolean;
|
|
14
|
+
clip?: Box;
|
|
15
|
+
};
|
|
16
|
+
export type PwLocator = {
|
|
17
|
+
first(): PwLocator;
|
|
18
|
+
count(): Promise<number>;
|
|
19
|
+
hover(options?: {
|
|
20
|
+
timeout?: number;
|
|
21
|
+
}): Promise<void>;
|
|
22
|
+
isVisible(): Promise<boolean>;
|
|
23
|
+
boundingBox(): Promise<Box | null>;
|
|
24
|
+
scrollIntoViewIfNeeded(): Promise<void>;
|
|
25
|
+
waitFor(options: {
|
|
26
|
+
state: 'visible';
|
|
27
|
+
timeout: number;
|
|
28
|
+
}): Promise<void>;
|
|
29
|
+
screenshot(options: ShotOptions): Promise<unknown>;
|
|
30
|
+
evaluate<R>(fn: (el: Element) => R): Promise<R>;
|
|
31
|
+
};
|
|
32
|
+
export type PwPage = {
|
|
33
|
+
goto(url: string, options: {
|
|
34
|
+
waitUntil: 'load' | 'networkidle';
|
|
35
|
+
}): Promise<unknown>;
|
|
36
|
+
setDefaultTimeout(ms: number): void;
|
|
37
|
+
locator(selector: string): PwLocator;
|
|
38
|
+
evaluate<R>(expression: string): Promise<R>;
|
|
39
|
+
evaluate<R, A>(fn: (arg: A) => R, arg: A): Promise<R>;
|
|
40
|
+
setContent(html: string, options: {
|
|
41
|
+
waitUntil: 'load';
|
|
42
|
+
}): Promise<void>;
|
|
43
|
+
keyboard: {
|
|
44
|
+
press(key: string): Promise<void>;
|
|
45
|
+
};
|
|
46
|
+
mouse: {
|
|
47
|
+
move(x: number, y: number): Promise<void>;
|
|
48
|
+
};
|
|
49
|
+
screenshot(options: ShotOptions): Promise<unknown>;
|
|
50
|
+
};
|
|
51
|
+
export type PwContext = {
|
|
52
|
+
newPage(): Promise<PwPage>;
|
|
53
|
+
addInitScript(script: string): Promise<unknown>;
|
|
54
|
+
close(): Promise<void>;
|
|
55
|
+
};
|
|
56
|
+
export type ContextOptions = {
|
|
57
|
+
viewport: {
|
|
58
|
+
width: number;
|
|
59
|
+
height: number;
|
|
60
|
+
};
|
|
61
|
+
deviceScaleFactor: number;
|
|
62
|
+
isMobile: boolean;
|
|
63
|
+
hasTouch: boolean;
|
|
64
|
+
colorScheme: Scheme;
|
|
65
|
+
reducedMotion: 'reduce';
|
|
66
|
+
};
|
|
67
|
+
export type PwBrowser<C extends PwContext = PwContext> = {
|
|
68
|
+
newContext(options: ContextOptions): Promise<C>;
|
|
69
|
+
};
|
|
70
|
+
type PwServer = {
|
|
71
|
+
wsEndpoint(): string;
|
|
72
|
+
kill(): Promise<void>;
|
|
73
|
+
};
|
|
74
|
+
export type Playwright<B extends PwBrowser = PwBrowser> = {
|
|
75
|
+
chromium: {
|
|
76
|
+
launchServer(options: {
|
|
77
|
+
channel?: string;
|
|
78
|
+
chromiumSandbox: boolean;
|
|
79
|
+
}): Promise<PwServer>;
|
|
80
|
+
connect(wsEndpoint: string): Promise<B>;
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
/** Thrown for failures that print one line and exit 1 (no stack). */
|
|
84
|
+
export declare class ShotError extends Error {
|
|
85
|
+
}
|
|
86
|
+
export type PlaywrightResolver = (cwd: string) => Playwright | null;
|
|
87
|
+
export declare const isPlaywright: (mod: unknown) => mod is Playwright;
|
|
88
|
+
export declare const resolvePlaywright: PlaywrightResolver;
|
|
89
|
+
export type LaunchOptions = {
|
|
90
|
+
/** Tried in order; `undefined` is the bundled chromium. Default: bundled, then `chrome`. */
|
|
91
|
+
channels?: readonly (string | undefined)[];
|
|
92
|
+
/** Receives this run's kill, so the caller's signal handler can stop it. */
|
|
93
|
+
stops?: (() => unknown)[];
|
|
94
|
+
};
|
|
95
|
+
/**
|
|
96
|
+
* One Chrome for `fn`, killed afterwards. `launchServer` + `connect` + `kill()` — a graceful
|
|
97
|
+
* `close()` hangs under Bun. Every failed launch keeps its first error line for the final message.
|
|
98
|
+
*/
|
|
99
|
+
export declare function withBrowser<B extends PwBrowser, T>(pw: Playwright<B>, fn: (browser: B, channel: string) => Promise<T>, { channels, stops }?: LaunchOptions): Promise<T>;
|
|
100
|
+
export {};
|
|
101
|
+
//# sourceMappingURL=playwright.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"playwright.d.ts","sourceRoot":"","sources":["../../../src/cli/shot/playwright.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AAErC,eAAO,MAAM,YAAY,+FACqE,CAAA;AAI9F,MAAM,MAAM,GAAG,GAAG;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAA;AAEzE,MAAM,MAAM,WAAW,GAAG;IACxB,IAAI,EAAE,MAAM,CAAA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@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
const INSTALL_LINE = "basalt-ui shot needs Playwright: bun add -d playwright && bunx playwright install chromium";
|
|
4
|
+
const KILL_TIMEOUT_MS = 1e4;
|
|
5
|
+
class ShotError extends Error {
|
|
6
|
+
}
|
|
7
|
+
const isPlaywright = (mod) => {
|
|
8
|
+
const chromium = mod?.chromium;
|
|
9
|
+
return typeof chromium?.["launchServer"] === "function" && typeof chromium["connect"] === "function";
|
|
10
|
+
};
|
|
11
|
+
const resolvePlaywright = (cwd) => {
|
|
12
|
+
const require2 = createRequire(join(cwd, "package.json"));
|
|
13
|
+
for (const name of ["playwright", "playwright-core"]) {
|
|
14
|
+
try {
|
|
15
|
+
const mod = require2(name);
|
|
16
|
+
if (isPlaywright(mod)) return mod;
|
|
17
|
+
} catch {
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return null;
|
|
21
|
+
};
|
|
22
|
+
const firstLine = (error) => (error instanceof Error ? error.message : String(error)).split("\n")[0] ?? "";
|
|
23
|
+
function killBounded(server) {
|
|
24
|
+
const guard = new Promise((resolve) => setTimeout(resolve, KILL_TIMEOUT_MS).unref());
|
|
25
|
+
return Promise.race([server.kill().catch(() => void 0), guard]);
|
|
26
|
+
}
|
|
27
|
+
async function withBrowser(pw, fn, { channels = [void 0, "chrome"], stops } = {}) {
|
|
28
|
+
const chromiumSandbox = !process.env["CI"];
|
|
29
|
+
const failures = [];
|
|
30
|
+
for (const channel of channels) {
|
|
31
|
+
const label = channel ?? "bundled chromium";
|
|
32
|
+
let server;
|
|
33
|
+
let browser;
|
|
34
|
+
try {
|
|
35
|
+
server = await pw.chromium.launchServer(
|
|
36
|
+
channel ? { channel, chromiumSandbox } : { chromiumSandbox }
|
|
37
|
+
);
|
|
38
|
+
browser = await pw.chromium.connect(server.wsEndpoint());
|
|
39
|
+
} catch (error) {
|
|
40
|
+
failures.push(` ${label}: ${firstLine(error)}`);
|
|
41
|
+
if (server) await killBounded(server);
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
const live = server;
|
|
45
|
+
stops?.push(() => killBounded(live));
|
|
46
|
+
try {
|
|
47
|
+
return await fn(browser, channel ?? "chromium");
|
|
48
|
+
} finally {
|
|
49
|
+
await killBounded(live);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
throw new ShotError([INSTALL_LINE, ...failures].join("\n"));
|
|
53
|
+
}
|
|
54
|
+
export {
|
|
55
|
+
INSTALL_LINE,
|
|
56
|
+
ShotError,
|
|
57
|
+
isPlaywright,
|
|
58
|
+
resolvePlaywright,
|
|
59
|
+
withBrowser
|
|
60
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serve.d.ts","sourceRoot":"","sources":["../../../src/cli/shot/serve.ts"],"names":[],"mappings":"AAKA,yGAAyG;AACzG,wBAAgB,SAAS,CAAC,OAAO,EAAE,MAAM,GAAG;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,IAAI,CAAA;CAAE,CA4B/E"}
|