@astryxdesign/cli 0.6.6-canary.6ea1fb7 → 0.6.6-canary.7c8ddf2

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 (176) hide show
  1. package/README.md +83 -83
  2. package/api/build/_adapter.d.mts +15 -0
  3. package/api/build/_adapter.mjs +22 -0
  4. package/api/build/build.doc.mjs +1 -1
  5. package/api/build/build.type.d.mts +4 -0
  6. package/api/build/build.type.mjs +1 -0
  7. package/api/build/help/help.mjs +1 -1
  8. package/api/build/kit/kit.mjs +12 -1
  9. package/api/component/component-replacement.test.mjs +12 -0
  10. package/api/component/component.doc.mjs +5 -2
  11. package/api/docs/_adapter.mjs +48 -1
  12. package/api/docs/compiled-topics.test.mjs +1 -1
  13. package/api/docs/detail/section/section.d.mts +44 -0
  14. package/api/docs/detail/section/section.mjs +164 -13
  15. package/api/docs/detail/section/section.test.mjs +4 -4
  16. package/api/docs/docOverlays.test.mjs +28 -17
  17. package/api/docs/docs.doc.mjs +3 -3
  18. package/api/docs/docs.mjs +11 -16
  19. package/api/docs/docs.test.mjs +9 -10
  20. package/api/docs/integration-tree.test.mjs +26 -26
  21. package/api/docs/integrationDocs.test.mjs +2 -2
  22. package/api/docs/namespace-section.test.mjs +260 -0
  23. package/api/docs/token-tables.test.mjs +93 -0
  24. package/api/doctor/doctor.d.mts +6 -0
  25. package/api/doctor/doctor.mjs +149 -40
  26. package/api/doctor/doctor.test.mjs +104 -0
  27. package/api/doctor/theme-checks.mjs +1 -1
  28. package/api/gap-report/gap-report.doc.mjs +2 -1
  29. package/api/init/run/run.mjs +1 -0
  30. package/api/integration/validate-integration.mjs +20 -4
  31. package/api/integration/validate-integration.test.mjs +73 -0
  32. package/api/layout/check/check.d.mts +1 -0
  33. package/api/layout/check/check.mjs +1 -0
  34. package/api/layout/expand/expand.d.mts +1 -0
  35. package/api/layout/expand/expand.mjs +1 -0
  36. package/api/layout/grammar/grammar.d.mts +1 -0
  37. package/api/layout/grammar/grammar.mjs +1 -0
  38. package/api/layout/layoutCheck.doc.mjs +1 -1
  39. package/api/layout/layoutExpand.doc.mjs +1 -1
  40. package/api/layout/layoutGrammar.doc.mjs +1 -1
  41. package/api/search/search.d.mts +44 -0
  42. package/api/search/search.domain-priority.test.mjs +261 -0
  43. package/api/search/search.mjs +191 -29
  44. package/api/swizzle/swizzle.doc.mjs +5 -2
  45. package/api/template/copy/copy.mjs +12 -0
  46. package/api/template/template.doc.mjs +1 -1
  47. package/api/template/template.type.d.mts +3 -0
  48. package/api/template/template.type.mjs +3 -0
  49. package/api/theme/build/build.d.mts +70 -0
  50. package/api/theme/build/build.mjs +205 -41
  51. package/api/theme/build/font-warning.d.mts +11 -0
  52. package/api/theme/build/font-warning.mjs +28 -3
  53. package/assets/docs/author-a-theme.doc.mjs +16 -11
  54. package/assets/docs/getting-started.doc.mjs +2 -2
  55. package/assets/docs/principles.doc.dense.mjs +3 -3
  56. package/assets/docs/principles.doc.mjs +4 -4
  57. package/assets/docs/principles.doc.zh.mjs +3 -3
  58. package/assets/docs/styling-overview.doc.mjs +6 -6
  59. package/assets/docs/theme.doc.dense.mjs +1 -1
  60. package/assets/docs/theme.doc.zh.mjs +1 -1
  61. package/assets/docs/tree/component-migration.doc.mjs +107 -0
  62. package/assets/docs/tree/css-and-stylex.doc.mjs +150 -0
  63. package/assets/docs/tree/font-setup.doc.mjs +122 -0
  64. package/assets/docs/tree/headers-and-footers.doc.mjs +0 -1
  65. package/assets/docs/tree/i18n-library-integration.doc.mjs +167 -0
  66. package/assets/docs/tree/i18n-quick-start.doc.mjs +101 -0
  67. package/assets/docs/{internationalization.doc.mjs → tree/i18n-rtl-and-testing.doc.mjs} +17 -223
  68. package/assets/docs/tree/internationalization.doc.mjs +29 -0
  69. package/assets/docs/tree/layout.doc.mjs +23 -0
  70. package/assets/docs/{migration.doc.mjs → tree/migration-setup.doc.mjs} +24 -129
  71. package/assets/docs/tree/migration-verification.doc.mjs +67 -0
  72. package/assets/docs/tree/migration.doc.mjs +28 -0
  73. package/assets/docs/tree/non-css-processing.doc.mjs +112 -0
  74. package/assets/docs/tree/other-libraries.doc.mjs +218 -0
  75. package/assets/docs/tree/responsive.doc.mjs +0 -4
  76. package/assets/docs/tree/scaffold.doc.mjs +0 -5
  77. package/assets/docs/tree/side-panels.doc.mjs +0 -1
  78. package/assets/docs/tree/structure.doc.mjs +0 -4
  79. package/assets/docs/tree/styling-advanced.doc.mjs +159 -0
  80. package/assets/docs/tree/styling-basics.doc.mjs +181 -0
  81. package/assets/docs/tree/styling-libraries.doc.mjs +30 -0
  82. package/assets/docs/tree/styling.doc.mjs +29 -0
  83. package/assets/docs/tree/tailwind.doc.mjs +65 -0
  84. package/assets/docs/tree/tokens-and-setup.doc.mjs +128 -0
  85. package/assets/docs/tree/tokens-border.doc.mjs +46 -0
  86. package/assets/docs/tree/tokens-color.doc.dense.mjs +10 -0
  87. package/assets/docs/tree/tokens-color.doc.mjs +438 -0
  88. package/assets/docs/tree/tokens-color.doc.zh.mjs +10 -0
  89. package/assets/docs/tree/tokens-data.doc.mjs +323 -0
  90. package/assets/docs/tree/tokens-duration.doc.mjs +78 -0
  91. package/assets/docs/tree/tokens-ease.doc.mjs +46 -0
  92. package/assets/docs/tree/tokens-focus.doc.mjs +57 -0
  93. package/assets/docs/tree/tokens-font-weight.doc.mjs +58 -0
  94. package/assets/docs/tree/tokens-radius.doc.dense.mjs +10 -0
  95. package/assets/docs/tree/tokens-radius.doc.mjs +70 -0
  96. package/assets/docs/tree/tokens-radius.doc.zh.mjs +10 -0
  97. package/assets/docs/tree/tokens-shadow.doc.dense.mjs +10 -0
  98. package/assets/docs/tree/tokens-shadow.doc.mjs +74 -0
  99. package/assets/docs/tree/tokens-shadow.doc.zh.mjs +10 -0
  100. package/assets/docs/tree/tokens-size.doc.dense.mjs +10 -0
  101. package/assets/docs/tree/tokens-size.doc.mjs +54 -0
  102. package/assets/docs/tree/tokens-size.doc.zh.mjs +10 -0
  103. package/assets/docs/tree/tokens-spacing.doc.dense.mjs +10 -0
  104. package/assets/docs/tree/tokens-spacing.doc.mjs +102 -0
  105. package/assets/docs/tree/tokens-spacing.doc.zh.mjs +10 -0
  106. package/assets/docs/tree/tokens-syntax.doc.mjs +98 -0
  107. package/assets/docs/tree/tokens-text-size.doc.mjs +90 -0
  108. package/assets/docs/tree/tokens-type-scale.doc.mjs +210 -0
  109. package/assets/docs/tree/tokens-typography.doc.mjs +54 -0
  110. package/assets/docs/tree/tokens-usage.doc.dense.mjs +10 -0
  111. package/assets/docs/tree/tokens-usage.doc.mjs +36 -0
  112. package/assets/docs/tree/tokens-usage.doc.zh.mjs +10 -0
  113. package/assets/docs/tree/tokens.doc.mjs +28 -0
  114. package/assets/docs/tree/type-scale.doc.mjs +147 -0
  115. package/assets/docs/tree/typography-best-practices.doc.mjs +55 -0
  116. package/assets/docs/tree/typography.doc.mjs +29 -0
  117. package/assets/docs/use-a-theme.doc.mjs +6 -3
  118. package/assets/templates/pages/incident-console/page.tsx +1 -1
  119. package/assets/theme.template.ts +4 -3
  120. package/clients/cli/commands/blog.mjs +3 -3
  121. package/clients/cli/commands/build-theme.adaptations.test.mjs +1 -1
  122. package/clients/cli/commands/build-theme.ascii-output.test.mjs +11 -1
  123. package/clients/cli/commands/build-theme.batch-report.test.mjs +335 -0
  124. package/clients/cli/commands/build-theme.font-warning.test.mjs +36 -7
  125. package/clients/cli/commands/build-theme.import-path.test.mjs +3 -3
  126. package/clients/cli/commands/build-theme.mjs +68 -18
  127. package/clients/cli/commands/build-theme.watch.test.mjs +33 -0
  128. package/clients/cli/commands/build.mjs +1 -0
  129. package/clients/cli/commands/component/index.mjs +4 -1
  130. package/clients/cli/commands/component-replacement.text.test.mjs +45 -0
  131. package/clients/cli/commands/docs.doc.mjs +2 -1
  132. package/clients/cli/commands/docs.test.mjs +15 -12
  133. package/clients/cli/commands/doctor-integration-components.doc.mjs +1 -1
  134. package/clients/cli/commands/doctor.test.mjs +50 -1
  135. package/clients/cli/commands/import-hint-correctness.test.mjs +23 -0
  136. package/clients/cli/commands/layout-check.doc.mjs +1 -0
  137. package/clients/cli/commands/layout-expand.doc.mjs +1 -0
  138. package/clients/cli/commands/layout-grammar.doc.mjs +1 -0
  139. package/clients/cli/commands/layout.deprecation-lifecycle.test.mjs +140 -0
  140. package/clients/cli/commands/layout.doc.mjs +1 -0
  141. package/clients/cli/commands/layout.mjs +6 -11
  142. package/clients/cli/commands/search.test.mjs +6 -2
  143. package/clients/cli/commands/swizzle.routing.test.mjs +2 -1
  144. package/clients/cli/commands/template.mjs +4 -3
  145. package/clients/cli/commands/theme-add-lifecycle.test.mjs +9 -6
  146. package/clients/cli/commands/theme-build.doc.mjs +3 -1
  147. package/clients/cli/commands/upgrade.config-ordering.test.mjs +2 -1
  148. package/clients/cli/index.mjs +1 -1
  149. package/clients/cli/lib/component-format.mjs +13 -3
  150. package/clients/cli/lib/component-format.test.mjs +1 -1
  151. package/clients/cli/lib/define-command.mjs +4 -0
  152. package/foundation/agent-docs/agent-docs.mjs +18 -2
  153. package/foundation/agent-docs/agent-docs.test.mjs +18 -0
  154. package/foundation/config/project.test.mjs +1 -1
  155. package/foundation/discovery/docs-discovery.test.mjs +1 -1
  156. package/foundation/discovery/template-needs.d.mts +31 -0
  157. package/foundation/discovery/template-needs.mjs +156 -0
  158. package/foundation/discovery/template-needs.test.mjs +203 -0
  159. package/foundation/doc-compiler/bundle.mjs +52 -1
  160. package/foundation/doc-compiler/split-topic-keywords.test.mjs +73 -0
  161. package/foundation/doc-compiler/tree.d.mts +10 -0
  162. package/foundation/doc-compiler/tree.mjs +22 -0
  163. package/foundation/doc-compiler/tree.test.mjs +3 -2
  164. package/foundation/env/package-manager.d.mts +31 -8
  165. package/foundation/env/package-manager.mjs +120 -13
  166. package/foundation/env/package-manager.test.mjs +174 -1
  167. package/foundation/integrations/validate-contributions.d.mts +16 -0
  168. package/foundation/integrations/validate-contributions.mjs +56 -8
  169. package/foundation/response/response-types.doc.mjs +4 -4
  170. package/package.json +9 -9
  171. package/assets/docs/styling-libraries.doc.mjs +0 -469
  172. package/assets/docs/styling.doc.mjs +0 -412
  173. package/assets/docs/tokens.doc.dense.mjs +0 -15
  174. package/assets/docs/tokens.doc.mjs +0 -1491
  175. package/assets/docs/tokens.doc.zh.mjs +0 -15
  176. package/assets/docs/typography.doc.mjs +0 -286
@@ -24,6 +24,50 @@ export function sameWord(a: string, b: string): boolean;
24
24
  * @returns {string[]}
25
25
  */
26
26
  export function tokenizeQuery(term: string): string[];
27
+ /**
28
+ * A match's domain priority: 0 (none), 1, or 2 (a doc the whole query names).
29
+ * A component, hook, template, or theme with priority ranks ahead of any doc
30
+ * without it, whatever the two scores are. Docs among themselves rank by
31
+ * priority, then score; everything else ranks among itself by score.
32
+ *
33
+ * A component, hook, template, or theme has priority (1) when one of the
34
+ * query's words, or the whole query, hits its name or an authored keyword at
35
+ * {@link STRONG_TOKEN_SCORE} or above, or when it matches every word.
36
+ *
37
+ * A doc has priority when the reader asked for that doc:
38
+ * - 2: the whole query is the topic's name, its last route segment, or its
39
+ * own title, as words and whatever the plural (`font setup` is
40
+ * typography/font-setup, `side panel` is "Side panels");
41
+ * - 1: a word of the query is the topic's name (`illustration` in a longer
42
+ * question is the illustrations guide);
43
+ * - 1: the query holds one of the doc's titles whole (`resizable side panels`
44
+ * names "Side panels"; `light dark mode button` names "Light/Dark Mode");
45
+ * - 1: the doc matches every word of the query and one of them is a word of
46
+ * its title (`switch to a dark theme` and "Use a theme").
47
+ * A section answers to its topic's name.
48
+ *
49
+ * A doc that matched only by keyword, by a heading holding the query, or by
50
+ * words spread through its text has no priority. Docs are split into many
51
+ * small topics, and each declares its own keywords and headings, so a common
52
+ * phrase such as `font size` hits a guide's keyword or heading exactly
53
+ * (170-190) while the component the reader is after matches one word by
54
+ * keyword (`Text`, 98). Ranked on text alone, every split adds another doc
55
+ * above the component. Broad reference pages match every word of many queries
56
+ * in their text the same way. The score stays the text-match strength the
57
+ * result reports, so callers that gate on it (`build`) see the same numbers.
58
+ *
59
+ * @param {string} term - Lowercased search term.
60
+ * @param {string[]} tokens - Content tokens from tokenizeQuery(term).
61
+ * @param {Candidate} candidate
62
+ * @param {{score: number, matched: number, total: number}} hit - The
63
+ * candidate's scoreQuery result.
64
+ * @returns {0 | 1 | 2}
65
+ */
66
+ export function domainPriority(term: string, tokens: string[], candidate: Candidate, hit: {
67
+ score: number;
68
+ matched: number;
69
+ total: number;
70
+ }): 0 | 1 | 2;
27
71
  /**
28
72
  * The first title or heading that holds every word of the query, in order and
29
73
  * side by side, or null.
@@ -0,0 +1,261 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file Domain priority in `search` ranking (api/search/search.mjs), run
5
+ * against the real docs tree and @astryxdesign/core registry.
6
+ *
7
+ * Splitting a large doc into smaller topics gives each topic its own keywords
8
+ * and headings, and a doc keyword or heading that holds the whole query scores
9
+ * 170-190, above a component that matches one word by keyword (98). Every
10
+ * split then puts another doc above the component a reader asked for. Domain
11
+ * priority ranks a component, hook, template, or theme that a query word hits
12
+ * by name or keyword ahead of docs the reader did not ask for, and keeps a doc
13
+ * the query names, or whose title the query holds, where it was.
14
+ *
15
+ * Both directions are asserted together, so neither can be traded for the
16
+ * other: component queries find the component first, and the obvious question
17
+ * for each guide still finds that guide first.
18
+ */
19
+
20
+ import {describe, it, expect} from 'vitest';
21
+ import * as path from 'node:path';
22
+ import {fileURLToPath} from 'node:url';
23
+ import {docs} from '../docs/docs.mjs';
24
+ import {domainPriority, search, scoreQuery, tokenizeQuery} from './search.mjs';
25
+
26
+ const REPO = path.resolve(
27
+ path.dirname(fileURLToPath(import.meta.url)),
28
+ '../../../..',
29
+ );
30
+ const cwd = REPO;
31
+ const SLOW = 30_000;
32
+
33
+ /**
34
+ * @param {string} q
35
+ * @param {object} candidate
36
+ * @returns {0 | 1 | 2}
37
+ */
38
+ const priority = (q, candidate) => {
39
+ const tokens = tokenizeQuery(q);
40
+ const hit = scoreQuery(q, tokens, candidate) ?? {
41
+ score: 0,
42
+ matched: 0,
43
+ total: 1,
44
+ };
45
+ return domainPriority(q, tokens, /** @type {any} */ (candidate), hit);
46
+ };
47
+
48
+ /**
49
+ * Every placed guide under a namespace, walked through `docs`.
50
+ * @param {string} route
51
+ * @param {Set<string>} [seen]
52
+ * @returns {Promise<string[]>}
53
+ */
54
+ async function placedGuides(route, seen = new Set()) {
55
+ /** @type {any} */
56
+ let node;
57
+ try {
58
+ node = (await docs(route)).data;
59
+ } catch {
60
+ return [];
61
+ }
62
+ /** @type {string[]} */
63
+ const out = [];
64
+ for (const child of (node.slots ?? []).flatMap(
65
+ (/** @type {any} */ slot) => slot.children,
66
+ )) {
67
+ if (!child.route || seen.has(child.route)) continue;
68
+ seen.add(child.route);
69
+ if (child.kind === 'generic' && child.route.includes('/')) {
70
+ out.push(child.route);
71
+ }
72
+ out.push(...(await placedGuides(child.route, seen)));
73
+ }
74
+ return out;
75
+ }
76
+
77
+ describe('search domain priority — the rule', () => {
78
+ it('gives a doc no priority for a keyword or heading match', () => {
79
+ const topic = {
80
+ domain: 'doc',
81
+ name: 'typography/font-setup',
82
+ keywords: ['font size'],
83
+ titles: ['Font Setup', 'Font Sizes'],
84
+ };
85
+ expect(priority('font size', topic)).toBe(0);
86
+ });
87
+
88
+ it('gives a doc priority when the query names it', () => {
89
+ const topic = {domain: 'doc', name: 'typography/font-setup'};
90
+ // The whole query spells its last route segment.
91
+ expect(priority('font setup', topic)).toBe(2);
92
+ expect(priority('typography/font-setup', topic)).toBe(2);
93
+ // A word of a longer query is a flat topic's name, or its plural.
94
+ expect(
95
+ priority('product illustration', {domain: 'doc', name: 'illustrations'}),
96
+ ).toBe(1);
97
+ });
98
+
99
+ it('names a doc by its route or title whatever the plural', () => {
100
+ const guide = {
101
+ domain: 'doc',
102
+ name: 'layout/side-panels',
103
+ titles: ['Side panels'],
104
+ };
105
+ expect(priority('side panel', guide)).toBe(2);
106
+ expect(priority('side panels', guide)).toBe(2);
107
+ expect(priority('side panels', {...guide, name: 'layout/side-panel'})).toBe(
108
+ 2,
109
+ );
110
+ // The topic's own title names it even where the route words differ.
111
+ const titled = {
112
+ domain: 'doc',
113
+ name: 'guides/lookups',
114
+ titles: ['Looking up components'],
115
+ };
116
+ expect(priority('looking up component', titled)).toBe(2);
117
+ // A section's title is a heading inside its topic, not the topic's name.
118
+ const section = {
119
+ domain: 'doc',
120
+ name: 'font-sizes',
121
+ _topic: 'typography',
122
+ _section: 'font-sizes',
123
+ titles: ['Font Sizes'],
124
+ };
125
+ expect(priority('font size', section)).toBe(0);
126
+ });
127
+
128
+ it('names a section by its topic', () => {
129
+ // A section candidate's own name is its section key; the reader names the
130
+ // topic it belongs to.
131
+ const section = {
132
+ domain: 'doc',
133
+ name: 'drop-shadows',
134
+ _topic: 'elevation',
135
+ titles: ['Drop shadows'],
136
+ keywords: ['Drop shadows'],
137
+ };
138
+ expect(priority('elevation', section)).toBe(2);
139
+ expect(priority('elevation for a raised tile', section)).toBe(1);
140
+ });
141
+
142
+ it('gives a doc priority when the query holds one of its titles whole', () => {
143
+ const guide = {
144
+ domain: 'doc',
145
+ name: 'layout/side-panels',
146
+ titles: ['Side panels'],
147
+ };
148
+ expect(priority('resizable side panels with a toolbar', guide)).toBe(1);
149
+ });
150
+
151
+ it('gives a doc that matches every word priority only when its title names one', () => {
152
+ const titled = {
153
+ domain: 'doc',
154
+ name: 'use-a-theme',
155
+ titles: ['Use a theme'],
156
+ keywords: ['switcher', 'night'],
157
+ description: 'Apply a theme to your app.',
158
+ };
159
+ expect(priority('night theme switcher', titled)).toBe(1);
160
+ // A reference page that holds every word, but whose title names none.
161
+ const reference = {
162
+ domain: 'doc',
163
+ name: 'api/reference',
164
+ titles: ['Response shapes'],
165
+ keywords: ['switcher', 'night', 'theme'],
166
+ };
167
+ expect(priority('night theme switcher', reference)).toBe(0);
168
+ });
169
+
170
+ it('gives a component priority for a name or keyword hit, not for prose', () => {
171
+ expect(
172
+ priority('font size', {
173
+ domain: 'component',
174
+ name: 'Text',
175
+ keywords: ['font'],
176
+ }),
177
+ ).toBe(1);
178
+ expect(
179
+ priority('date picker', {domain: 'component', name: 'DatePicker'}),
180
+ ).toBe(1);
181
+ expect(
182
+ priority('font size', {
183
+ domain: 'component',
184
+ name: 'Card',
185
+ description: 'A surface whose font size follows its parent.',
186
+ }),
187
+ ).toBe(0);
188
+ });
189
+ });
190
+
191
+ describe('search domain priority — real docs and components', () => {
192
+ it(
193
+ 'finds the Text component before the typography guides for `font size`',
194
+ async () => {
195
+ for (const q of ['font size', 'font weight']) {
196
+ const results = (await search(q, {cwd})).data.results;
197
+ expect(results[0], q).toMatchObject({
198
+ domain: 'component',
199
+ name: 'Text',
200
+ });
201
+ const guide = results.findIndex(
202
+ r => r.domain === 'doc' && /^typography(?:\/|$)/.test(r.name),
203
+ );
204
+ // The guide still matches, below the component.
205
+ expect(guide, q).toBeGreaterThan(0);
206
+ }
207
+ },
208
+ SLOW,
209
+ );
210
+
211
+ it(
212
+ 'reports the text-match score unchanged',
213
+ async () => {
214
+ const results = (await search('font size', {cwd})).data.results;
215
+ const guide = results.find(
216
+ r => r.domain === 'doc' && /^typography(?:\/|$)/.test(r.name),
217
+ );
218
+ expect(guide?.score).toBeGreaterThan(results[0].score);
219
+ },
220
+ SLOW,
221
+ );
222
+
223
+ it(
224
+ 'still finds a flat guide first by its name',
225
+ async () => {
226
+ for (const [q, name] of [
227
+ ['motion', 'motion'],
228
+ ['icons', 'icons'],
229
+ ['tokens', 'tokens'],
230
+ ['illustration', 'illustrations'],
231
+ ['use a theme', 'use-a-theme'],
232
+ ['author a theme', 'author-a-theme'],
233
+ ]) {
234
+ const results = (await search(q, {cwd})).data.results;
235
+ expect(results[0], q).toMatchObject({domain: 'doc', name});
236
+ }
237
+ },
238
+ SLOW,
239
+ );
240
+
241
+ it(
242
+ 'still finds each layout guide first by its own name',
243
+ async () => {
244
+ // Each search reads the whole index, so this checks one split namespace
245
+ // (layout, six guides) rather than every guide in the tree.
246
+ const guides = await placedGuides('layout');
247
+ expect(guides).toContain('layout/layout-spacing');
248
+ /** @type {string[]} */
249
+ const misses = [];
250
+ for (const route of guides) {
251
+ const q = route.slice(route.lastIndexOf('/') + 1).replace(/-/g, ' ');
252
+ const top = (await search(q, {cwd})).data.results[0];
253
+ if (top?.domain !== 'doc' || top.name !== route) {
254
+ misses.push(`${q} -> ${top?.domain}:${top?.name}`);
255
+ }
256
+ }
257
+ expect(misses).toEqual([]);
258
+ },
259
+ SLOW,
260
+ );
261
+ });
@@ -54,6 +54,13 @@
54
54
  * description hold both words of `troubleshoot integration` answers it better
55
55
  * than a doc named `integration`.
56
56
  *
57
+ * Between domains, ranking adds one rule ahead of the score: domain priority
58
+ * (see {@link domainPriority}). A component, hook, template, or theme whose
59
+ * name or keyword a query word hits ranks ahead of every doc the reader did not
60
+ * ask for by name or title, so `font size` finds `Text` before a typography
61
+ * topic that declares the phrase. A doc the query names, or whose title the
62
+ * query holds, keeps its place.
63
+ *
57
64
  * Description and guidance are separate tiers on purpose. A component's own
58
65
  * one-line description saying "notification" is a claim about what it IS; the
59
66
  * same word inside another component's best-practice advice is a passing
@@ -476,13 +483,110 @@ const FULL_COVERAGE_SCORE = 151;
476
483
  */
477
484
  const STRONG_TOKEN_SCORE = 70;
478
485
 
486
+ /**
487
+ * A match's domain priority: 0 (none), 1, or 2 (a doc the whole query names).
488
+ * A component, hook, template, or theme with priority ranks ahead of any doc
489
+ * without it, whatever the two scores are. Docs among themselves rank by
490
+ * priority, then score; everything else ranks among itself by score.
491
+ *
492
+ * A component, hook, template, or theme has priority (1) when one of the
493
+ * query's words, or the whole query, hits its name or an authored keyword at
494
+ * {@link STRONG_TOKEN_SCORE} or above, or when it matches every word.
495
+ *
496
+ * A doc has priority when the reader asked for that doc:
497
+ * - 2: the whole query is the topic's name, its last route segment, or its
498
+ * own title, as words and whatever the plural (`font setup` is
499
+ * typography/font-setup, `side panel` is "Side panels");
500
+ * - 1: a word of the query is the topic's name (`illustration` in a longer
501
+ * question is the illustrations guide);
502
+ * - 1: the query holds one of the doc's titles whole (`resizable side panels`
503
+ * names "Side panels"; `light dark mode button` names "Light/Dark Mode");
504
+ * - 1: the doc matches every word of the query and one of them is a word of
505
+ * its title (`switch to a dark theme` and "Use a theme").
506
+ * A section answers to its topic's name.
507
+ *
508
+ * A doc that matched only by keyword, by a heading holding the query, or by
509
+ * words spread through its text has no priority. Docs are split into many
510
+ * small topics, and each declares its own keywords and headings, so a common
511
+ * phrase such as `font size` hits a guide's keyword or heading exactly
512
+ * (170-190) while the component the reader is after matches one word by
513
+ * keyword (`Text`, 98). Ranked on text alone, every split adds another doc
514
+ * above the component. Broad reference pages match every word of many queries
515
+ * in their text the same way. The score stays the text-match strength the
516
+ * result reports, so callers that gate on it (`build`) see the same numbers.
517
+ *
518
+ * @param {string} term - Lowercased search term.
519
+ * @param {string[]} tokens - Content tokens from tokenizeQuery(term).
520
+ * @param {Candidate} candidate
521
+ * @param {{score: number, matched: number, total: number}} hit - The
522
+ * candidate's scoreQuery result.
523
+ * @returns {0 | 1 | 2}
524
+ */
525
+ export function domainPriority(term, tokens, candidate, hit) {
526
+ const words = [term, ...tokens];
527
+ if (candidate.domain === 'doc') {
528
+ const topic = String(candidate._topic ?? candidate.name).toLowerCase();
529
+ // The whole query, read as words, is the topic's route, its last
530
+ // segment, or its own title, whatever the plural: `side panel` is
531
+ // layout/side-panels, and `header and footer` is "Headers and footers".
532
+ // A section's title is a heading inside a topic, not the topic's name.
533
+ const named = [topic, topic.slice(topic.lastIndexOf('/') + 1)];
534
+ if (candidate._section == null && candidate.titles?.[0]) {
535
+ named.push(candidate.titles[0]);
536
+ }
537
+ if (named.some(n => samePhrase(term, n))) {
538
+ return 2;
539
+ }
540
+ if (
541
+ words.some(
542
+ w =>
543
+ (scoreCandidate(w, {name: topic}, {fuzzy: false})?.score ?? 0) >= 95,
544
+ )
545
+ ) {
546
+ return 1;
547
+ }
548
+ // A whole title inside the query is the only path to 160-169.
549
+ if (hit.score >= TITLE_IN_QUERY_SCORE && hit.score < TITLE_PHRASE_SCORE) {
550
+ return 1;
551
+ }
552
+ // Every word matched, and one of them names the doc in its title.
553
+ if (
554
+ hit.score >= FULL_COVERAGE_SCORE &&
555
+ hit.score < TITLE_IN_QUERY_SCORE &&
556
+ hit.matched === hit.total
557
+ ) {
558
+ const titleWords = phraseWords(candidate.titles?.[0] ?? '');
559
+ if (
560
+ (tokens.length ? tokens : [term]).some(t =>
561
+ titleWords.some(w => samePhraseWord(t, w) || sameWord(t, w)),
562
+ )
563
+ ) {
564
+ return 1;
565
+ }
566
+ }
567
+ return 0;
568
+ }
569
+ if (hit.score >= FULL_COVERAGE_SCORE) return 1;
570
+ const fuzzy = tokens.length <= 1;
571
+ return words.some(
572
+ w =>
573
+ (bestForToken(w, candidate, {fuzzy})?.score ?? 0) >= STRONG_TOKEN_SCORE,
574
+ )
575
+ ? 1
576
+ : 0;
577
+ }
578
+
479
579
  /**
480
580
  * The words of a title or query, lowercased, without punctuation or code ticks.
481
581
  * @param {string} text
482
582
  * @returns {string[]}
483
583
  */
484
584
  function phraseWords(text) {
485
- return unlinkText(text).toLowerCase().match(/[a-z0-9]+/g) ?? [];
585
+ return (
586
+ unlinkText(text)
587
+ .toLowerCase()
588
+ .match(/[a-z0-9]+/g) ?? []
589
+ );
486
590
  }
487
591
 
488
592
  /**
@@ -501,6 +605,24 @@ function samePhraseWord(a, b) {
501
605
  );
502
606
  }
503
607
 
608
+ /**
609
+ * Whether two phrases are the same words, in order, ignoring case,
610
+ * punctuation, and a plural on any word: `side panel` is "Side panels", and
611
+ * `headers and footers` is `header-and-footer`.
612
+ * @param {string} a
613
+ * @param {string} b
614
+ * @returns {boolean}
615
+ */
616
+ function samePhrase(a, b) {
617
+ const x = phraseWords(a);
618
+ const y = phraseWords(b);
619
+ return (
620
+ x.length > 0 &&
621
+ x.length === y.length &&
622
+ x.every((w, k) => samePhraseWord(w, y[k]) || sameWord(w, y[k]))
623
+ );
624
+ }
625
+
504
626
  /**
505
627
  * Whether `plural` is the plural of `word`: `integrations` of `integration`,
506
628
  * `boxes` of `box`. `es` only follows s, x, z, ch, or sh, so `notes` is not a
@@ -679,9 +801,7 @@ export function scoreQuery(term, tokens, candidate) {
679
801
  score:
680
802
  FULL_COVERAGE_SCORE +
681
803
  Math.min(
682
- Math.floor(
683
- (tokenSum - matched * MIN_TOKEN_SCORE) / (matched * 5),
684
- ),
804
+ Math.floor((tokenSum - matched * MIN_TOKEN_SCORE) / (matched * 5)),
685
805
  8,
686
806
  ),
687
807
  reason,
@@ -1115,17 +1235,15 @@ export async function componentKeywords(coreDir, cwd) {
1115
1235
  coreDir,
1116
1236
  loadedIntegrations,
1117
1237
  );
1118
- const integrations = loadedIntegrations.map(
1119
- async integration => {
1120
- const {components} = await discoverValidIntegrationComponents(integration);
1121
- return Promise.all(
1122
- components.map(async rec => ({
1123
- name: rec.name,
1124
- keywords: keywordsOf(await loadModuleDoc(rec.docPath)),
1125
- })),
1126
- );
1127
- },
1128
- );
1238
+ const integrations = loadedIntegrations.map(async integration => {
1239
+ const {components} = await discoverValidIntegrationComponents(integration);
1240
+ return Promise.all(
1241
+ components.map(async rec => ({
1242
+ name: rec.name,
1243
+ keywords: keywordsOf(await loadModuleDoc(rec.docPath)),
1244
+ })),
1245
+ );
1246
+ });
1129
1247
  return [
1130
1248
  ...(await Promise.all(core)).filter(
1131
1249
  component => !replacements.forTarget(component.name),
@@ -1221,7 +1339,11 @@ async function gatherDocs(cwd) {
1221
1339
  const packages = new Map([
1222
1340
  [entry.providerId ?? entry.package, entry.package],
1223
1341
  ...entry.extensions.map(
1224
- ext => /** @type {[string, string]} */ ([ext.providerId ?? ext.package, ext.package]),
1342
+ ext =>
1343
+ /** @type {[string, string]} */ ([
1344
+ ext.providerId ?? ext.package,
1345
+ ext.package,
1346
+ ]),
1225
1347
  ),
1226
1348
  ]);
1227
1349
  candidates.push(
@@ -1299,7 +1421,8 @@ async function gatherDocs(cwd) {
1299
1421
  _topic: node.route,
1300
1422
  _title: path.join(' › '),
1301
1423
  _command: `astryx docs ${node.route}`,
1302
- _parent: node.parent == null ? 'astryx docs' : `astryx docs ${node.parent}`,
1424
+ _parent:
1425
+ node.parent == null ? 'astryx docs' : `astryx docs ${node.parent}`,
1303
1426
  _package: node.provider,
1304
1427
  });
1305
1428
  }
@@ -1463,7 +1586,9 @@ function topicCandidates(
1463
1586
  _title: `${docTitle} › ${section.title}`,
1464
1587
  _command: `astryx docs ${name} ${key}`,
1465
1588
  _parent: `astryx docs ${name} --index`,
1466
- ...((sectionPackage?.(key) ?? pkg) ? {_package: sectionPackage?.(key) ?? pkg} : {}),
1589
+ ...((sectionPackage?.(key) ?? pkg)
1590
+ ? {_package: sectionPackage?.(key) ?? pkg}
1591
+ : {}),
1467
1592
  });
1468
1593
  }
1469
1594
  return out;
@@ -1711,24 +1836,61 @@ export async function search(query, options = {}) {
1711
1836
  // role (page / block / component) and takes the top of each, so there's no
1712
1837
  // cross-role competition to engineer — a target page only needs to be the
1713
1838
  // strongest PAGE, not outrank every component.
1714
- const scored = [];
1839
+ /** @type {{result: any, priority: 0 | 1 | 2}[]} */
1840
+ const ranked = [];
1715
1841
  for (const candidate of all) {
1716
1842
  const hit = scoreQuery(term, tokens, candidate);
1717
1843
  if (hit)
1718
- scored.push(
1719
- toResult(candidate, hit.score, hit.reason, hit.matched, hit.total),
1720
- );
1844
+ ranked.push({
1845
+ result: toResult(
1846
+ candidate,
1847
+ hit.score,
1848
+ hit.reason,
1849
+ hit.matched,
1850
+ hit.total,
1851
+ ),
1852
+ priority: domainPriority(term, tokens, candidate, hit),
1853
+ });
1721
1854
  }
1722
1855
 
1723
- // Sort by score desc, then domain (stable order), then name.
1856
+ // Docs and everything else are each sorted by score desc, then domain
1857
+ // (stable order), then name. The two are merged: at each step the stronger
1858
+ // head goes next, except that a head with domain priority goes ahead of a
1859
+ // doc head without it (see domainPriority). Each side keeps its own order.
1724
1860
  /** @type {Record<string, number>} */
1725
1861
  const domainOrder = {component: 0, hook: 1, doc: 2, template: 3, theme: 4};
1726
- scored.sort(
1727
- (a, b) =>
1728
- b.score - a.score ||
1729
- (domainOrder[a.domain] ?? 9) - (domainOrder[b.domain] ?? 9) ||
1730
- a.name.localeCompare(b.name),
1731
- );
1862
+ /**
1863
+ * @param {{result: any}} x
1864
+ * @param {{result: any}} y
1865
+ */
1866
+ const byScore = ({result: a}, {result: b}) =>
1867
+ b.score - a.score ||
1868
+ (domainOrder[a.domain] ?? 9) - (domainOrder[b.domain] ?? 9) ||
1869
+ a.name.localeCompare(b.name);
1870
+ const others = ranked.filter(r => r.result.domain !== 'doc').sort(byScore);
1871
+ // Against other domains, a doc the reader asked for competes ahead of docs
1872
+ // they did not, so it is not held behind a broad page that outscores it.
1873
+ // Docs alone keep their text-match order, with a doc the whole query names
1874
+ // first.
1875
+ const docs = ranked
1876
+ .filter(r => r.result.domain === 'doc')
1877
+ .sort((x, y) =>
1878
+ others.length > 0
1879
+ ? y.priority - x.priority || byScore(x, y)
1880
+ : Number(y.priority === 2) - Number(x.priority === 2) || byScore(x, y),
1881
+ );
1882
+ const scored = [];
1883
+ let i = 0;
1884
+ let j = 0;
1885
+ while (i < others.length || j < docs.length) {
1886
+ const other = others[i];
1887
+ const doc = docs[j];
1888
+ const otherFirst =
1889
+ !doc ||
1890
+ (other != null &&
1891
+ ((other.priority && !doc.priority) || byScore(other, doc) < 0));
1892
+ scored.push(otherFirst ? others[i++].result : docs[j++].result);
1893
+ }
1732
1894
 
1733
1895
  // `results` is bounded by `limit` so a caller (and the recorded run that
1734
1896
  // quotes it) never carries an unbounded payload. `matchCount` is the number
@@ -19,7 +19,10 @@ export const doc = {
19
19
  'It copies from the locally resolved @astryxdesign/core (or the owning integration) ' +
20
20
  'package source, rewriting imports that escape the component directory to the owner ' +
21
21
  "package's subpaths and flagging whether any copied file uses StyleX. With no name " +
22
- '(or list) it returns the swizzlable component names instead.',
22
+ '(or list) it returns the swizzlable component names instead. An integration ' +
23
+ 'component that replaces a Core component is what that Core name copies; ' +
24
+ "options.package '@astryxdesign/core' copies the original. The list names Core " +
25
+ 'components, including one an integration component replaces.',
23
26
  importPath: '@astryxdesign/cli/api',
24
27
  signature:
25
28
  'swizzle(component?: string, options?: SwizzleOptions): Promise<SwizzleListResponse | SwizzleCopyResponse>',
@@ -48,7 +51,7 @@ export const doc = {
48
51
  name: 'options.package',
49
52
  type: 'string',
50
53
  description:
51
- 'Owning package to copy from when the name is provided by more than one.',
54
+ "Owning package to copy from when the name is provided by more than one. Use '@astryxdesign/core' to copy an original replaced by an integration component.",
52
55
  },
53
56
  {
54
57
  name: 'options.list',
@@ -19,6 +19,7 @@ import {
19
19
  import {AstryxError, writeFailed} from '../../error.mjs';
20
20
  import {ERROR_CODES} from '../../../foundation/response/error-codes.mjs';
21
21
  import {pkgOf, replaceDemoMedia} from '../../../foundation/discovery/template-adapter.mjs';
22
+ import {analyzeTemplateNeeds} from '../../../foundation/discovery/template-needs.mjs';
22
23
 
23
24
  /**
24
25
  * Scaffold an already-resolved template to `targetPath` (relative to `cwd`) and
@@ -93,6 +94,14 @@ export function templateCopy(match, {targetPath, cwd, overwrite = false}) {
93
94
  throw writeFailed(outputFilePath, cwd, err);
94
95
  }
95
96
 
97
+ // Analyze what the template needs that the project lacks: external packages
98
+ // and the StyleX compiler. Derived from the source's own imports.
99
+ const {notes, missingPackages, installCommand} = analyzeTemplateNeeds(
100
+ outputSource,
101
+ cwd,
102
+ );
103
+ const missing = missingPackages;
104
+
96
105
  const relOutput = path.relative(cwd, outputDir) || '.';
97
106
  return {
98
107
  type: 'template.copy',
@@ -103,6 +112,9 @@ export function templateCopy(match, {targetPath, cwd, overwrite = false}) {
103
112
  fileName: outputFileName,
104
113
  filesCopied: 1,
105
114
  demoMediaReplaced,
115
+ notes,
116
+ missingPackages: missing,
117
+ installCommand,
106
118
  },
107
119
  };
108
120
  }
@@ -124,7 +124,7 @@ export const doc = {
124
124
  {
125
125
  type: 'template.copy',
126
126
  description:
127
- 'A receipt after scaffolding the template into the project: the template id, output directory, written file name, file count, and `demoMediaReplaced` — how many Astryx demo media references (images, posters, videos) in the written file were replaced with placeholders for you to swap for your own media.',
127
+ 'A receipt after scaffolding the template into the project: the template id, output directory, written file name, file count, `demoMediaReplaced` — how many Astryx demo media references were replaced with placeholders, `notes` — setup notes naming what the template needs that the project lacks (missing packages, missing StyleX compiler; empty when satisfied), `missingPackages` — external package names the template imports that are not in the project, and `installCommand` — a ready-to-run install command with the detected package manager and workspace version ranges (null when nothing is missing).',
128
128
  },
129
129
  {
130
130
  type: 'template.cdn',
@@ -105,6 +105,9 @@ export type TemplateCopyResponse = {
105
105
  fileName: string;
106
106
  filesCopied: number;
107
107
  demoMediaReplaced: number;
108
+ notes: string[];
109
+ missingPackages: string[];
110
+ installCommand: string | null;
108
111
  };
109
112
  };
110
113
  /**