musubi-cli 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.ja.md +137 -0
- package/README.md +127 -2
- package/dist/add.js +75 -0
- package/dist/check.js +123 -0
- package/dist/cli.js +58 -0
- package/dist/definition.js +17 -0
- package/dist/fingerprint.js +16 -0
- package/dist/graph.js +263 -0
- package/dist/json.js +131 -0
- package/dist/report.js +26 -0
- package/dist/rules.js +154 -0
- package/dist/semver.js +42 -0
- package/dist/skills.js +180 -0
- package/package.json +42 -4
- package/schema/2026-11-19/check.schema.json +68 -0
- package/schema/2026-11-19/musubi.schema.json +77 -0
- package/schema/2026-11-20/check.schema.json +68 -0
- package/schema/2026-11-20/musubi.schema.json +81 -0
- package/spec/musubi.ja.md +261 -0
- package/spec/musubi.md +245 -0
package/dist/graph.js
ADDED
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import { writeFileSync } from "node:fs";
|
|
2
|
+
import { load } from "./check.js";
|
|
3
|
+
import { Report } from "./report.js";
|
|
4
|
+
import { format } from "./semver.js";
|
|
5
|
+
const escape = (text) => text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
6
|
+
// Inside a quoted attribute only "&", "<", and the quote need escaping, so edges keep their "->".
|
|
7
|
+
const attribute = (text) => text.replace(/&/g, "&").replace(/</g, "<").replace(/"/g, """);
|
|
8
|
+
const NODE_W = 232;
|
|
9
|
+
const NODE_H = 64;
|
|
10
|
+
const GAP_X = 160;
|
|
11
|
+
const GAP_Y = 26;
|
|
12
|
+
const PAD = 24;
|
|
13
|
+
// Labels are drawn in a monospace face at this size, and each declares its width as textLength,
|
|
14
|
+
// so the room left for them does not depend on the font the viewer has.
|
|
15
|
+
const LABEL_SIZE = 11;
|
|
16
|
+
const LABEL_CHAR = LABEL_SIZE * 0.6;
|
|
17
|
+
const LABEL_INSET = 12;
|
|
18
|
+
const LABEL_RISE = 8;
|
|
19
|
+
const LABEL_STEP = 15;
|
|
20
|
+
const BEND_ROOM = 96;
|
|
21
|
+
// A line leaves its box from a small round port and ends in a ">" head just left of the box it points to.
|
|
22
|
+
const PORT_R = 3.5;
|
|
23
|
+
const HEAD_GAP = 3;
|
|
24
|
+
const HEAD_LEN = 7;
|
|
25
|
+
const HEAD_HALF = 5.5;
|
|
26
|
+
// Lines into one box arrive this far apart, kept this far inside the box's top and bottom.
|
|
27
|
+
const ENTRY_GAP = 12;
|
|
28
|
+
const ENTRY_MARGIN = 10;
|
|
29
|
+
// The light runs along each line once a loop. Its trail is three strokes, measured in hundredths of
|
|
30
|
+
// the line, so every line takes the same time from end to end. The light travels for TRAVEL of the
|
|
31
|
+
// loop, reaches the head at ARRIVE, and the head lights up as it arrives.
|
|
32
|
+
const LOOP = "2.4s";
|
|
33
|
+
const TAILS = [
|
|
34
|
+
[26, 5, 0.14],
|
|
35
|
+
[15, 3, 0.4],
|
|
36
|
+
[6, 2.2, 0.95],
|
|
37
|
+
];
|
|
38
|
+
const TRAVEL = 0.8;
|
|
39
|
+
const REACH = 100 + TAILS[0][0];
|
|
40
|
+
const ARRIVE = (TRAVEL * 100) / REACH;
|
|
41
|
+
const labelWidth = (label) => Math.ceil(label.length * LABEL_CHAR);
|
|
42
|
+
const num = (value) => String(Math.round(value * 100) / 100);
|
|
43
|
+
const times = (...values) => values.map((v) => String(Math.round(v * 1000) / 1000)).join(";");
|
|
44
|
+
// Ids of the lines, so the light can follow them. The prefix comes from the edges, so two graphs on
|
|
45
|
+
// one page do not share ids and the same graph always gets the same ids.
|
|
46
|
+
function idPrefix(edges) {
|
|
47
|
+
let hash = 0x811c9dc5;
|
|
48
|
+
for (const char of edges.map((e) => e.key).join("\n")) {
|
|
49
|
+
hash ^= char.codePointAt(0);
|
|
50
|
+
hash = Math.imul(hash, 0x01000193) >>> 0;
|
|
51
|
+
}
|
|
52
|
+
return `musubi-${hash.toString(36)}`;
|
|
53
|
+
}
|
|
54
|
+
// The line, its moving light, its head, and its port. The line comes first, so readers of the SVG
|
|
55
|
+
// that take an edge's first path get the line.
|
|
56
|
+
function edgeLight(id, d, start, tip) {
|
|
57
|
+
const loop = `dur="${LOOP}" repeatCount="indefinite"`;
|
|
58
|
+
const tails = TAILS.map(([length, width, opacity]) => `<path class="tail" d="${d}" pathLength="100" stroke-width="${width}" opacity="${opacity}" stroke-dasharray="${length} 300">` +
|
|
59
|
+
`<animate attributeName="stroke-dashoffset" values="${length};${length - REACH};${length - REACH}" keyTimes="${times(0, TRAVEL, 1)}" ${loop}/></path>`).join("");
|
|
60
|
+
const [tx, ty] = tip;
|
|
61
|
+
const head = `M${num(tx - HEAD_LEN)},${num(ty - HEAD_HALF)} L${num(tx)},${num(ty)} L${num(tx - HEAD_LEN)},${num(ty + HEAD_HALF)}`;
|
|
62
|
+
const flash = `<animate attributeName="opacity" values="0;0;1;0;0" keyTimes="${times(0, ARRIVE - 0.035, ARRIVE + 0.015, ARRIVE + 0.225, 1)}" ${loop}/>`;
|
|
63
|
+
return [
|
|
64
|
+
`<path class="line" id="${id}" d="${d}"/>`,
|
|
65
|
+
tails,
|
|
66
|
+
`<path class="chevron" d="${head}"/>`,
|
|
67
|
+
`<path class="chevron flash" d="${head}">${flash}</path>`,
|
|
68
|
+
`<circle class="dot" r="3"><animateMotion ${loop} calcMode="linear" keyPoints="0;1;1" keyTimes="${times(0, ARRIVE, 1)}"><mpath href="#${id}"/></animateMotion>`,
|
|
69
|
+
`<animate attributeName="opacity" values="1;1;0;0" keyTimes="${times(0, ARRIVE, ARRIVE + 0.025, 1)}" ${loop}/></circle>`,
|
|
70
|
+
`<circle class="port" cx="${num(start[0])}" cy="${num(start[1])}" r="${PORT_R}"/>`,
|
|
71
|
+
].join("");
|
|
72
|
+
}
|
|
73
|
+
// Draws each node as a box and each edge as an arrow from the follower to the provider, as one SVG.
|
|
74
|
+
// A node's column is the length of the longest chain of nodes that follow it, so providers sit to the
|
|
75
|
+
// right of the skills that follow them and every arrow points right.
|
|
76
|
+
// Every arrow leaves its box in a straight line that runs past the longest label leaving that column,
|
|
77
|
+
// and only bends after it, so no line crosses a label. Labels of one box stack upward over its line.
|
|
78
|
+
export function graphSvg(nodes, edges) {
|
|
79
|
+
const depth = new Map();
|
|
80
|
+
const column = (id, seen) => {
|
|
81
|
+
if (depth.has(id))
|
|
82
|
+
return depth.get(id);
|
|
83
|
+
if (seen.has(id))
|
|
84
|
+
return 0;
|
|
85
|
+
seen.add(id);
|
|
86
|
+
const followers = edges.filter((e) => e.to === id).map((e) => column(e.from, seen) + 1);
|
|
87
|
+
const value = followers.length > 0 ? Math.max(...followers) : 0;
|
|
88
|
+
depth.set(id, value);
|
|
89
|
+
return value;
|
|
90
|
+
};
|
|
91
|
+
const placed = new Map();
|
|
92
|
+
const rows = new Map();
|
|
93
|
+
for (const node of nodes) {
|
|
94
|
+
const c = column(node.id, new Set());
|
|
95
|
+
const row = rows.get(c) ?? 0;
|
|
96
|
+
rows.set(c, row + 1);
|
|
97
|
+
placed.set(node.id, { node, column: c, row });
|
|
98
|
+
}
|
|
99
|
+
const drawn = edges.filter((e) => placed.has(e.from) && placed.has(e.to));
|
|
100
|
+
// The straight run out of a column ends past the widest label that leaves any box in it.
|
|
101
|
+
const run = new Map();
|
|
102
|
+
const stack = new Map();
|
|
103
|
+
for (const e of drawn) {
|
|
104
|
+
const c = placed.get(e.from).column;
|
|
105
|
+
run.set(c, Math.max(run.get(c) ?? 0, LABEL_INSET * 2 + labelWidth(e.label)));
|
|
106
|
+
stack.set(e.from, (stack.get(e.from) ?? 0) + 1);
|
|
107
|
+
}
|
|
108
|
+
const gap = Math.max(GAP_X, ...[...run.values()].map((r) => r + BEND_ROOM));
|
|
109
|
+
const stacked = Math.max(1, ...stack.values());
|
|
110
|
+
// Room above the first row for the highest label of a box with many arrows.
|
|
111
|
+
const head = PAD + Math.max(0, LABEL_RISE + LABEL_SIZE + (stacked - 1) * LABEL_STEP - (PAD + NODE_H / 2));
|
|
112
|
+
const columns = Math.max(1, ...[...placed.values()].map((p) => p.column + 1));
|
|
113
|
+
const maxRows = Math.max(1, ...rows.values());
|
|
114
|
+
const width = PAD * 2 + columns * NODE_W + (columns - 1) * gap;
|
|
115
|
+
const height = head + PAD + maxRows * NODE_H + (maxRows - 1) * GAP_Y;
|
|
116
|
+
// Columns with fewer boxes are centred against the tallest column.
|
|
117
|
+
const top = (c) => (height - head - PAD - ((rows.get(c) ?? 1) * NODE_H + ((rows.get(c) ?? 1) - 1) * GAP_Y)) / 2;
|
|
118
|
+
const x = (p) => PAD + p.column * (NODE_W + gap);
|
|
119
|
+
const y = (p) => head + top(p.column) + p.row * (NODE_H + GAP_Y);
|
|
120
|
+
// Lines into one box arrive at different heights, in the order of the boxes they come from, so their
|
|
121
|
+
// heads do not sit on one point and the lines do not cross on the way in.
|
|
122
|
+
const middle = (id) => y(placed.get(id)) + NODE_H / 2;
|
|
123
|
+
const entry = new Map();
|
|
124
|
+
for (const to of new Set(drawn.map((e) => e.to))) {
|
|
125
|
+
const into = drawn.filter((e) => e.to === to).sort((a, b) => middle(a.from) - middle(b.from) || drawn.indexOf(a) - drawn.indexOf(b));
|
|
126
|
+
const step = into.length > 1 ? Math.min(ENTRY_GAP, (NODE_H - 2 * ENTRY_MARGIN) / (into.length - 1)) : 0;
|
|
127
|
+
into.forEach((e, i) => entry.set(e, (i - (into.length - 1) / 2) * step));
|
|
128
|
+
}
|
|
129
|
+
const prefix = idPrefix(drawn);
|
|
130
|
+
const seen = new Map();
|
|
131
|
+
const edgeSvg = drawn
|
|
132
|
+
.map((e, i) => {
|
|
133
|
+
const a = placed.get(e.from);
|
|
134
|
+
const b = placed.get(e.to);
|
|
135
|
+
const x1 = x(a) + NODE_W;
|
|
136
|
+
const y1 = y(a) + NODE_H / 2;
|
|
137
|
+
const sx = x1 + PORT_R;
|
|
138
|
+
const xs = x1 + run.get(a.column);
|
|
139
|
+
const tip = x(b) - HEAD_GAP;
|
|
140
|
+
const x2 = tip - 0.5;
|
|
141
|
+
const y2 = y(b) + NODE_H / 2 + entry.get(e);
|
|
142
|
+
const k = seen.get(e.from) ?? 0;
|
|
143
|
+
seen.set(e.from, k + 1);
|
|
144
|
+
const straight = Math.abs(y1 - y2) < 0.01;
|
|
145
|
+
// A box's only line, when it does not bend, carries its label over its middle. Every other label
|
|
146
|
+
// sits over the straight run its box's lines share, before any of them bends.
|
|
147
|
+
const alone = straight && stack.get(e.from) === 1;
|
|
148
|
+
const mid = (xs + x2) / 2;
|
|
149
|
+
const d = alone
|
|
150
|
+
? `M${num(sx)},${num(y1)} H${num(x2)}`
|
|
151
|
+
: straight
|
|
152
|
+
? `M${num(sx)},${num(y1)} H${num(xs)} H${num(x2)}`
|
|
153
|
+
: `M${num(sx)},${num(y1)} H${num(xs)} C${num(mid)},${num(y1)} ${num(mid)},${num(y2)} ${num(x2)},${num(y2)}`;
|
|
154
|
+
const centre = alone ? (sx + x2) / 2 : (sx + xs) / 2;
|
|
155
|
+
const labelW = labelWidth(e.label);
|
|
156
|
+
return [
|
|
157
|
+
`<g class="edge" data-edge="${attribute(e.key)}">`,
|
|
158
|
+
edgeLight(`${prefix}-${i}`, d, [x1, y1], [tip, y2]),
|
|
159
|
+
`<text class="label" x="${num(centre - labelW / 2)}" y="${num(y1 - LABEL_RISE - k * LABEL_STEP)}" font-size="${LABEL_SIZE}" textLength="${labelW}" lengthAdjust="spacingAndGlyphs">${escape(e.label)}</text>`,
|
|
160
|
+
"</g>",
|
|
161
|
+
].join("");
|
|
162
|
+
})
|
|
163
|
+
.join("\n");
|
|
164
|
+
const nodeSvg = [...placed.values()]
|
|
165
|
+
.map((p) => {
|
|
166
|
+
const n = p.node;
|
|
167
|
+
const classes = ["node", n.missing ? "missing" : "", n.current ? "current" : ""].filter(Boolean).join(" ");
|
|
168
|
+
const cx = num(x(p) + NODE_W / 2);
|
|
169
|
+
const box = [
|
|
170
|
+
`<rect x="${x(p)}" y="${y(p)}" width="${NODE_W}" height="${NODE_H}" rx="10"/>`,
|
|
171
|
+
`<text class="name" x="${cx}" y="${num(y(p) + 22)}" text-anchor="middle" dominant-baseline="central">${escape(n.name)}</text>`,
|
|
172
|
+
`<text class="detail" x="${cx}" y="${num(y(p) + 43)}" text-anchor="middle" dominant-baseline="central">${escape(n.detail)}</text>`,
|
|
173
|
+
].join("");
|
|
174
|
+
const inner = n.href ? `<a href="${attribute(n.href)}">${box}</a>` : box;
|
|
175
|
+
return `<g class="${classes}" data-skill="${attribute(n.id)}">${inner}</g>`;
|
|
176
|
+
})
|
|
177
|
+
.join("\n");
|
|
178
|
+
// Lines come after the boxes, so the half of each port that sits over a box's edge stays visible.
|
|
179
|
+
const svg = `<svg width="${width}" height="${height}" viewBox="0 0 ${width} ${height}" role="img" aria-label="Skill dependency graph">
|
|
180
|
+
${nodeSvg}
|
|
181
|
+
${edgeSvg}
|
|
182
|
+
</svg>`;
|
|
183
|
+
return { svg, width, height };
|
|
184
|
+
}
|
|
185
|
+
// Writes one self-contained HTML page that draws each skill as a box and each followed content as
|
|
186
|
+
// an arrow from the consumer to the provider. Providers sit to the right of the skills that follow them.
|
|
187
|
+
export function graph(roots, out, cwd) {
|
|
188
|
+
const report = new Report(cwd);
|
|
189
|
+
const { declarations } = load(roots, report);
|
|
190
|
+
const edges = [];
|
|
191
|
+
const follows = [];
|
|
192
|
+
for (const declaration of declarations.values()) {
|
|
193
|
+
for (const follow of declaration.follows) {
|
|
194
|
+
const range = follow.range?.text ?? "?";
|
|
195
|
+
follows.push({ from: declaration.skill.name, ref: follow.ref, range });
|
|
196
|
+
edges.push({
|
|
197
|
+
from: declaration.skill.name,
|
|
198
|
+
to: follow.skill,
|
|
199
|
+
label: `${follow.content} ${range}`,
|
|
200
|
+
key: `${declaration.skill.name} -> ${follow.ref}`,
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
const names = [...new Set([...declarations.keys(), ...edges.map((e) => e.to)])].sort();
|
|
205
|
+
const nodes = names.map((name) => {
|
|
206
|
+
const provides = [...(declarations.get(name)?.provides.values() ?? [])].map((p) => `${p.name} ${p.version ? format(p.version) : "?"}`);
|
|
207
|
+
const detail = provides.length > 0 ? `provides ${provides.join(", ")}` : declarations.has(name) ? "follows only" : "not installed";
|
|
208
|
+
return { id: name, name, detail, missing: !declarations.has(name) };
|
|
209
|
+
});
|
|
210
|
+
const { svg } = graphSvg(nodes, edges);
|
|
211
|
+
const rowsHtml = follows
|
|
212
|
+
.map((f) => `<tr><td>${escape(f.from)}</td><td>${escape(f.ref)}</td><td>${escape(f.range)}</td></tr>`)
|
|
213
|
+
.join("\n");
|
|
214
|
+
const html = `<!doctype html>
|
|
215
|
+
<html lang="en">
|
|
216
|
+
<head>
|
|
217
|
+
<meta charset="utf-8">
|
|
218
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
219
|
+
<title>Skill dependencies</title>
|
|
220
|
+
<style>
|
|
221
|
+
:root { --bg: #f7f7f5; --fg: #1d1d1b; --muted: #5f5f5a; --box: #ffffff; --line: #8a8a84; --accent: #2f6f5e; }
|
|
222
|
+
@media (prefers-color-scheme: dark) { :root { --bg: #161615; --fg: #ecece8; --muted: #a3a39c; --box: #22221f; --line: #77776f; --accent: #6fbfa6; } }
|
|
223
|
+
body { margin: 0; padding: 24px 16px; background: var(--bg); color: var(--fg); font: 15px/1.5 system-ui, sans-serif; }
|
|
224
|
+
h1 { font-size: 20px; margin: 0 0 4px; }
|
|
225
|
+
p { margin: 0 0 16px; color: var(--muted); }
|
|
226
|
+
.canvas { overflow-x: auto; }
|
|
227
|
+
svg text { fill: var(--fg); font: 13px system-ui, sans-serif; }
|
|
228
|
+
.node rect { fill: var(--box); stroke: var(--accent); stroke-width: 1.5; }
|
|
229
|
+
.node.missing rect { stroke: #c0503a; stroke-dasharray: 5 4; }
|
|
230
|
+
.node .name { font-weight: 600; font-size: 14px; }
|
|
231
|
+
.node .detail { fill: var(--muted); font-size: 12px; }
|
|
232
|
+
.edge .line { fill: none; stroke: var(--line); stroke-width: 1.5; stroke-linecap: round; }
|
|
233
|
+
.edge .tail { fill: none; stroke: var(--accent); stroke-linecap: round; }
|
|
234
|
+
.edge .dot { fill: var(--accent); filter: drop-shadow(0 0 2px var(--accent)) drop-shadow(0 0 5px var(--accent)); }
|
|
235
|
+
.edge .chevron { fill: none; stroke: var(--line); stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
|
|
236
|
+
.edge .chevron.flash { stroke: var(--accent); opacity: 0; filter: drop-shadow(0 0 4px var(--accent)); }
|
|
237
|
+
.edge .port { fill: var(--bg); stroke: var(--line); stroke-width: 1.5; }
|
|
238
|
+
.edge text { fill: var(--muted); font: 11px ui-monospace, "JetBrains Mono", Menlo, monospace; }
|
|
239
|
+
@media (prefers-reduced-motion: reduce) { .edge .tail, .edge .dot, .edge .flash { display: none; } }
|
|
240
|
+
table { border-collapse: collapse; margin-top: 20px; }
|
|
241
|
+
th, td { text-align: left; padding: 6px 14px 6px 0; border-bottom: 1px solid var(--line); }
|
|
242
|
+
</style>
|
|
243
|
+
</head>
|
|
244
|
+
<body>
|
|
245
|
+
<h1>Skill dependencies</h1>
|
|
246
|
+
<p>Each arrow runs from a skill to the content it follows. ${nodes.length} skills, ${follows.length} followed contents.</p>
|
|
247
|
+
<div class="canvas">
|
|
248
|
+
${svg}
|
|
249
|
+
</div>
|
|
250
|
+
<table>
|
|
251
|
+
<thead><tr><th>Skill</th><th>Follows</th><th>Range</th></tr></thead>
|
|
252
|
+
<tbody>
|
|
253
|
+
${rowsHtml}
|
|
254
|
+
</tbody>
|
|
255
|
+
</table>
|
|
256
|
+
</body>
|
|
257
|
+
</html>
|
|
258
|
+
`;
|
|
259
|
+
writeFileSync(out, html);
|
|
260
|
+
report.print();
|
|
261
|
+
process.stderr.write(`musubi graph: wrote ${report.where(out)} (skills=${nodes.length}, follows=${follows.length})\n`);
|
|
262
|
+
return 0;
|
|
263
|
+
}
|
package/dist/json.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// A JSON reader that remembers the line of every key, so problems can name the file and line.
|
|
2
|
+
export class JsonError extends Error {
|
|
3
|
+
line;
|
|
4
|
+
constructor(message, line) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.line = line;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export function pointer(...keys) {
|
|
10
|
+
return keys.map((key) => `/${key.replace(/~/g, "~0").replace(/\//g, "~1")}`).join("");
|
|
11
|
+
}
|
|
12
|
+
export function parseLocated(text) {
|
|
13
|
+
let i = 0;
|
|
14
|
+
let line = 1;
|
|
15
|
+
const lines = new Map([["", 1]]);
|
|
16
|
+
const fail = (message) => {
|
|
17
|
+
throw new JsonError(message, line);
|
|
18
|
+
};
|
|
19
|
+
const space = () => {
|
|
20
|
+
while (i < text.length) {
|
|
21
|
+
const c = text[i];
|
|
22
|
+
if (c === "\n") {
|
|
23
|
+
line += 1;
|
|
24
|
+
i += 1;
|
|
25
|
+
}
|
|
26
|
+
else if (c === " " || c === "\t" || c === "\r") {
|
|
27
|
+
i += 1;
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
const string = () => {
|
|
35
|
+
const start = i;
|
|
36
|
+
i += 1;
|
|
37
|
+
while (i < text.length && text[i] !== '"') {
|
|
38
|
+
if (text[i] === "\n")
|
|
39
|
+
fail("a string runs past the end of the line");
|
|
40
|
+
i += text[i] === "\\" ? 2 : 1;
|
|
41
|
+
}
|
|
42
|
+
if (i >= text.length)
|
|
43
|
+
fail("a string is not closed");
|
|
44
|
+
i += 1;
|
|
45
|
+
try {
|
|
46
|
+
return JSON.parse(text.slice(start, i));
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
return fail("a string has an invalid escape");
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
const value = (at) => {
|
|
53
|
+
space();
|
|
54
|
+
const c = text[i];
|
|
55
|
+
if (c === "{") {
|
|
56
|
+
i += 1;
|
|
57
|
+
const object = {};
|
|
58
|
+
space();
|
|
59
|
+
if (text[i] === "}") {
|
|
60
|
+
i += 1;
|
|
61
|
+
return object;
|
|
62
|
+
}
|
|
63
|
+
for (;;) {
|
|
64
|
+
space();
|
|
65
|
+
if (text[i] !== '"')
|
|
66
|
+
fail("expected a key in double quotes");
|
|
67
|
+
const keyLine = line;
|
|
68
|
+
const key = string();
|
|
69
|
+
if (Object.hasOwn(object, key))
|
|
70
|
+
fail(`the key "${key}" appears twice`);
|
|
71
|
+
space();
|
|
72
|
+
if (text[i] !== ":")
|
|
73
|
+
fail(`expected ":" after "${key}"`);
|
|
74
|
+
i += 1;
|
|
75
|
+
const child = at + pointer(key);
|
|
76
|
+
lines.set(child, keyLine);
|
|
77
|
+
object[key] = value(child);
|
|
78
|
+
space();
|
|
79
|
+
if (text[i] === ",") {
|
|
80
|
+
i += 1;
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
if (text[i] === "}") {
|
|
84
|
+
i += 1;
|
|
85
|
+
return object;
|
|
86
|
+
}
|
|
87
|
+
fail('expected "," or "}"');
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (c === "[") {
|
|
91
|
+
i += 1;
|
|
92
|
+
const array = [];
|
|
93
|
+
space();
|
|
94
|
+
if (text[i] === "]") {
|
|
95
|
+
i += 1;
|
|
96
|
+
return array;
|
|
97
|
+
}
|
|
98
|
+
for (;;) {
|
|
99
|
+
space();
|
|
100
|
+
const child = `${at}/${array.length}`;
|
|
101
|
+
lines.set(child, line);
|
|
102
|
+
array.push(value(child));
|
|
103
|
+
space();
|
|
104
|
+
if (text[i] === ",") {
|
|
105
|
+
i += 1;
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
if (text[i] === "]") {
|
|
109
|
+
i += 1;
|
|
110
|
+
return array;
|
|
111
|
+
}
|
|
112
|
+
fail('expected "," or "]"');
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
if (c === '"')
|
|
116
|
+
return string();
|
|
117
|
+
const literal = /^(?:-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?|true|false|null)/.exec(text.slice(i));
|
|
118
|
+
if (!literal)
|
|
119
|
+
return fail("expected a value");
|
|
120
|
+
i += literal[0].length;
|
|
121
|
+
return JSON.parse(literal[0]);
|
|
122
|
+
};
|
|
123
|
+
const result = value("");
|
|
124
|
+
space();
|
|
125
|
+
if (i < text.length)
|
|
126
|
+
fail("unexpected text after the JSON value");
|
|
127
|
+
return { value: result, lines };
|
|
128
|
+
}
|
|
129
|
+
export function isObject(value) {
|
|
130
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
131
|
+
}
|
package/dist/report.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { relative } from "node:path";
|
|
2
|
+
// Collects problems as `<file>:<line>: <message>` and warnings, with paths relative to the
|
|
3
|
+
// folder the command runs in.
|
|
4
|
+
export class Report {
|
|
5
|
+
problems = [];
|
|
6
|
+
warnings = [];
|
|
7
|
+
cwd;
|
|
8
|
+
constructor(cwd) {
|
|
9
|
+
this.cwd = cwd;
|
|
10
|
+
}
|
|
11
|
+
problem(file, line, message) {
|
|
12
|
+
this.problems.push({ file, line, message });
|
|
13
|
+
}
|
|
14
|
+
warn(message) {
|
|
15
|
+
this.warnings.push(message);
|
|
16
|
+
}
|
|
17
|
+
where(file) {
|
|
18
|
+
return relative(this.cwd, file) || file;
|
|
19
|
+
}
|
|
20
|
+
print() {
|
|
21
|
+
for (const warning of this.warnings)
|
|
22
|
+
process.stderr.write(`warning: ${warning}\n`);
|
|
23
|
+
for (const p of this.problems)
|
|
24
|
+
process.stderr.write(`${this.where(p.file)}:${p.line}: ${p.message}\n`);
|
|
25
|
+
}
|
|
26
|
+
}
|
package/dist/rules.js
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { definitionVersions } from "./definition.js";
|
|
3
|
+
import { isObject, JsonError, parseLocated, pointer } from "./json.js";
|
|
4
|
+
const FIELDS = {
|
|
5
|
+
headings: { headings: "strings" },
|
|
6
|
+
forbid: { text: "strings" },
|
|
7
|
+
"table-columns": { heading: "string", columns: "strings" },
|
|
8
|
+
};
|
|
9
|
+
// Reads and validates a rules file, reporting each mistake at its line. Returns null when it is unusable.
|
|
10
|
+
export function readRules(path, report) {
|
|
11
|
+
let located;
|
|
12
|
+
try {
|
|
13
|
+
located = parseLocated(readFileSync(path, "utf8"));
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
if (error instanceof JsonError)
|
|
17
|
+
report.problem(path, error.line, `not valid JSON: ${error.message}`);
|
|
18
|
+
else
|
|
19
|
+
report.problem(path, 1, "the check file cannot be read");
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
const { value, lines } = located;
|
|
23
|
+
const at = (...keys) => lines.get(pointer(...keys)) ?? 1;
|
|
24
|
+
if (!isObject(value)) {
|
|
25
|
+
report.problem(path, 1, "a check file holds one JSON object");
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
let usable = true;
|
|
29
|
+
for (const key of Object.keys(value)) {
|
|
30
|
+
if (key !== "$schema" && key !== "musubi" && key !== "rules") {
|
|
31
|
+
report.problem(path, at(key), `unknown field "${key}"`);
|
|
32
|
+
usable = false;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (typeof value.musubi !== "string" || !definitionVersions.includes(value.musubi)) {
|
|
36
|
+
report.problem(path, at("musubi"), `"musubi" must name a definition version (${definitionVersions.join(", ")})`);
|
|
37
|
+
usable = false;
|
|
38
|
+
}
|
|
39
|
+
if (!Array.isArray(value.rules)) {
|
|
40
|
+
report.problem(path, at("rules"), '"rules" must be a list of rules');
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
const rules = [];
|
|
44
|
+
value.rules.forEach((rule, index) => {
|
|
45
|
+
const line = lines.get(`/rules/${index}`) ?? 1;
|
|
46
|
+
if (!isObject(rule) || typeof rule.kind !== "string" || !(rule.kind in FIELDS)) {
|
|
47
|
+
report.problem(path, line, `rule ${index + 1} has no known "kind" (${Object.keys(FIELDS).join(", ")})`);
|
|
48
|
+
usable = false;
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const fields = FIELDS[rule.kind];
|
|
52
|
+
for (const key of Object.keys(rule)) {
|
|
53
|
+
if (key !== "kind" && !(key in fields)) {
|
|
54
|
+
report.problem(path, lines.get(`/rules/${index}${pointer(key)}`) ?? line, `rule ${index + 1} has an unknown field "${key}"`);
|
|
55
|
+
usable = false;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
for (const [key, type] of Object.entries(fields)) {
|
|
59
|
+
const field = rule[key];
|
|
60
|
+
const ok = type === "string"
|
|
61
|
+
? typeof field === "string" && field !== ""
|
|
62
|
+
: Array.isArray(field) && field.length > 0 && field.every((item) => typeof item === "string" && item !== "");
|
|
63
|
+
if (!ok) {
|
|
64
|
+
report.problem(path, line, `rule ${index + 1} needs "${key}" as ${type === "string" ? "text" : "a list of text"}`);
|
|
65
|
+
usable = false;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
rules.push(rule);
|
|
69
|
+
});
|
|
70
|
+
return usable ? rules : null;
|
|
71
|
+
}
|
|
72
|
+
// Lines outside fenced code blocks, with their 1-based numbers.
|
|
73
|
+
function prose(text) {
|
|
74
|
+
let fenced = false;
|
|
75
|
+
const out = [];
|
|
76
|
+
text.split("\n").forEach((raw, index) => {
|
|
77
|
+
const line = raw.replace(/\s+$/, "");
|
|
78
|
+
if (line.startsWith("```") || line.startsWith("~~~")) {
|
|
79
|
+
fenced = !fenced;
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
if (!fenced)
|
|
83
|
+
out.push({ number: index + 1, text: line });
|
|
84
|
+
});
|
|
85
|
+
return out;
|
|
86
|
+
}
|
|
87
|
+
const level = (text) => /^(#{1,6}) /.exec(text)?.[1].length ?? 0;
|
|
88
|
+
function cells(row) {
|
|
89
|
+
return row
|
|
90
|
+
.replace(/^\|/, "")
|
|
91
|
+
.replace(/\|$/, "")
|
|
92
|
+
.split("|")
|
|
93
|
+
.map((cell) => cell.trim());
|
|
94
|
+
}
|
|
95
|
+
// Applies the rules to one example. Sections the consumer added are skipped, because each rule
|
|
96
|
+
// looks only for what it requires.
|
|
97
|
+
export function applyRules(rules, text) {
|
|
98
|
+
const lines = prose(text);
|
|
99
|
+
const findings = [];
|
|
100
|
+
for (const rule of rules) {
|
|
101
|
+
if (rule.kind === "headings") {
|
|
102
|
+
let previous = null;
|
|
103
|
+
for (const heading of rule.headings) {
|
|
104
|
+
const found = lines.find((line) => line.text === heading);
|
|
105
|
+
if (!found) {
|
|
106
|
+
findings.push({ line: previous?.line ?? 1, message: `the heading "${heading}" is missing` });
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if (previous && found.number < previous.line) {
|
|
110
|
+
findings.push({ line: found.number, message: `the heading "${heading}" must come after "${previous.heading}"` });
|
|
111
|
+
}
|
|
112
|
+
previous = { heading, line: found.number };
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
else if (rule.kind === "forbid") {
|
|
116
|
+
for (const needle of rule.text) {
|
|
117
|
+
for (const line of lines) {
|
|
118
|
+
if (line.text.includes(needle))
|
|
119
|
+
findings.push({ line: line.number, message: `"${needle}" must not remain` });
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
const start = lines.findIndex((line) => line.text === rule.heading);
|
|
125
|
+
if (start === -1) {
|
|
126
|
+
findings.push({ line: 1, message: `no heading "${rule.heading}" to hold the table` });
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
const depth = level(rule.heading);
|
|
130
|
+
const section = [];
|
|
131
|
+
for (const line of lines.slice(start + 1)) {
|
|
132
|
+
const l = level(line.text);
|
|
133
|
+
if (l > 0 && l <= depth)
|
|
134
|
+
break;
|
|
135
|
+
section.push(line);
|
|
136
|
+
}
|
|
137
|
+
const header = section.findIndex((line, i) => line.text.startsWith("|") && /^\|[-:| ]+\|$/.test(section[i + 1]?.text ?? ""));
|
|
138
|
+
if (header === -1) {
|
|
139
|
+
findings.push({ line: lines[start].number, message: `no table under "${rule.heading}"` });
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
const have = cells(section[header].text);
|
|
143
|
+
for (const column of rule.columns) {
|
|
144
|
+
if (!have.includes(column)) {
|
|
145
|
+
findings.push({
|
|
146
|
+
line: section[header].number,
|
|
147
|
+
message: `the table under "${rule.heading}" has no column "${column}"`,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return findings;
|
|
154
|
+
}
|
package/dist/semver.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// Content versions are MAJOR.MINOR.PATCH, and ranges are `^X.Y.Z`, `~X.Y.Z`, or an exact `X.Y.Z`,
|
|
2
|
+
// read the way npm reads them.
|
|
3
|
+
const VERSION = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/;
|
|
4
|
+
export function parseVersion(text) {
|
|
5
|
+
const match = VERSION.exec(text);
|
|
6
|
+
return match ? [Number(match[1]), Number(match[2]), Number(match[3])] : null;
|
|
7
|
+
}
|
|
8
|
+
export function parseRange(text) {
|
|
9
|
+
const operator = text.startsWith("^") || text.startsWith("~") ? text[0] : "";
|
|
10
|
+
const min = parseVersion(text.slice(operator.length));
|
|
11
|
+
if (!min)
|
|
12
|
+
return null;
|
|
13
|
+
const [major, minor, patch] = min;
|
|
14
|
+
let below;
|
|
15
|
+
if (operator === "^") {
|
|
16
|
+
if (major > 0)
|
|
17
|
+
below = [major + 1, 0, 0];
|
|
18
|
+
else if (minor > 0)
|
|
19
|
+
below = [0, minor + 1, 0];
|
|
20
|
+
else
|
|
21
|
+
below = [0, 0, patch + 1];
|
|
22
|
+
}
|
|
23
|
+
else if (operator === "~") {
|
|
24
|
+
below = [major, minor + 1, 0];
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
below = [major, minor, patch + 1];
|
|
28
|
+
}
|
|
29
|
+
return { text, min, below };
|
|
30
|
+
}
|
|
31
|
+
function compare(a, b) {
|
|
32
|
+
for (let i = 0; i < 3; i += 1)
|
|
33
|
+
if (a[i] !== b[i])
|
|
34
|
+
return a[i] - b[i];
|
|
35
|
+
return 0;
|
|
36
|
+
}
|
|
37
|
+
export function satisfies(version, range) {
|
|
38
|
+
return compare(version, range.min) >= 0 && compare(version, range.below) < 0;
|
|
39
|
+
}
|
|
40
|
+
export function format(version) {
|
|
41
|
+
return version.join(".");
|
|
42
|
+
}
|