okforest 0.5.0 → 0.6.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/dist/cli.mjs +177 -8
- package/package.json +3 -3
package/dist/cli.mjs
CHANGED
|
@@ -6,7 +6,7 @@ import { fileURLToPath } from "node:url";
|
|
|
6
6
|
import { parseArgs, styleText } from "node:util";
|
|
7
7
|
import { buildInventory } from "@okforest/okf";
|
|
8
8
|
import { readTree } from "@okforest/okf/node";
|
|
9
|
-
import { ruleUrl, toReport, validate } from "@okforest/validate";
|
|
9
|
+
import { defaultRules, okfRules, ruleDocs, ruleUrl, toReport, validate } from "@okforest/validate";
|
|
10
10
|
//#region src/io.ts
|
|
11
11
|
/** Exit codes: 0 success, 1 validation errors, 2 usage or read problems. */
|
|
12
12
|
const EXIT = {
|
|
@@ -51,7 +51,7 @@ function formatOption(value) {
|
|
|
51
51
|
}
|
|
52
52
|
//#endregion
|
|
53
53
|
//#region src/commands/init.ts
|
|
54
|
-
const HELP$
|
|
54
|
+
const HELP$4 = `Usage: okforest init [path] --template <name> [--name <name>]
|
|
55
55
|
|
|
56
56
|
Create a new OKF v0.2 tree at path (default: the current directory, which must
|
|
57
57
|
be empty apart from .git) from a starter template. Starter concepts are drafts
|
|
@@ -68,7 +68,7 @@ async function initCommand(args, io) {
|
|
|
68
68
|
name: { type: "string" }
|
|
69
69
|
});
|
|
70
70
|
if (values.help) {
|
|
71
|
-
io.stdout(HELP$
|
|
71
|
+
io.stdout(HELP$4);
|
|
72
72
|
return EXIT.ok;
|
|
73
73
|
}
|
|
74
74
|
const templates = await listTemplates();
|
|
@@ -141,7 +141,7 @@ async function loadTree(path, io) {
|
|
|
141
141
|
}
|
|
142
142
|
//#endregion
|
|
143
143
|
//#region src/commands/inventory.ts
|
|
144
|
-
const HELP$
|
|
144
|
+
const HELP$3 = `Usage: okforest inventory [path] [--format text|json]
|
|
145
145
|
|
|
146
146
|
List every type, tag, and frontmatter key in use in the tree at path (default:
|
|
147
147
|
the current directory), with counts, lifecycle and trust totals, and likely
|
|
@@ -153,7 +153,7 @@ Options
|
|
|
153
153
|
async function inventoryCommand(args, io) {
|
|
154
154
|
const { values, path } = parseCommand(args, { format: { type: "string" } });
|
|
155
155
|
if (values.help) {
|
|
156
|
-
io.stdout(HELP$
|
|
156
|
+
io.stdout(HELP$3);
|
|
157
157
|
return EXIT.ok;
|
|
158
158
|
}
|
|
159
159
|
const format = formatOption(values.format);
|
|
@@ -198,6 +198,170 @@ function inventoryText(inventory, io) {
|
|
|
198
198
|
return `${out.join("\n")}\n`;
|
|
199
199
|
}
|
|
200
200
|
//#endregion
|
|
201
|
+
//#region src/commands/rule.ts
|
|
202
|
+
const HELP$2 = `Usage: okforest rule [rule] [--format text|json]
|
|
203
|
+
|
|
204
|
+
Explain a rule: why it exists, an example that breaks it, what
|
|
205
|
+
\`okforest validate\` reports for it, and the fix. Without a rule, list every
|
|
206
|
+
rule with its severity.
|
|
207
|
+
|
|
208
|
+
Options
|
|
209
|
+
--format text|json Output format (default: text). JSON is for tools and agents.
|
|
210
|
+
|
|
211
|
+
Examples
|
|
212
|
+
okforest rule okf/type
|
|
213
|
+
okforest rule --format json okforest/slug
|
|
214
|
+
`;
|
|
215
|
+
const COLOR$1 = {
|
|
216
|
+
error: "red",
|
|
217
|
+
warning: "yellow",
|
|
218
|
+
info: "cyan"
|
|
219
|
+
};
|
|
220
|
+
/** Text is wrapped to this width, as a terminal shows it comfortably. */
|
|
221
|
+
const WIDTH = 80;
|
|
222
|
+
async function ruleCommand(args, io) {
|
|
223
|
+
const { values, positionals } = parseArgs({
|
|
224
|
+
args: [...args],
|
|
225
|
+
options: {
|
|
226
|
+
format: { type: "string" },
|
|
227
|
+
help: {
|
|
228
|
+
type: "boolean",
|
|
229
|
+
short: "h"
|
|
230
|
+
}
|
|
231
|
+
},
|
|
232
|
+
allowPositionals: true,
|
|
233
|
+
strict: true
|
|
234
|
+
});
|
|
235
|
+
if (values.help) {
|
|
236
|
+
io.stdout(HELP$2);
|
|
237
|
+
return EXIT.ok;
|
|
238
|
+
}
|
|
239
|
+
if (positionals.length > 1) throw new UsageError(`Expected one rule, got ${positionals.length}: ${positionals.join(" ")}`);
|
|
240
|
+
const format = formatOption(values.format);
|
|
241
|
+
const [id] = positionals;
|
|
242
|
+
if (id === void 0) {
|
|
243
|
+
io.stdout(format === "json" ? json(defaultRules.map(describe)) : list(io));
|
|
244
|
+
return EXIT.ok;
|
|
245
|
+
}
|
|
246
|
+
const rule = defaultRules.find((found) => found.id === id);
|
|
247
|
+
if (!rule) {
|
|
248
|
+
const near = closest(id);
|
|
249
|
+
throw new UsageError(`Unknown rule \`${id}\`.${near ? ` Did you mean \`${near}\`?` : ""} Run \`okforest rule\` to list them.`);
|
|
250
|
+
}
|
|
251
|
+
io.stdout(format === "json" ? json(describe(rule)) : explain(rule, io));
|
|
252
|
+
return EXIT.ok;
|
|
253
|
+
}
|
|
254
|
+
/** Which set a rule belongs to. */
|
|
255
|
+
const set = (rule) => okfRules.includes(rule) ? "OKF specification" : "OKForest conventions";
|
|
256
|
+
/** A rule as data: what `--format json` prints. */
|
|
257
|
+
function describe(rule) {
|
|
258
|
+
const docs = ruleDocs[rule.id];
|
|
259
|
+
return {
|
|
260
|
+
id: rule.id,
|
|
261
|
+
severity: rule.severity,
|
|
262
|
+
set: set(rule),
|
|
263
|
+
description: rule.description,
|
|
264
|
+
url: ruleUrl(rule.id),
|
|
265
|
+
...docs && {
|
|
266
|
+
why: docs.why,
|
|
267
|
+
example: docs.example
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
const json = (value) => `${JSON.stringify(value, null, 2)}\n`;
|
|
272
|
+
/** Every rule, by set and then by ID (as the docs list them), with its
|
|
273
|
+
* severity and description. */
|
|
274
|
+
function list(io) {
|
|
275
|
+
const width = Math.max(...defaultRules.map((rule) => rule.id.length));
|
|
276
|
+
const out = [];
|
|
277
|
+
for (const name of ["OKF specification", "OKForest conventions"]) {
|
|
278
|
+
out.push(paint(io, "underline", name));
|
|
279
|
+
const rules = defaultRules.filter((found) => set(found) === name).sort((a, b) => a.id.localeCompare(b.id));
|
|
280
|
+
for (const rule of rules) out.push(` ${rule.id.padEnd(width)} ${paint(io, COLOR$1[rule.severity], rule.severity.padEnd(7))} ${rule.description}`);
|
|
281
|
+
out.push("");
|
|
282
|
+
}
|
|
283
|
+
out.push("Run `okforest rule <rule>` for why a rule exists and how to fix it.");
|
|
284
|
+
return `${out.join("\n")}\n`;
|
|
285
|
+
}
|
|
286
|
+
/** One rule, explained: what the text format prints. */
|
|
287
|
+
function explain(rule, io) {
|
|
288
|
+
const docs = ruleDocs[rule.id];
|
|
289
|
+
const heading = (text) => paint(io, "underline", text);
|
|
290
|
+
const out = [
|
|
291
|
+
`${paint(io, "bold", rule.id)} ${paint(io, COLOR$1[rule.severity], rule.severity)} ${paint(io, "dim", set(rule))}`,
|
|
292
|
+
"",
|
|
293
|
+
...wrap(rule.description, ""),
|
|
294
|
+
""
|
|
295
|
+
];
|
|
296
|
+
const links = [];
|
|
297
|
+
if (docs) {
|
|
298
|
+
out.push(heading("Why"), ...wrap(plain(docs.why, links), " "), "");
|
|
299
|
+
const { file, fails, findings, fixed, fixedFile = file } = docs.example;
|
|
300
|
+
out.push(heading(`Example: ${file} breaks the rule`), ...block(fails), "");
|
|
301
|
+
out.push(heading("okforest validate reports"), ` ${file}`);
|
|
302
|
+
for (const finding of findings) {
|
|
303
|
+
const [at = "", ...message] = finding.split(" ");
|
|
304
|
+
out.push(` ${at === "-:-" ? "" : at} ${paint(io, COLOR$1[rule.severity], rule.severity.padEnd(7))} ${message.join(" ")}`);
|
|
305
|
+
}
|
|
306
|
+
out.push("");
|
|
307
|
+
if (fixed === void 0) out.push(heading("Nothing to fix"), "");
|
|
308
|
+
else {
|
|
309
|
+
const renamed = fixedFile === file ? "" : `, renamed to ${fixedFile}`;
|
|
310
|
+
out.push(heading(`Fixed${renamed}`), ...block(fixed), "");
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
if (links.length > 0) out.push(heading("Links"), ...links.map((url, i) => ` [${i + 1}] ${url}`), "");
|
|
314
|
+
out.push(`Docs: ${ruleUrl(rule.id)}`);
|
|
315
|
+
return `${out.join("\n")}\n`;
|
|
316
|
+
}
|
|
317
|
+
/** Markdown as terminal text: each link becomes `text [n]`, its URL listed
|
|
318
|
+
* under Links; bold is dropped; code spans keep their backticks. */
|
|
319
|
+
function plain(markdown, links) {
|
|
320
|
+
return markdown.replace(/\[([^\]]+)\]\(([^)]+)\)/g, (_match, text, url) => {
|
|
321
|
+
const known = links.indexOf(url);
|
|
322
|
+
return `${text} [${known === -1 ? links.push(url) : known + 1}]`;
|
|
323
|
+
}).replace(/\*\*([^*]+)\*\*/g, "$1");
|
|
324
|
+
}
|
|
325
|
+
/** A paragraph wrapped to WIDTH, each line indented. */
|
|
326
|
+
function wrap(text, indent) {
|
|
327
|
+
const lines = [];
|
|
328
|
+
let line = "";
|
|
329
|
+
for (const word of text.split(/\s+/)) if (line && indent.length + line.length + 1 + word.length > WIDTH) {
|
|
330
|
+
lines.push(`${indent}${line}`);
|
|
331
|
+
line = word;
|
|
332
|
+
} else line = line ? `${line} ${word}` : word;
|
|
333
|
+
if (line) lines.push(`${indent}${line}`);
|
|
334
|
+
return lines;
|
|
335
|
+
}
|
|
336
|
+
/** A file's text, indented as a block. */
|
|
337
|
+
const block = (text) => text.trimEnd().split("\n").map((line) => line ? ` ${line}` : "");
|
|
338
|
+
/** The rule ID nearest to a mistyped one, if any is close: the same name
|
|
339
|
+
* in the other set (`okf/slug` → `okforest/slug`), or one or two edits away. */
|
|
340
|
+
function closest(id) {
|
|
341
|
+
const name = id.split("/").pop() ?? id;
|
|
342
|
+
const sameName = defaultRules.find((rule) => rule.id.split("/").pop() === name);
|
|
343
|
+
if (sameName) return sameName.id;
|
|
344
|
+
let best;
|
|
345
|
+
for (const rule of defaultRules) {
|
|
346
|
+
const distance = edits(id, rule.id);
|
|
347
|
+
if (distance <= 2 && (!best || distance < best.distance)) best = {
|
|
348
|
+
id: rule.id,
|
|
349
|
+
distance
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
return best?.id;
|
|
353
|
+
}
|
|
354
|
+
/** Levenshtein distance. */
|
|
355
|
+
function edits(a, b) {
|
|
356
|
+
let previous = Array.from({ length: b.length + 1 }, (_, i) => i);
|
|
357
|
+
for (let i = 1; i <= a.length; i++) {
|
|
358
|
+
const current = [i];
|
|
359
|
+
for (let j = 1; j <= b.length; j++) current[j] = Math.min(previous[j] + 1, current[j - 1] + 1, previous[j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
360
|
+
previous = current;
|
|
361
|
+
}
|
|
362
|
+
return previous[b.length];
|
|
363
|
+
}
|
|
364
|
+
//#endregion
|
|
201
365
|
//#region src/commands/validate.ts
|
|
202
366
|
const HELP$1 = `Usage: okforest validate [path] [--format text|json]
|
|
203
367
|
|
|
@@ -252,18 +416,19 @@ function toText(result, io) {
|
|
|
252
416
|
});
|
|
253
417
|
out.push("");
|
|
254
418
|
}
|
|
255
|
-
out.push(...ruleDocs(result.findings, io));
|
|
419
|
+
out.push(...ruleDocs$1(result.findings, io));
|
|
256
420
|
out.push(summary(result, io));
|
|
257
421
|
return `${out.join("\n")}\n`;
|
|
258
422
|
}
|
|
259
423
|
/** Each rule that reported something, with its documentation page. */
|
|
260
|
-
function ruleDocs(findings, io) {
|
|
424
|
+
function ruleDocs$1(findings, io) {
|
|
261
425
|
const ids = [...new Set(findings.map((f) => f.ruleId))];
|
|
262
426
|
if (ids.length === 0) return [];
|
|
263
427
|
const width = Math.max(...ids.map((id) => id.length));
|
|
264
428
|
return [
|
|
265
429
|
paint(io, "underline", "Rule docs"),
|
|
266
430
|
...ids.map((id) => ` ${id.padEnd(width)} ${paint(io, "dim", ruleUrl(id))}`),
|
|
431
|
+
` ${paint(io, "dim", "Run `okforest rule <rule>` for why and how to fix.")}`,
|
|
267
432
|
""
|
|
268
433
|
];
|
|
269
434
|
}
|
|
@@ -282,6 +447,7 @@ Usage
|
|
|
282
447
|
okforest validate [path] [--format text|json]
|
|
283
448
|
okforest inventory [path] [--format text|json]
|
|
284
449
|
okforest init [path] --template company [--name <name>]
|
|
450
|
+
okforest rule [rule] [--format text|json]
|
|
285
451
|
|
|
286
452
|
Commands
|
|
287
453
|
validate Check a tree against OKF v0.2 and OKForest conventions.
|
|
@@ -289,6 +455,8 @@ Commands
|
|
|
289
455
|
inventory List every type, tag, and frontmatter key in use, with counts
|
|
290
456
|
and likely near-duplicates.
|
|
291
457
|
init Create a new tree from a starter template (templates: company).
|
|
458
|
+
rule Explain a rule: why it exists, an example, and the fix.
|
|
459
|
+
Without a rule, list every rule.
|
|
292
460
|
|
|
293
461
|
Options
|
|
294
462
|
-h, --help Show help (also: okforest <command> --help)
|
|
@@ -303,7 +471,8 @@ Docs and source: https://github.com/okforest/cli
|
|
|
303
471
|
const COMMANDS = {
|
|
304
472
|
validate: validateCommand,
|
|
305
473
|
inventory: inventoryCommand,
|
|
306
|
-
init: initCommand
|
|
474
|
+
init: initCommand,
|
|
475
|
+
rule: ruleCommand
|
|
307
476
|
};
|
|
308
477
|
/** Run the CLI and return its exit code. */
|
|
309
478
|
async function main(argv, io) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "okforest",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "The OKForest CLI: validate Open Knowledge Format (OKF) v0.2 trees, inventory their metadata, and scaffold new trees from templates.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"okf",
|
|
@@ -32,8 +32,8 @@
|
|
|
32
32
|
"node": ">=22"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@okforest/okf": "0.
|
|
36
|
-
"@okforest/validate": "0.
|
|
35
|
+
"@okforest/okf": "0.6.0",
|
|
36
|
+
"@okforest/validate": "0.6.0"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"@types/node": "^26.6.5",
|