@astryxdesign/cli 0.6.3-canary.f839b67 → 0.6.4-canary.06c8fa3

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 (189) hide show
  1. package/CHANGELOG.md +194 -0
  2. package/README.md +64 -66
  3. package/api/component/_adapter.d.mts +25 -0
  4. package/api/component/_adapter.mjs +59 -5
  5. package/api/component/component.d.mts +6 -3
  6. package/api/component/component.doc.mjs +23 -10
  7. package/api/component/component.mjs +249 -9
  8. package/api/component/component.type.d.mts +25 -0
  9. package/api/component/component.type.mjs +44 -0
  10. package/api/discover/_adapter.d.mts +114 -6
  11. package/api/discover/_adapter.mjs +372 -17
  12. package/api/discover/_adapter.test.mjs +215 -0
  13. package/api/discover/_catalog-view.d.mts +115 -0
  14. package/api/discover/_catalog-view.mjs +203 -0
  15. package/api/discover/_catalog-view.test.mjs +128 -0
  16. package/api/discover/detail/detail.d.mts +18 -6
  17. package/api/discover/detail/detail.mjs +67 -13
  18. package/api/discover/detail/detail.test.mjs +85 -0
  19. package/api/discover/detail/item/item.d.mts +26 -0
  20. package/api/discover/detail/item/item.mjs +78 -0
  21. package/api/discover/detail/item/item.test.mjs +73 -0
  22. package/api/discover/discover.d.mts +3 -9
  23. package/api/discover/discover.doc.mjs +61 -18
  24. package/api/discover/discover.mjs +220 -36
  25. package/api/discover/discover.test.mjs +11 -2
  26. package/api/discover/discover.type.d.mts +147 -8
  27. package/api/discover/discover.type.mjs +102 -12
  28. package/api/discover/list/list.d.mts +20 -6
  29. package/api/discover/list/list.mjs +45 -12
  30. package/api/discover/list/list.test.mjs +46 -0
  31. package/api/discover/search/search.d.mts +18 -16
  32. package/api/discover/search/search.mjs +102 -56
  33. package/api/discover/search/search.test.mjs +144 -10
  34. package/api/docs/_adapter.d.mts +49 -7
  35. package/api/docs/_adapter.mjs +284 -33
  36. package/api/docs/docs.doc.mjs +4 -3
  37. package/api/docs/docs.test.mjs +2 -0
  38. package/api/docs/docs.type.d.mts +21 -5
  39. package/api/docs/docs.type.mjs +21 -5
  40. package/api/docs/integration-tree.test.mjs +9 -1
  41. package/api/docs/reference-blocks.test.mjs +406 -0
  42. package/api/doctor/doctor.d.mts +8 -3
  43. package/api/doctor/doctor.mjs +95 -10
  44. package/api/doctor/doctor.test.mjs +122 -10
  45. package/api/index.d.mts +1 -2
  46. package/api/index.mjs +4 -4
  47. package/api/integration/add-helpers.d.mts +5 -2
  48. package/api/integration/add-helpers.mjs +36 -9
  49. package/api/integration/authoring-checks.mjs +14 -1
  50. package/api/integration/pack-check.lifecycle-output.test.mjs +105 -0
  51. package/api/integration/pack-check.mjs +28 -3
  52. package/api/json/index.ts +0 -1
  53. package/api/search/search.test.mjs +18 -0
  54. package/api/template/table-floating-bulk-actions.test.mjs +66 -0
  55. package/api/template/template-integration.test.mjs +1 -65
  56. package/api/template/template.mjs +1 -1
  57. package/api/theme/add/add.mjs +17 -25
  58. package/api/theme/add/add.rollback.test.mjs +158 -0
  59. package/api/theme/add/add.staging.test.mjs +40 -23
  60. package/api/theme/build/build.family.test.mjs +7 -12
  61. package/api/theme/build/build.mjs +8 -18
  62. package/api/theme/build/build.rollback.test.mjs +148 -0
  63. package/api/upgrade/run/files-changed.test.mjs +111 -0
  64. package/api/upgrade/run/run.mjs +6 -4
  65. package/api/upgrade/upgrade.type.mjs +2 -2
  66. package/assets/codemods/__tests__/registry.test.mjs +1 -0
  67. package/assets/codemods/__tests__/runner.test.mjs +3 -1
  68. package/assets/codemods/file-count.test.mjs +163 -0
  69. package/assets/codemods/integration-runner.mjs +3 -3
  70. package/assets/codemods/registry.mjs +1 -0
  71. package/assets/codemods/runner.mjs +5 -4
  72. package/assets/codemods/transforms/next/index.mjs +1 -19
  73. package/assets/codemods/transforms/v0.6.4/index.mjs +31 -0
  74. package/assets/docs/internationalization.doc.mjs +7 -5
  75. package/assets/docs/styling-libraries.doc.mjs +1 -1
  76. package/assets/docs/theme.doc.mjs +1 -1
  77. package/assets/docs/tree/component-lookups.doc.mjs +149 -0
  78. package/assets/docs/tree/integrations.doc.mjs +20 -1
  79. package/assets/templates/blocks/components/CheckboxList/CheckboxListSelectAllPattern.doc.mjs +1 -1
  80. package/assets/templates/blocks/components/CheckboxList/CheckboxListSelectAllPattern.tsx +1 -3
  81. package/assets/templates/blocks/components/InternationalizationProvider/InternationalizationProvider01ShippedLocale.tsx +1 -1
  82. package/assets/templates/blocks/components/Table/TableBulkActionsTable.doc.mjs +14 -0
  83. package/assets/templates/blocks/components/Table/TableBulkActionsTable.tsx +71 -0
  84. package/assets/templates/blocks/components/Table/TableFloatingBulkActionsTable.doc.mjs +19 -0
  85. package/assets/templates/blocks/components/Table/TableFloatingBulkActionsTable.tsx +139 -0
  86. package/authoring/config/config.doc.mjs +9 -1
  87. package/authoring/config/parse.d.mts +2 -0
  88. package/authoring/config/parse.mjs +19 -0
  89. package/authoring/config/parse.test.mjs +8 -0
  90. package/authoring/config/type.ts +13 -2
  91. package/authoring/discover/discover.doc.d.mts +13 -0
  92. package/authoring/discover/discover.doc.mjs +138 -0
  93. package/authoring/discover/parse.d.mts +24 -0
  94. package/authoring/discover/parse.mjs +128 -0
  95. package/authoring/discover/parse.test.mjs +124 -0
  96. package/authoring/discover/type.ts +87 -0
  97. package/authoring/doctypes/_schema.d.mts +72 -0
  98. package/authoring/doctypes/_schema.mjs +25 -1
  99. package/authoring/doctypes/command/command.doc.mjs +1 -1
  100. package/authoring/doctypes/command/type.ts +1 -1
  101. package/authoring/doctypes/load-contract.test.mjs +25 -0
  102. package/authoring/doctypes/reference/reference.doc.mjs +31 -4
  103. package/authoring/doctypes/reference/type.ts +20 -5
  104. package/authoring/index.d.mts +1 -0
  105. package/authoring/index.d.ts +10 -0
  106. package/authoring/index.mjs +1 -0
  107. package/clients/cli/command-result-coverage.test.mjs +7 -7
  108. package/clients/cli/commands/component/index.mjs +152 -55
  109. package/clients/cli/commands/component-batch.test.mjs +341 -0
  110. package/clients/cli/commands/component-ownership.test.mjs +89 -0
  111. package/clients/cli/commands/component.doc.mjs +23 -6
  112. package/clients/cli/commands/debug-result-summary.test.mjs +2 -2
  113. package/clients/cli/commands/discover.doc.mjs +53 -9
  114. package/clients/cli/commands/discover.mjs +393 -118
  115. package/clients/cli/commands/discover.sources.test.mjs +267 -0
  116. package/clients/cli/commands/docs.mjs +2 -2
  117. package/clients/cli/commands/docs.test.mjs +29 -0
  118. package/clients/cli/commands/doctor-integration-docs.doc.mjs +1 -1
  119. package/clients/cli/commands/text-json-parity.test.mjs +0 -17
  120. package/clients/cli/index.mjs +0 -4
  121. package/clients/cli/lib/exit-codes.test.mjs +1 -8
  122. package/clients/cli/lib/json-shim.mjs +24 -14
  123. package/clients/cli/lib/json-shim.test.mjs +6 -20
  124. package/clients/cli/lib/manifest.mjs +3 -8
  125. package/clients/cli/lib/manifest.test.mjs +5 -2
  126. package/clients/cli/lib/parse-error-format.test.mjs +81 -0
  127. package/foundation/discovery/authoring-self-docs.d.mts +12 -0
  128. package/foundation/discovery/authoring-self-docs.mjs +21 -8
  129. package/foundation/discovery/authoring-self-docs.test.mjs +7 -2
  130. package/foundation/discovery/docs-discovery.d.mts +4 -1
  131. package/foundation/discovery/docs-discovery.mjs +40 -6
  132. package/foundation/discovery/docs-discovery.test.mjs +40 -0
  133. package/foundation/discovery/template-adapter.mjs +1 -1
  134. package/foundation/doc-compiler/doc-loads.test.mjs +0 -12
  135. package/foundation/doc-compiler/lenses.d.mts +5 -3
  136. package/foundation/doc-compiler/lenses.mjs +48 -3
  137. package/foundation/doc-compiler/links.d.mts +30 -3
  138. package/foundation/doc-compiler/links.mjs +46 -5
  139. package/foundation/doc-compiler/links.test.mjs +53 -0
  140. package/foundation/doc-compiler/tree.test.mjs +9 -1
  141. package/foundation/integrations/cli-requirement.test.mjs +17 -42
  142. package/foundation/integrations/integrations.d.mts +14 -1
  143. package/foundation/integrations/integrations.mjs +41 -1
  144. package/foundation/integrations/integrations.test.mjs +31 -0
  145. package/foundation/response/batch.type.d.mts +33 -0
  146. package/foundation/response/batch.type.mjs +34 -0
  147. package/foundation/response/response-types.doc.mjs +15 -21
  148. package/foundation/response/response-types.doc.test.mjs +23 -0
  149. package/foundation/xle/browser.d.mts +3 -3
  150. package/foundation/xle/browser.mjs +3 -3
  151. package/foundation/xle/expand.mjs +2 -2
  152. package/foundation/xle/parse.mjs +1 -1
  153. package/foundation/xle/print.mjs +2 -2
  154. package/foundation/xle/splice.mjs +1 -1
  155. package/package.json +9 -9
  156. package/api/layout/_adapter.d.mts +0 -34
  157. package/api/layout/_adapter.mjs +0 -148
  158. package/api/layout/check/check.d.mts +0 -16
  159. package/api/layout/check/check.mjs +0 -40
  160. package/api/layout/expand/expand.d.mts +0 -22
  161. package/api/layout/expand/expand.mjs +0 -155
  162. package/api/layout/expand/expand.path-safety.test.mjs +0 -53
  163. package/api/layout/grammar/grammar.d.mts +0 -13
  164. package/api/layout/grammar/grammar.mjs +0 -87
  165. package/api/layout/layout.d.mts +0 -6
  166. package/api/layout/layout.mjs +0 -17
  167. package/api/layout/layout.test.mjs +0 -297
  168. package/api/layout/layout.type.d.mts +0 -89
  169. package/api/layout/layout.type.mjs +0 -103
  170. package/api/layout/layoutCheck.doc.d.mts +0 -11
  171. package/api/layout/layoutCheck.doc.mjs +0 -85
  172. package/api/layout/layoutExpand.doc.d.mts +0 -11
  173. package/api/layout/layoutExpand.doc.mjs +0 -107
  174. package/api/layout/layoutGrammar.doc.d.mts +0 -11
  175. package/api/layout/layoutGrammar.doc.mjs +0 -57
  176. package/clients/cli/commands/layout-check.doc.mjs +0 -65
  177. package/clients/cli/commands/layout-expand.doc.mjs +0 -83
  178. package/clients/cli/commands/layout-grammar.doc.mjs +0 -30
  179. package/clients/cli/commands/layout.doc.mjs +0 -34
  180. package/clients/cli/commands/layout.error-codes.test.mjs +0 -66
  181. package/clients/cli/commands/layout.exit-parity.test.mjs +0 -41
  182. package/clients/cli/commands/layout.mjs +0 -275
  183. package/clients/cli/commands/layout.path-help.test.mjs +0 -33
  184. package/clients/cli/commands/layout.stdin-cap.test.mjs +0 -47
  185. package/clients/cli/commands/layout.text-fields.test.mjs +0 -39
  186. /package/assets/codemods/transforms/{next → v0.6.4}/__tests__/migrate-native-picker-to-presentation.test.mjs +0 -0
  187. /package/assets/codemods/transforms/{next → v0.6.4}/__tests__/migrate-theme-catalog-to-descriptors.test.mjs +0 -0
  188. /package/assets/codemods/transforms/{next → v0.6.4}/migrate-native-picker-to-presentation.mjs +0 -0
  189. /package/assets/codemods/transforms/{next → v0.6.4}/migrate-theme-catalog-to-descriptors.mjs +0 -0
@@ -2,14 +2,28 @@
2
2
  // DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
3
3
 
4
4
  /**
5
- * Build the discover.list response. An empty `packages` set produces the empty
6
- * envelope carrying `meta.configured`, so callers can distinguish "nothing
7
- * configured" from "configured but nothing discovered".
5
+ * Build the discover.list response. `data` lists installed integrations. An
6
+ * empty `data` carries `meta.configured`, so callers can distinguish "nothing
7
+ * configured" from "configured but nothing discovered". With discover sources,
8
+ * `meta.available` lists what the project could add and `meta.sources` reports
9
+ * what each source did.
8
10
  *
9
- * @param {import('../_package-scanner.mjs').ScannedPackage[]} packages
10
- * @param {{configured: boolean}} meta
11
+ * @param {Array<import('../_adapter.mjs').InstalledPackage & {latest?: string}>} packages
12
+ * @param {{
13
+ * configured: boolean,
14
+ * available?: Array<Record<string, unknown>>,
15
+ * sources?: import('../_adapter.mjs').SourceState[],
16
+ * type?: import('../../../authoring/discover/type').DiscoverKind,
17
+ * only?: 'installed' | 'available',
18
+ * }} options
11
19
  * @returns {import('../discover.type.mjs').DiscoverListResponse}
12
20
  */
13
- export function list(packages: import("../_package-scanner.mjs").ScannedPackage[], { configured }: {
21
+ export function list(packages: Array<import("../_adapter.mjs").InstalledPackage & {
22
+ latest?: string;
23
+ }>, { configured, available, sources, type, only }: {
14
24
  configured: boolean;
25
+ available?: Array<Record<string, unknown>>;
26
+ sources?: import("../_adapter.mjs").SourceState[];
27
+ type?: import("../../../authoring/discover/type").DiscoverKind;
28
+ only?: "installed" | "available";
15
29
  }): import("../discover.type.mjs").DiscoverListResponse;
@@ -1,26 +1,59 @@
1
1
  // Copyright (c) Meta Platforms, Inc. and affiliates.
2
2
 
3
3
  /**
4
- * @file discover.list leaf — list the discovered external packages.
4
+ * @file discover.list leaf — the integrations the project has, and, when it has
5
+ * discover sources, the ones it could add.
5
6
  *
6
- * @position api/discover/list — pure projection of the packages resolved by
7
- * ../_adapter into the discover.list envelope.
7
+ * @position api/discover/list — pure projection of the packages and catalog
8
+ * resolved by ../_adapter into the discover.list envelope.
8
9
  */
9
10
 
10
11
  import {toEntry} from '../_adapter.mjs';
12
+ import {hasKind} from '../_catalog-view.mjs';
11
13
 
12
14
  /**
13
- * Build the discover.list response. An empty `packages` set produces the empty
14
- * envelope carrying `meta.configured`, so callers can distinguish "nothing
15
- * configured" from "configured but nothing discovered".
15
+ * Build the discover.list response. `data` lists installed integrations. An
16
+ * empty `data` carries `meta.configured`, so callers can distinguish "nothing
17
+ * configured" from "configured but nothing discovered". With discover sources,
18
+ * `meta.available` lists what the project could add and `meta.sources` reports
19
+ * what each source did.
16
20
  *
17
- * @param {import('../_package-scanner.mjs').ScannedPackage[]} packages
18
- * @param {{configured: boolean}} meta
21
+ * @param {Array<import('../_adapter.mjs').InstalledPackage & {latest?: string}>} packages
22
+ * @param {{
23
+ * configured: boolean,
24
+ * available?: Array<Record<string, unknown>>,
25
+ * sources?: import('../_adapter.mjs').SourceState[],
26
+ * type?: import('../../../authoring/discover/type').DiscoverKind,
27
+ * only?: 'installed' | 'available',
28
+ * }} options
19
29
  * @returns {import('../discover.type.mjs').DiscoverListResponse}
20
30
  */
21
- export function list(packages, {configured}) {
22
- if (packages.length === 0) {
23
- return {type: 'discover.list', data: [], meta: {configured}};
31
+ export function list(packages, {configured, available, sources, type, only}) {
32
+ const keep = (/** @type {Record<string, unknown>} */ entry) =>
33
+ type == null || hasKind(entry, type);
34
+ const data =
35
+ only === 'available'
36
+ ? []
37
+ : packages
38
+ .map(toEntry)
39
+ .filter(entry =>
40
+ keep(/** @type {Record<string, unknown>} */ (entry)),
41
+ );
42
+
43
+ /** @type {Record<string, unknown>} */
44
+ const meta = {};
45
+ if (data.length === 0) meta.configured = configured;
46
+ if (sources !== undefined) {
47
+ meta.available = only === 'installed' ? [] : (available ?? []).filter(keep);
48
+ meta.sources = sources;
24
49
  }
25
- return {type: 'discover.list', data: packages.map(toEntry)};
50
+ return Object.keys(meta).length > 0
51
+ ? {
52
+ type: 'discover.list',
53
+ data,
54
+ meta: /** @type {import('../discover.type.mjs').DiscoverListMeta} */ (
55
+ meta
56
+ ),
57
+ }
58
+ : {type: 'discover.list', data};
26
59
  }
@@ -57,3 +57,49 @@ describe('discover.list leaf', () => {
57
57
  });
58
58
  });
59
59
  });
60
+
61
+ describe('discover.list leaf with discover sources', () => {
62
+ const installed = [
63
+ pkg('@acme/widgets', ['Alpha'], {
64
+ version: '1.2.3',
65
+ latest: '1.3.0',
66
+ templates: ['pages/Home'],
67
+ }),
68
+ ];
69
+ const available = [
70
+ {name: '@acme/charts', version: '2.0.0', components: ['Chart'], source: 'Acme'},
71
+ {name: '@acme/themes', version: '1.0.0', components: [], themes: ['ocean'], source: 'Acme'},
72
+ ];
73
+ const sources = [{name: 'Acme', from: 'astryx.config', status: 'fresh'}];
74
+
75
+ it('lists what is installed in data and what could be added in meta', () => {
76
+ const res = list(installed, {configured: true, available, sources});
77
+ expect(res.data).toEqual([
78
+ {
79
+ name: '@acme/widgets',
80
+ category: '@acme/widgets',
81
+ components: ['Alpha'],
82
+ version: '1.2.3',
83
+ templates: ['pages/Home'],
84
+ latest: '1.3.0',
85
+ },
86
+ ]);
87
+ expect(res.meta).toEqual({available, sources});
88
+ });
89
+
90
+ it('keeps one side with only', () => {
91
+ expect(
92
+ list(installed, {configured: true, available, sources, only: 'installed'})
93
+ .meta?.available,
94
+ ).toEqual([]);
95
+ const res = list(installed, {configured: true, available, sources, only: 'available'});
96
+ expect(res.data).toEqual([]);
97
+ expect(res.meta).toEqual({configured: true, available, sources});
98
+ });
99
+
100
+ it('keeps only packages that add a kind with type', () => {
101
+ const res = list(installed, {configured: true, available, sources, type: 'theme'});
102
+ expect(res.data).toEqual([]);
103
+ expect(res.meta?.available?.map(e => e.name)).toEqual(['@acme/themes']);
104
+ });
105
+ });
@@ -1,25 +1,27 @@
1
1
  // @generated by scripts/sync-api-types.mjs from the JSDoc in api/**/*.mjs.
2
2
  // DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
3
3
 
4
- /**
5
- * @typedef {import('../_package-scanner.mjs').ScannedPackage} ScannedPackage
6
- */
7
4
  /**
8
5
  * Search all packages for `query` (a free-text term that never starts with
9
- * `@`). Resolves to a single component's docs when unambiguous, a search
10
- * response when several match, or throws AstryxError (ERR_NOT_FOUND) — with
11
- * fuzzy suggestions when any exist.
6
+ * `@`). Resolves to a search response when anything matches, even a single
7
+ * item or an exact component name, or throws AstryxError (ERR_NOT_FOUND) —
8
+ * with fuzzy suggestions when any exist.
12
9
  *
13
- * @param {ScannedPackage[]} packages
10
+ * @param {ScannedPackage[]} packages installed packages
14
11
  * @param {string} query
15
- * @param {{lang?: string | null, zh?: boolean}} opts
16
- * @returns {Promise<
17
- * import('../discover.type.mjs').DiscoverDetailDocResponse |
18
- * import('../discover.type.mjs').DiscoverSearchResponse
19
- * >}
12
+ * @param {{
13
+ * items?: SearchItem[],
14
+ * type?: import('../../../authoring/discover/type').DiscoverKind,
15
+ * only?: 'installed' | 'available',
16
+ * limit?: number,
17
+ * }} opts
18
+ * @returns {Promise<import('../discover.type.mjs').DiscoverSearchResponse>}
20
19
  */
21
- export function search(packages: ScannedPackage[], query: string, { lang, zh }: {
22
- lang?: string | null;
23
- zh?: boolean;
24
- }): Promise<import("../discover.type.mjs").DiscoverDetailDocResponse | import("../discover.type.mjs").DiscoverSearchResponse>;
20
+ export function search(packages: ScannedPackage[], query: string, opts: {
21
+ items?: SearchItem[];
22
+ type?: import("../../../authoring/discover/type").DiscoverKind;
23
+ only?: "installed" | "available";
24
+ limit?: number;
25
+ }): Promise<import("../discover.type.mjs").DiscoverSearchResponse>;
25
26
  export type ScannedPackage = import("../_package-scanner.mjs").ScannedPackage;
27
+ export type SearchItem = import("../_catalog-view.mjs").SearchItem;
@@ -1,44 +1,76 @@
1
1
  // Copyright (c) Meta Platforms, Inc. and affiliates.
2
2
 
3
3
  /**
4
- * @file discover.search leaf — free-text search across external packages.
4
+ * @file discover.search leaf — free-text search across installed packages and,
5
+ * when the project has discover sources, everything it could add.
5
6
  *
6
- * Resolution order (matching the flat command exactly):
7
- * 1. exact component name -> discover.detail.doc
8
- * 2. single substring hit -> discover.detail.doc
9
- * 3. multiple substring hits -> discover.search
10
- * 4. fuzzy hits (distance <= 3) -> throw ERR_NOT_FOUND with suggestions
11
- * 5. otherwise -> throw ERR_NOT_FOUND
7
+ * A free-text query always answers with a list, so its response type depends
8
+ * on the form of the query and never on what the project has:
9
+ * 1. any matches -> discover.search
10
+ * 2. fuzzy hits (distance <= 3) -> throw ERR_NOT_FOUND with suggestions
11
+ * 3. otherwise -> throw ERR_NOT_FOUND
12
+ * Matches are installed components, the project's other installed items,
13
+ * packages, and every item a source lists for a package the project could add.
14
+ * A caller opens one item by its package path, which ../detail answers.
12
15
  *
13
- * @position api/discover/search — projection over ../_adapter's resolver;
14
- * delegates the single-component cases to ../detail/doc.
16
+ * @position api/discover/search — pure ranking over the installed component
17
+ * names and ../_catalog-view's search items.
15
18
  */
16
19
 
17
- import {findComponent} from '../_adapter.mjs';
18
- import {docFromResult} from '../detail/doc/doc.mjs';
19
20
  import {levenshteinDistance} from '../../../foundation/text/string-utils.mjs';
20
21
  import {AstryxError} from '../../error.mjs';
21
22
  import {ERROR_CODES} from '../../../foundation/response/error-codes.mjs';
23
+ import {DISCOVER_KINDS} from '../../../authoring/discover/parse.mjs';
22
24
 
23
25
  /**
24
26
  * @typedef {import('../_package-scanner.mjs').ScannedPackage} ScannedPackage
27
+ * @typedef {import('../_catalog-view.mjs').SearchItem} SearchItem
25
28
  */
26
29
 
30
+ const KIND_ORDER = ['package', ...DISCOVER_KINDS];
31
+
32
+ /**
33
+ * How well an item matches: 0 exact name, 1 name prefix, 2 name substring,
34
+ * 3 title, summary, keyword, or description; null for no match.
35
+ * @param {SearchItem} item
36
+ * @param {string} lower
37
+ * @returns {number | null}
38
+ */
39
+ function rank(item, lower) {
40
+ const name = item.name.toLowerCase();
41
+ if (name === lower) return 0;
42
+ if (name.startsWith(lower)) return 1;
43
+ if (name.includes(lower)) return 2;
44
+ const text = [
45
+ item.title,
46
+ item.summary,
47
+ item.description,
48
+ ...(item.keywords ?? []),
49
+ ]
50
+ .filter(Boolean)
51
+ .join('\n')
52
+ .toLowerCase();
53
+ return text.includes(lower) ? 3 : null;
54
+ }
55
+
27
56
  /**
28
57
  * Search all packages for `query` (a free-text term that never starts with
29
- * `@`). Resolves to a single component's docs when unambiguous, a search
30
- * response when several match, or throws AstryxError (ERR_NOT_FOUND) — with
31
- * fuzzy suggestions when any exist.
58
+ * `@`). Resolves to a search response when anything matches, even a single
59
+ * item or an exact component name, or throws AstryxError (ERR_NOT_FOUND) —
60
+ * with fuzzy suggestions when any exist.
32
61
  *
33
- * @param {ScannedPackage[]} packages
62
+ * @param {ScannedPackage[]} packages installed packages
34
63
  * @param {string} query
35
- * @param {{lang?: string | null, zh?: boolean}} opts
36
- * @returns {Promise<
37
- * import('../discover.type.mjs').DiscoverDetailDocResponse |
38
- * import('../discover.type.mjs').DiscoverSearchResponse
39
- * >}
64
+ * @param {{
65
+ * items?: SearchItem[],
66
+ * type?: import('../../../authoring/discover/type').DiscoverKind,
67
+ * only?: 'installed' | 'available',
68
+ * limit?: number,
69
+ * }} opts
70
+ * @returns {Promise<import('../discover.type.mjs').DiscoverSearchResponse>}
40
71
  */
41
- export async function search(packages, query, {lang, zh}) {
72
+ export async function search(packages, query, opts) {
73
+ const {items = [], type, only, limit} = opts;
42
74
  // An empty query must error, not match every component via `.includes('')`
43
75
  // (parity with the api/search leaf). The discover() dispatcher already routes
44
76
  // an empty query to list, but the leaf must be safe on its own.
@@ -51,50 +83,64 @@ export async function search(packages, query, {lang, zh}) {
51
83
  }
52
84
  const lower = query.toLowerCase();
53
85
 
54
- const exact = findComponent(packages, query);
55
- if (exact) return await docFromResult(exact, {lang, zh});
56
-
57
- const substringMatches =
58
- /** @type {Array<{pkg: ScannedPackage, comp: string}>} */ ([]);
59
- for (const pkg of packages) {
60
- for (const comp of pkg.components) {
61
- if (comp.toLowerCase().includes(lower)) {
62
- substringMatches.push({pkg, comp});
63
- }
64
- }
65
- }
86
+ /** @type {SearchItem[]} */
87
+ const installedComponents = packages.flatMap(pkg =>
88
+ pkg.components.map(name => ({
89
+ package: pkg.name,
90
+ kind: /** @type {const} */ ('component'),
91
+ name,
92
+ installed: true,
93
+ })),
94
+ );
66
95
 
67
- if (substringMatches.length === 1) {
68
- const match = substringMatches[0];
69
- const result = findComponent([match.pkg], match.comp);
70
- if (result) return await docFromResult(result, {lang, zh});
71
- }
96
+ const ranked = [...installedComponents, ...items]
97
+ .filter(item => (type == null ? true : item.kind === type))
98
+ .filter(item =>
99
+ only === 'installed'
100
+ ? item.installed
101
+ : only === 'available'
102
+ ? !item.installed
103
+ : true,
104
+ )
105
+ .map(item => ({item, score: rank(item, lower)}))
106
+ .filter(
107
+ /** @returns {m is {item: SearchItem, score: number}} */
108
+ m => m.score != null,
109
+ )
110
+ .sort(
111
+ (a, b) =>
112
+ a.score - b.score ||
113
+ Number(b.item.installed) - Number(a.item.installed) ||
114
+ KIND_ORDER.indexOf(a.item.kind) - KIND_ORDER.indexOf(b.item.kind) ||
115
+ a.item.package.localeCompare(b.item.package) ||
116
+ a.item.name.localeCompare(b.item.name),
117
+ );
72
118
 
73
- if (substringMatches.length > 1) {
119
+ if (ranked.length > 0) {
120
+ const matches = ranked.map(({item}) => ({
121
+ package: item.package,
122
+ component: item.name,
123
+ kind: item.kind,
124
+ installed: item.installed,
125
+ ...(item.title ? {title: item.title} : {}),
126
+ ...(item.summary ? {summary: item.summary} : {}),
127
+ }));
128
+ const shown = limit == null ? matches : matches.slice(0, limit);
74
129
  return {
75
130
  type: 'discover.search',
76
131
  data: {
77
132
  query,
78
- matches: substringMatches.map(m => ({
79
- package: m.pkg.name,
80
- component: m.comp,
81
- })),
133
+ matches: shown,
134
+ ...(shown.length < matches.length ? {total: matches.length} : {}),
82
135
  },
83
136
  };
84
137
  }
85
138
 
86
- // Fuzzy fallback
87
- const allComponents =
88
- /** @type {Array<{pkg: ScannedPackage, comp: string}>} */ ([]);
89
- for (const pkg of packages) {
90
- for (const comp of pkg.components) {
91
- allComponents.push({pkg, comp});
92
- }
93
- }
94
- const fuzzyMatches = allComponents
95
- .map(item => ({
96
- ...item,
97
- distance: levenshteinDistance(lower, item.comp.toLowerCase()),
139
+ // Fuzzy fallback over installed components.
140
+ const fuzzyMatches = installedComponents
141
+ .map(entry => ({
142
+ ...entry,
143
+ distance: levenshteinDistance(lower, entry.name.toLowerCase()),
98
144
  }))
99
145
  .filter(m => m.distance <= 3)
100
146
  .sort((a, b) => a.distance - b.distance)
@@ -104,7 +150,7 @@ export async function search(packages, query, {lang, zh}) {
104
150
  throw new AstryxError(
105
151
  `"${query}" not found`,
106
152
  fuzzyMatches.map(m => ({
107
- name: m.pkg.name + '/' + m.comp,
153
+ name: m.package + '/' + m.name,
108
154
  reason: 'similar name',
109
155
  })),
110
156
  ERROR_CODES.ERR_NOT_FOUND,
@@ -1,8 +1,8 @@
1
1
  // Copyright (c) Meta Platforms, Inc. and affiliates.
2
2
 
3
3
  /**
4
- * @file Colocated tests for the discover.search leaf. Exact/single matches load
5
- * a real `.doc.mjs`, so these drive a small temp docs directory.
4
+ * @file Colocated tests for the discover.search leaf, over a small temp docs
5
+ * directory shaped like a scanned package.
6
6
  */
7
7
 
8
8
  import {describe, it, expect, beforeAll, afterAll} from 'vitest';
@@ -47,16 +47,31 @@ afterAll(() => {
47
47
  });
48
48
 
49
49
  describe('discover.search leaf', () => {
50
- it('an exact component name resolves to its docs', async () => {
50
+ it('lists an exact component name first, with every other match', async () => {
51
51
  const res = await search(packages, 'Alpha', {});
52
- expect(res.type).toBe('discover.detail.doc');
53
- expect(res.data.name).toBe('Alpha');
52
+ expect(res.type).toBe('discover.search');
53
+ expect(res.data.matches.map(m => m.component)).toEqual([
54
+ 'Alpha',
55
+ 'AlphaCard',
56
+ ]);
54
57
  });
55
58
 
56
- it('a single substring match resolves to its docs', async () => {
59
+ it('lists a single match too, so the response type never depends on the data', async () => {
57
60
  const res = await search(packages, 'card', {});
58
- expect(res.type).toBe('discover.detail.doc');
59
- expect(res.data.name).toBe('AlphaCard');
61
+ expect(res).toEqual({
62
+ type: 'discover.search',
63
+ data: {
64
+ query: 'card',
65
+ matches: [
66
+ {
67
+ package: '@acme/widgets',
68
+ component: 'AlphaCard',
69
+ kind: 'component',
70
+ installed: true,
71
+ },
72
+ ],
73
+ },
74
+ });
60
75
  });
61
76
 
62
77
  it('multiple substring matches return a search response', async () => {
@@ -66,8 +81,18 @@ describe('discover.search leaf', () => {
66
81
  data: {
67
82
  query: 'alph',
68
83
  matches: [
69
- {package: '@acme/widgets', component: 'Alpha'},
70
- {package: '@acme/widgets', component: 'AlphaCard'},
84
+ {
85
+ package: '@acme/widgets',
86
+ component: 'Alpha',
87
+ kind: 'component',
88
+ installed: true,
89
+ },
90
+ {
91
+ package: '@acme/widgets',
92
+ component: 'AlphaCard',
93
+ kind: 'component',
94
+ installed: true,
95
+ },
71
96
  ],
72
97
  },
73
98
  });
@@ -116,3 +141,112 @@ describe('discover.search leaf — empty query (parity with api/search)', () =>
116
141
  });
117
142
  });
118
143
  });
144
+
145
+ describe('discover.search leaf across every kind and source', () => {
146
+ /** @type {any[]} */
147
+ const items = [
148
+ {
149
+ package: '@acme/widgets',
150
+ kind: 'template',
151
+ name: 'pages/AlphaHome',
152
+ installed: true,
153
+ },
154
+ {
155
+ package: '@acme/charts',
156
+ kind: 'package',
157
+ name: '@acme/charts',
158
+ installed: false,
159
+ description: 'Charts for alpha dashboards',
160
+ },
161
+ {
162
+ package: '@acme/charts',
163
+ kind: 'component',
164
+ name: 'AlphaChart',
165
+ installed: false,
166
+ },
167
+ {
168
+ package: '@acme/charts',
169
+ kind: 'doc',
170
+ name: 'guide',
171
+ installed: false,
172
+ summary: 'How to chart',
173
+ },
174
+ ];
175
+
176
+ it('ranks every match by how closely its name matches, installed first among equals', async () => {
177
+ const res = await search(packages, 'alph', {items});
178
+ expect(res.type).toBe('discover.search');
179
+ expect(
180
+ res.data.matches.map(m => [m.kind, m.component, m.installed]),
181
+ ).toEqual([
182
+ ['component', 'Alpha', true],
183
+ ['component', 'AlphaCard', true],
184
+ ['component', 'AlphaChart', false],
185
+ ['template', 'pages/AlphaHome', true],
186
+ ['package', '@acme/charts', false],
187
+ ]);
188
+ });
189
+
190
+ it('lists an exact installed component name with the matches from every source', async () => {
191
+ const res = await search(packages, 'Alpha', {items});
192
+ expect(res.type).toBe('discover.search');
193
+ expect(res.data.matches[0]).toEqual({
194
+ package: '@acme/widgets',
195
+ component: 'Alpha',
196
+ kind: 'component',
197
+ installed: true,
198
+ });
199
+ expect(res.data.matches.map(m => m.component)).toContain('AlphaChart');
200
+ });
201
+
202
+ it('lists a single partial component match when other items match too', async () => {
203
+ const res = await search(packages, 'card', {
204
+ items: [
205
+ ...items,
206
+ {
207
+ package: '@acme/charts',
208
+ kind: 'template',
209
+ name: 'pages/CardGrid',
210
+ installed: false,
211
+ },
212
+ ],
213
+ });
214
+ expect(res.type).toBe('discover.search');
215
+ expect(res.data.matches.map(m => m.component)).toEqual([
216
+ 'AlphaCard',
217
+ 'pages/CardGrid',
218
+ ]);
219
+ });
220
+
221
+ it('lists one installed component when nothing else matches', async () => {
222
+ const res = await search(packages, 'card', {items});
223
+ expect(res.type).toBe('discover.search');
224
+ expect(res.data.matches.map(m => m.component)).toEqual(['AlphaCard']);
225
+ });
226
+
227
+ it('keeps one kind with type, and one side with only', async () => {
228
+ const templates = await search(packages, 'alph', {items, type: 'template'});
229
+ expect(templates.data.matches.map(m => m.component)).toEqual([
230
+ 'pages/AlphaHome',
231
+ ]);
232
+ const available = await search(packages, 'alph', {
233
+ items,
234
+ only: 'available',
235
+ });
236
+ expect(available.data.matches.map(m => m.component)).toEqual([
237
+ 'AlphaChart',
238
+ '@acme/charts',
239
+ ]);
240
+ });
241
+
242
+ it('caps the list at limit and reports the total', async () => {
243
+ const res = await search(packages, 'alph', {items, limit: 2});
244
+ expect(res.data.matches).toHaveLength(2);
245
+ expect(res.data.total).toBe(5);
246
+ });
247
+
248
+ it('matches titles, summaries, keywords, and descriptions too', async () => {
249
+ const res = await search(packages, 'how to chart', {items});
250
+ expect(res.data.matches.map(m => m.component)).toEqual(['guide']);
251
+ });
252
+ });