@astryxdesign/cli 0.7.16-canary.g4da09b8 → 0.7.16-canary.g4e39f9c

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.
@@ -12,6 +12,7 @@
12
12
  * @property {string} category The template's own `Family - Variant` label; empty when it declares none.
13
13
  * @property {string[]} keywords The ideas the page serves, as its own descriptor names them; empty when it declares none.
14
14
  * @property {string} filePath Absolute path to the template source file on disk.
15
+ * @property {string[]} components The components its page composes, read from its source on first use; empty when the source cannot be read.
15
16
  */
16
17
  /**
17
18
  * A component the project can use, as the ranker reads it.
@@ -158,6 +159,10 @@ export type PageTemplate = {
158
159
  * Absolute path to the template source file on disk.
159
160
  */
160
161
  filePath: string;
162
+ /**
163
+ * The components its page composes, read from its source on first use; empty when the source cannot be read.
164
+ */
165
+ components: string[];
161
166
  };
162
167
  /**
163
168
  * A component the project can use, as the ranker reads it.
@@ -28,6 +28,7 @@ import {componentKeywords} from '../search/search.mjs';
28
28
  import {findCoreDir, requireCoreDir} from '../../foundation/fs/paths.mjs';
29
29
  import {persistedDerived} from '../../foundation/fs/gather-cache.mjs';
30
30
  import {analyzeTemplateNeeds} from '../../foundation/discovery/template-needs.mjs';
31
+ import {extractComponents} from '../../foundation/discovery/template-adapter.mjs';
31
32
 
32
33
  /**
33
34
  * Runs one build with a single directory listing per path: the kit resolves
@@ -47,6 +48,7 @@ export {withListingCache} from '../../foundation/fs/listing-cache.mjs';
47
48
  * @property {string} category The template's own `Family - Variant` label; empty when it declares none.
48
49
  * @property {string[]} keywords The ideas the page serves, as its own descriptor names them; empty when it declares none.
49
50
  * @property {string} filePath Absolute path to the template source file on disk.
51
+ * @property {string[]} components The components its page composes, read from its source on first use; empty when the source cannot be read.
50
52
  */
51
53
 
52
54
  /**
@@ -99,18 +101,23 @@ export async function loadTemplates(cwd) {
99
101
  }
100
102
  const pages = templates
101
103
  .filter(t => t.type === 'page' && t.isReady !== false)
102
- .map(t => ({
103
- name: t.dirName,
104
- displayName: t.displayName || t.name,
105
- description: t.description || '',
106
- package: t.package ?? '@astryxdesign/core',
107
- category: t.category || '',
108
- keywords: t.keywords ?? [],
109
- filePath: t.filePath,
110
- // The id `template()` resolves back to this entry: an active replacement
111
- // owns the Core id it names, so that id selects it, not its own.
112
- command: `astryx template ${t.replaces ?? t.dirName} --type page`,
113
- }));
104
+ .map(t =>
105
+ withComponents(
106
+ {
107
+ name: t.dirName,
108
+ displayName: t.displayName || t.name,
109
+ description: t.description || '',
110
+ package: t.package ?? '@astryxdesign/core',
111
+ category: t.category || '',
112
+ keywords: t.keywords ?? [],
113
+ filePath: t.filePath,
114
+ // The id `template()` resolves back to this entry: an active
115
+ // replacement owns the Core id it names, so that id selects it.
116
+ command: `astryx template ${t.replaces ?? t.dirName} --type page`,
117
+ },
118
+ t.filePath,
119
+ ),
120
+ );
114
121
  const blocks = templates
115
122
  .filter(t => t.type === 'block')
116
123
  .map(t => ({
@@ -362,3 +369,31 @@ export function templateSetupNotes(template, cwd) {
362
369
  export function requireBuildCoreDir(cwd) {
363
370
  return requireCoreDir(cwd);
364
371
  }
372
+
373
+ /**
374
+ * Adds a lazy, non-enumerable `components` getter to a page entry: the page's
375
+ * source is read and parsed only when a caller reads it, once, so loading the
376
+ * catalog stays cheap and the entry's own fields are unchanged.
377
+ * @template {object} T
378
+ * @param {T} entry
379
+ * @param {string | undefined} filePath
380
+ * @returns {T & {readonly components: string[]}}
381
+ */
382
+ function withComponents(entry, filePath) {
383
+ /** @type {string[] | undefined} */
384
+ let components;
385
+ return /** @type {T & {readonly components: string[]}} */ (
386
+ Object.defineProperty(entry, 'components', {
387
+ get() {
388
+ if (components === undefined) {
389
+ try {
390
+ components = filePath ? extractComponents(filePath) : [];
391
+ } catch {
392
+ components = [];
393
+ }
394
+ }
395
+ return components;
396
+ },
397
+ })
398
+ );
399
+ }
@@ -175,6 +175,16 @@ export function rankPages(query, pages) {
175
175
  return d ? Math.log(1 + (n - d + 0.5) / (d + 0.5)) : 0;
176
176
  };
177
177
 
178
+ // How many templates of each family carry each term, for how typical of its
179
+ // family a template's descriptor is.
180
+ /** @type {Map<string, Map<string, number>>} */
181
+ const familyDf = new Map();
182
+ for (const {family, bag} of docs) {
183
+ const counts = familyDf.get(family) ?? new Map();
184
+ familyDf.set(family, counts);
185
+ for (const t of bag.keys()) counts.set(t, (counts.get(t) ?? 0) + 1);
186
+ }
187
+
178
188
  const familyOfWord = familyWordsOf(pages);
179
189
  /** @param {string[]} ts */
180
190
  const familiesIn = ts =>
@@ -226,8 +236,12 @@ export function rankPages(query, pages) {
226
236
  else if (familyNamed) score += BODY_FAMILY;
227
237
  const base = normalized(page.name) === normalized(family);
228
238
  if (familyNamed && base) score += FAMILY_BASE;
239
+ const inFamily = familyDf.get(family);
240
+ let shared = 0;
241
+ for (const t of bag.keys()) shared += (inFamily?.get(t) ?? 1) - 1;
229
242
  return {
230
243
  name: page.name,
244
+ typical: bag.size ? shared / bag.size : 0,
231
245
  score,
232
246
  hits,
233
247
  familyNamed,
@@ -92,4 +92,6 @@ export type DescribedTemplate = {
92
92
  displayName?: string | undefined;
93
93
  category?: string | undefined;
94
94
  keywords?: string[] | undefined;
95
+ description?: string | undefined;
96
+ components?: string[] | undefined;
95
97
  };
@@ -233,6 +233,8 @@ function memberFeatures(v) {
233
233
  * @property {string} [displayName]
234
234
  * @property {string} [category]
235
235
  * @property {string[]} [keywords]
236
+ * @property {string} [description]
237
+ * @property {string[]} [components]
236
238
  */
237
239
 
238
240
  /**
@@ -259,6 +261,16 @@ function ownWords(name, catalog, words) {
259
261
  return n;
260
262
  }
261
263
 
264
+ /**
265
+ * A template's family: the part of its category before ` - `.
266
+ * @param {string} name
267
+ * @param {DescribedTemplate[]} catalog
268
+ */
269
+ const familyOf = (name, catalog) =>
270
+ String(catalog.find(c => c.name === name)?.category ?? '')
271
+ .split(' - ')[0]
272
+ .trim();
273
+
262
274
  /** How many fewer own words a template whose family the idea names first may
263
275
  * name than the listed template it competes with, and still start. */
264
276
  const FAMILY_SLACK = 2;
@@ -330,6 +342,118 @@ function blend(idea, ranked, pick, catalog, weights) {
330
342
  return {cands, scores, position};
331
343
  }
332
344
 
345
+ /**
346
+ * The most typical of the ready templates the ranker ties with `name` in its
347
+ * family (same score, same id length): the one whose descriptor's words are
348
+ * shared by the most of its family's templates, on average. `name` keeps its
349
+ * place on an equal value. Reads only the templates' descriptors.
350
+ * @param {string} name
351
+ * @param {import('./rank.mjs').RankedPage[]} ranked
352
+ * @param {string[]} cands
353
+ * @returns {string}
354
+ */
355
+ function typicalOfTie(name, ranked, cands) {
356
+ const self = ranked.find(r => r.name === name);
357
+ if (!self || self.family === SHELL_FAMILY || !self.family) return name;
358
+ const length = (/** @type {string} */ n) => n.split('-').length;
359
+ let chosen = self;
360
+ for (const r of ranked) {
361
+ if (
362
+ r.score === self.score &&
363
+ r.family === self.family &&
364
+ length(r.name) === length(self.name) &&
365
+ cands.includes(r.name) &&
366
+ r.typical > chosen.typical
367
+ )
368
+ chosen = r;
369
+ }
370
+ return chosen.name;
371
+ }
372
+
373
+ /**
374
+ * The words of a template's whole descriptor, stemmed, less search's
375
+ * stopwords: its display name, category, keywords, description, and the names
376
+ * of the components its page composes.
377
+ * @param {DescribedTemplate} t
378
+ * @returns {Set<string>}
379
+ */
380
+ function descriptorWords(t) {
381
+ const components = (t.components ?? []).map(c =>
382
+ c.replace(/([a-z0-9])([A-Z])/g, '$1 $2'),
383
+ );
384
+ return new Set(
385
+ weightWords(
386
+ [
387
+ t.displayName ?? '',
388
+ t.category ?? '',
389
+ ...(t.keywords ?? []),
390
+ t.description ?? '',
391
+ ...components,
392
+ ].join(' '),
393
+ )
394
+ .filter(w => !STOPWORDS.has(w))
395
+ .map(stem),
396
+ );
397
+ }
398
+
399
+ /**
400
+ * Settles an exact tie in the ranker by a fuller reading of each tied
401
+ * template's own descriptor. Words every tied template names cannot choose
402
+ * between them, so they are left out; each other word of the idea that a
403
+ * template's descriptor names adds its rarity within the family. The highest
404
+ * total starts. An equal total goes to the most typical of the family
405
+ * (`typicalOfTie`), then to `name`. Reads only the templates' descriptors.
406
+ * @param {string} name
407
+ * @param {string} idea
408
+ * @param {import('./rank.mjs').RankedPage[]} ranked
409
+ * @param {string[]} cands
410
+ * @param {DescribedTemplate[]} catalog
411
+ * @returns {string}
412
+ */
413
+ function fullerOfTie(name, idea, ranked, cands, catalog) {
414
+ const self = ranked.find(r => r.name === name);
415
+ if (!self || self.family === SHELL_FAMILY || !self.family) return name;
416
+ const length = (/** @type {string} */ n) => n.split('-').length;
417
+ const tied = ranked.filter(
418
+ r =>
419
+ r.score === self.score &&
420
+ r.family === self.family &&
421
+ length(r.name) === length(self.name) &&
422
+ cands.includes(r.name),
423
+ );
424
+ if (tied.length < 2) return name;
425
+ const family = catalog.filter(t => familyOf(t.name, catalog) === self.family);
426
+ const words = new Map(family.map(t => [t.name, descriptorWords(t)]));
427
+ const df = (/** @type {string} */ w) =>
428
+ family.filter(t => words.get(t.name)?.has(w)).length;
429
+ const said = new Set(
430
+ weightWords(idea)
431
+ .filter(w => !STOPWORDS.has(w))
432
+ .map(stem),
433
+ );
434
+ const own = tied.map(r => words.get(r.name) ?? new Set());
435
+ const n = family.length;
436
+ const score = tied.map((_, i) => {
437
+ let s = 0;
438
+ for (const w of said) {
439
+ if (!own[i].has(w) || own.every(o => o.has(w))) continue;
440
+ const d = df(w);
441
+ s += Math.log(1 + (n - d + 0.5) / (d + 0.5));
442
+ }
443
+ return s;
444
+ });
445
+ const top = Math.max(...score);
446
+ if (top === 0) return typicalOfTie(name, ranked, cands);
447
+ const leaders = tied.filter((_, i) => score[i] >= top - 1e-9);
448
+ if (leaders.length === 1) return leaders[0].name;
449
+ const lead = leaders.some(r => r.name === name) ? self : leaders[0];
450
+ return typicalOfTie(
451
+ lead.name,
452
+ ranked,
453
+ leaders.map(r => r.name),
454
+ );
455
+ }
456
+
333
457
  /**
334
458
  * Every ready, non-shell template in the blend's own order, best first: the
335
459
  * order the page start is chosen from, so the alternatives build names beside
@@ -401,15 +525,22 @@ export function weighStart(
401
525
  const said = new Set(content.map(stem));
402
526
  const mine = ownWords(kept, catalog, said);
403
527
  const theirs = ownWords(listed, catalog, said);
404
- if (mine >= theirs) return kept;
405
528
  // An idea that names the template's family before the listed one's
406
529
  // ("a dashboard with a data table") is that family's page, unless it
407
530
  // names far more of the listed template's words.
408
531
  const order = content.map(stem);
409
- return familyAt(kept, catalog, order) < familyAt(listed, catalog, order) &&
410
- mine >= theirs - FAMILY_SLACK
411
- ? kept
412
- : listed;
532
+ const stays =
533
+ mine >= theirs ||
534
+ (familyAt(kept, catalog, order) < familyAt(listed, catalog, order) &&
535
+ mine >= theirs - FAMILY_SLACK);
536
+ if (!stays) return listed;
537
+ // A tie in the ranker is no preference: every table that names "table"
538
+ // scores the same, and the alphabet must not choose the page. Among the
539
+ // family's templates the ranker ties with this unlisted one, the template
540
+ // whose own descriptor best matches the idea starts (`fullerOfTie`), then
541
+ // the one most typical of the family. Only the templates' descriptors
542
+ // decide this tie, never the tables.
543
+ return fullerOfTie(kept, idea, ranked, cands, catalog);
413
544
  }
414
545
  const top = best(() => true);
415
546
  if (top !== SHELL) return top;
@@ -69,5 +69,6 @@ export type RankedPage = {
69
69
  family: string;
70
70
  base: boolean;
71
71
  matched: Set<string>;
72
+ typical: number;
72
73
  };
73
74
  export type IdeaKind = "page" | "part" | "edit";
@@ -163,7 +163,7 @@ export const SYNONYMS_OF = (() => {
163
163
  /**
164
164
  * @typedef {import('../_adapter.mjs').PageTemplate} PageTemplate
165
165
  * @typedef {import('../_adapter.mjs').ComponentWords} ComponentWords
166
- * @typedef {{name: string, score: number, hits: number, familyNamed: boolean, headNamed: boolean, containerMatched: boolean, family: string, base: boolean, matched: Set<string>}} RankedPage
166
+ * @typedef {{name: string, score: number, hits: number, familyNamed: boolean, headNamed: boolean, containerMatched: boolean, family: string, base: boolean, matched: Set<string>, typical: number}} RankedPage
167
167
  * @typedef {'page' | 'part' | 'edit'} IdeaKind
168
168
  */
169
169