@uselazuli/cli 0.0.0-stage → 1.0.2
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 +120 -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 +747 -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 +19 -0
- package/rules/content/_area.yaml +8 -0
- package/rules/content/review.yaml +378 -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 +18 -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 +271 -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 +246 -0
- package/rules/ui/grid.yaml +75 -0
- package/rules/ui/review.yaml +400 -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 +183 -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 +36 -0
- package/skills/lazuli/procedures/show-options.md +33 -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 +40 -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 +45 -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 +162 -0
- package/src/commands/capture.mjs +313 -0
- package/src/commands/catalog.mjs +37 -0
- package/src/commands/check-design.mjs +463 -0
- package/src/commands/check-goals.mjs +57 -0
- package/src/commands/compare-designs.mjs +160 -0
- package/src/commands/draft-memo.mjs +162 -0
- package/src/commands/hooks.mjs +345 -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 +325 -0
- package/src/commands/record-task.mjs +250 -0
- package/src/commands/render-options.mjs +227 -0
- package/src/commands/start-project.mjs +32 -0
- package/src/measure/a11y.mjs +391 -0
- package/src/measure/asset.mjs +206 -0
- package/src/measure/at-rest.mjs +49 -0
- package/src/measure/cases.mjs +566 -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 +86 -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 +522 -0
- package/src/measure/in-page/color.js +348 -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 +377 -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 +171 -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 +269 -0
- package/src/measure/in-page/weight.js +207 -0
- package/src/measure/layout.mjs +100 -0
- package/src/measure/motion.mjs +509 -0
- package/src/measure/piece.mjs +195 -0
- package/src/measure/registry.mjs +253 -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 +168 -0
- package/src/project/lazuli-md.mjs +521 -0
- package/src/project/log.mjs +335 -0
- package/src/project/match.mjs +200 -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 +227 -0
- package/src/report/evaluators/chart.mjs +151 -0
- package/src/report/evaluators/coherence.mjs +103 -0
- package/src/report/evaluators/color.mjs +133 -0
- package/src/report/evaluators/content.mjs +112 -0
- package/src/report/evaluators/extreme.mjs +87 -0
- package/src/report/evaluators/forms.mjs +116 -0
- package/src/report/evaluators/layout.mjs +103 -0
- package/src/report/evaluators/motion.mjs +280 -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 +166 -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 +274 -0
- package/src/rules/public-text.mjs +56 -0
- package/src/rules/schema.mjs +220 -0
- package/templates/LAZULI.md +88 -0
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
import { BULLET, FIELD, HEADING, SECTION_NAMES, knownSection, stripComments } from "./lazuli-md.mjs";
|
|
2
|
+
|
|
3
|
+
// Writes one entry of the Breaks or Decisions log into the text of LAZULI.md and returns the new text.
|
|
4
|
+
// Everything the entry does not own stays byte for byte: other sections, comments, hand-written lines
|
|
5
|
+
// and the line ending (LF or CRLF), including a file that has no final newline.
|
|
6
|
+
|
|
7
|
+
const STATUSES = ["trying", "kept", "reverted"];
|
|
8
|
+
const norm = (s) => String(s ?? "").trim().toLowerCase();
|
|
9
|
+
const label = (key) => key.charAt(0).toUpperCase() + key.slice(1);
|
|
10
|
+
|
|
11
|
+
export class LogInputError extends Error {
|
|
12
|
+
constructor(problems) {
|
|
13
|
+
super(problems.join("\n"));
|
|
14
|
+
this.name = "LogInputError";
|
|
15
|
+
this.code = "LOG_INPUT";
|
|
16
|
+
this.problems = problems;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const BREAKS = { section: "breaks", heading: "Breaks", open: "rule", fields: ["piece", "target", "effect", "check", "status"] };
|
|
21
|
+
const DECISIONS = { section: "decisions", heading: "Decisions", open: "decision", fields: ["piece", "target", "goal", "rule", "measure"] };
|
|
22
|
+
|
|
23
|
+
// Lines keep their own terminator so nothing about them changes unless we edit them.
|
|
24
|
+
function splitLines(text) {
|
|
25
|
+
const lines = [];
|
|
26
|
+
const re = /\r\n|\n|\r/g;
|
|
27
|
+
let at = 0;
|
|
28
|
+
let m;
|
|
29
|
+
while ((m = re.exec(text)) !== null) {
|
|
30
|
+
lines.push({ text: text.slice(at, m.index), eol: m[0] });
|
|
31
|
+
at = m.index + m[0].length;
|
|
32
|
+
}
|
|
33
|
+
if (at < text.length) {
|
|
34
|
+
lines.push({ text: text.slice(at), eol: "" });
|
|
35
|
+
}
|
|
36
|
+
return lines;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// What the parser sees: comments removed, one string per line.
|
|
40
|
+
function visibleLines(lines) {
|
|
41
|
+
const joined = lines.map((l) => l.text).join("\n").replace(new RegExp("^" + String.fromCharCode(0xfeff)), "");
|
|
42
|
+
return stripComments(joined, []).split("\n");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Content ranges { start, end } (end exclusive) of every section with this name, as the parser opens them.
|
|
46
|
+
function sectionRanges(vis, name) {
|
|
47
|
+
const opens = [];
|
|
48
|
+
for (let i = 0; i < vis.length; i++) {
|
|
49
|
+
const line = vis[i];
|
|
50
|
+
const heading = HEADING.exec(line);
|
|
51
|
+
const exact = heading ? SECTION_NAMES.find((n) => n === heading[2].toLowerCase()) : undefined;
|
|
52
|
+
const plain = line.trim().toLowerCase();
|
|
53
|
+
const setext = !heading && SECTION_NAMES.includes(plain) && /^[ \t]*(?:=+|-+)[ \t]*$/.test(vis[i + 1] ?? "");
|
|
54
|
+
if (heading && heading[1] === "##") {
|
|
55
|
+
opens.push({ at: i, start: i + 1, name: knownSection(heading[2]) ?? heading[2].toLowerCase() });
|
|
56
|
+
} else if (exact) {
|
|
57
|
+
opens.push({ at: i, start: i + 1, name: exact });
|
|
58
|
+
} else if (setext) {
|
|
59
|
+
opens.push({ at: i, start: i + 2, name: plain });
|
|
60
|
+
i++;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return opens
|
|
64
|
+
.map((o, n) => ({ ...o, end: n + 1 < opens.length ? opens[n + 1].at : vis.length }))
|
|
65
|
+
.filter((o) => o.name === name);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// A bullet read as { indent, key, value }, the way the parser reads it.
|
|
69
|
+
function readBullet(line) {
|
|
70
|
+
const bullet = BULLET.exec(line);
|
|
71
|
+
if (!bullet) {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
const field = FIELD.exec(bullet[2]);
|
|
75
|
+
let value = field ? field[3] : bullet[2];
|
|
76
|
+
if (field && field[1] && value.endsWith(field[1]) && !value.startsWith(field[1])) {
|
|
77
|
+
value = value.slice(0, -field[1].length).trim();
|
|
78
|
+
}
|
|
79
|
+
return { indent: bullet[1].replace(/\t/g, " ").length, key: field ? field[2].toLowerCase() : null, value };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Entries of one section range, grouped exactly as the parser groups them: a head bullet at the base
|
|
83
|
+
// indent opens an entry, field bullets (nested or flat, across blank lines) fill it, a base-indent
|
|
84
|
+
// bullet that is neither resets it. { head, last (line of its last bullet), end (range end), value,
|
|
85
|
+
// fields: { key: { line, value } } }
|
|
86
|
+
function scanEntries(vis, range, spec) {
|
|
87
|
+
const bullets = [];
|
|
88
|
+
for (let i = range.start; i < range.end; i++) {
|
|
89
|
+
const b = readBullet(vis[i]);
|
|
90
|
+
if (b !== null) {
|
|
91
|
+
bullets.push({ ...b, i });
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const base = Math.min(...bullets.map((b) => b.indent));
|
|
95
|
+
const found = [];
|
|
96
|
+
let current = null;
|
|
97
|
+
for (const b of bullets) {
|
|
98
|
+
if (b.key === spec.open && b.indent <= base) {
|
|
99
|
+
current = { head: b.i, last: b.i, end: range.end, value: b.value, fields: {} };
|
|
100
|
+
found.push(current);
|
|
101
|
+
} else if (b.key && spec.fields.includes(b.key) && current) {
|
|
102
|
+
current.last = b.i;
|
|
103
|
+
if (!(b.key in current.fields)) {
|
|
104
|
+
current.fields[b.key] = { line: b.i, value: b.value };
|
|
105
|
+
}
|
|
106
|
+
} else if (b.indent > base && current) {
|
|
107
|
+
current.last = b.i;
|
|
108
|
+
} else {
|
|
109
|
+
current = null;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return found;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const leading = (line) => /^[ \t]*/.exec(line)[0];
|
|
116
|
+
|
|
117
|
+
// Lines right after a field line that wrap its value: indented, not a bullet, not a Key: line.
|
|
118
|
+
function continuation(lines, vis, line, end) {
|
|
119
|
+
const out = [];
|
|
120
|
+
for (let j = line + 1; j < end; j++) {
|
|
121
|
+
if (lines[j].text.trim() === "" || !/^[ \t]/.test(vis[j]) || readBullet(vis[j]) !== null || FIELD.test(vis[j].trim())) {
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
out.push(j);
|
|
125
|
+
}
|
|
126
|
+
return out;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// True when the end of line k sits inside a closed comment; the entry must go after the comment ends.
|
|
130
|
+
function commentEndLine(lines, k) {
|
|
131
|
+
const text = lines.map((l) => l.text).join("\n");
|
|
132
|
+
const ends = [];
|
|
133
|
+
let at = 0;
|
|
134
|
+
for (const l of lines) {
|
|
135
|
+
at += l.text.length + 1;
|
|
136
|
+
ends.push(at - 1);
|
|
137
|
+
}
|
|
138
|
+
let from = 0;
|
|
139
|
+
for (;;) {
|
|
140
|
+
const open = text.indexOf("<!--", from);
|
|
141
|
+
if (open === -1) {
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
const close = text.indexOf("-->", open + 4);
|
|
145
|
+
if (close === -1) {
|
|
146
|
+
from = text.indexOf("\n", open) === -1 ? text.length : text.indexOf("\n", open);
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
if (open < ends[k] && close >= ends[k]) {
|
|
150
|
+
return ends.findIndex((e) => e >= close);
|
|
151
|
+
}
|
|
152
|
+
from = close + 3;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function insertAfter(lines, after, texts, fileEol) {
|
|
157
|
+
let k = after;
|
|
158
|
+
for (let again = commentEndLine(lines, k); again !== null; again = commentEndLine(lines, k)) {
|
|
159
|
+
k = again;
|
|
160
|
+
}
|
|
161
|
+
const eol = lines[k].eol || fileEol;
|
|
162
|
+
const added = texts.map((text) => ({ text, eol }));
|
|
163
|
+
if (lines[k].eol === "") {
|
|
164
|
+
// The file had no final newline: it gets one here and the last new line ends the file instead.
|
|
165
|
+
lines[k].eol = fileEol;
|
|
166
|
+
added[added.length - 1].eol = "";
|
|
167
|
+
}
|
|
168
|
+
lines.splice(k + 1, 0, ...added);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function clean(value, name, problems) {
|
|
172
|
+
const text = String(value).replace(/\s+/g, " ").trim();
|
|
173
|
+
if (text.includes("<!--") || text.includes("-->")) {
|
|
174
|
+
problems.push(`${name} cannot contain <!-- or -->, it would hide text in LAZULI.md.`);
|
|
175
|
+
}
|
|
176
|
+
return text;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function fieldLine(key, value) {
|
|
180
|
+
return value === "" ? `- ${label(key)}:` : `- ${label(key)}: ${value}`;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Rewrites only the value of a field line: its bullet, name style, closing emphasis and every comment stay.
|
|
184
|
+
// The value goes where the old visible text was; a comment before the value stays before it.
|
|
185
|
+
function withValue(raw, key, value) {
|
|
186
|
+
const emphasis = String.raw`(?:\*\*|__|\*|_)?`;
|
|
187
|
+
const re = new RegExp(String.raw`^([ \t]*(?:[-*+]|\d+[.)])[ \t]+(${emphasis})${key}${emphasis}[ \t]*:${emphasis}[ \t]*)(.*)$`, "i");
|
|
188
|
+
const m = re.exec(raw);
|
|
189
|
+
if (!m) {
|
|
190
|
+
return raw;
|
|
191
|
+
}
|
|
192
|
+
const parts = m[3].split(/(<!--.*?(?:-->|$))/);
|
|
193
|
+
const shown = parts.filter((_, n) => n % 2 === 0).join("").trim();
|
|
194
|
+
const closing = m[2] && shown.endsWith(m[2]) && !shown.startsWith(m[2]) ? m[2] : "";
|
|
195
|
+
let placed = false;
|
|
196
|
+
const body = parts.map((p, n) => {
|
|
197
|
+
if (n % 2 === 1 || p.trim() === "") {
|
|
198
|
+
return p;
|
|
199
|
+
}
|
|
200
|
+
if (placed) {
|
|
201
|
+
return "";
|
|
202
|
+
}
|
|
203
|
+
placed = true;
|
|
204
|
+
return `${leading(p)}${value}${closing}${/\s$/.test(p) ? " " : ""}`;
|
|
205
|
+
});
|
|
206
|
+
return placed ? `${m[1]}${body.join("").trimEnd()}` : `${m[1]}${value}${closing}${m[3].trim() === "" ? "" : ` ${m[3].trim()}`}`;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// sameTarget(a, b): whether two Target values name the same file; written text compared case-insensitively by default.
|
|
210
|
+
function write(text, spec, head, given, problems, sameTarget = (a, b) => norm(a) === norm(b)) {
|
|
211
|
+
const lines = splitLines(text);
|
|
212
|
+
const fileEol = lines.find((l) => l.eol !== "")?.eol ?? "\n";
|
|
213
|
+
const vis = visibleLines(lines);
|
|
214
|
+
const ranges = sectionRanges(vis, spec.section);
|
|
215
|
+
const entries = ranges.flatMap((r) => scanEntries(vis, r, spec));
|
|
216
|
+
// An entry is its head, its piece and its Target: the same rule on two files of one piece is two entries.
|
|
217
|
+
const hit = entries.find((e) => norm(e.value) === norm(head) && norm(e.fields.piece?.value) === norm(given.piece) && sameTarget(e.fields.target?.value, given.target));
|
|
218
|
+
const keys = spec.fields.filter((k) => given[k] !== undefined);
|
|
219
|
+
|
|
220
|
+
if (hit) {
|
|
221
|
+
if (problems.length > 0) {
|
|
222
|
+
throw new LogInputError(problems);
|
|
223
|
+
}
|
|
224
|
+
const added = [];
|
|
225
|
+
for (const key of keys) {
|
|
226
|
+
if (key === "piece") {
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
const have = hit.fields[key];
|
|
230
|
+
// The same file written another way keeps the person's text.
|
|
231
|
+
if (key === "target" && have !== undefined && sameTarget(have.value, given[key])) {
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
if (have === undefined) {
|
|
235
|
+
added.push(key);
|
|
236
|
+
} else if (have.value !== given[key]) {
|
|
237
|
+
lines[have.line].text = withValue(lines[have.line].text, key, given[key]);
|
|
238
|
+
// The parser reads one line of a value, so wrapped value text and a person's note look the same: never delete either.
|
|
239
|
+
if (continuation(lines, vis, have.line, hit.end).length > 0) {
|
|
240
|
+
throw new LogInputError(["Nothing was written: the field continues on lines Lazuli cannot safely replace."]);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
if (added.length > 0) {
|
|
245
|
+
const last = Math.max(hit.head, ...Object.values(hit.fields).map((f) => f.line));
|
|
246
|
+
const sample = Object.values(hit.fields)[0]?.line;
|
|
247
|
+
const indent = sample === undefined ? `${leading(lines[hit.head].text)} ` : leading(lines[sample].text);
|
|
248
|
+
insertAfter(lines, last, added.map((k) => `${indent}${fieldLine(k, given[k])}`), fileEol);
|
|
249
|
+
}
|
|
250
|
+
return lines.filter((l) => !l.gone).map((l) => l.text + l.eol).join("");
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const absent = (k) => given[k] === undefined || given[k] === "";
|
|
254
|
+
if (spec === BREAKS && (absent("effect") || absent("check"))) {
|
|
255
|
+
problems.push("A new break needs an effect and a check.");
|
|
256
|
+
}
|
|
257
|
+
if (spec === BREAKS && absent("status")) {
|
|
258
|
+
problems.push("A new break needs a status.");
|
|
259
|
+
}
|
|
260
|
+
if (spec === DECISIONS && (absent("goal") || absent("rule") || absent("measure"))) {
|
|
261
|
+
problems.push("A new decision needs a goal, a rule and a measure.");
|
|
262
|
+
}
|
|
263
|
+
if (problems.length > 0) {
|
|
264
|
+
throw new LogInputError(problems);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (entries.length > 0) {
|
|
268
|
+
// Same indent as the entries already there, so the base indent the parser reads does not move.
|
|
269
|
+
const prev = entries[entries.length - 1];
|
|
270
|
+
const sample = Object.values(prev.fields)[0]?.line;
|
|
271
|
+
const headIndent = leading(lines[prev.head].text);
|
|
272
|
+
const fieldIndent = sample === undefined ? `${headIndent} ` : leading(lines[sample].text);
|
|
273
|
+
const block = [`${headIndent}${fieldLine(spec.open, head)}`, ...keys.map((k) => `${fieldIndent}${fieldLine(k, given[k])}`)];
|
|
274
|
+
// After the notes the person wrote under the previous entry, not between its last bullet and them.
|
|
275
|
+
let after = prev.last;
|
|
276
|
+
while (after + 1 < prev.end && lines[after + 1].text.trim() !== "" && /^[ \t]/.test(lines[after + 1].text) && readBullet(vis[after + 1]) === null) {
|
|
277
|
+
after++;
|
|
278
|
+
}
|
|
279
|
+
insertAfter(lines, after, block, fileEol);
|
|
280
|
+
return lines.map((l) => l.text + l.eol).join("");
|
|
281
|
+
}
|
|
282
|
+
const block = [fieldLine(spec.open, head), ...keys.map((k) => ` ${fieldLine(k, given[k])}`)];
|
|
283
|
+
if (ranges.length > 0) {
|
|
284
|
+
const range = ranges[ranges.length - 1];
|
|
285
|
+
let k = range.start - 1;
|
|
286
|
+
for (let j = range.end - 1; j >= range.start; j--) {
|
|
287
|
+
if (lines[j].text.trim() !== "") {
|
|
288
|
+
k = j;
|
|
289
|
+
break;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
insertAfter(lines, k, ["", ...block], fileEol);
|
|
293
|
+
} else if (lines.length === 0) {
|
|
294
|
+
lines.push(...[`## ${spec.heading}`, "", ...block].map((t) => ({ text: t, eol: fileEol })));
|
|
295
|
+
} else {
|
|
296
|
+
const spacer = lines[lines.length - 1].text.trim() === "" ? [] : [""];
|
|
297
|
+
insertAfter(lines, lines.length - 1, [...spacer, `## ${spec.heading}`, "", ...block], fileEol);
|
|
298
|
+
}
|
|
299
|
+
return lines.map((l) => l.text + l.eol).join("");
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
export function writeBreak(text, brk, { sameTarget } = {}) {
|
|
303
|
+
const problems = [];
|
|
304
|
+
const rule = clean(brk.rule ?? "", "Rule", problems);
|
|
305
|
+
const piece = clean(brk.piece ?? "", "Piece", problems);
|
|
306
|
+
if (rule === "" || piece === "") {
|
|
307
|
+
problems.push("A break needs a rule and a piece.");
|
|
308
|
+
}
|
|
309
|
+
const given = { piece };
|
|
310
|
+
for (const key of ["target", "effect", "check", "status"]) {
|
|
311
|
+
if (brk[key] !== undefined) {
|
|
312
|
+
given[key] = clean(brk[key], label(key), problems);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
if (given.status !== undefined && given.status !== "" && !STATUSES.includes(given.status)) {
|
|
316
|
+
problems.push("Status must be trying, kept or reverted.");
|
|
317
|
+
}
|
|
318
|
+
return write(text, BREAKS, rule, given, problems, sameTarget);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
export function writeDecision(text, d, { sameTarget } = {}) {
|
|
322
|
+
const problems = [];
|
|
323
|
+
const decision = clean(d.decision ?? "", "Decision", problems);
|
|
324
|
+
const piece = clean(d.piece ?? "", "Piece", problems);
|
|
325
|
+
if (decision === "" || piece === "") {
|
|
326
|
+
problems.push("A decision needs a decision text and a piece.");
|
|
327
|
+
}
|
|
328
|
+
const given = { piece };
|
|
329
|
+
for (const key of ["target", "goal", "rule", "measure"]) {
|
|
330
|
+
if (d[key] !== undefined) {
|
|
331
|
+
given[key] = clean(d[key], label(key), problems);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
return write(text, DECISIONS, decision, given, problems, sameTarget);
|
|
335
|
+
}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { toUrl } from "../browser.mjs";
|
|
5
|
+
import { isOptionsGoal } from "./lazuli-md.mjs";
|
|
6
|
+
|
|
7
|
+
// A page identity string: file URLs decoded, web URLs by origin + path (no trailing slash) + query.
|
|
8
|
+
export function identity(target, cwd) {
|
|
9
|
+
const url = toUrl(target, cwd);
|
|
10
|
+
if (/^file:/i.test(url)) {
|
|
11
|
+
let decoded = url;
|
|
12
|
+
try {
|
|
13
|
+
decoded = decodeURIComponent(url);
|
|
14
|
+
} catch {
|
|
15
|
+
// keep the raw URL when it holds a stray percent sign
|
|
16
|
+
}
|
|
17
|
+
return process.platform === "win32" ? decoded.toLowerCase() : decoded;
|
|
18
|
+
}
|
|
19
|
+
try {
|
|
20
|
+
const u = new URL(url);
|
|
21
|
+
return `${u.origin.toLowerCase()}${u.pathname.replace(/\/+$/, "")}${u.search}`;
|
|
22
|
+
} catch {
|
|
23
|
+
return url;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function sameTarget(a, b, cwd) {
|
|
28
|
+
if (typeof a !== "string" || typeof b !== "string" || a.trim() === "" || b.trim() === "") {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
return identity(a.trim(), cwd) === identity(b.trim(), cwd);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const KEY_BASE_MAX = 40;
|
|
35
|
+
|
|
36
|
+
// A folder-safe key for a page: its last path segment (the host for a URL root) plus a short hash of its
|
|
37
|
+
// identity. Every written form that sameTarget treats as equal gives the same key.
|
|
38
|
+
export function targetKey(target, cwd) {
|
|
39
|
+
const text = String(target).trim();
|
|
40
|
+
const hash = createHash("sha1").update(identity(text, cwd)).digest("hex").slice(0, 10);
|
|
41
|
+
let base = "";
|
|
42
|
+
try {
|
|
43
|
+
const u = new URL(toUrl(text, cwd));
|
|
44
|
+
const last = u.pathname.split("/").filter((p) => p !== "").pop();
|
|
45
|
+
base = last ?? (u.protocol === "file:" ? "" : u.hostname);
|
|
46
|
+
try {
|
|
47
|
+
base = decodeURIComponent(base);
|
|
48
|
+
} catch {
|
|
49
|
+
// keep the raw segment when it holds a stray percent sign
|
|
50
|
+
}
|
|
51
|
+
} catch {
|
|
52
|
+
// an unparseable target keeps the generic base
|
|
53
|
+
}
|
|
54
|
+
const slug = base.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, KEY_BASE_MAX).replace(/-+$/, "");
|
|
55
|
+
return `${slug || "page"}-${hash}`;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// A path relative to cwd with forward slashes and no leading ./, or null for a URL target.
|
|
59
|
+
export function relativeFile(target, cwd) {
|
|
60
|
+
const url = toUrl(String(target).trim(), cwd);
|
|
61
|
+
if (!/^file:/i.test(url)) {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
let file;
|
|
65
|
+
try {
|
|
66
|
+
file = fileURLToPath(url);
|
|
67
|
+
} catch {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
return path.relative(path.resolve(cwd), file).split(path.sep).join("/");
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// The Target glob of an options goal against a local file: * matches any run of characters except /.
|
|
74
|
+
function globMatches(glob, target, cwd) {
|
|
75
|
+
const rel = relativeFile(target, cwd);
|
|
76
|
+
if (rel === null) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
const pattern = path.posix.normalize(glob.trim().replace(/\\/g, "/")).replace(/^\.\//, "");
|
|
80
|
+
const source = pattern
|
|
81
|
+
.split("*")
|
|
82
|
+
.map((p) => p.replace(/[.+?^${}()|[\]\\]/g, "\\$&"))
|
|
83
|
+
.join("[^/]*");
|
|
84
|
+
return new RegExp(`^${source}$`, process.platform === "win32" ? "i" : "").test(rel);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// The Target of a break or decision as record-break and record-decision write it: a local file relative to cwd
|
|
88
|
+
// with forward slashes and no leading ./, a URL as written.
|
|
89
|
+
export function entryTargetForm(target, cwd) {
|
|
90
|
+
return relativeFile(target, cwd) ?? String(target).trim();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Two optional entry Targets name the same file: both absent, or the same page.
|
|
94
|
+
export function sameEntryTarget(a, b, cwd) {
|
|
95
|
+
const blank = (t) => t === undefined || t === null || String(t).trim() === "";
|
|
96
|
+
return blank(a) || blank(b) ? blank(a) && blank(b) : sameTarget(a, b, cwd);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// The goals of this page in file order: an exact Target, or the glob of an options goal that covers it.
|
|
100
|
+
export function matchGoals(md, target, cwd) {
|
|
101
|
+
return (md?.goals ?? []).filter((g) => g.target && (isOptionsGoal(g) ? globMatches(g.target, target, cwd) : sameTarget(g.target, target, cwd)));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const norm = (s) => String(s ?? "").trim().toLowerCase();
|
|
105
|
+
|
|
106
|
+
// Applies the declared breaks of the goals of this page to the findings. `unapplied` lists the breaks
|
|
107
|
+
// that did nothing and why: any break whose rule id is not in knownIds, and each break of a goal of this
|
|
108
|
+
// page that was not applied. A break of another page, or one whose Target names another file, is not listed.
|
|
109
|
+
export function applyBreaks(findings, breaks, goals, rules, knownIds, target, cwd) {
|
|
110
|
+
const pieces = new Set(goals.map((g) => norm(g.piece)));
|
|
111
|
+
const known = knownIds ? new Set([...knownIds].map(norm)) : null;
|
|
112
|
+
const here = (b) => !b.target || sameTarget(b.target, target, cwd);
|
|
113
|
+
const live = breaks.filter((b) => (b.status === "kept" || b.status === "trying") && pieces.has(norm(b.piece)) && here(b));
|
|
114
|
+
const applied = new Set();
|
|
115
|
+
const out = findings.map((f) => {
|
|
116
|
+
if (f.level !== "fail" && f.level !== "hint") {
|
|
117
|
+
return f;
|
|
118
|
+
}
|
|
119
|
+
const brk = live.find((b) => norm(b.rule) === norm(f.ruleId));
|
|
120
|
+
if (!brk) {
|
|
121
|
+
return f;
|
|
122
|
+
}
|
|
123
|
+
const rule = rules.find((r) => norm(r.id) === norm(f.ruleId));
|
|
124
|
+
if (rule?.origin === "norm") {
|
|
125
|
+
return { ...f, evidence: [...(f.evidence ?? []), `A declared break on ${brk.piece} does not waive a norm, so this stays a fail and the break was not applied.`] };
|
|
126
|
+
}
|
|
127
|
+
applied.add(brk);
|
|
128
|
+
return {
|
|
129
|
+
...f,
|
|
130
|
+
level: "break",
|
|
131
|
+
message: [`Declared break (${brk.status}):`, brk.effect, brk.check ? `Check: ${brk.check}` : ""].filter((p) => p !== "").join(" "),
|
|
132
|
+
evidence: [f.message, ...(f.evidence ?? [])],
|
|
133
|
+
};
|
|
134
|
+
});
|
|
135
|
+
const appliedRules = new Set([...applied].map((b) => norm(b.rule)));
|
|
136
|
+
const unapplied = [];
|
|
137
|
+
for (const b of breaks) {
|
|
138
|
+
let reason = null;
|
|
139
|
+
if (known && !known.has(norm(b.rule))) {
|
|
140
|
+
reason = "unknown rule id";
|
|
141
|
+
} else if (!pieces.has(norm(b.piece)) || !here(b)) {
|
|
142
|
+
continue;
|
|
143
|
+
} else if (b.status === "reverted") {
|
|
144
|
+
reason = "status is reverted";
|
|
145
|
+
} else if (b.status !== "kept" && b.status !== "trying") {
|
|
146
|
+
reason = "no status";
|
|
147
|
+
} else if (rules.find((r) => norm(r.id) === norm(b.rule))?.origin === "norm") {
|
|
148
|
+
reason = "a norm is never waived";
|
|
149
|
+
} else if (!applied.has(b) && !appliedRules.has(norm(b.rule))) {
|
|
150
|
+
const rule = rules.find((r) => norm(r.id) === norm(b.rule));
|
|
151
|
+
reason = rule && !rule.measure ? "not measured by check-design" : "no fail or hint for this rule on this page";
|
|
152
|
+
}
|
|
153
|
+
if (reason) {
|
|
154
|
+
unapplied.push({ rule: b.rule, piece: b.piece, reason });
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return { findings: out, applied: [...applied], unapplied };
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Applies the declared moves of the goals of this page, after applyBreaks. A hint whose rule lists
|
|
161
|
+
// declared_by is explained by a decision "<move> followed|broken" or a live break (trying or kept) on the
|
|
162
|
+
// move, of a goal of this page, with no Target or this Target. Only where the finding measured the move
|
|
163
|
+
// (moves[move].holds) does the hint become level declared; elsewhere it stays a hint and says why.
|
|
164
|
+
// The original message always stays as the first evidence line of a declared finding.
|
|
165
|
+
export function applyDeclarations(findings, md, goals, rules, target, cwd) {
|
|
166
|
+
const pieces = new Set(goals.map((g) => norm(g.piece)));
|
|
167
|
+
const here = (e) => pieces.has(norm(e.piece)) && (!e.target || sameTarget(e.target, target, cwd));
|
|
168
|
+
const declarationOf = (move) => {
|
|
169
|
+
const d = (md?.decisions ?? []).find((x) => norm(x.rule) === norm(move) && (x.action === "followed" || x.action === "broken") && here(x));
|
|
170
|
+
if (d) {
|
|
171
|
+
return { piece: d.piece, by: "decision", text: d.decision };
|
|
172
|
+
}
|
|
173
|
+
const b = (md?.breaks ?? []).find((x) => norm(x.rule) === norm(move) && (x.status === "trying" || x.status === "kept") && here(x));
|
|
174
|
+
return b ? { piece: b.piece, by: "break", text: b.effect } : null;
|
|
175
|
+
};
|
|
176
|
+
const applied = [];
|
|
177
|
+
const out = findings.map((f) => {
|
|
178
|
+
if (f.level !== "hint") {
|
|
179
|
+
return f;
|
|
180
|
+
}
|
|
181
|
+
const rule = rules.find((r) => norm(r.id) === norm(f.ruleId));
|
|
182
|
+
for (const move of rule?.declared_by ?? []) {
|
|
183
|
+
const found = declarationOf(move);
|
|
184
|
+
if (!found) {
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
const read = f.moves?.[move];
|
|
188
|
+
if (!read) {
|
|
189
|
+
return { ...f, evidence: [`${move} is declared for ${found.piece}, but the move was not measured here.`, ...(f.evidence ?? [])] };
|
|
190
|
+
}
|
|
191
|
+
if (!read.holds) {
|
|
192
|
+
return { ...f, evidence: [`${move} is declared for ${found.piece}, but here ${read.detail}, so it reads as the move's excess, not the move.`, ...(f.evidence ?? [])] };
|
|
193
|
+
}
|
|
194
|
+
applied.push({ ruleId: f.ruleId, move, piece: found.piece, width: f.width, by: found.by, text: found.text });
|
|
195
|
+
return { ...f, level: "declared", message: `Declared move ${move} (${found.text}). Measured: ${read.detail}.`, evidence: [f.message, ...(f.evidence ?? [])] };
|
|
196
|
+
}
|
|
197
|
+
return f;
|
|
198
|
+
});
|
|
199
|
+
return { findings: out, applied };
|
|
200
|
+
}
|