@astryxdesign/cli 0.6.3-canary.4485852 → 0.6.3-canary.46279eb

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 (60) hide show
  1. package/README.md +6 -5
  2. package/api/discover/_adapter.d.mts +114 -6
  3. package/api/discover/_adapter.mjs +372 -17
  4. package/api/discover/_adapter.test.mjs +215 -0
  5. package/api/discover/_catalog-view.d.mts +115 -0
  6. package/api/discover/_catalog-view.mjs +203 -0
  7. package/api/discover/_catalog-view.test.mjs +128 -0
  8. package/api/discover/detail/detail.d.mts +18 -6
  9. package/api/discover/detail/detail.mjs +67 -13
  10. package/api/discover/detail/detail.test.mjs +85 -0
  11. package/api/discover/detail/item/item.d.mts +26 -0
  12. package/api/discover/detail/item/item.mjs +78 -0
  13. package/api/discover/detail/item/item.test.mjs +73 -0
  14. package/api/discover/discover.d.mts +3 -9
  15. package/api/discover/discover.doc.mjs +61 -18
  16. package/api/discover/discover.mjs +220 -36
  17. package/api/discover/discover.test.mjs +11 -2
  18. package/api/discover/discover.type.d.mts +147 -8
  19. package/api/discover/discover.type.mjs +102 -12
  20. package/api/discover/list/list.d.mts +20 -6
  21. package/api/discover/list/list.mjs +45 -12
  22. package/api/discover/list/list.test.mjs +46 -0
  23. package/api/discover/search/search.d.mts +18 -16
  24. package/api/discover/search/search.mjs +102 -56
  25. package/api/discover/search/search.test.mjs +144 -10
  26. package/api/integration/add-helpers.d.mts +5 -2
  27. package/api/integration/add-helpers.mjs +36 -9
  28. package/api/theme/add/add.mjs +17 -25
  29. package/api/theme/add/add.rollback.test.mjs +158 -0
  30. package/api/theme/add/add.staging.test.mjs +40 -23
  31. package/api/theme/build/build.family.test.mjs +7 -12
  32. package/api/theme/build/build.mjs +8 -18
  33. package/api/theme/build/build.rollback.test.mjs +148 -0
  34. package/assets/docs/internationalization.doc.mjs +7 -5
  35. package/assets/docs/tree/integrations.doc.mjs +20 -1
  36. package/assets/templates/blocks/components/InternationalizationProvider/InternationalizationProvider01ShippedLocale.tsx +1 -1
  37. package/authoring/config/config.doc.mjs +8 -0
  38. package/authoring/config/parse.d.mts +2 -0
  39. package/authoring/config/parse.mjs +19 -0
  40. package/authoring/config/parse.test.mjs +8 -0
  41. package/authoring/config/type.ts +11 -0
  42. package/authoring/discover/discover.doc.d.mts +13 -0
  43. package/authoring/discover/discover.doc.mjs +138 -0
  44. package/authoring/discover/parse.d.mts +24 -0
  45. package/authoring/discover/parse.mjs +128 -0
  46. package/authoring/discover/parse.test.mjs +124 -0
  47. package/authoring/discover/type.ts +87 -0
  48. package/authoring/index.d.mts +1 -0
  49. package/authoring/index.d.ts +10 -0
  50. package/authoring/index.mjs +1 -0
  51. package/clients/cli/commands/discover.doc.mjs +53 -9
  52. package/clients/cli/commands/discover.mjs +393 -118
  53. package/clients/cli/commands/discover.sources.test.mjs +267 -0
  54. package/clients/cli/lib/manifest.mjs +1 -0
  55. package/foundation/discovery/authoring-self-docs.mjs +1 -0
  56. package/foundation/integrations/integrations.d.mts +14 -1
  57. package/foundation/integrations/integrations.mjs +41 -1
  58. package/foundation/integrations/integrations.test.mjs +31 -0
  59. package/foundation/response/response-types.doc.mjs +10 -4
  60. package/package.json +9 -9
package/README.md CHANGED
@@ -82,7 +82,7 @@ Options:
82
82
  | `blog` | Read the Astryx blog from the published feed |
83
83
  | `build` | Build a page: the template to start from, or the workflow playbook (no query) |
84
84
  | `component` | List components or print component docs |
85
- | `discover` | Discover external packages and components |
85
+ | `discover` | Browse and search integrations: the ones you have and the ones you could add |
86
86
  | `docs` | Print reference docs |
87
87
  | `doctor` | Diagnose Astryx projects and integration packages |
88
88
  | `gap-report` | Route a design-system gap to its owning package |
@@ -435,10 +435,11 @@ Every response has a `type` discriminant. The full set is below (generated from
435
435
  | `docs.node` | One node of the docs tree, read by its route: its id, kind, package, title, summary, and breadcrumb, plus a namespace's slots with their children (one level down) or a typed doc's content, and links ({up, previous, next, related}: the commands that open its parent, its neighbors, and the docs it names). |
436
436
  | `blog.list` | The feed URL plus every post parsed from the RSS feed, each with slug, title, description, date, type, authors, link, and plaintext URL. |
437
437
  | `blog.detail` | One post's metadata plus the feed URL and the post's full plaintext body. |
438
- | `discover.list` | The configured external packages (name, category, components, version, description); when empty it carries meta.configured to tell "nothing configured" from "nothing discovered". |
439
- | `discover.detail` | A single external package entry, for an @scope/name query. |
440
- | `discover.detail.doc` | The validated ComponentDoc for one external component: an @scope/name/Component query, or a free-text term resolving to exactly one component. |
441
- | `discover.search` | The echoed query plus the matching {package, component} pairs, when a free-text term matches several components. |
438
+ | `discover.list` | The integrations the project loads (name, category, components, version, a list per other kind they add, and latest when a source knows it); with a discover source, meta.available lists what the project could add and meta.sources reports each source; when empty it carries meta.configured to tell "nothing configured" from "nothing discovered". |
439
+ | `discover.detail` | One package, for an @scope/name or @scope/name@version query: what the shown version adds, whether the project has it, and, when a source knows the package, its versions, latest release, and the command that adds it. |
440
+ | `discover.detail.doc` | The validated ComponentDoc for one installed component, for an @scope/name/Component query. |
441
+ | `discover.item` | One item that is not an installed component, for an @scope/name/<item> query: its kind, name, package, version, and whether the project has the package. |
442
+ | `discover.search` | The echoed query plus every matching item and package across all packages, each with its kind and whether the project has it, even when only one matches; total is set when --limit cut the list. |
442
443
  | `search` | The echoed query, `matchCount` (total matches, before `limit`), and results, a ranked SearchResultEntry[] bounded by `limit`: each {domain, name, score, reason, description, command}, plus import (components, hooks), title, parent (the command that opens the level above), package (for a docs-tree hit), and, for a hit on one section, section (docs), or displayName and kind (templates). |
443
444
  | `build.help` | The how-to-build-a-page playbook, emitted when no query is given: `playbook: true`, a title, the ordered steps (title, commands, optional returns), the on-system rules, and related lookups. Commands are bare subcommands for the caller to render with its own invocation. |
444
445
  | `build.kit` | The template to start from and its kit: query, hasResults, matchCount (never a cap), directMatch, start {name, command, basis, reason, alternatives, ...}, pages (search's closest templates), blocks and domain as SearchResultEntry[], frame, foundation, and hint {reason, commands} when thin. |
@@ -12,20 +12,78 @@
12
12
  * configured" (`false`) from "configured but nothing discovered" (`true`),
13
13
  * which the list leaf surfaces as `meta`.
14
14
  *
15
- * @returns {Promise<{packages: ScannedPackage[], configured: boolean}>}
15
+ * @returns {Promise<{packages: ScannedPackage[], configured: boolean, project: Project}>}
16
16
  */
17
17
  export function discoverPackages(): Promise<{
18
18
  packages: ScannedPackage[];
19
19
  configured: boolean;
20
+ project: Project;
20
21
  }>;
21
22
  /**
22
- * Project a scanned package into the discover list/detail entry shape. Shared
23
- * by the list and detail leaves so the entry keys (and their order) stay in one
24
- * place.
25
- * @param {ScannedPackage} pkg
23
+ * Every integration the project loads, with what it adds. An integration with
24
+ * no components root is included with `components: []`; one that failed to
25
+ * load is left out (the integration-issues nudge and Doctor report it).
26
+ *
27
+ * @param {Project | undefined} project
28
+ * @param {ScannedPackage[]} scanned the packages {@link discoverPackages} found
29
+ * @returns {Promise<InstalledPackage[]>}
30
+ */
31
+ export function describeInstalled(project: Project | undefined, scanned: ScannedPackage[]): Promise<InstalledPackage[]>;
32
+ /**
33
+ * Whether the project has any discover source: its own `discover` config field,
34
+ * or an integration's `discover` named export (valid or not).
35
+ * @param {Project | undefined} project
36
+ * @returns {boolean}
37
+ */
38
+ export function hasSources(project: Project | undefined): boolean;
39
+ /**
40
+ * Package names the project's package.json declares in any dependency field.
41
+ * @param {Project | undefined} project
42
+ * @returns {Set<string>}
43
+ */
44
+ export function declaredDependencies(project: Project | undefined): Set<string>;
45
+ /**
46
+ * The command that adds a package with the project's package manager. Discover
47
+ * only prints it; it never runs it.
48
+ * @param {Project | undefined} project
49
+ * @returns {(name: string, version?: string) => string}
50
+ */
51
+ export function addCommand(project: Project | undefined): (name: string, version?: string) => string;
52
+ /**
53
+ * The per-user cache directory the saved copies live in, never inside a
54
+ * project. Standard platform locations only.
55
+ * @returns {string}
56
+ */
57
+ export function defaultCacheDir(): string;
58
+ /**
59
+ * Call every discover source — the project's `discover` first, then each
60
+ * integration's in load order — once per distinct function. A source that
61
+ * throws, times out, or returns an invalid catalog falls back to its saved copy;
62
+ * with none, it is reported and adds nothing. The others always count.
63
+ *
64
+ * @param {Project | undefined} project
65
+ * @param {{package?: string, version?: string}} [request] ask for one package,
66
+ * and optionally one version's contributions
67
+ * @param {{cacheDir?: string, timeoutMs?: number}} [options]
68
+ * @returns {Promise<CatalogResult>}
69
+ */
70
+ export function callSources(project: Project | undefined, request?: {
71
+ package?: string;
72
+ version?: string;
73
+ }, options?: {
74
+ cacheDir?: string;
75
+ timeoutMs?: number;
76
+ }): Promise<CatalogResult>;
77
+ /**
78
+ * Project a scanned or installed package into the discover list/detail entry
79
+ * shape. Shared by the list and detail leaves so the entry keys (and their
80
+ * order) stay in one place. A kind list appears only when it is not empty.
81
+ * @param {InstalledPackage & {latest?: string}} pkg
26
82
  * @returns {import('./discover.type.mjs').DiscoverListEntry}
27
83
  */
28
- export function toEntry(pkg: ScannedPackage): import("./discover.type.mjs").DiscoverListEntry;
84
+ export function toEntry(pkg: InstalledPackage & {
85
+ latest?: string;
86
+ }): import("./discover.type.mjs").DiscoverListEntry;
29
87
  /**
30
88
  * Locate a component's doc file within the given packages (case-insensitive).
31
89
  * @param {ScannedPackage[]} packages
@@ -46,7 +104,56 @@ export function loadValidatedDoc(result: ComponentResolution, { lang, zh }: {
46
104
  lang?: string | null;
47
105
  zh?: boolean;
48
106
  }): Promise<import("./discover.type.mjs").DiscoverDetailDocResponse["data"]>;
107
+ /** The list and detail entry fields for each item kind, in display order. */
108
+ export const KIND_FIELDS: readonly [readonly ["component", "components"], readonly ["template", "templates"], readonly ["doc", "docs"], readonly ["theme", "themes"], readonly ["codemod", "codemods"], readonly ["agent-doc", "agentDocs"]];
49
109
  export type ScannedPackage = import("./_package-scanner.mjs").ScannedPackage;
110
+ export type DiscoverPackage = import("../../authoring/discover/type").DiscoverPackage;
111
+ export type DiscoverSource = import("../../authoring/discover/type").DiscoverSource;
112
+ /**
113
+ * An installed integration with what it adds, per kind. `components` is the
114
+ * scanned component list the discover leaves have always used.
115
+ */
116
+ export type InstalledPackage = ScannedPackage & {
117
+ templates?: string[];
118
+ docs?: string[];
119
+ themes?: string[];
120
+ codemods?: string[];
121
+ };
122
+ /**
123
+ * What one source did on this run.
124
+ */
125
+ export type SourceState = {
126
+ /**
127
+ * the source's own name, or where it came from when it
128
+ * gave none
129
+ */
130
+ name: string;
131
+ /**
132
+ * `astryx.config`, or the integration that exports it
133
+ */
134
+ from: string;
135
+ status: "fresh" | "saved" | "failed";
136
+ generatedAt?: string | undefined;
137
+ complete?: boolean | undefined;
138
+ /**
139
+ * when the saved copy in use was written
140
+ */
141
+ savedAt?: string | undefined;
142
+ /**
143
+ * why the live call failed
144
+ */
145
+ error?: string | undefined;
146
+ };
147
+ /**
148
+ * Everything every source returned, merged. When two sources list the same
149
+ * package, the earlier source's entry is kept.
150
+ */
151
+ export type CatalogResult = {
152
+ sources: SourceState[];
153
+ packages: Array<DiscoverPackage & {
154
+ source: string;
155
+ }>;
156
+ };
50
157
  /**
51
158
  * A located component doc within a scanned package — the shape returned by
52
159
  * {@link findComponent}, consumed by {@link loadValidatedDoc}.
@@ -56,3 +163,4 @@ export type ComponentResolution = {
56
163
  docPath: string;
57
164
  componentName: string;
58
165
  };
166
+ import { Project } from '../../foundation/config/project.mjs';
@@ -1,31 +1,81 @@
1
1
  // Copyright (c) Meta Platforms, Inc. and affiliates.
2
2
 
3
3
  /**
4
- * @file Shared external-package adapter for the discover leaves.
4
+ * @file Shared adapter for the discover leaves: the one place discover reaches
5
+ * the environment.
5
6
  *
6
7
  * Every `discover.*` leaf projects already-resolved data into a typed response;
7
- * none of them touch the integration system or the filesystem directly. This
8
- * adapter is the single place that does: it discovers the configured external
9
- * packages, locates a component's doc file within them, and loads + validates a
10
- * component's docs. Keeping that I/O here (rather than duplicated across leaves)
11
- * is what lets `list`, `detail`, `detail/doc`, and `search` stay pure
12
- * projections.
8
+ * none of them touch the integration system, the filesystem, or a source. This
9
+ * adapter does: it resolves the project's installed integrations and what each
10
+ * one adds, calls every discover source and keeps a saved copy of each answer,
11
+ * locates and loads a component's doc, and names the package manager's add
12
+ * command. Keeping that I/O here (INV21) is what lets `list`, `detail`,
13
+ * `detail/doc`, `detail/item`, and `search` stay pure projections.
13
14
  *
14
- * @position api/discover — orchestration over lib/project, lib/package-scanner,
15
- * and lib/component-loader; the discover dispatcher + leaves consume it.
15
+ * @position api/discover — orchestration over foundation/config/project,
16
+ * foundation/integrations, the discovery loaders, and discover sources.
16
17
  */
17
18
 
19
+ import {createHash} from 'node:crypto';
20
+ import * as fs from 'node:fs';
21
+ import * as os from 'node:os';
22
+ import * as path from 'node:path';
18
23
  import {Project} from '../../foundation/config/project.mjs';
19
24
  import {
20
25
  scanAllPackages,
21
26
  findComponentInPackages,
22
27
  } from './_package-scanner.mjs';
23
28
  import {loadDocs} from '../../foundation/discovery/component-loader.mjs';
29
+ import {collectIdentities} from '../../foundation/integrations/contribution-inventory.mjs';
30
+ import {detectPackageManager} from '../../foundation/env/package-manager.mjs';
31
+ import {assertWithin} from '../../foundation/fs/path-safety.mjs';
32
+ import {parseDiscoverCatalog} from '../../authoring/discover/parse.mjs';
24
33
  import {AstryxError} from '../error.mjs';
25
34
  import {ERROR_CODES} from '../../foundation/response/error-codes.mjs';
26
35
 
36
+ const SOURCE_TIMEOUT_MS = 30_000;
37
+
38
+ const DEPENDENCY_FIELDS = [
39
+ 'dependencies',
40
+ 'devDependencies',
41
+ 'optionalDependencies',
42
+ 'peerDependencies',
43
+ ];
44
+
27
45
  /**
28
46
  * @typedef {import('./_package-scanner.mjs').ScannedPackage} ScannedPackage
47
+ * @typedef {import('../../authoring/discover/type').DiscoverPackage} DiscoverPackage
48
+ * @typedef {import('../../authoring/discover/type').DiscoverSource} DiscoverSource
49
+ */
50
+
51
+ /**
52
+ * An installed integration with what it adds, per kind. `components` is the
53
+ * scanned component list the discover leaves have always used.
54
+ * @typedef {ScannedPackage & {
55
+ * templates?: string[],
56
+ * docs?: string[],
57
+ * themes?: string[],
58
+ * codemods?: string[],
59
+ * }} InstalledPackage
60
+ */
61
+
62
+ /**
63
+ * What one source did on this run.
64
+ * @typedef {object} SourceState
65
+ * @property {string} name the source's own name, or where it came from when it
66
+ * gave none
67
+ * @property {string} from `astryx.config`, or the integration that exports it
68
+ * @property {'fresh' | 'saved' | 'failed'} status
69
+ * @property {string} [generatedAt]
70
+ * @property {boolean} [complete]
71
+ * @property {string} [savedAt] when the saved copy in use was written
72
+ * @property {string} [error] why the live call failed
73
+ */
74
+
75
+ /**
76
+ * Everything every source returned, merged. When two sources list the same
77
+ * package, the earlier source's entry is kept.
78
+ * @typedef {{sources: SourceState[], packages: Array<DiscoverPackage & {source: string}>}} CatalogResult
29
79
  */
30
80
 
31
81
  /**
@@ -68,7 +118,7 @@ function validateDocs(docs) {
68
118
  * configured" (`false`) from "configured but nothing discovered" (`true`),
69
119
  * which the list leaf surfaces as `meta`.
70
120
  *
71
- * @returns {Promise<{packages: ScannedPackage[], configured: boolean}>}
121
+ * @returns {Promise<{packages: ScannedPackage[], configured: boolean, project: Project}>}
72
122
  */
73
123
  export async function discoverPackages() {
74
124
  const project = await Project.load();
@@ -86,25 +136,323 @@ export async function discoverPackages() {
86
136
  docsDir: integration.components,
87
137
  }));
88
138
  if (explicitPackages.length === 0) {
89
- return {packages: [], configured: loadedIntegrations.length > 0};
139
+ return {packages: [], configured: loadedIntegrations.length > 0, project};
90
140
  }
91
141
 
92
142
  const packages = scanAllPackages(
93
143
  [],
94
144
  /** @type {ScannedPackage[]} */ (/** @type {unknown} */ (explicitPackages)),
95
145
  );
96
- return {packages, configured: true};
146
+ return {packages, configured: true, project};
147
+ }
148
+
149
+ /**
150
+ * Every integration the project loads, with what it adds. An integration with
151
+ * no components root is included with `components: []`; one that failed to
152
+ * load is left out (the integration-issues nudge and Doctor report it).
153
+ *
154
+ * @param {Project | undefined} project
155
+ * @param {ScannedPackage[]} scanned the packages {@link discoverPackages} found
156
+ * @returns {Promise<InstalledPackage[]>}
157
+ */
158
+ export async function describeInstalled(project, scanned) {
159
+ if (project == null) return scanned;
160
+ const byName = new Map(scanned.map(pkg => [pkg.name, pkg]));
161
+ /** @type {InstalledPackage[]} */
162
+ const installed = [];
163
+ for (const integration of project.loadedIntegrations) {
164
+ if (integration.__loadError) continue;
165
+ const base =
166
+ byName.get(integration.name) ??
167
+ /** @type {ScannedPackage} */ (
168
+ /** @type {unknown} */ ({
169
+ name: integration.name,
170
+ category: integration.name,
171
+ version: integration.version,
172
+ components: [],
173
+ })
174
+ );
175
+ // Components come from the scan above; loading every component doc again
176
+ // here would only slow the list down.
177
+ const {identities} = await collectIdentities({
178
+ ...integration,
179
+ components: undefined,
180
+ });
181
+ installed.push({
182
+ ...base,
183
+ templates: identities.templates.map(t => t.id),
184
+ docs: identities.docs,
185
+ themes: identities.themes.map(t => t.slug),
186
+ codemods: identities.codemods.map(c => `${c.version}/${c.id}`),
187
+ });
188
+ }
189
+ return installed;
97
190
  }
98
191
 
99
192
  /**
100
- * Project a scanned package into the discover list/detail entry shape. Shared
101
- * by the list and detail leaves so the entry keys (and their order) stay in one
102
- * place.
103
- * @param {ScannedPackage} pkg
193
+ * Whether the project has any discover source: its own `discover` config field,
194
+ * or an integration's `discover` named export (valid or not).
195
+ * @param {Project | undefined} project
196
+ * @returns {boolean}
197
+ */
198
+ export function hasSources(project) {
199
+ if (project == null) return false;
200
+ if (typeof project.config?.discover === 'function') return true;
201
+ return project.loadedIntegrations.some(
202
+ i => i.__discover != null || i.__discoverError != null,
203
+ );
204
+ }
205
+
206
+ /**
207
+ * Package names the project's package.json declares in any dependency field.
208
+ * @param {Project | undefined} project
209
+ * @returns {Set<string>}
210
+ */
211
+ export function declaredDependencies(project) {
212
+ /** @type {Set<string>} */
213
+ const names = new Set();
214
+ if (project == null) return names;
215
+ try {
216
+ const pkg = JSON.parse(
217
+ fs.readFileSync(path.join(project.cwd, 'package.json'), 'utf8'),
218
+ );
219
+ for (const field of DEPENDENCY_FIELDS) {
220
+ for (const name of Object.keys(pkg?.[field] ?? {})) names.add(name);
221
+ }
222
+ } catch {
223
+ // No readable package.json: nothing is declared.
224
+ }
225
+ return names;
226
+ }
227
+
228
+ /**
229
+ * The command that adds a package with the project's package manager. Discover
230
+ * only prints it; it never runs it.
231
+ * @param {Project | undefined} project
232
+ * @returns {(name: string, version?: string) => string}
233
+ */
234
+ export function addCommand(project) {
235
+ const pm = detectPackageManager(project?.cwd ?? process.cwd());
236
+ const verb =
237
+ pm === 'pnpm'
238
+ ? 'pnpm add'
239
+ : pm === 'yarn'
240
+ ? 'yarn add'
241
+ : pm === 'bun'
242
+ ? 'bun add'
243
+ : 'npm install';
244
+ return (name, version) => `${verb} ${version ? `${name}@${version}` : name}`;
245
+ }
246
+
247
+ /**
248
+ * The per-user cache directory the saved copies live in, never inside a
249
+ * project. Standard platform locations only.
250
+ * @returns {string}
251
+ */
252
+ export function defaultCacheDir() {
253
+ const home = os.homedir();
254
+ if (process.platform === 'win32') {
255
+ return path.join(
256
+ process.env.LOCALAPPDATA || path.join(home, 'AppData', 'Local'),
257
+ 'astryx',
258
+ 'Cache',
259
+ );
260
+ }
261
+ if (process.platform === 'darwin') {
262
+ return path.join(home, 'Library', 'Caches', 'astryx');
263
+ }
264
+ return path.join(
265
+ process.env.XDG_CACHE_HOME || path.join(home, '.cache'),
266
+ 'astryx',
267
+ );
268
+ }
269
+
270
+ /**
271
+ * @param {string} cacheDir
272
+ * @param {string} from
273
+ * @param {{package?: string, version?: string}} request
274
+ */
275
+ function savedCopyPath(cacheDir, from, request) {
276
+ const key = createHash('sha256')
277
+ .update(`${from}\n${request.package ?? ''}\n${request.version ?? ''}`)
278
+ .digest('hex')
279
+ .slice(0, 32);
280
+ const dir = path.join(cacheDir, 'discover');
281
+ return assertWithin(`${key}.json`, dir, {label: 'discover saved copy'});
282
+ }
283
+
284
+ /**
285
+ * @param {string} file
286
+ * @param {string} label
287
+ */
288
+ function readSavedCopy(file, label) {
289
+ try {
290
+ const saved = JSON.parse(fs.readFileSync(file, 'utf8'));
291
+ return {
292
+ savedAt: String(saved.savedAt),
293
+ catalog: parseDiscoverCatalog(saved.catalog, label),
294
+ };
295
+ } catch {
296
+ return null;
297
+ }
298
+ }
299
+
300
+ /**
301
+ * @param {string} file
302
+ * @param {import('../../authoring/discover/type').DiscoverCatalog} catalog
303
+ */
304
+ function writeSavedCopy(file, catalog) {
305
+ try {
306
+ fs.mkdirSync(path.dirname(file), {recursive: true});
307
+ const tmp = `${file}.${process.pid}.tmp`;
308
+ fs.writeFileSync(
309
+ tmp,
310
+ JSON.stringify({savedAt: new Date().toISOString(), catalog}),
311
+ );
312
+ fs.renameSync(tmp, file);
313
+ } catch {
314
+ // An unwritable cache only costs the offline copy.
315
+ }
316
+ }
317
+
318
+ /**
319
+ * @param {DiscoverSource} source
320
+ * @param {{package?: string, version?: string}} request
321
+ * @param {string} label
322
+ * @param {number} timeoutMs
323
+ */
324
+ async function callSource(source, request, label, timeoutMs) {
325
+ const controller = new AbortController();
326
+ /** @type {ReturnType<typeof setTimeout> | undefined} */
327
+ let timer;
328
+ const timedOut = new Promise((_, reject) => {
329
+ timer = setTimeout(() => {
330
+ controller.abort();
331
+ reject(
332
+ new Error(`${label} did not answer within ${timeoutMs / 1000} seconds`),
333
+ );
334
+ }, timeoutMs);
335
+ });
336
+ try {
337
+ const answer = await Promise.race([
338
+ Promise.resolve().then(() =>
339
+ source({signal: controller.signal, ...request}),
340
+ ),
341
+ timedOut,
342
+ ]);
343
+ return parseDiscoverCatalog(answer, label);
344
+ } finally {
345
+ clearTimeout(timer);
346
+ }
347
+ }
348
+
349
+ /**
350
+ * Call every discover source — the project's `discover` first, then each
351
+ * integration's in load order — once per distinct function. A source that
352
+ * throws, times out, or returns an invalid catalog falls back to its saved copy;
353
+ * with none, it is reported and adds nothing. The others always count.
354
+ *
355
+ * @param {Project | undefined} project
356
+ * @param {{package?: string, version?: string}} [request] ask for one package,
357
+ * and optionally one version's contributions
358
+ * @param {{cacheDir?: string, timeoutMs?: number}} [options]
359
+ * @returns {Promise<CatalogResult>}
360
+ */
361
+ export async function callSources(project, request = {}, options = {}) {
362
+ const {cacheDir = defaultCacheDir(), timeoutMs = SOURCE_TIMEOUT_MS} = options;
363
+ /** @type {CatalogResult} */
364
+ const result = {sources: [], packages: []};
365
+ if (project == null) return result;
366
+
367
+ /** @type {Array<{from: string, source?: DiscoverSource, error?: string}>} */
368
+ const origins = [];
369
+ if (typeof project.config?.discover === 'function') {
370
+ origins.push({from: 'astryx.config', source: project.config.discover});
371
+ }
372
+ for (const integration of project.loadedIntegrations) {
373
+ if (integration.__discoverError) {
374
+ origins.push({from: integration.name, error: integration.__discoverError});
375
+ } else if (integration.__discover) {
376
+ origins.push({from: integration.name, source: integration.__discover});
377
+ }
378
+ }
379
+
380
+ /** @type {Set<unknown>} */
381
+ const called = new Set();
382
+ /** @type {Set<string>} */
383
+ const seen = new Set();
384
+ const ask = request.package
385
+ ? {package: request.package, ...(request.version ? {version: request.version} : {})}
386
+ : {};
387
+ for (const {from, source, error} of origins) {
388
+ if (source == null) {
389
+ result.sources.push({name: from, from, status: 'failed', error});
390
+ continue;
391
+ }
392
+ if (called.has(source)) continue;
393
+ called.add(source);
394
+
395
+ const label = `the discover source from ${from}`;
396
+ const file = savedCopyPath(cacheDir, from, ask);
397
+ /** @type {import('../../authoring/discover/type').DiscoverCatalog} */
398
+ let catalog;
399
+ try {
400
+ catalog = await callSource(source, ask, label, timeoutMs);
401
+ writeSavedCopy(file, catalog);
402
+ result.sources.push({
403
+ name: catalog.source.name,
404
+ from,
405
+ status: 'fresh',
406
+ generatedAt: catalog.source.generatedAt,
407
+ complete: catalog.source.complete,
408
+ });
409
+ } catch (err) {
410
+ const message = err instanceof Error ? err.message : String(err);
411
+ const saved = readSavedCopy(file, label);
412
+ if (saved == null) {
413
+ result.sources.push({name: from, from, status: 'failed', error: message});
414
+ continue;
415
+ }
416
+ catalog = saved.catalog;
417
+ result.sources.push({
418
+ name: catalog.source.name,
419
+ from,
420
+ status: 'saved',
421
+ generatedAt: catalog.source.generatedAt,
422
+ complete: catalog.source.complete,
423
+ savedAt: saved.savedAt,
424
+ error: message,
425
+ });
426
+ }
427
+ for (const pkg of catalog.packages) {
428
+ if (seen.has(pkg.package)) continue;
429
+ seen.add(pkg.package);
430
+ result.packages.push({...pkg, source: catalog.source.name});
431
+ }
432
+ }
433
+ return result;
434
+ }
435
+
436
+ /** The list and detail entry fields for each item kind, in display order. */
437
+ export const KIND_FIELDS = /** @type {const} */ ([
438
+ ['component', 'components'],
439
+ ['template', 'templates'],
440
+ ['doc', 'docs'],
441
+ ['theme', 'themes'],
442
+ ['codemod', 'codemods'],
443
+ ['agent-doc', 'agentDocs'],
444
+ ]);
445
+
446
+ /**
447
+ * Project a scanned or installed package into the discover list/detail entry
448
+ * shape. Shared by the list and detail leaves so the entry keys (and their
449
+ * order) stay in one place. A kind list appears only when it is not empty.
450
+ * @param {InstalledPackage & {latest?: string}} pkg
104
451
  * @returns {import('./discover.type.mjs').DiscoverListEntry}
105
452
  */
106
453
  export function toEntry(pkg) {
107
- return {
454
+ /** @type {Record<string, unknown>} */
455
+ const entry = {
108
456
  name: pkg.name,
109
457
  category: pkg.category,
110
458
  components: pkg.components,
@@ -112,6 +460,13 @@ export function toEntry(pkg) {
112
460
  description: pkg.description,
113
461
  displayName: pkg.displayName,
114
462
  };
463
+ for (const [, field] of KIND_FIELDS) {
464
+ if (field === 'components') continue;
465
+ const values = /** @type {Record<string, unknown>} */ (pkg)[field];
466
+ if (Array.isArray(values) && values.length > 0) entry[field] = values;
467
+ }
468
+ if (pkg.latest) entry.latest = pkg.latest;
469
+ return /** @type {import('./discover.type.mjs').DiscoverListEntry} */ (entry);
115
470
  }
116
471
 
117
472
  /**