okforest 0.8.0 → 0.10.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.
Files changed (3) hide show
  1. package/README.md +1 -0
  2. package/dist/cli.mjs +111 -11
  3. package/package.json +3 -3
package/README.md CHANGED
@@ -10,6 +10,7 @@ npx okforest validate . --format json # for CI and tools
10
10
  npx okforest inventory . # types, tags, keys, near-duplicates
11
11
  npx okforest init sales-okf --template company --name "Sales knowledge"
12
12
  npx okforest rule okf/type # why a rule exists, and how to fix it
13
+ npx okforest types init . # draft a JSON Schema for each type
13
14
  ```
14
15
 
15
16
  `npx` runs it without installing anything. It needs Node.js 22 or later. To
package/dist/cli.mjs CHANGED
@@ -4,9 +4,9 @@ import { mkdir, readFile, readdir, stat, writeFile } from "node:fs/promises";
4
4
  import { basename, join, resolve } from "node:path";
5
5
  import { fileURLToPath } from "node:url";
6
6
  import { parseArgs, styleText } from "node:util";
7
- import { buildInventory } from "@okforest/okf";
7
+ import { TYPES_FOLDER, buildInventory } from "@okforest/okf";
8
8
  import { readTree } from "@okforest/okf/node";
9
- import { defaultRules, okfRules, ruleDocs, ruleUrl, toReport, validate } from "@okforest/validate";
9
+ import { defaultRules, draftTypeSchema, okfRules, ruleDocs, ruleUrl, toReport, validate } from "@okforest/validate";
10
10
  // src/io.ts
11
11
  /** Exit codes: 0 success, 1 validation errors, 2 usage or read problems. */
12
12
  const EXIT = {
@@ -49,7 +49,7 @@ function formatOption(value) {
49
49
  throw new UsageError(`--format must be \`text\` or \`json\` (got \`${String(value)}\`).`);
50
50
  }
51
51
  // src/commands/init.ts
52
- const HELP$4 = `Usage: okforest init [path] --template <name> [--name <name>]
52
+ const HELP$5 = `Usage: okforest init [path] --template <name> [--name <name>]
53
53
 
54
54
  Create a new OKF v0.2 tree at path (default: the current directory, which must
55
55
  be empty apart from .git) from a starter template. Starter concepts are drafts
@@ -66,7 +66,7 @@ async function initCommand(args, io) {
66
66
  name: { type: "string" }
67
67
  });
68
68
  if (values.help) {
69
- io.stdout(HELP$4);
69
+ io.stdout(HELP$5);
70
70
  return EXIT.ok;
71
71
  }
72
72
  const templates = await listTemplates();
@@ -75,7 +75,7 @@ async function initCommand(args, io) {
75
75
  const target = resolve(io.cwd, path);
76
76
  const blocking = (await readdir(target).catch(() => [])).filter((name) => name !== ".git" && name !== ".DS_Store");
77
77
  if (blocking.length > 0) throw new UsageError(`${path} is not empty (${blocking.slice(0, 3).join(", ")}${blocking.length > 3 ? ", ..." : ""}).`);
78
- const name = typeof values.name === "string" ? values.name.trim() : `${capitalize(slug(basename(target).replace(/-okf$/, "")) || "company")} knowledge`;
78
+ const name = typeof values.name === "string" ? values.name.trim() : `${capitalize(slug$1(basename(target).replace(/-okf$/, "")) || "company")} knowledge`;
79
79
  if (name === "" || [...name].length > 100) throw new UsageError(`--name must be 1 to 100 characters (got ${[...name].length.toLocaleString("en-US")}).`);
80
80
  const now = /* @__PURE__ */ new Date();
81
81
  const tokens = {
@@ -117,7 +117,7 @@ async function copyTemplate(from, to, tokens) {
117
117
  await walk("");
118
118
  return written;
119
119
  }
120
- function slug(value) {
120
+ function slug$1(value) {
121
121
  return value.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "");
122
122
  }
123
123
  function capitalize(value) {
@@ -137,7 +137,7 @@ async function loadTree(path, io) {
137
137
  }
138
138
  }
139
139
  // src/commands/inventory.ts
140
- const HELP$3 = `Usage: okforest inventory [path] [--format text|json]
140
+ const HELP$4 = `Usage: okforest inventory [path] [--format text|json]
141
141
 
142
142
  List every type, tag, and frontmatter key in use in the tree at path (default:
143
143
  the current directory), with counts, lifecycle and trust totals, and likely
@@ -149,7 +149,7 @@ Options
149
149
  async function inventoryCommand(args, io) {
150
150
  const { values, path } = parseCommand(args, { format: { type: "string" } });
151
151
  if (values.help) {
152
- io.stdout(HELP$3);
152
+ io.stdout(HELP$4);
153
153
  return EXIT.ok;
154
154
  }
155
155
  const format = formatOption(values.format);
@@ -194,7 +194,7 @@ function inventoryText(inventory, io) {
194
194
  return `${out.join("\n")}\n`;
195
195
  }
196
196
  // src/commands/rule.ts
197
- const HELP$2 = `Usage: okforest rule [rule] [--format text|json]
197
+ const HELP$3 = `Usage: okforest rule [rule] [--format text|json]
198
198
 
199
199
  Explain a rule: why it exists, an example that breaks it, what
200
200
  \`okforest validate\` reports for it, and the fix. Without a rule, list every
@@ -228,7 +228,7 @@ async function ruleCommand(args, io) {
228
228
  strict: true
229
229
  });
230
230
  if (values.help) {
231
- io.stdout(HELP$2);
231
+ io.stdout(HELP$3);
232
232
  return EXIT.ok;
233
233
  }
234
234
  if (positionals.length > 1) throw new UsageError(`Expected one rule, got ${positionals.length}: ${positionals.join(" ")}`);
@@ -356,6 +356,102 @@ function edits(a, b) {
356
356
  }
357
357
  return previous[b.length];
358
358
  }
359
+ // src/commands/types.ts
360
+ const HELP$2 = `Usage: okforest types <command> [path]
361
+
362
+ Work with the tree's type schemas: a JSON Schema for each type's frontmatter
363
+ in ${TYPES_FOLDER}, which \`okforest validate\` checks concepts against.
364
+
365
+ Commands
366
+ init Draft a schema for each type in use that has none.
367
+
368
+ Run \`okforest types <command> --help\` for its options.
369
+ `;
370
+ const INIT_HELP = `Usage: okforest types init [path] [--type <type>]... [--dry-run]
371
+
372
+ Draft a JSON Schema for each type the tree at path (default: the current
373
+ directory) uses that has no schema yet, as ${TYPES_FOLDER}<type>.schema.json.
374
+ Each draft holds the fields the type's concepts use beyond OKF's, typed by
375
+ their values, with a format, allowed values, or required fields where every
376
+ concept agrees, so they all match it. Review and tighten each one; no file is
377
+ ever overwritten.
378
+
379
+ Options
380
+ --type <type> Only this type (repeatable), as concepts write it.
381
+ --dry-run Print the drafts instead of writing them.
382
+ `;
383
+ const COMMANDS$1 = { init: typesInit };
384
+ async function typesCommand(args, io) {
385
+ const [command, ...rest] = args;
386
+ if (command === void 0 || command === "-h" || command === "--help") {
387
+ io.stdout(HELP$2);
388
+ return command === void 0 ? EXIT.usage : EXIT.ok;
389
+ }
390
+ if (!Object.hasOwn(COMMANDS$1, command)) throw new UsageError(`Unknown command \`types ${command}\`. Run \`okforest types --help\` for usage.`);
391
+ return await COMMANDS$1[command](rest, io);
392
+ }
393
+ async function typesInit(args, io) {
394
+ const { values, path } = parseCommand(args, {
395
+ type: {
396
+ type: "string",
397
+ multiple: true
398
+ },
399
+ "dry-run": { type: "boolean" }
400
+ });
401
+ if (values.help) {
402
+ io.stdout(INIT_HELP);
403
+ return EXIT.ok;
404
+ }
405
+ const tree = await loadTree(path, io);
406
+ const byType = /* @__PURE__ */ new Map();
407
+ for (const concept of tree.concepts) {
408
+ if (concept.type === void 0) continue;
409
+ byType.set(concept.type, [...byType.get(concept.type) ?? [], concept]);
410
+ }
411
+ const wanted = values.type ?? [...byType.keys()].sort();
412
+ const unknown = wanted.filter((type) => !byType.has(type));
413
+ if (unknown.length > 0) throw new UsageError(`No concept has type ${unknown.map((t) => `\`${t}\``).join(", ")}. Run \`okforest inventory ${path}\` to list the types in use.`);
414
+ const schemas = new Set(tree.types.flatMap((schema) => schema.name ? [schema.name] : []));
415
+ const files = new Set(tree.files);
416
+ const lines = [];
417
+ let drafted = 0;
418
+ for (const type of wanted) {
419
+ const concepts = byType.get(type);
420
+ const file = `${TYPES_FOLDER}${slug(type)}.schema.json`;
421
+ if (schemas.has(type)) {
422
+ lines.push(` skipped ${type}: it has a schema already`);
423
+ continue;
424
+ }
425
+ if (files.has(file)) {
426
+ lines.push(` skipped ${type}: ${file} exists, for another type`);
427
+ continue;
428
+ }
429
+ const { $schema, ...draft } = draftTypeSchema(type, concepts);
430
+ const schema = {
431
+ $schema,
432
+ $comment: `Drafted by okforest ${VERSION} types init from ${concepts.length} ${concepts.length === 1 ? "concept" : "concepts"}: tighten it (required fields, allowed values, formats).`,
433
+ ...draft
434
+ };
435
+ const text = `${JSON.stringify(schema, null, 2)}\n`;
436
+ drafted++;
437
+ if (values["dry-run"]) {
438
+ lines.push(` ${file}`, ...text.trimEnd().split("\n").map((line) => ` ${line}`));
439
+ continue;
440
+ }
441
+ const target = resolve(io.cwd, path, file);
442
+ await mkdir(join(target, ".."), { recursive: true });
443
+ await writeFile(target, text, { flag: "wx" });
444
+ lines.push(` wrote ${file} (${type}, from ${concepts.length})`);
445
+ }
446
+ const verb = values["dry-run"] ? "Would draft" : "Drafted";
447
+ io.stdout(`${verb} ${drafted} type ${drafted === 1 ? "schema" : "schemas"} in ${path}.\n` + (lines.length > 0 ? `${lines.join("\n")}\n` : "") + (drafted > 0 && !values["dry-run"] ? `\nNext: review each draft, then check the tree with \`okforest validate ${path}\`.\n` : ""));
448
+ return EXIT.ok;
449
+ }
450
+ /** `BigQuery Table` → `bigquery-table`, `顧客` → `顧客`; `type` when no
451
+ * letter or digit is left. */
452
+ function slug(type) {
453
+ return type.toLowerCase().replace(/[^\p{L}\p{N}]+/gu, "-").replace(/^-+|-+$/g, "") || "type";
454
+ }
359
455
  // src/commands/validate.ts
360
456
  const HELP$1 = `Usage: okforest validate [path] [--format text|json]
361
457
 
@@ -441,6 +537,7 @@ Usage
441
537
  okforest inventory [path] [--format text|json]
442
538
  okforest init [path] --template company [--name <name>]
443
539
  okforest rule [rule] [--format text|json]
540
+ okforest types init [path] [--type <type>]... [--dry-run]
444
541
 
445
542
  Commands
446
543
  validate Check a tree against OKF v0.2 and OKForest conventions.
@@ -450,6 +547,8 @@ Commands
450
547
  init Create a new tree from a starter template (templates: company).
451
548
  rule Explain a rule: why it exists, an example, and the fix.
452
549
  Without a rule, list every rule.
550
+ types Work with the tree's type schemas (.okforest/types/):
551
+ \`types init\` drafts one for each type in use that has none.
453
552
 
454
553
  Options
455
554
  -h, --help Show help (also: okforest <command> --help)
@@ -465,7 +564,8 @@ const COMMANDS = {
465
564
  validate: validateCommand,
466
565
  inventory: inventoryCommand,
467
566
  init: initCommand,
468
- rule: ruleCommand
567
+ rule: ruleCommand,
568
+ types: typesCommand
469
569
  };
470
570
  /** Run the CLI and return its exit code. */
471
571
  async function main(argv, io) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "okforest",
3
- "version": "0.8.0",
3
+ "version": "0.10.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.8.0",
36
- "@okforest/validate": "0.8.0"
35
+ "@okforest/okf": "0.10.0",
36
+ "@okforest/validate": "0.10.0"
37
37
  },
38
38
  "devDependencies": {
39
39
  "@types/node": "^26.6.6",