@uselazuli/cli 0.0.0-stage → 1.0.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/CHANGELOG.md +98 -0
- package/LICENSE +21 -0
- package/README.md +202 -2
- package/agents/lazuli-design-reviewer.md +30 -0
- package/agents/lazuli-first-look.md +22 -0
- package/agents/lazuli-task-tester.md +25 -0
- package/bin/lazuli.mjs +699 -0
- package/package.json +11 -4
- package/rules/accessibility/HYPOTHESES.md +8 -0
- package/rules/accessibility/_area.yaml +7 -0
- package/rules/accessibility/operable.yaml +100 -0
- package/rules/accessibility/perceivable.yaml +153 -0
- package/rules/accessibility/robust.yaml +24 -0
- package/rules/accessibility/understandable.yaml +72 -0
- package/rules/color/HYPOTHESES.md +27 -0
- package/rules/color/_area.yaml +10 -0
- package/rules/color/adapt.yaml +183 -0
- package/rules/color/create.yaml +259 -0
- package/rules/color/review.yaml +215 -0
- package/rules/color/signal.yaml +196 -0
- package/rules/content/HYPOTHESES.md +18 -0
- package/rules/content/_area.yaml +8 -0
- package/rules/content/review.yaml +347 -0
- package/rules/content/voice.yaml +144 -0
- package/rules/content/writing.yaml +202 -0
- package/rules/data/HYPOTHESES.md +11 -0
- package/rules/data/_area.yaml +7 -0
- package/rules/data/charts.yaml +189 -0
- package/rules/data/dashboards.yaml +141 -0
- package/rules/data/review.yaml +159 -0
- package/rules/graphic/HYPOTHESES.md +17 -0
- package/rules/graphic/_area.yaml +8 -0
- package/rules/graphic/identity.yaml +141 -0
- package/rules/graphic/pieces.yaml +164 -0
- package/rules/graphic/review.yaml +242 -0
- package/rules/graphic/symbol.yaml +141 -0
- package/rules/motion/HYPOTHESES.md +19 -0
- package/rules/motion/_area.yaml +7 -0
- package/rules/motion/micro.yaml +143 -0
- package/rules/motion/review.yaml +347 -0
- package/rules/motion/transitions.yaml +169 -0
- package/rules/typography/HYPOTHESES.md +12 -0
- package/rules/typography/_area.yaml +8 -0
- package/rules/typography/adapt.yaml +45 -0
- package/rules/typography/create.yaml +126 -0
- package/rules/typography/review.yaml +127 -0
- package/rules/ui/HYPOTHESES.md +23 -0
- package/rules/ui/_area.yaml +9 -0
- package/rules/ui/compose.yaml +219 -0
- package/rules/ui/grid.yaml +75 -0
- package/rules/ui/review.yaml +399 -0
- package/rules/ui/states.yaml +257 -0
- package/rules/ux/HYPOTHESES.md +13 -0
- package/rules/ux/_area.yaml +8 -0
- package/rules/ux/flows.yaml +164 -0
- package/rules/ux/forms.yaml +142 -0
- package/rules/ux/review.yaml +186 -0
- package/skills/lazuli/SKILL.md +180 -0
- package/skills/lazuli/procedures/create.md +29 -0
- package/skills/lazuli/procedures/explain-decisions.md +22 -0
- package/skills/lazuli/procedures/improve.md +30 -0
- package/skills/lazuli/procedures/remember-this.md +41 -0
- package/skills/lazuli/procedures/review-design.md +35 -0
- package/skills/lazuli/procedures/show-options.md +32 -0
- package/skills/lazuli-accessibility-check/SKILL.md +68 -0
- package/skills/lazuli-color-context-and-culture/SKILL.md +53 -0
- package/skills/lazuli-color-context-and-culture/references/sectors.md +26 -0
- package/skills/lazuli-color-palette-by-intent/SKILL.md +54 -0
- package/skills/lazuli-color-palette-by-intent/references/intent-chords.md +51 -0
- package/skills/lazuli-color-screen-review/SKILL.md +51 -0
- package/skills/lazuli-color-states-and-signals/SKILL.md +53 -0
- package/skills/lazuli-content-interface-writing/SKILL.md +53 -0
- package/skills/lazuli-content-voice/SKILL.md +39 -0
- package/skills/lazuli-data-charts-and-dashboards/SKILL.md +59 -0
- package/skills/lazuli-graphic-derived-pieces/SKILL.md +40 -0
- package/skills/lazuli-graphic-derived-pieces/references/piece-template.html +24 -0
- package/skills/lazuli-graphic-identity/SKILL.md +43 -0
- package/skills/lazuli-motion-micro-and-transitions/SKILL.md +49 -0
- package/skills/lazuli-typography-choose-and-pair/SKILL.md +50 -0
- package/skills/lazuli-typography-screen-review/SKILL.md +51 -0
- package/skills/lazuli-ui-composition/SKILL.md +42 -0
- package/skills/lazuli-ui-composition/references/option-template.html +34 -0
- package/skills/lazuli-ui-layout-review/SKILL.md +47 -0
- package/skills/lazuli-ui-states-and-system/SKILL.md +54 -0
- package/skills/lazuli-ux-flows-and-forms/SKILL.md +51 -0
- package/skills/lazuli-ux-task-test/SKILL.md +44 -0
- package/src/browser.mjs +161 -0
- package/src/commands/capture.mjs +313 -0
- package/src/commands/catalog.mjs +37 -0
- package/src/commands/check-design.mjs +428 -0
- package/src/commands/check-goals.mjs +57 -0
- package/src/commands/compare-designs.mjs +97 -0
- package/src/commands/draft-memo.mjs +161 -0
- package/src/commands/hooks.mjs +343 -0
- package/src/commands/install-hooks.mjs +120 -0
- package/src/commands/install.mjs +132 -0
- package/src/commands/list-rules.mjs +49 -0
- package/src/commands/map-page.mjs +594 -0
- package/src/commands/record-answers.mjs +79 -0
- package/src/commands/record-log.mjs +296 -0
- package/src/commands/record-task.mjs +250 -0
- package/src/commands/render-options.mjs +164 -0
- package/src/commands/start-project.mjs +32 -0
- package/src/measure/a11y.mjs +383 -0
- package/src/measure/asset.mjs +206 -0
- package/src/measure/at-rest.mjs +49 -0
- package/src/measure/cases.mjs +549 -0
- package/src/measure/chart-read.mjs +401 -0
- package/src/measure/chart.mjs +264 -0
- package/src/measure/coherence.mjs +145 -0
- package/src/measure/color.mjs +79 -0
- package/src/measure/content.mjs +82 -0
- package/src/measure/cvd.mjs +24 -0
- package/src/measure/extreme.mjs +241 -0
- package/src/measure/forms.mjs +150 -0
- package/src/measure/in-page/a11y.js +489 -0
- package/src/measure/in-page/color.js +346 -0
- package/src/measure/in-page/composition.js +1496 -0
- package/src/measure/in-page/contrast.js +15 -0
- package/src/measure/in-page/cvd.js +173 -0
- package/src/measure/in-page/extreme.js +355 -0
- package/src/measure/in-page/frames.js +781 -0
- package/src/measure/in-page/look.js +28 -0
- package/src/measure/in-page/motion-probe.js +169 -0
- package/src/measure/in-page/style.js +107 -0
- package/src/measure/in-page/svg-strokes.js +112 -0
- package/src/measure/in-page/svg-uses.js +50 -0
- package/src/measure/in-page/symbol.js +123 -0
- package/src/measure/in-page/type.js +266 -0
- package/src/measure/in-page/weight.js +207 -0
- package/src/measure/layout.mjs +91 -0
- package/src/measure/motion.mjs +508 -0
- package/src/measure/piece.mjs +195 -0
- package/src/measure/registry.mjs +252 -0
- package/src/measure/rhythm.mjs +81 -0
- package/src/measure/signature.mjs +65 -0
- package/src/measure/states.mjs +240 -0
- package/src/measure/style.mjs +13 -0
- package/src/measure/symbol.mjs +268 -0
- package/src/measure/terms.mjs +153 -0
- package/src/measure/tiles.mjs +20 -0
- package/src/measure/timeline.mjs +621 -0
- package/src/measure/type.mjs +167 -0
- package/src/measure/weight.mjs +18 -0
- package/src/private-strings.mjs +81 -0
- package/src/project/blind.mjs +70 -0
- package/src/project/edits.mjs +154 -0
- package/src/project/gate.mjs +159 -0
- package/src/project/lazuli-md.mjs +497 -0
- package/src/project/log.mjs +329 -0
- package/src/project/match.mjs +112 -0
- package/src/project/records.mjs +380 -0
- package/src/project/tasks.mjs +153 -0
- package/src/report/evaluate.mjs +27 -0
- package/src/report/evaluators/a11y.mjs +206 -0
- package/src/report/evaluators/chart.mjs +151 -0
- package/src/report/evaluators/coherence.mjs +103 -0
- package/src/report/evaluators/color.mjs +86 -0
- package/src/report/evaluators/content.mjs +112 -0
- package/src/report/evaluators/extreme.mjs +53 -0
- package/src/report/evaluators/forms.mjs +116 -0
- package/src/report/evaluators/layout.mjs +94 -0
- package/src/report/evaluators/motion.mjs +273 -0
- package/src/report/evaluators/piece.mjs +69 -0
- package/src/report/evaluators/rhythm.mjs +99 -0
- package/src/report/evaluators/states.mjs +39 -0
- package/src/report/evaluators/style.mjs +35 -0
- package/src/report/evaluators/symbol.mjs +110 -0
- package/src/report/evaluators/terms.mjs +49 -0
- package/src/report/evaluators/type.mjs +163 -0
- package/src/report/evaluators/weight.mjs +51 -0
- package/src/report/evidence.mjs +4 -0
- package/src/report/finding.mjs +11 -0
- package/src/report/format.mjs +215 -0
- package/src/rules/hypotheses.mjs +28 -0
- package/src/rules/load.mjs +256 -0
- package/src/rules/public-text.mjs +56 -0
- package/src/rules/schema.mjs +212 -0
- package/templates/LAZULI.md +86 -0
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { openBrowser, openTarget, resolveTarget } from "../browser.mjs";
|
|
4
|
+
import { MEASURERS, measurersFor } from "../measure/registry.mjs";
|
|
5
|
+
import { KIT_RULES_DIR, loadKit } from "../rules/load.mjs";
|
|
6
|
+
import { evaluate } from "../report/evaluate.mjs";
|
|
7
|
+
import { noteGuessChanges } from "../report/evaluators/weight.mjs";
|
|
8
|
+
import { emptyBrand, isAssetGoal, readLazuliMd } from "../project/lazuli-md.mjs";
|
|
9
|
+
import { AssetInputError, assetDir, assetOfType, loadAssetBytes, resolveAsset } from "../measure/asset.mjs";
|
|
10
|
+
import { applyBreaks, matchGoals, sameTarget } from "../project/match.mjs";
|
|
11
|
+
import { gateStatus } from "../project/gate.mjs";
|
|
12
|
+
import { makeBlindFolder, removeBlindFolder } from "../project/blind.mjs";
|
|
13
|
+
|
|
14
|
+
export class UnknownAreaError extends Error {
|
|
15
|
+
constructor(name, known) {
|
|
16
|
+
super(`Unknown area ${name}. Known areas: ${known.join(", ")}.`);
|
|
17
|
+
this.name = "UnknownAreaError";
|
|
18
|
+
this.code = "UNKNOWN_AREA";
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class ScreensInputError extends Error {
|
|
23
|
+
constructor() {
|
|
24
|
+
super("check-design --site needs two or more screens: add Target lines to the goals in LAZULI.md or give more targets.");
|
|
25
|
+
this.name = "ScreensInputError";
|
|
26
|
+
this.code = "SCREENS_INPUT";
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Rules and measured rules per area. A measurer that failed at any width (failed: its names) no longer
|
|
31
|
+
// counts its rules as measured in the area it serves, and the area lists it. When site measurers were
|
|
32
|
+
// selected but had fewer than two screens (siteSkipped), their rules are not counted as measured either.
|
|
33
|
+
// A rule counts only when this run can run its measurer: page and site measurers on a page run, the asset
|
|
34
|
+
// measurers of the asset's kind on an asset run (kind). all: every registered measurer, so a page run still
|
|
35
|
+
// knows which rules read assets. On a page run an area with an asset rule and no page or site rule is
|
|
36
|
+
// assetOnly (the report leaves it out when it has no finding).
|
|
37
|
+
export function areaCoverage(areas, rules, measurers, { failed = new Set(), siteSkipped = false, kind = null, all = measurers } = {}) {
|
|
38
|
+
const skippedIds = siteSkipped ? measurers.filter((x) => x.scope === "site").flatMap((x) => x.ids) : [];
|
|
39
|
+
const owner = (r) => all.find((x) => x.ids.includes(r.measure));
|
|
40
|
+
const runnable = (r) => {
|
|
41
|
+
const o = owner(r);
|
|
42
|
+
return kind ? o?.scope === "asset" && o.kind === kind : o?.scope !== "asset";
|
|
43
|
+
};
|
|
44
|
+
return areas.map((a) => {
|
|
45
|
+
const own = rules.filter((r) => r.area === a.area);
|
|
46
|
+
const counted = own.filter((r) => r.measure && runnable(r) && !skippedIds.includes(r.measure));
|
|
47
|
+
const entry = { area: a.area, title: a.title, rules: own.length, measured: counted.length };
|
|
48
|
+
if (!kind) {
|
|
49
|
+
const scopes = own.filter((r) => r.measure).map((r) => owner(r)?.scope);
|
|
50
|
+
if (scopes.includes("asset") && !scopes.some((s) => s === "page" || s === "site")) entry.assetOnly = true;
|
|
51
|
+
}
|
|
52
|
+
const down = measurers.filter((x) => failed.has(x.name) && own.some((r) => x.ids.includes(r.measure)));
|
|
53
|
+
if (down.length > 0) {
|
|
54
|
+
entry.failed = down.map((x) => x.name);
|
|
55
|
+
entry.measured = counted.filter((r) => !down.some((x) => x.ids.includes(r.measure))).length;
|
|
56
|
+
}
|
|
57
|
+
return entry;
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Opens every screen at one width and runs the site measurers on them. A screen that cannot be opened
|
|
62
|
+
// stops the site measurers at that width with an error naming it. Every context closes in finally.
|
|
63
|
+
async function measureSite({ browser, screens, width, cwd, siteMeasurers, rules, goals }) {
|
|
64
|
+
const m = { width, errors: {} };
|
|
65
|
+
const pages = [];
|
|
66
|
+
try {
|
|
67
|
+
let failure = null;
|
|
68
|
+
for (const target of screens) {
|
|
69
|
+
try {
|
|
70
|
+
pages.push({ target, page: await openTarget(browser, target, width, { cwd }) });
|
|
71
|
+
} catch (err) {
|
|
72
|
+
failure = `Screen ${target} could not be opened: ${String(err?.message ?? err).split("\n")[0]}`;
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
for (const measurer of siteMeasurers) {
|
|
77
|
+
if (failure) {
|
|
78
|
+
m[measurer.name] = null;
|
|
79
|
+
m.errors[measurer.name] = failure;
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
const own = rules.filter((r) => r.measure && measurer.ids.includes(r.measure));
|
|
84
|
+
m[measurer.name] = await measurer.run(pages, { width, screens: [...screens], goals, rules: own });
|
|
85
|
+
} catch (err) {
|
|
86
|
+
m[measurer.name] = null;
|
|
87
|
+
m.errors[measurer.name] = err?.message ?? String(err);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
} finally {
|
|
91
|
+
for (const { page } of pages) await page.context().close().catch(() => {});
|
|
92
|
+
}
|
|
93
|
+
return m;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Runs the asset measurers of the asset's kind: once per size for a piece, once for a symbol. A throw becomes
|
|
97
|
+
// errors[name] on that entry, except an AssetInputError (an input the measurer found unusable once loaded),
|
|
98
|
+
// which is rethrown. Each entry's width is a size label.
|
|
99
|
+
export async function measureAsset({ asset, browser, measurers, rules, cwd, md, goals }) {
|
|
100
|
+
const own = measurers.filter((x) => x.scope === "asset" && x.kind === asset.kind);
|
|
101
|
+
const base = { browser, cwd, brand: md?.brand ?? emptyBrand(), goals, dir: assetDir(cwd, asset.target) };
|
|
102
|
+
const ctxOf = (x, size) => ({ ...base, rules: rules.filter((r) => r.measure && x.ids.includes(r.measure)), size });
|
|
103
|
+
const measures = [];
|
|
104
|
+
const written = [];
|
|
105
|
+
if (own.length === 0) return { measures, written, notes: [] };
|
|
106
|
+
const runOne = async (m, x, size) => {
|
|
107
|
+
try {
|
|
108
|
+
const report = await x.run(asset, ctxOf(x, size));
|
|
109
|
+
m[x.name] = report;
|
|
110
|
+
written.push(...(report?.written ?? []));
|
|
111
|
+
} catch (err) {
|
|
112
|
+
if (err?.code === "ASSET_INPUT") throw err;
|
|
113
|
+
m[x.name] = null;
|
|
114
|
+
m.errors[x.name] = err?.message ?? String(err);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
if (asset.kind === "piece") {
|
|
118
|
+
for (const size of asset.sizes) {
|
|
119
|
+
const m = { width: `${size.width}x${size.height}`, errors: {} };
|
|
120
|
+
for (const x of own) await runOne(m, x, size);
|
|
121
|
+
measures.push(m);
|
|
122
|
+
}
|
|
123
|
+
} else {
|
|
124
|
+
const m = { width: SYMBOL_LABEL, errors: {} };
|
|
125
|
+
for (const x of own) await runOne(m, x, null);
|
|
126
|
+
// The label is the ladder the measurer read, largest to smallest; it stays SYMBOL_LABEL when none was read.
|
|
127
|
+
const read = own.map((x) => m[x.name]?.sizes).find((s) => Array.isArray(s) && s.length > 0);
|
|
128
|
+
if (read) m.width = `${Math.max(...read)} to ${Math.min(...read)}`;
|
|
129
|
+
measures.push(m);
|
|
130
|
+
}
|
|
131
|
+
// finish gets the files this run wrote (ctx.written) and returns { written, notes }.
|
|
132
|
+
const notes = [];
|
|
133
|
+
for (const x of own.filter((y) => typeof y.finish === "function")) {
|
|
134
|
+
try {
|
|
135
|
+
const done = await x.finish(asset, { ...ctxOf(x, null), written: [...written] });
|
|
136
|
+
written.push(...(done?.written ?? []));
|
|
137
|
+
notes.push(...(done?.notes ?? []));
|
|
138
|
+
} catch (err) {
|
|
139
|
+
if (err?.code === "ASSET_INPUT") throw err;
|
|
140
|
+
const last = measures.at(-1);
|
|
141
|
+
if (last && !last.errors[x.name]) last.errors[x.name] = err?.message ?? String(err);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return { measures, written, notes };
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const SYMBOL_LABEL = "no size read";
|
|
148
|
+
|
|
149
|
+
// check-design --asset: one asset, no page target. Areas: --area when given, else every area with a rule
|
|
150
|
+
// read by an asset measurer of this kind. Goals, breaks and effects work as for a page.
|
|
151
|
+
async function checkAsset({ asset: assetTarget, sizes, target, more, out, copyTo, cwd, givenBrowser, registered, allMeasurers, wanted }) {
|
|
152
|
+
if (target !== undefined || more.length > 0) throw new AssetInputError("check-design --asset checks one asset; give no page target with it.");
|
|
153
|
+
// Read before any browser opens: a missing local file and a piece without a size fail here.
|
|
154
|
+
const md = await readLazuliMd(cwd);
|
|
155
|
+
let asset = resolveAsset({ target: assetTarget, sizes, cwd, md });
|
|
156
|
+
const kit = await loadKit({
|
|
157
|
+
dirs: [process.env.LAZULI_RULES_DIR ?? KIT_RULES_DIR],
|
|
158
|
+
projectDir: path.join(cwd, ".lazuli", "rules"),
|
|
159
|
+
});
|
|
160
|
+
const known = kit.areas.map((a) => a.area);
|
|
161
|
+
for (const name of wanted ?? []) {
|
|
162
|
+
if (!known.includes(name)) throw new UnknownAreaError(name, known);
|
|
163
|
+
}
|
|
164
|
+
const goals = matchGoals(md, assetTarget, cwd);
|
|
165
|
+
const browser = givenBrowser ?? (await openBrowser());
|
|
166
|
+
let chosen;
|
|
167
|
+
let rules;
|
|
168
|
+
let measurers;
|
|
169
|
+
let result;
|
|
170
|
+
try {
|
|
171
|
+
// A web URL with no known extension: the content type of its bytes decides the kind.
|
|
172
|
+
if (asset.kind === null) asset = assetOfType(asset, (await loadAssetBytes(asset, browser)).type, { sizes });
|
|
173
|
+
const ofKind = (x) => x.scope === "asset" && x.kind === asset.kind;
|
|
174
|
+
const readers = registered.filter(ofKind);
|
|
175
|
+
chosen = kit.areas.filter((a) => (wanted ? wanted.includes(a.area) : kit.rules.some((r) => r.area === a.area && readers.some((x) => x.ids.includes(r.measure)))));
|
|
176
|
+
rules = kit.rules.filter((r) => chosen.some((a) => a.area === r.area));
|
|
177
|
+
measurers = (allMeasurers ? registered : measurersFor(rules, registered)).filter(ofKind);
|
|
178
|
+
result = await measureAsset({ asset, browser, measurers, rules, cwd, md, goals });
|
|
179
|
+
} finally {
|
|
180
|
+
if (!givenBrowser) await browser.close();
|
|
181
|
+
}
|
|
182
|
+
const { target: t, kind, raster, sizes: s, notes } = asset;
|
|
183
|
+
const blindSheet = kind === "symbol" ? await copyBlindSheet(cwd, assetTarget, result.written) : null;
|
|
184
|
+
await keepPageReport(cwd, out, assetTarget);
|
|
185
|
+
return summarize({
|
|
186
|
+
kit,
|
|
187
|
+
chosen,
|
|
188
|
+
rules,
|
|
189
|
+
measurers,
|
|
190
|
+
registered,
|
|
191
|
+
measures: result.measures,
|
|
192
|
+
siteMeasures: [],
|
|
193
|
+
siteMeasurers: [],
|
|
194
|
+
screens: [t],
|
|
195
|
+
siteShort: false,
|
|
196
|
+
siteSkipped: false,
|
|
197
|
+
md,
|
|
198
|
+
goals,
|
|
199
|
+
target: t,
|
|
200
|
+
cwd,
|
|
201
|
+
out,
|
|
202
|
+
copyTo,
|
|
203
|
+
kind,
|
|
204
|
+
asset: { target: t, kind, raster, sizes: s, written: result.written, notes: [...notes, ...result.notes], ...(blindSheet ? { blindSheet } : {}) },
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// A piece's page run (text contrast RV2 and type) comes before its --asset run, which writes the same report
|
|
209
|
+
// file: a page report of the same target found there is kept beside it as PAGE_REPORT, so capture puts both in
|
|
210
|
+
// the reviewer packet.
|
|
211
|
+
export const PAGE_REPORT = "report-page.json";
|
|
212
|
+
async function keepPageReport(cwd, out, target) {
|
|
213
|
+
const file = path.resolve(cwd, out);
|
|
214
|
+
let parsed;
|
|
215
|
+
try {
|
|
216
|
+
parsed = JSON.parse(await fs.readFile(file, "utf8"));
|
|
217
|
+
} catch {
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
if (parsed?.asset || typeof parsed?.target !== "string" || !sameTarget(parsed.target, target, cwd)) return;
|
|
221
|
+
await fs.copyFile(file, path.join(path.dirname(file), PAGE_REPORT));
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// The blind look (GR5) gets a neutral copy of the symbol's contact sheet: sheet.png in a fresh lz-<token> folder
|
|
225
|
+
// outside the project (as the first look's images), so neither its path nor its pixels name the file, the
|
|
226
|
+
// project or the brand. The folder of the previous run of this asset, kept in <assetDir>/blind.json, is removed.
|
|
227
|
+
// Returns the copy's path, or null when the run wrote no sheet.
|
|
228
|
+
async function copyBlindSheet(cwd, target, written) {
|
|
229
|
+
const sheet = written.find((f) => path.basename(f) === "symbol-sheet.png");
|
|
230
|
+
if (!sheet) return null;
|
|
231
|
+
const memo = path.join(assetDir(cwd, target), "blind.json");
|
|
232
|
+
try {
|
|
233
|
+
await removeBlindFolder(JSON.parse(await fs.readFile(memo, "utf8")));
|
|
234
|
+
} catch {
|
|
235
|
+
// no earlier blind sheet, or one already removed
|
|
236
|
+
}
|
|
237
|
+
const blind = await makeBlindFolder(cwd);
|
|
238
|
+
const copy = path.join(blind.dir, "sheet.png");
|
|
239
|
+
await fs.copyFile(sheet, copy);
|
|
240
|
+
await fs.writeFile(memo, JSON.stringify(blind));
|
|
241
|
+
return copy;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export async function checkDesign({
|
|
245
|
+
target,
|
|
246
|
+
screens: more = [],
|
|
247
|
+
site = false,
|
|
248
|
+
widths = [1440, 768, 390],
|
|
249
|
+
out = ".lazuli/report.json",
|
|
250
|
+
// A second file the report is also written to (CLI --out); the report at `out` is always written.
|
|
251
|
+
copyTo,
|
|
252
|
+
cwd = process.cwd(),
|
|
253
|
+
browser: givenBrowser,
|
|
254
|
+
measurers: registered = MEASURERS,
|
|
255
|
+
allMeasurers = false,
|
|
256
|
+
areas: wanted,
|
|
257
|
+
asset,
|
|
258
|
+
sizes = null,
|
|
259
|
+
// Review sheets (the chart sheet) are written unless the post-edit hook turned them off (LAZULI_SHEETS=off).
|
|
260
|
+
sheets = process.env.LAZULI_SHEETS !== "off",
|
|
261
|
+
}) {
|
|
262
|
+
if (asset !== undefined) {
|
|
263
|
+
return checkAsset({ asset, sizes, target, more, out, copyTo, cwd, givenBrowser, registered, allMeasurers, wanted });
|
|
264
|
+
}
|
|
265
|
+
// A missing local file fails here, before any browser starts.
|
|
266
|
+
for (const s of [target, ...more]) resolveTarget(s, cwd);
|
|
267
|
+
const kit = await loadKit({
|
|
268
|
+
dirs: [process.env.LAZULI_RULES_DIR ?? KIT_RULES_DIR],
|
|
269
|
+
projectDir: path.join(cwd, ".lazuli", "rules"),
|
|
270
|
+
});
|
|
271
|
+
const known = kit.areas.map((a) => a.area);
|
|
272
|
+
for (const name of wanted ?? []) {
|
|
273
|
+
if (!known.includes(name)) throw new UnknownAreaError(name, known);
|
|
274
|
+
}
|
|
275
|
+
const chosen = kit.areas.filter((a) => !wanted || wanted.includes(a.area));
|
|
276
|
+
const rules = kit.rules.filter((r) => chosen.some((a) => a.area === r.area));
|
|
277
|
+
// Only the measurers that a selected rule uses run (allMeasurers: true runs every given one, for tests).
|
|
278
|
+
const measurers = allMeasurers ? registered : measurersFor(rules, registered);
|
|
279
|
+
const pageMeasurers = measurers.filter((x) => x.scope === "page");
|
|
280
|
+
const siteMeasurers = measurers.filter((x) => x.scope === "site");
|
|
281
|
+
// Read before any browser opens: measurers get the goals of this page (ctx.goals, in file order).
|
|
282
|
+
const md = await readLazuliMd(cwd);
|
|
283
|
+
const goals = matchGoals(md, target, cwd);
|
|
284
|
+
// The distinct screens, in order: the target, the other targets given, then with --site the Target of
|
|
285
|
+
// every goal that is a page (a derived piece or a symbol is not a screen). The same page written two ways counts once.
|
|
286
|
+
const fromGoals = site ? (md?.goals ?? []).filter((g) => g.target && !isAssetGoal(g)) : [];
|
|
287
|
+
const candidates = [...more, ...fromGoals.map((g) => g.target)];
|
|
288
|
+
const screens = [target];
|
|
289
|
+
for (const s of candidates) if (!screens.some((x) => sameTarget(x, s, cwd))) screens.push(s);
|
|
290
|
+
for (const s of screens) {
|
|
291
|
+
try {
|
|
292
|
+
resolveTarget(s, cwd);
|
|
293
|
+
} catch (err) {
|
|
294
|
+
// A screen read from LAZULI.md names the line it came from, so the person knows what to fix.
|
|
295
|
+
const goal = err?.code === "FILE_NOT_FOUND" && !more.includes(s) && fromGoals.find((g) => g.target === s);
|
|
296
|
+
if (goal) err.message += ` (from the line "Target: ${s}" of the goal "${goal.piece}" in LAZULI.md).`;
|
|
297
|
+
throw err;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
if (site && screens.length < 2) throw new ScreensInputError();
|
|
301
|
+
// With one screen the site measurers do not run and their rules are not counted as measured. The report
|
|
302
|
+
// says so (siteSkipped) only when more screens were given and they were the same page.
|
|
303
|
+
const siteShort = siteMeasurers.length > 0 && screens.length < 2;
|
|
304
|
+
const siteSkipped = siteShort && more.length > 0;
|
|
305
|
+
const browser = givenBrowser ?? (await openBrowser());
|
|
306
|
+
const opened = [];
|
|
307
|
+
const measures = [];
|
|
308
|
+
const siteMeasures = [];
|
|
309
|
+
const cache = new Map();
|
|
310
|
+
const once = (key, fn) => {
|
|
311
|
+
if (!cache.has(key)) cache.set(key, Promise.resolve().then(fn));
|
|
312
|
+
return cache.get(key);
|
|
313
|
+
};
|
|
314
|
+
try {
|
|
315
|
+
for (const width of widths) {
|
|
316
|
+
const ctx = {
|
|
317
|
+
width,
|
|
318
|
+
target,
|
|
319
|
+
goals,
|
|
320
|
+
once,
|
|
321
|
+
cwd,
|
|
322
|
+
sheets,
|
|
323
|
+
// Pages opened here may be shared across widths through once(), so they close at the end.
|
|
324
|
+
// bare and prepare: a raw open with an init script and a route (the motion recorder); noScript and
|
|
325
|
+
// navigate: a script-free open whose document the caller serves (the extreme case pages).
|
|
326
|
+
openPage: async (opts = {}) => {
|
|
327
|
+
const p = await openTarget(browser, target, opts.width ?? width, { still: opts.still, bare: opts.bare, prepare: opts.prepare, noScript: opts.noScript, navigate: opts.navigate, cwd });
|
|
328
|
+
opened.push(p.context());
|
|
329
|
+
return p;
|
|
330
|
+
},
|
|
331
|
+
};
|
|
332
|
+
const page = await openTarget(browser, target, width, { cwd });
|
|
333
|
+
try {
|
|
334
|
+
const m = { width, errors: {} };
|
|
335
|
+
for (const measurer of pageMeasurers) {
|
|
336
|
+
try {
|
|
337
|
+
// ctx.rules: the selected rules whose measure is one of this measurer's ids.
|
|
338
|
+
const own = rules.filter((r) => r.measure && measurer.ids.includes(r.measure));
|
|
339
|
+
m[measurer.name] = await measurer.run(page, { ...ctx, rules: own });
|
|
340
|
+
} catch (err) {
|
|
341
|
+
m[measurer.name] = null;
|
|
342
|
+
m.errors[measurer.name] = err?.message ?? String(err);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
measures.push(m);
|
|
346
|
+
} finally {
|
|
347
|
+
await page.context().close();
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
// Site measurers compare the screens after the page phase, once per width.
|
|
351
|
+
if (siteMeasurers.length > 0 && screens.length >= 2) {
|
|
352
|
+
for (const width of widths) {
|
|
353
|
+
siteMeasures.push(await measureSite({ browser, screens, width, cwd, siteMeasurers, rules, goals }));
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
} finally {
|
|
357
|
+
for (const c of opened) await c.close().catch(() => {});
|
|
358
|
+
if (!givenBrowser) await browser.close();
|
|
359
|
+
}
|
|
360
|
+
return summarize({ kit, chosen, rules, measurers, registered, measures, siteMeasures, siteMeasurers, screens, siteShort, siteSkipped, md, goals, target, cwd, out, copyTo });
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// Coverage, findings, breaks and recorded effects of a run, written to the report file. kind and asset are set
|
|
364
|
+
// on an asset run.
|
|
365
|
+
async function summarize({ kit, chosen, rules, measurers, registered, measures, siteMeasures, siteMeasurers, screens, siteShort, siteSkipped, md, goals, target, cwd, out, copyTo, kind = null, asset }) {
|
|
366
|
+
const failedNames = new Set([...measures, ...siteMeasures].flatMap((m) => Object.keys(m.errors)));
|
|
367
|
+
const areas = areaCoverage(chosen, rules, measurers, { failed: failedNames, siteSkipped: siteShort, kind, all: registered });
|
|
368
|
+
// The site measurers that did not run for want of a second screen, named in the report: every one when more
|
|
369
|
+
// screens were given and they were the same page (siteSkipped); with a single target only terms, whose rules
|
|
370
|
+
// read nothing on one screen, so coherence stays unnamed there as before.
|
|
371
|
+
const notRun = siteShort ? siteMeasurers.map((x) => x.name) : [];
|
|
372
|
+
const siteNotMeasured = siteSkipped ? notRun : notRun.filter((name) => name === "terms");
|
|
373
|
+
|
|
374
|
+
// A norm or learned rule whose measurer failed could not be checked; the CLI exits 1 for it, like a fail.
|
|
375
|
+
const failedMeasurers = measurers.filter((x) => failedNames.has(x.name));
|
|
376
|
+
const uncheckedNorms = rules
|
|
377
|
+
.filter((r) => (r.origin === "norm" || r.origin === "project") && r.status !== "hypothesis" && failedMeasurers.some((x) => x.ids.includes(r.measure)))
|
|
378
|
+
.map((r) => r.id);
|
|
379
|
+
|
|
380
|
+
// Every kit and project rule id counts as known, not only the selected areas, so --area never makes a break look misspelled.
|
|
381
|
+
// Site findings carry the screens they compare. A guessed main action that differs between widths says so.
|
|
382
|
+
const siteFindings = evaluate(rules, siteMeasures, siteMeasurers).map((f) => ({ ...f, screens: [...screens] }));
|
|
383
|
+
const { findings, applied, unapplied: allUnapplied } = applyBreaks([...noteGuessChanges(evaluate(rules, measures, measurers), measures), ...siteFindings], md?.breaks ?? [], goals, kit.rules, new Set(kit.rules.map((r) => r.id)));
|
|
384
|
+
// With --area, a break on a rule of an area that was not selected has no findings here, which is not a fault of the break.
|
|
385
|
+
const selected = new Set(rules.map((r) => r.id.toLowerCase()));
|
|
386
|
+
const unapplied = allUnapplied.filter((u) => !(u.reason.startsWith("no fail or hint") || u.reason === "not measured by check-design") || selected.has(String(u.rule).trim().toLowerCase()));
|
|
387
|
+
// A records file that cannot be read never stops the measuring: the goals just show no recorded check.
|
|
388
|
+
let effects = [];
|
|
389
|
+
const problems = [...(md?.problems ?? [])];
|
|
390
|
+
try {
|
|
391
|
+
effects = (await gateStatus({ cwd, md, target })).goals;
|
|
392
|
+
} catch (err) {
|
|
393
|
+
problems.push(err?.message ?? String(err));
|
|
394
|
+
}
|
|
395
|
+
const outFile = path.resolve(cwd, out);
|
|
396
|
+
const copyFile = copyTo ? path.resolve(cwd, copyTo) : null;
|
|
397
|
+
const result = {
|
|
398
|
+
target,
|
|
399
|
+
...(asset ? { asset } : {}),
|
|
400
|
+
screens,
|
|
401
|
+
areas,
|
|
402
|
+
findings,
|
|
403
|
+
measures,
|
|
404
|
+
siteMeasures,
|
|
405
|
+
siteSkipped,
|
|
406
|
+
siteNotMeasured,
|
|
407
|
+
uncheckedNorms,
|
|
408
|
+
hasLazuliMd: md !== null,
|
|
409
|
+
mode: md?.mode ?? null,
|
|
410
|
+
goals,
|
|
411
|
+
effects,
|
|
412
|
+
unmatchedGoals: (md?.goals.length ?? 0) - goals.length,
|
|
413
|
+
breaks: applied,
|
|
414
|
+
unappliedBreaks: unapplied,
|
|
415
|
+
problems,
|
|
416
|
+
projectProblems: kit.projectProblems,
|
|
417
|
+
reportPath: outFile,
|
|
418
|
+
...(copyFile ? { copiedTo: copyFile } : {}),
|
|
419
|
+
};
|
|
420
|
+
|
|
421
|
+
await fs.mkdir(path.dirname(outFile), { recursive: true });
|
|
422
|
+
await fs.writeFile(outFile, JSON.stringify(result, null, 2));
|
|
423
|
+
if (copyFile) {
|
|
424
|
+
await fs.mkdir(path.dirname(copyFile), { recursive: true });
|
|
425
|
+
await fs.writeFile(copyFile, JSON.stringify(result, null, 2));
|
|
426
|
+
}
|
|
427
|
+
return result;
|
|
428
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { readLazuliMd } from "../project/lazuli-md.mjs";
|
|
2
|
+
import { gateStatus } from "../project/gate.mjs";
|
|
3
|
+
|
|
4
|
+
export class NoLazuliMdError extends Error {
|
|
5
|
+
constructor() {
|
|
6
|
+
super("No LAZULI.md here. Run start-project first.");
|
|
7
|
+
this.name = "NoLazuliMdError";
|
|
8
|
+
this.code = "NO_LAZULI_MD";
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export async function checkGoals({ cwd = process.cwd(), target } = {}) {
|
|
13
|
+
const md = await readLazuliMd(cwd);
|
|
14
|
+
if (md === null) {
|
|
15
|
+
throw new NoLazuliMdError();
|
|
16
|
+
}
|
|
17
|
+
const gate = await gateStatus({ cwd, md, ...(target ? { target } : {}) });
|
|
18
|
+
gate.notes.push(...md.problems.map((p) => `LAZULI.md: ${p}`));
|
|
19
|
+
return gate;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Says what was recorded, by whom and when. It never says an effect was achieved, only what the reviewer judged.
|
|
23
|
+
export function formatGate(gate) {
|
|
24
|
+
const lines = [`Mode: ${gate.mode}`, ""];
|
|
25
|
+
if (gate.goals.length === 0) {
|
|
26
|
+
lines.push("No goals to check.");
|
|
27
|
+
} else {
|
|
28
|
+
lines.push("Goals:");
|
|
29
|
+
for (const g of gate.goals) {
|
|
30
|
+
let text = ` ${g.piece} [${g.status}]`;
|
|
31
|
+
if (g.status === "pending") {
|
|
32
|
+
text += `, pending: ${g.reason}`;
|
|
33
|
+
} else if (g.verdict) {
|
|
34
|
+
text += `, review ${g.verdict}`;
|
|
35
|
+
if (g.reviewer) {
|
|
36
|
+
text += ` by ${g.reviewer}`;
|
|
37
|
+
}
|
|
38
|
+
if (g.reviewedAt) {
|
|
39
|
+
text += ` on ${g.reviewedAt}`;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
lines.push(text);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (gate.items.length > 0) {
|
|
46
|
+
lines.push("", "Open points:", ...gate.items.map((i) => ` - ${i}`));
|
|
47
|
+
}
|
|
48
|
+
if (gate.notes.length > 0) {
|
|
49
|
+
lines.push("", "Notes:", ...gate.notes.map((n) => ` - ${n}`));
|
|
50
|
+
}
|
|
51
|
+
if (gate.passed) {
|
|
52
|
+
lines.push("", "Gate passed: every goal with a Target has a fresh approved review.");
|
|
53
|
+
} else {
|
|
54
|
+
lines.push("", gate.mode === "delegate" ? "Gate open. Delivery waits until these checks pass." : "Gate open. You decide whether to deliver.");
|
|
55
|
+
}
|
|
56
|
+
return lines.join("\n");
|
|
57
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { openBrowser, openTarget, resolveTarget } from "../browser.mjs";
|
|
4
|
+
import { measureLayout } from "../measure/layout.mjs";
|
|
5
|
+
import { measureRhythm } from "../measure/rhythm.mjs";
|
|
6
|
+
import { distance, signatureOf } from "../measure/signature.mjs";
|
|
7
|
+
import { sameTarget } from "../project/match.mjs";
|
|
8
|
+
|
|
9
|
+
// Listing limit, not a rule: how many of the most different components the text names per width.
|
|
10
|
+
const TOP_PARTS = 3;
|
|
11
|
+
|
|
12
|
+
export const H1_LINE = 'This is hypothesis H1 (ui): it is not yet validated that this distance tracks what people call "looks different".';
|
|
13
|
+
// Carried by the text output, --json and compare.json, so the number never travels without it.
|
|
14
|
+
export const COMPARE_NOTE = 'Hypothesis H1 (ui): a measurement, not a verdict. No cutoff says what distance is "different enough".';
|
|
15
|
+
|
|
16
|
+
export class CompareInputError extends Error {
|
|
17
|
+
constructor(message) {
|
|
18
|
+
super(message);
|
|
19
|
+
this.name = "CompareInputError";
|
|
20
|
+
this.code = "COMPARE_INPUT";
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const round = (n) => Math.round(n * 100) / 100;
|
|
25
|
+
|
|
26
|
+
async function signatureAt(browser, target, width, cwd) {
|
|
27
|
+
const page = await openTarget(browser, target, width, { cwd });
|
|
28
|
+
try {
|
|
29
|
+
const layout = await measureLayout(page);
|
|
30
|
+
const rhythm = await measureRhythm(page, {});
|
|
31
|
+
return signatureOf(layout, rhythm);
|
|
32
|
+
} finally {
|
|
33
|
+
await page.context().close();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Structural distance of a page from a baseline made without the kit for the same brief, per width.
|
|
38
|
+
// It measures; it gives no verdict, because no cutoff for "different enough" exists yet (H1).
|
|
39
|
+
export async function compareDesigns({ target, baseline, widths = [1440, 390], cwd = process.cwd(), out = path.join(".lazuli", "compare.json"), browser: givenBrowser }) {
|
|
40
|
+
if (typeof baseline !== "string" || baseline.trim() === "") {
|
|
41
|
+
throw new CompareInputError("compare-designs needs a baseline: a page made without the kit for the same brief.");
|
|
42
|
+
}
|
|
43
|
+
// Both checks run before any browser starts.
|
|
44
|
+
resolveTarget(target, cwd);
|
|
45
|
+
resolveTarget(baseline, cwd);
|
|
46
|
+
if (sameTarget(target, baseline, cwd)) {
|
|
47
|
+
throw new CompareInputError("The baseline is the same page as the target; compare against a page made without the kit for the same brief.");
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const perWidth = [];
|
|
51
|
+
const browser = givenBrowser ?? (await openBrowser());
|
|
52
|
+
try {
|
|
53
|
+
for (const width of widths) {
|
|
54
|
+
const a = await signatureAt(browser, target, width, cwd);
|
|
55
|
+
const b = await signatureAt(browser, baseline, width, cwd);
|
|
56
|
+
const d = distance(a, b);
|
|
57
|
+
perWidth.push({ width, value: d.value, components: d.components });
|
|
58
|
+
}
|
|
59
|
+
} finally {
|
|
60
|
+
if (!givenBrowser) await browser.close();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const file = path.resolve(cwd, out);
|
|
64
|
+
const result = { target, baseline, widths: perWidth, value: round(perWidth.reduce((s, w) => s + w.value, 0) / perWidth.length), path: file, note: COMPARE_NOTE };
|
|
65
|
+
await fs.mkdir(path.dirname(file), { recursive: true });
|
|
66
|
+
await fs.writeFile(file, JSON.stringify(result, null, 2));
|
|
67
|
+
return result;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const shown = (name, v) => {
|
|
71
|
+
if (name === "sections") return `${v.length} section${v.length === 1 ? "" : "s"}`;
|
|
72
|
+
if (v === null || v === undefined) return "n/a";
|
|
73
|
+
if (typeof v === "object") return `(${v.x}, ${v.y})`;
|
|
74
|
+
return String(v);
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
// Per width the value and the components that differ most (ties in component order), then the H1 caveat.
|
|
78
|
+
export function formatCompare(r) {
|
|
79
|
+
const lines = [`Target: ${r.target}`, `Baseline: ${r.baseline}`];
|
|
80
|
+
for (const w of r.widths) {
|
|
81
|
+
if (w.components.every((c) => c.diff === 0)) {
|
|
82
|
+
lines.push(`At ${w.width} px: ${w.value}; the structures match.`);
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
const top = w.components
|
|
86
|
+
.map((c, i) => ({ c, i }))
|
|
87
|
+
.filter(({ c }) => c.diff > 0)
|
|
88
|
+
.sort((x, y) => y.c.diff - x.c.diff || x.i - y.i)
|
|
89
|
+
.slice(0, TOP_PARTS)
|
|
90
|
+
.map(({ c }) => `${c.name} ${c.diff} (target ${shown(c.name, c.a)}, baseline ${shown(c.name, c.b)})`);
|
|
91
|
+
lines.push(`At ${w.width} px: ${w.value}; differs most in ${top.join("; ")}`);
|
|
92
|
+
}
|
|
93
|
+
lines.push(`Distance from default: ${r.value} (0 is the same structure, 1 shares nothing). ${H1_LINE}`);
|
|
94
|
+
lines.push(COMPARE_NOTE);
|
|
95
|
+
lines.push(`Measures: ${r.path}`);
|
|
96
|
+
return lines.join("\n");
|
|
97
|
+
}
|