@dialecte/cli 0.5.0 → 0.7.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 (5) hide show
  1. package/README.md +424 -37
  2. package/dist/cli.js +56727 -46978
  3. package/dist/lookups.d.ts +369 -19
  4. package/dist/lookups.js +4552 -1579
  5. package/package.json +20 -1
package/dist/lookups.d.ts CHANGED
@@ -8,9 +8,10 @@ declare const ROOTS: readonly ["q", "tx", "doc", "project"];
8
8
  type MethodRoot = (typeof ROOTS)[number];
9
9
  /**
10
10
  * One method of a package as `api.json` ships it: what `dialecte api find` and `api show` read. A method of a
11
- * root or a function a package entry exports: `root` tells them apart, and only an export has an `entry`.
11
+ * root, a function a package entry exports or one the package's source exports: `root` tells them apart, and only
12
+ * an export has an `entry`.
12
13
  */
13
- type ApiMethod = ApiRootMethod | ApiExportedFunction;
14
+ type ApiMethod = ApiRootMethod | ApiExportedFunction | ApiSourceFunction;
14
15
  /** A method reached from a root (`q`, `tx`, `doc`, `project`). */
15
16
  type ApiRootMethod = ApiMethodFields & {
16
17
  root: MethodRoot;
@@ -22,6 +23,11 @@ type ApiExportedFunction = ApiMethodFields & {
22
23
  /** The package entry the function comes from ('@dialecte/core/sqlite'). */
23
24
  entry: string;
24
25
  };
26
+ /** An exported function of the package's own source (`src/lib/git.ts#readGit`), read from the files, not from `api.json`. */
27
+ type ApiSourceFunction = ApiMethodFields & {
28
+ root: 'source';
29
+ entry?: undefined;
30
+ };
25
31
  /** What every row of `api.json` holds, whatever its root. */
26
32
  type ApiMethodFields = {
27
33
  /** The canonical route, as a caller writes it: `doc.query.info.of`, `@dialecte/core/sqlite.connectHub`. */
@@ -43,6 +49,9 @@ type ApiJson = {
43
49
  methods: ApiMethod[];
44
50
  };
45
51
 
52
+ /** The exit codes a lookup answers with: 0 answer, 1 nothing found, 2 usage. */
53
+ declare const LOOKUP_EXIT_CODES: readonly [0, 1, 2];
54
+
46
55
  declare const printedBrand: unique symbol;
47
56
  /**
48
57
  * A text the cli may print, every line escaped once (or JSON): only `textOf` and `jsonOf` return one, so a command that
@@ -53,13 +62,16 @@ type PrintedText = string & {
53
62
  readonly [printedBrand]: true;
54
63
  };
55
64
 
65
+ /** An exit code of a lookup: 0 answer, 1 nothing found, 2 usage (no lookup exits 3). */
66
+ type LookupExitCode = (typeof LOOKUP_EXIT_CODES)[number];
56
67
  /**
57
- * What a lookup command answers inside the cli: the text it prints, built by the print boundary only, and its exit
58
- * code (0 answer, 1 nothing found, 2 usage). Assignable to the public `LookupOutput`.
68
+ * What a lookup answers inside the cli (`runApi`, `runDoctor`, `runModels`, `answerSchema`, the schema and members
69
+ * reads, and the helpers they return): a `CommandOutput` whose exit code is a lookup's, so it stays assignable to
70
+ * the public `LookupOutput`.
59
71
  */
60
- type CommandOutput = {
72
+ type LookupCommandOutput = {
61
73
  output: PrintedText;
62
- exitCode: 0 | 1 | 2;
74
+ exitCode: LookupExitCode;
63
75
  };
64
76
  /**
65
77
  * What a lookup answers, as `@dialecte/cli/lookups` exports it (`CommandOutput` there): the text the command would
@@ -69,23 +81,23 @@ type CommandOutput = {
69
81
  */
70
82
  type LookupOutput = {
71
83
  output: string;
72
- exitCode: 0 | 1 | 2;
84
+ exitCode: LookupExitCode;
73
85
  };
74
86
 
75
87
  /**
76
88
  * What `dialecte api <find|show> ...` prints for `argv` (the words after `api`) run in `cwd`: the methods found for a
77
- * need, or one method's contract, read from the `api.json` of the installed packages; the usage with exit code 2 on
78
- * a bad call.
89
+ * need, or one method's contract, read from the `api.json` of the installed packages (with `--source` or a
90
+ * `<file>#<name>` path, the own package's source functions too); the usage with exit code 2 on a bad call.
79
91
  *
80
92
  * @intent answer an api lookup without the command line, find a Dialecte method by what it is for, show a method's contract from another program
81
93
  */
82
94
  declare function runApi(params: {
83
95
  argv: readonly string[];
84
96
  cwd: string;
85
- }): CommandOutput;
97
+ }): LookupCommandOutput;
86
98
  /**
87
- * The packages an api lookup searched, as its not-found answer names them: their names joined by commas, or the line
88
- * that says no installed package ships an `api.json`.
99
+ * The packages an api lookup searched, as its not-found answer names them: their names joined by commas (a package
100
+ * searched by its index and its source named once), or the line that says no installed package ships an `api.json`.
89
101
  *
90
102
  * @intent name the packages an api lookup searched, word a not-found api answer
91
103
  */
@@ -104,7 +116,7 @@ declare function runDoctor(params: {
104
116
  dialecteRoot: string | undefined;
105
117
  /** The cli's version, or how to read it: `--brief` reads it inside its guard, so a failure never shows. */
106
118
  cliVersion: string | (() => string);
107
- }): CommandOutput;
119
+ }): LookupCommandOutput;
108
120
 
109
121
  /** One public method of the q / tx / doc / project surface, with what its JSDoc says about it. */
110
122
  type MethodEntry = {
@@ -135,7 +147,7 @@ type MemberEntriesRead = {
135
147
  expression: string;
136
148
  } | {
137
149
  kind: 'output';
138
- output: CommandOutput;
150
+ output: LookupCommandOutput;
139
151
  };
140
152
 
141
153
  /**
@@ -157,7 +169,7 @@ declare function readMemberEntries(params: {
157
169
  declare function runModels(params: {
158
170
  argv: readonly string[];
159
171
  cwd: string;
160
- }): CommandOutput & {
172
+ }): LookupCommandOutput & {
161
173
  warnings: PrintedText;
162
174
  };
163
175
 
@@ -247,7 +259,7 @@ type SchemaDialectesRead = {
247
259
  installed: string[];
248
260
  } | {
249
261
  kind: 'output';
250
- output: CommandOutput;
262
+ output: LookupCommandOutput;
251
263
  };
252
264
 
253
265
  /**
@@ -269,7 +281,7 @@ declare function answerMembers(params: {
269
281
  entries: readonly MethodEntry[];
270
282
  expression: string;
271
283
  json: boolean;
272
- }): CommandOutput;
284
+ }): LookupCommandOutput;
273
285
 
274
286
  /**
275
287
  * Every `api.json` an agent can search from `cwd`: the package's own skills (inside a dialecte repository)
@@ -281,6 +293,344 @@ declare function readApiIndexes(params: {
281
293
  cwd: string;
282
294
  }): ApiJson[];
283
295
 
296
+ /**
297
+ * What a step is and, optionally, what runs inside it (`hook: unit suite`). `hintWhenSilent`: a step
298
+ * running something that prints as it works (a hook): silent past the hint time, its spinner says what
299
+ * that often is (waiting for input, watch mode).
300
+ */
301
+ type StepDescription = {
302
+ name: string;
303
+ detail?: string;
304
+ hintWhenSilent?: boolean;
305
+ };
306
+ /** A step announced and not finished yet. */
307
+ type RunningStep = {
308
+ /**
309
+ * The step ended: its lines (the last one its result) replace the spinner and what is shown under it,
310
+ * or follow the start line.
311
+ */
312
+ finish(...lines: string[]): void;
313
+ /**
314
+ * On a terminal: the lines shown live under the spinner (a hook's step and warnings), or how to render
315
+ * them at each redraw (a step's elapsed time). Off a terminal: nothing.
316
+ */
317
+ show(lines: LiveLines): void;
318
+ /**
319
+ * The step printed something, shown or not: its silence starts again for the silent hint (on both); the
320
+ * running lines off a terminal wait for a line written (`wrote`).
321
+ */
322
+ touch(): void;
323
+ /**
324
+ * Off a terminal: a line of the step was written (a hook's step or warning), so its running lines start
325
+ * again; output heard but filtered out does not count. On a terminal: nothing.
326
+ */
327
+ wrote(): void;
328
+ };
329
+ type StepProgress = {
330
+ /** Announces the step before any of its work begins. */
331
+ start(step: StepDescription): RunningStep;
332
+ };
333
+ /** Lines shown live under a spinner, or rendered at each redraw from the clock's time. */
334
+ type LiveLines = string[] | ((now: number) => string[]);
335
+
336
+ /** The kinds a line of a standard's markdown file is classified as, one original line at a time. */
337
+ type StandardLineKind = 'blank' | 'table' | 'code' | 'toc' | 'heading' | 'definition' | 'caption' | 'prose';
338
+ /** One logical line: original lines `from` to `to` (1-based), their kind, and the normalised text. */
339
+ type StandardLogicalLine = {
340
+ from: number;
341
+ to: number;
342
+ kind: StandardLineKind;
343
+ text: string;
344
+ };
345
+ /** A kept heading: `id` is `<file id>#<number>`, with `~<pass>` from the second pass of the file on. */
346
+ type StandardHeading = {
347
+ id: string;
348
+ number: string;
349
+ title: string;
350
+ from: number;
351
+ to: number;
352
+ depth: number;
353
+ parent: string | null;
354
+ pass: number;
355
+ };
356
+ type StandardDefinition = {
357
+ term: string;
358
+ number: string;
359
+ from: number;
360
+ to: number;
361
+ };
362
+ type StandardTable = {
363
+ id: string;
364
+ caption: string | null;
365
+ number: string | null;
366
+ from: number;
367
+ to: number;
368
+ clause: string | null;
369
+ columns: string[];
370
+ };
371
+ type StandardCrossReference = {
372
+ from: number;
373
+ inClause: string | null;
374
+ target: string;
375
+ resolved: boolean;
376
+ };
377
+ /** Information only: a line seen 20 or more times with a median gap of 20 or more lines. Never dropped. */
378
+ type StandardRepeatedLine = {
379
+ text: string;
380
+ count: number;
381
+ length: number;
382
+ medianGap: number;
383
+ };
384
+ type StandardBlockVerdict = 'good' | 'poor' | 'unreadable';
385
+ type StandardFileVerdict = 'good' | 'partial' | 'unreadable';
386
+ /** A block of up to 1,000 original lines: `dropped` lines over `lines` is its noise ratio. */
387
+ type StandardQualityBlock = {
388
+ from: number;
389
+ to: number;
390
+ lines: number;
391
+ dropped: number;
392
+ noiseRatio: number;
393
+ verdict: StandardBlockVerdict;
394
+ };
395
+ type StandardQuality = {
396
+ blocks: StandardQualityBlock[];
397
+ verdict: StandardFileVerdict;
398
+ readableLines: number;
399
+ droppedLines: number;
400
+ headingCandidatesKept: number;
401
+ headingCandidatesTurnedProse: number;
402
+ repeatedLines: StandardRepeatedLine[];
403
+ };
404
+ /** A range of original lines of a file, first and last included (1-based). */
405
+ type StandardOriginalRange = {
406
+ from: number;
407
+ to: number;
408
+ };
409
+ /**
410
+ * What indexing one file's text gives: the logical lines and what is read out of them. `noiseRanges`
411
+ * are the dropped one-character runs (the blank lines inside a run included), in file order.
412
+ */
413
+ type StandardFileIndex = {
414
+ fileId: string;
415
+ lineCount: number;
416
+ logicalLines: StandardLogicalLine[];
417
+ noiseRanges: StandardOriginalRange[];
418
+ headings: StandardHeading[];
419
+ definitions: StandardDefinition[];
420
+ tables: StandardTable[];
421
+ crossReferences: StandardCrossReference[];
422
+ quality: StandardQuality;
423
+ };
424
+
425
+ /** A kept folder or file: its lowercased id and its name as on disk. */
426
+ type StandardNamed = {
427
+ id: string;
428
+ name: string;
429
+ };
430
+ /** A file of a standard: its id, its name as on disk and its path (a symlink kept as given). */
431
+ type StandardFile = StandardNamed & {
432
+ path: string;
433
+ };
434
+ /** A standard folder of the root: its id, its name, its path and its files in id order (none: skipped). */
435
+ type StandardFolder = StandardNamed & {
436
+ folder: string;
437
+ files: StandardFile[];
438
+ };
439
+
440
+ /**
441
+ * The tags, attributes and enumeration values of the selected dialectes' definitions: the names the tag
442
+ * links are made of.
443
+ */
444
+ type StandardVocabulary = {
445
+ tags: string[];
446
+ attributes: string[];
447
+ values: string[];
448
+ };
449
+ /**
450
+ * What a unit of search is: a clause's own lines (to its first child heading) minus its tables and
451
+ * their captions, a table with its caption, or the lines before the first heading of a file.
452
+ */
453
+ type StandardUnitKind = 'clause' | 'table' | 'front';
454
+ /**
455
+ * A unit find scores. Lines are numbered across the standard: the logical lines of its files one after
456
+ * the other, in the order of `files` (a "standard line"). `from`/`to` are original lines of its file;
457
+ * `firstLine`/`lastLine` the standard lines of its first and last logical line (a heading, a caption or
458
+ * a table's first row first); `titleLine` the standard line of its heading or caption, null when none.
459
+ * `clause` is the number of the heading holding it (null for front matter and a table outside every
460
+ * clause), `title` the heading title or the caption (null when none), `target` the cross-reference
461
+ * target naming it (a heading number, or the table prefix and the caption number; null when none).
462
+ */
463
+ type StandardUnit = {
464
+ fileId: string;
465
+ kind: StandardUnitKind;
466
+ clause: string | null;
467
+ title: string | null;
468
+ target: string | null;
469
+ from: number;
470
+ to: number;
471
+ firstLine: number;
472
+ lastLine: number;
473
+ titleLine: number | null;
474
+ };
475
+ /** The units of a standard and, per standard line, the index of the unit it belongs to. */
476
+ type StandardUnits = {
477
+ units: StandardUnit[];
478
+ lineUnits: number[];
479
+ };
480
+ /**
481
+ * The word index of one standard: per stem, the standard lines holding it in increasing order (their
482
+ * count is the stem's document frequency), and the count of logical lines of the whole standard.
483
+ */
484
+ type StandardWordIndex = {
485
+ logicalLineCount: number;
486
+ stems: Record<string, number[]>;
487
+ };
488
+ /**
489
+ * A vocabulary name linked to a unit (its index in `units`): the strength of its strongest hit (3, 2
490
+ * or 1) and the standard lines of the unit where it is named.
491
+ */
492
+ type StandardTagLink = {
493
+ unit: number;
494
+ strength: number;
495
+ lines: number[];
496
+ };
497
+ /** Vocabulary name (case kept) to the units it links. Empty when the vocabulary is empty. */
498
+ type StandardTagLinks = Record<string, StandardTagLink[]>;
499
+ /**
500
+ * The search structures of one standard: its files, the word index (independent of the vocabulary),
501
+ * the units and the tag links of the vocabulary. Plain data, so it can be written as JSON and read back.
502
+ */
503
+ type StandardSearchIndex = StandardUnits & {
504
+ files: StandardFileIndex[];
505
+ vocabulary: StandardVocabulary;
506
+ words: StandardWordIndex;
507
+ tagLinks: StandardTagLinks;
508
+ };
509
+ /** One answer of find: a range of whole logical lines of one unit, its score, the terms that hit. */
510
+ type StandardSlice = {
511
+ file: string;
512
+ from: number;
513
+ to: number;
514
+ clause: string | null;
515
+ title: string | null;
516
+ score: number;
517
+ terms: string[];
518
+ linked: boolean;
519
+ };
520
+ type FindInStandardParams = {
521
+ indexes: StandardSearchIndex[];
522
+ query: string;
523
+ about?: string;
524
+ limit?: number;
525
+ };
526
+
527
+ /**
528
+ * The vocabulary of the selected installed dialectes, their sorted `name@version` list, and the hash of that
529
+ * list naming its tags file (none when no dialecte is selected).
530
+ */
531
+ type StandardInstalledVocabulary = {
532
+ vocabulary: StandardVocabulary;
533
+ packages: string[];
534
+ installedHash: string | undefined;
535
+ };
536
+ /** Whether an index was built by this run or found complete in the cache. */
537
+ type StandardIndexState = 'built' | 'reused';
538
+ /** An index ready to answer: its standard, its manifest hash, how it came, its files and search index. */
539
+ type StandardReadyIndex = {
540
+ standardId: string;
541
+ manifestHash: string;
542
+ state: StandardIndexState;
543
+ elapsed: number;
544
+ search: StandardSearchIndex;
545
+ };
546
+
547
+ /**
548
+ * The documents the ask may read: the selected standards' indexes and each folder's files key; or none (with the
549
+ * standard's declared name and its key, null when the dialecte declares none).
550
+ */
551
+ type AskStandard = {
552
+ kind: 'documents';
553
+ indexes: StandardSearchIndex[];
554
+ folders: {
555
+ id: string;
556
+ files: string;
557
+ }[];
558
+ } | {
559
+ kind: 'none';
560
+ name: string | null;
561
+ key: string | null;
562
+ };
563
+ /** What `loadStandardIndexes` takes: where it runs, its environment, the folders and the vocabulary to read. */
564
+ type LoadStandardIndexesParams = {
565
+ cwd: string;
566
+ environment: NodeJS.ProcessEnv;
567
+ /** The ids of the root's folders to load; every folder of the root when left out. */
568
+ standardIds?: readonly string[];
569
+ /** The one installed dialecte whose vocabulary scores the slices (`--package`); every installed one when left out. */
570
+ packageName?: string;
571
+ rebuild?: boolean;
572
+ /** False: the installed dialectes are not read, the vocabulary is empty (a read that scores nothing). */
573
+ readVocabulary?: boolean;
574
+ /** A refusal of the vocabulary read, checked before any build (`--about` naming a tag no dialecte declares). */
575
+ checkVocabulary?: (params: {
576
+ installed: StandardInstalledVocabulary;
577
+ }) => string | undefined;
578
+ /** The running line, on a terminal only. */
579
+ progress?: StepProgress;
580
+ /** A test seam called right after the build reads each file's text. */
581
+ afterFileRead?: (params: {
582
+ path: string;
583
+ }) => void;
584
+ };
585
+ /** The documents of the ask, and what `standard` prints of them: the ready indexes, the folders without a file. */
586
+ type LoadedStandards = {
587
+ kind: 'loaded';
588
+ standard: Extract<AskStandard, {
589
+ kind: 'documents';
590
+ }>;
591
+ ready: StandardReadyIndex[];
592
+ skipped: StandardFolder[];
593
+ installed: StandardInstalledVocabulary;
594
+ };
595
+ /**
596
+ * The standards loaded; a refusal (exit 2, `layout` the expected layout of the root when the refusal is about it);
597
+ * a `packageName` no installed dialecte has (exit 2, the installed ones named); or an aborted build (exit 1).
598
+ */
599
+ type LoadedStandardIndexes = LoadedStandards | {
600
+ kind: 'refused';
601
+ message: string;
602
+ layout?: string;
603
+ } | {
604
+ kind: 'unknownPackage';
605
+ name: string;
606
+ installed: string[];
607
+ } | {
608
+ kind: 'aborted';
609
+ message: string;
610
+ };
611
+
612
+ /**
613
+ * The selected standards' search indexes (built in the cache when missing), each folder's files key and the
614
+ * installed vocabulary; or a refusal (exit 2: the root, a folder, an id the root does not hold, a cache inside a
615
+ * git work tree, a `packageName` not installed, the caller's vocabulary check), or an aborted build (exit 1). A
616
+ * folder holding no `.md` file is skipped. The one loading path of `standard` and `ask`. Writes a missing or
617
+ * outdated index to the index cache.
618
+ *
619
+ * @intent load the search indexes of standard folders, build the index of the standard text
620
+ */
621
+ declare function loadStandardIndexes(params: LoadStandardIndexesParams): Promise<LoadedStandardIndexes>;
622
+
623
+ /**
624
+ * Finds the slices of the standards that answer a query: one ranking of the units of every standard by
625
+ * score (rounded), then file id, then first line; the first `limit` are the direct slices. Then, for
626
+ * each direct slice in rank order, each resolved cross-reference inside it gives the slice of its target
627
+ * unit (from its heading or caption, capped), once per unit, at most `limit` of them, after every direct
628
+ * one. Pure, over search indexes already built.
629
+ *
630
+ * @intent find the passages of a standard that answer a question, search the standard text
631
+ */
632
+ declare function findInStandard(params: FindInStandardParams): StandardSlice[];
633
+
284
634
  /**
285
635
  * The `version` of the nearest package.json above `startFolder`; by default this file's folder (dist/ in the bundle,
286
636
  * src/lib/ in the sources), so the cli's own version. Throws when no package.json is above it.
@@ -304,7 +654,7 @@ declare function answerSchema(params: {
304
654
  /** A parent (`--under`): answers the declaration(s) the tag has there. */
305
655
  under?: string | undefined;
306
656
  json: boolean;
307
- }): CommandOutput;
657
+ }): LookupCommandOutput;
308
658
 
309
659
  /**
310
660
  * What an element may hold and where: the declaration that applies under `parent`, or the tag level (the union of
@@ -319,4 +669,4 @@ declare function describeElement(params: {
319
669
  parent?: string | undefined;
320
670
  }): ElementDescription | undefined;
321
671
 
322
- export { type ApiExportedFunction, type ApiJson, type ApiMethod, type ApiMethodFields, type ApiRootMethod, type AttributeDescription, type ChildDescription, type LookupOutput as CommandOutput, type ElementDescription, type LoadedDialecte, type MemberEntriesRead, type PrintedText, type SchemaDialectesRead, answerMembers, answerSchema, describeElement, packagesText, readApiIndexes, readCliVersion, readMemberEntries, readSchemaDialectes, runApi, runDoctor, runModels };
672
+ export { type ApiExportedFunction, type ApiJson, type ApiMethod, type ApiMethodFields, type ApiRootMethod, type ApiSourceFunction, type AskStandard, type AttributeDescription, type ChildDescription, type LookupOutput as CommandOutput, type ElementDescription, type FindInStandardParams, type LoadStandardIndexesParams, type LoadedDialecte, type LoadedStandardIndexes, type LoadedStandards, type MemberEntriesRead, type PrintedText, type SchemaDialectesRead, type StandardFolder, type StandardInstalledVocabulary, type StandardReadyIndex, type StandardSearchIndex, type StandardSlice, type StepProgress, answerMembers, answerSchema, describeElement, findInStandard, loadStandardIndexes, packagesText, readApiIndexes, readCliVersion, readMemberEntries, readSchemaDialectes, runApi, runDoctor, runModels };