@astryxdesign/cli 0.6.5-canary.b421f01 → 0.6.5-canary.b65e45f

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 (34) hide show
  1. package/api/build/_adapter.d.mts +14 -0
  2. package/api/build/_adapter.mjs +58 -1
  3. package/api/build/build.test.mjs +74 -1
  4. package/api/build/kit/kit.mjs +39 -7
  5. package/api/build/kit/rank.d.mts +8 -0
  6. package/api/build/kit/rank.mjs +14 -0
  7. package/api/build/kit/weights.d.mts +82 -0
  8. package/api/build/kit/weights.json +1 -0
  9. package/api/build/kit/weights.mjs +305 -0
  10. package/api/build/kit/weights.test.mjs +190 -0
  11. package/api/docs/docs.d.mts +6 -0
  12. package/api/docs/docs.depth.test.mjs +132 -0
  13. package/api/docs/docs.doc.mjs +31 -2
  14. package/api/docs/docs.mjs +44 -1
  15. package/api/docs/docs.type.d.mts +38 -1
  16. package/api/docs/docs.type.mjs +18 -1
  17. package/api/docs/node/node.d.mts +28 -3
  18. package/api/docs/node/node.mjs +146 -23
  19. package/api/hook/list/list.d.mts +1 -1
  20. package/api/integration/add-theme.mjs +93 -3
  21. package/api/integration/add-theme.test.mjs +100 -0
  22. package/api/integration/integrationAddTheme.doc.mjs +1 -1
  23. package/api/search/search.d.mts +8 -1
  24. package/api/search/search.mjs +48 -37
  25. package/api/search/search.test.mjs +114 -1
  26. package/assets/docs/tree/add-a-theme.doc.mjs +4 -0
  27. package/clients/cli/commands/docs.depth.test.mjs +219 -0
  28. package/clients/cli/commands/docs.doc.mjs +15 -2
  29. package/clients/cli/commands/docs.mjs +222 -5
  30. package/clients/cli/commands/integration-add.doc.mjs +1 -1
  31. package/clients/cli/commands/search.test.mjs +22 -13
  32. package/foundation/discovery/theme-discovery.d.mts +6 -0
  33. package/foundation/discovery/theme-discovery.mjs +9 -0
  34. package/package.json +9 -9
@@ -25,7 +25,8 @@ export const doc = {
25
25
  'ones the project\'s configured integrations contribute, including any ' +
26
26
  'topic an integration replaces or extends, so it depends on the cwd. ' +
27
27
  'A route opens a node of the docs tree instead: a namespace such as ' +
28
- "`cli/api` returns its children one level down, a typed doc such as " +
28
+ '`cli/api` returns its children one level down (`depth` reads as many ' +
29
+ 'levels as asked, and `detail` how much of each doc below), a typed doc such as ' +
29
30
  "`cli/api/functions/search` returns its content, and a guide the tree " +
30
31
  'places (`cli/integrations/quick-start`) reads like any topic. ' +
31
32
  'Every read but the list carries `links`, the commands that move from it: ' +
@@ -82,6 +83,18 @@ export const doc = {
82
83
  description:
83
84
  "Return the topic's section index (each section's key, title, and summary), even for a topic with one section.",
84
85
  },
86
+ {
87
+ name: 'options.depth',
88
+ type: "number | 'all'",
89
+ description:
90
+ "How many levels below a docs-tree namespace to read: 0 for the namespace alone, 1 for its children (the default), 'all' for every level. Where a read stops, a child with docs below it carries childCount. A doc with nothing below it reads the same at any depth.",
91
+ },
92
+ {
93
+ name: 'options.detail',
94
+ type: "'brief' | 'compact' | 'full'",
95
+ description:
96
+ "How much of each doc below the named one a depth read returns: brief (the default) is its identity; compact and full add its text (a guide's sections, a namespace's or typed doc's content). Given alone, it reads one level down.",
97
+ },
85
98
  {
86
99
  name: 'options.cwd',
87
100
  type: 'string',
@@ -113,10 +126,18 @@ export const doc = {
113
126
  {
114
127
  type: 'docs.node',
115
128
  description:
116
- "A namespace or typed doc in the docs tree, read by its route: {id, route, kind, package, title, summary, breadcrumb, slots, content}. A namespace lists each slot's children one level down; a typed doc carries its content.",
129
+ "A namespace or typed doc in the docs tree, read by its route: {id, route, kind, package, title, summary, breadcrumb, slots, content}. A namespace lists each slot's children one level down, or as deep as depth asks, each child carrying its own slots, childCount where the read stops, and its text at compact or full detail; a typed doc carries its content.",
117
130
  },
118
131
  ],
119
132
  throws: [
133
+ {
134
+ code: 'ERR_INVALID_ARGUMENT',
135
+ when: "depth is not a whole number of levels or 'all'",
136
+ },
137
+ {
138
+ code: 'ERR_INVALID_DETAIL',
139
+ when: 'detail is not brief, compact, or full',
140
+ },
120
141
  {
121
142
  code: 'ERR_UNKNOWN_TOPIC',
122
143
  when: 'the topic is not a string, or matches no topic and no docs-tree route',
@@ -134,6 +155,14 @@ export const doc = {
134
155
  code: "await docs('principles', undefined, {index: true});",
135
156
  },
136
157
  {label: 'A docs-tree namespace', code: "await docs('cli/api');"},
158
+ {
159
+ label: 'Every doc below a namespace, one entry each',
160
+ code: "await docs('cli', undefined, {depth: 'all'});",
161
+ },
162
+ {
163
+ label: 'A namespace and everything below it, in full',
164
+ code: "await docs('cli/integrations', undefined, {depth: 'all', detail: 'full'});",
165
+ },
137
166
  {label: 'One API function', code: "await docs('cli/api/functions/search');"},
138
167
  {
139
168
  label: 'A whole guide from the docs tree',
package/api/docs/docs.mjs CHANGED
@@ -11,6 +11,7 @@
11
11
  * docs(topic, undefined, {index: true}) -> index -> docs.index
12
12
  * docs(topic, section) -> section -> docs.detail.section
13
13
  * docs(route) -> node -> docs.node
14
+ * docs(route, undefined, {depth: 2}) -> node -> docs.node, two levels
14
15
  *
15
16
  * A topic read returns the whole doc, as it always has; `index` returns its
16
17
  * sections, so a reader can open one by its key (spec:AST-047). The CLI's text
@@ -33,6 +34,43 @@ import {ERROR_CODES} from '../../foundation/response/error-codes.mjs';
33
34
 
34
35
  export {list, index, detail, sectionLeaf as section, nodeLeaf as node};
35
36
 
37
+ const DETAILS = ['brief', 'compact', 'full'];
38
+
39
+ /**
40
+ * How far a docs-tree read goes, from the options: `depth` levels below the
41
+ * node, or every level for `'all'`, and `detail` for each doc below it (brief
42
+ * by default). `detail` alone reads one level down. Neither: the plain read.
43
+ * @param {import('./docs.type.mjs').DocsOptions} options
44
+ * @returns {import('./node/node.mjs').DepthRead | undefined}
45
+ */
46
+ function depthRead(options) {
47
+ const {depth, detail} = options;
48
+ if (detail != null && !DETAILS.includes(detail)) {
49
+ throw new AstryxError(
50
+ `detail is brief, compact, or full, not ${JSON.stringify(detail)}.`,
51
+ undefined,
52
+ ERROR_CODES.ERR_INVALID_DETAIL,
53
+ );
54
+ }
55
+ if (
56
+ depth != null &&
57
+ depth !== 'all' &&
58
+ !(Number.isInteger(depth) && /** @type {number} */ (depth) >= 0)
59
+ ) {
60
+ throw new AstryxError(
61
+ `depth is a number of levels (0, 1, 2, ...) or "all", not ${JSON.stringify(depth)}.`,
62
+ undefined,
63
+ ERROR_CODES.ERR_INVALID_ARGUMENT,
64
+ );
65
+ }
66
+ if (depth == null && detail == null) return undefined;
67
+ return {
68
+ depth: depth === 'all' ? Infinity : (depth ?? 1),
69
+ detail: /** @type {'brief' | 'compact' | 'full'} */ (detail ?? 'brief'),
70
+ lang: options.lang || (options.dense ? 'dense' : options.zh ? 'zh' : null),
71
+ };
72
+ }
73
+
36
74
  /**
37
75
  * @param {string} [topic]
38
76
  * @param {string} [section]
@@ -42,6 +80,10 @@ export {list, index, detail, sectionLeaf as section, nodeLeaf as node};
42
80
  * @param {boolean} [options.dense]
43
81
  * @param {boolean} [options.index] return the topic's section index instead of
44
82
  * the whole doc
83
+ * @param {number | 'all'} [options.depth] how many levels below a docs-tree
84
+ * namespace to read; a doc with nothing below it reads the same at any depth
85
+ * @param {'brief' | 'compact' | 'full'} [options.detail] how much of each doc
86
+ * below the named one a depth read returns (brief by default)
45
87
  * @param {string} [options.cwd]
46
88
  * @returns {Promise<
47
89
  * import('./docs.type.mjs').DocsListResponse |
@@ -52,6 +94,7 @@ export {list, index, detail, sectionLeaf as section, nodeLeaf as node};
52
94
  * >}
53
95
  */
54
96
  export async function docs(topic, section, options = {}) {
97
+ const read = depthRead(options);
55
98
  if (!topic) return list(options);
56
99
  const found = await resolveDocsArgument(topic, options);
57
100
  if (found.kind === 'node') {
@@ -77,7 +120,7 @@ export async function docs(topic, section, options = {}) {
77
120
  }
78
121
  return {
79
122
  type: 'docs.node',
80
- data: await nodeView(found.catalog, found.tree, found.node),
123
+ data: await nodeView(found.catalog, found.tree, found.node, read),
81
124
  };
82
125
  }
83
126
  if (section) return sectionLeaf(topic, section, options);
@@ -205,9 +205,14 @@ export type DocsNode = {
205
205
  breadcrumb: DocsNodeLink[];
206
206
  /**
207
207
  * a namespace's slots that hold children, in
208
- * order; empty for a typed doc
208
+ * order; empty for a typed doc, and for a depth read of 0
209
209
  */
210
210
  slots: DocsNodeSlot[];
211
+ /**
212
+ * with a depth read of 0, how many docs sit
213
+ * right below the namespace
214
+ */
215
+ childCount?: number | undefined;
211
216
  /**
212
217
  * a typed doc's content; empty for a namespace
213
218
  */
@@ -246,6 +251,26 @@ export type DocsNodeChild = {
246
251
  kind: string;
247
252
  title: string;
248
253
  summary: string;
254
+ /**
255
+ * with a depth read, its own slots, while
256
+ * the read goes deeper
257
+ */
258
+ slots?: DocsNodeSlot[] | undefined;
259
+ /**
260
+ * with a depth read, how many docs sit right
261
+ * below it where the read stops
262
+ */
263
+ childCount?: number | undefined;
264
+ /**
265
+ * with a depth read at compact or full detail: a namespace's intro or a typed
266
+ * doc's content
267
+ */
268
+ content?: import("@astryxdesign/cli/authoring").ReferenceContentBlock[] | undefined;
269
+ /**
270
+ * with a depth read at compact or full
271
+ * detail: a guide's sections
272
+ */
273
+ sections?: DocsReadSection[] | undefined;
249
274
  };
250
275
  /**
251
276
  * Options for `docs()`.
@@ -259,6 +284,18 @@ export type DocsOptions = {
259
284
  * whole doc
260
285
  */
261
286
  index?: boolean | undefined;
287
+ /**
288
+ * how many levels below a docs-tree
289
+ * namespace to read: 0 for the namespace alone, 1 for its children (the
290
+ * default), 'all' for every level
291
+ */
292
+ depth?: number | "all" | undefined;
293
+ /**
294
+ * how much of each doc below
295
+ * the named one a depth read returns: brief (the default) is its identity,
296
+ * compact and full add its text
297
+ */
298
+ detail?: "compact" | "full" | "brief" | undefined;
262
299
  /**
263
300
  * project directory whose configured integrations
264
301
  * contribute topics; defaults to process.cwd()
@@ -153,7 +153,9 @@
153
153
  * @property {string} summary
154
154
  * @property {DocsNodeLink[]} breadcrumb the namespaces above it, top first
155
155
  * @property {DocsNodeSlot[]} slots a namespace's slots that hold children, in
156
- * order; empty for a typed doc
156
+ * order; empty for a typed doc, and for a depth read of 0
157
+ * @property {number} [childCount] with a depth read of 0, how many docs sit
158
+ * right below the namespace
157
159
  * @property {import('@astryxdesign/cli/authoring').ReferenceContentBlock[]} content
158
160
  * a typed doc's content; empty for a namespace
159
161
  * @property {DocsLinks} links the moves from the node: up to its parent (the
@@ -181,6 +183,15 @@
181
183
  * @property {string} kind
182
184
  * @property {string} title
183
185
  * @property {string} summary
186
+ * @property {DocsNodeSlot[]} [slots] with a depth read, its own slots, while
187
+ * the read goes deeper
188
+ * @property {number} [childCount] with a depth read, how many docs sit right
189
+ * below it where the read stops
190
+ * @property {import('@astryxdesign/cli/authoring').ReferenceContentBlock[]} [content]
191
+ * with a depth read at compact or full detail: a namespace's intro or a typed
192
+ * doc's content
193
+ * @property {DocsReadSection[]} [sections] with a depth read at compact or full
194
+ * detail: a guide's sections
184
195
  */
185
196
 
186
197
  /**
@@ -191,6 +202,12 @@
191
202
  * @property {boolean} [dense]
192
203
  * @property {boolean} [index] return a topic's section index instead of its
193
204
  * whole doc
205
+ * @property {number | 'all'} [depth] how many levels below a docs-tree
206
+ * namespace to read: 0 for the namespace alone, 1 for its children (the
207
+ * default), 'all' for every level
208
+ * @property {'brief' | 'compact' | 'full'} [detail] how much of each doc below
209
+ * the named one a depth read returns: brief (the default) is its identity,
210
+ * compact and full add its text
194
211
  * @property {string} [cwd] project directory whose configured integrations
195
212
  * contribute topics; defaults to process.cwd()
196
213
  */
@@ -4,15 +4,28 @@
4
4
  /**
5
5
  * @typedef {import('../../../foundation/doc-compiler/tree.mjs').DocsTree} DocsTree
6
6
  * @typedef {import('../../../foundation/doc-compiler/tree.mjs').TreeNode} TreeNode
7
+ * @typedef {import('../../../foundation/discovery/docs-discovery.mjs').DocsCatalog} DocsCatalog
7
8
  */
8
9
  /**
9
- * The docs.node view of one tree node.
10
- * @param {import('../../../foundation/discovery/docs-discovery.mjs').DocsCatalog} catalog
10
+ * How far a read goes below the node it names, and how much of each doc below
11
+ * it shows: `depth` levels (Infinity for every level), and `detail` brief (its
12
+ * identity), or compact or full (with its text: a namespace's intro, a guide's
13
+ * sections, a typed doc's content). `lang` selects the guides' language.
14
+ * @typedef {object} DepthRead
15
+ * @property {number} depth
16
+ * @property {'brief' | 'compact' | 'full'} detail
17
+ * @property {string | null} lang
18
+ */
19
+ /**
20
+ * The docs.node view of one tree node. Without a depth read, a namespace lists
21
+ * its children one level down.
22
+ * @param {DocsCatalog} catalog
11
23
  * @param {DocsTree} tree
12
24
  * @param {TreeNode} node
25
+ * @param {DepthRead} [read] how far down to go, and how much of each doc below
13
26
  * @returns {Promise<import('../docs.type.mjs').DocsNode>}
14
27
  */
15
- export function nodeView(catalog: import("../../../foundation/discovery/docs-discovery.mjs").DocsCatalog, tree: DocsTree, node: TreeNode): Promise<import("../docs.type.mjs").DocsNode>;
28
+ export function nodeView(catalog: DocsCatalog, tree: DocsTree, node: TreeNode, read?: DepthRead): Promise<import("../docs.type.mjs").DocsNode>;
16
29
  /**
17
30
  * The typed edges a doc declares (spec:AST-047 FR5), resolved to routes: a
18
31
  * function doc's `command` and `related`, and a command doc's `fn` and
@@ -41,3 +54,15 @@ export function node(route: string, options?: {
41
54
  }): Promise<import("../docs.type.mjs").DocsNodeResponse>;
42
55
  export type DocsTree = import("../../../foundation/doc-compiler/tree.mjs").DocsTree;
43
56
  export type TreeNode = import("../../../foundation/doc-compiler/tree.mjs").TreeNode;
57
+ export type DocsCatalog = import("../../../foundation/discovery/docs-discovery.mjs").DocsCatalog;
58
+ /**
59
+ * How far a read goes below the node it names, and how much of each doc below
60
+ * it shows: `depth` levels (Infinity for every level), and `detail` brief (its
61
+ * identity), or compact or full (with its text: a namespace's intro, a guide's
62
+ * sections, a typed doc's content). `lang` selects the guides' language.
63
+ */
64
+ export type DepthRead = {
65
+ depth: number;
66
+ detail: "brief" | "compact" | "full";
67
+ lang: string | null;
68
+ };
@@ -7,14 +7,18 @@
7
7
  * typed doc such as `cli/api/functions/search`, plus {cwd}.
8
8
  * @output { type: 'docs.node', data: DocsNode } — the node's identity, title
9
9
  * and summary, the namespaces above it, and either its slots with their
10
- * children (a namespace) or its content (a typed doc). A namespace never
11
- * lists its grandchildren, so a reader goes down one level at a time.
12
- * Matches `astryx --json docs <route>`.
10
+ * children (a namespace) or its content (a typed doc). By default a
11
+ * namespace lists its children one level down; a depth read goes as far
12
+ * down as asked, with each doc below as its identity (brief) or with its
13
+ * text (compact, full). Matches `astryx --json docs <route> [--depth <n>]`.
13
14
  * @position Leaf under api/docs, beside the topic leaves. A guide the tree
14
15
  * places is a topic: the detail leaf reads it by its route.
15
16
  */
16
17
 
18
+ import {detailView} from '../../../foundation/doc-compiler/lenses.mjs';
17
19
  import {
20
+ compileTopic,
21
+ guideEntry,
18
22
  nodeContent,
19
23
  placeLinks,
20
24
  resolveDocsArgument,
@@ -24,18 +28,34 @@ import {
24
28
  /**
25
29
  * @typedef {import('../../../foundation/doc-compiler/tree.mjs').DocsTree} DocsTree
26
30
  * @typedef {import('../../../foundation/doc-compiler/tree.mjs').TreeNode} TreeNode
31
+ * @typedef {import('../../../foundation/discovery/docs-discovery.mjs').DocsCatalog} DocsCatalog
27
32
  */
28
33
 
29
34
  /**
30
- * The docs.node view of one tree node.
31
- * @param {import('../../../foundation/discovery/docs-discovery.mjs').DocsCatalog} catalog
35
+ * How far a read goes below the node it names, and how much of each doc below
36
+ * it shows: `depth` levels (Infinity for every level), and `detail` brief (its
37
+ * identity), or compact or full (with its text: a namespace's intro, a guide's
38
+ * sections, a typed doc's content). `lang` selects the guides' language.
39
+ * @typedef {object} DepthRead
40
+ * @property {number} depth
41
+ * @property {'brief' | 'compact' | 'full'} detail
42
+ * @property {string | null} lang
43
+ */
44
+
45
+ /**
46
+ * The docs.node view of one tree node. Without a depth read, a namespace lists
47
+ * its children one level down.
48
+ * @param {DocsCatalog} catalog
32
49
  * @param {DocsTree} tree
33
50
  * @param {TreeNode} node
51
+ * @param {DepthRead} [read] how far down to go, and how much of each doc below
34
52
  * @returns {Promise<import('../docs.type.mjs').DocsNode>}
35
53
  */
36
- export async function nodeView(catalog, tree, node) {
54
+ export async function nodeView(catalog, tree, node, read) {
37
55
  const {content} = await nodeContent(catalog, tree, node);
38
- return {
56
+ const slots = node.slots.filter(slot => slot.children.length > 0);
57
+ /** @type {import('../docs.type.mjs').DocsNode} */
58
+ const view = {
39
59
  id: node.id,
40
60
  route: node.route,
41
61
  kind: node.kind,
@@ -45,25 +65,128 @@ export async function nodeView(catalog, tree, node) {
45
65
  breadcrumb: tree
46
66
  .ancestors(node)
47
67
  .map(ancestor => ({route: ancestor.route, title: ancestor.title})),
48
- slots: node.slots
49
- .filter(slot => slot.children.length > 0)
50
- .map(slot => ({
51
- name: slot.name,
52
- title: slot.title,
53
- children: slot.children.map(route => {
54
- const child = /** @type {TreeNode} */ (tree.get(route));
55
- return {
56
- route: child.route,
57
- name: child.route.slice(child.route.lastIndexOf('/') + 1),
58
- kind: child.kind,
59
- title: child.title,
60
- summary: child.summary,
61
- };
62
- }),
63
- })),
68
+ slots: !read
69
+ ? slots.map(slot => ({
70
+ name: slot.name,
71
+ title: slot.title,
72
+ children: slot.children.map(route =>
73
+ childIdentity(/** @type {TreeNode} */ (tree.get(route))),
74
+ ),
75
+ }))
76
+ : read.depth >= 1
77
+ ? await slotsBelow(catalog, tree, slots, 1, read)
78
+ : [],
64
79
  content,
65
80
  links: nodeLinks(tree, node),
66
81
  };
82
+ if (read && read.depth < 1 && slots.length > 0) {
83
+ view.childCount = countChildren(slots);
84
+ }
85
+ return view;
86
+ }
87
+
88
+ /**
89
+ * The fields every child carries: what it is, and the route that opens it.
90
+ * @param {TreeNode} child
91
+ * @returns {import('../docs.type.mjs').DocsNodeChild}
92
+ */
93
+ function childIdentity(child) {
94
+ return {
95
+ route: child.route,
96
+ name: child.route.slice(child.route.lastIndexOf('/') + 1),
97
+ kind: child.kind,
98
+ title: child.title,
99
+ summary: child.summary,
100
+ };
101
+ }
102
+
103
+ /**
104
+ * @param {TreeNode['slots']} slots
105
+ * @returns {number}
106
+ */
107
+ function countChildren(slots) {
108
+ return slots.reduce((count, slot) => count + slot.children.length, 0);
109
+ }
110
+
111
+ /**
112
+ * Slots read `level` levels below the named node: each child at the read's
113
+ * detail, and its own slots while the read goes deeper.
114
+ * @param {DocsCatalog} catalog
115
+ * @param {DocsTree} tree
116
+ * @param {TreeNode['slots']} slots
117
+ * @param {number} level
118
+ * @param {DepthRead} read
119
+ * @returns {Promise<import('../docs.type.mjs').DocsNodeSlot[]>}
120
+ */
121
+ async function slotsBelow(catalog, tree, slots, level, read) {
122
+ return Promise.all(
123
+ slots.map(async slot => ({
124
+ name: slot.name,
125
+ title: slot.title,
126
+ children: await Promise.all(
127
+ slot.children.map(route =>
128
+ childBelow(
129
+ catalog,
130
+ tree,
131
+ /** @type {TreeNode} */ (tree.get(route)),
132
+ level,
133
+ read,
134
+ ),
135
+ ),
136
+ ),
137
+ })),
138
+ );
139
+ }
140
+
141
+ /**
142
+ * One child of a depth read. Where the read stops, a child with docs below it
143
+ * says how many, so a reader knows to go deeper.
144
+ * @param {DocsCatalog} catalog
145
+ * @param {DocsTree} tree
146
+ * @param {TreeNode} child
147
+ * @param {number} level
148
+ * @param {DepthRead} read
149
+ * @returns {Promise<import('../docs.type.mjs').DocsNodeChild>}
150
+ */
151
+ async function childBelow(catalog, tree, child, level, read) {
152
+ const view = childIdentity(child);
153
+ if (read.detail !== 'brief') {
154
+ Object.assign(view, await childText(catalog, tree, child, read.lang));
155
+ }
156
+ const slots = child.slots.filter(slot => slot.children.length > 0);
157
+ if (slots.length > 0) {
158
+ if (level < read.depth) {
159
+ view.slots = await slotsBelow(catalog, tree, slots, level + 1, read);
160
+ } else {
161
+ view.childCount = countChildren(slots);
162
+ }
163
+ }
164
+ return view;
165
+ }
166
+
167
+ /**
168
+ * A child's text: a guide's compiled sections, or the content of a namespace
169
+ * or typed doc.
170
+ * @param {DocsCatalog} catalog
171
+ * @param {DocsTree} tree
172
+ * @param {TreeNode} child
173
+ * @param {string | null} lang
174
+ * @returns {Promise<{content?: any[], sections?: any[]}>}
175
+ */
176
+ async function childText(catalog, tree, child, lang) {
177
+ if (child.kind === 'generic') {
178
+ const entry = child.ref?.topicFile
179
+ ? guideEntry(child)
180
+ : child.ref?.flatTopic
181
+ ? catalog.resolve(child.ref.flatTopic)
182
+ : null;
183
+ if (!entry) return {};
184
+ return {
185
+ sections: detailView(await compileTopic(catalog, entry, lang)).sections,
186
+ };
187
+ }
188
+ const {content} = await nodeContent(catalog, tree, child);
189
+ return {content};
67
190
  }
68
191
 
69
192
  /**
@@ -13,7 +13,7 @@
13
13
  export function list({ cwd, category, detail, zh, lang, }?: {
14
14
  cwd?: string | undefined;
15
15
  category?: string | undefined;
16
- detail?: "compact" | "full" | "names" | "brief" | undefined;
16
+ detail?: "compact" | "full" | "brief" | "names" | undefined;
17
17
  zh?: boolean | undefined;
18
18
  lang?: string | null | undefined;
19
19
  }): Promise<import("../hook.type.mjs").HookListResponse>;
@@ -10,8 +10,10 @@
10
10
  */
11
11
 
12
12
  import * as fs from 'node:fs';
13
+ import {isBuiltin} from 'node:module';
13
14
  import * as path from 'node:path';
14
15
  import {AstryxError} from '../error.mjs';
16
+ import {CLI_ROOT} from '../../foundation/fs/paths.mjs';
15
17
  import {ERROR_CODES} from '../../foundation/response/error-codes.mjs';
16
18
  import {
17
19
  assertWithin,
@@ -21,6 +23,7 @@ import {
21
23
  import {
22
24
  discoverBundledThemes,
23
25
  discoverThemeDirectory,
26
+ themeFileImports,
24
27
  } from '../../foundation/discovery/theme-discovery.mjs';
25
28
  import {
26
29
  findLocalIntegrationManifestOrNull,
@@ -214,6 +217,77 @@ function escapeRegExp(s) {
214
217
  return s.replace(/[.*+?^${}()|[\]\\]/gu, '\\$&');
215
218
  }
216
219
 
220
+ /** What every app that uses Astryx already has: Core, and React through Core's peers. */
221
+ const APP_PROVIDED = new Set(['@astryxdesign/core', 'react', 'react-dom']);
222
+
223
+ /**
224
+ * The npm package a module specifier names, or null for a relative path, an
225
+ * absolute path, or a Node built-in.
226
+ * @param {string} specifier
227
+ * @returns {string | null}
228
+ */
229
+ function packageOfSpecifier(specifier) {
230
+ if (specifier.startsWith('.') || specifier.startsWith('/')) return null;
231
+ if (isBuiltin(specifier)) return null;
232
+ const parts = specifier.split('/');
233
+ return specifier.startsWith('@') ? parts.slice(0, 2).join('/') : parts[0];
234
+ }
235
+
236
+ /**
237
+ * The npm packages a fork's copied files import that its package does not
238
+ * declare yet, each at the range the bundled themes are built against (the
239
+ * CLI's own package.json). The fork depends on them at runtime, so an app that
240
+ * installs the package needs them too. Derived from the imports, not copied
241
+ * from a base's whole dependency list. Core and React are left out, since every
242
+ * Astryx app has them; so is a fork of the package's own theme, whose imports
243
+ * the package already resolves.
244
+ *
245
+ * @param {import('../../foundation/discovery/theme-discovery.mjs').DiscoveredTheme} base
246
+ * @param {Record<string, any>} pkg - The package's package.json.
247
+ * @returns {Record<string, string>} package name to range
248
+ */
249
+ function forkDependencies(base, pkg) {
250
+ if (!base.bundled) return {};
251
+ /** @type {Record<string, any>} */
252
+ let cli;
253
+ try {
254
+ cli = JSON.parse(
255
+ fs.readFileSync(path.join(CLI_ROOT, 'package.json'), 'utf-8'),
256
+ );
257
+ } catch {
258
+ return {};
259
+ }
260
+ /** @type {Record<string, string>} */
261
+ const ranges = {
262
+ ...cli.devDependencies,
263
+ ...cli.peerDependencies,
264
+ ...cli.dependencies,
265
+ };
266
+ const declared = {
267
+ ...pkg.devDependencies,
268
+ ...pkg.peerDependencies,
269
+ ...pkg.dependencies,
270
+ };
271
+ /** @type {Record<string, string>} */
272
+ const needed = {};
273
+ for (const file of base.files) {
274
+ const code = /\.(?:[cm]?[jt]sx?)$/u.test(file);
275
+ if (!code || file.endsWith('.doc.mjs')) continue;
276
+ let specifiers;
277
+ try {
278
+ specifiers = themeFileImports(path.join(base.sourceDir, file));
279
+ } catch {
280
+ continue; // discovery already refuses a theme whose source does not parse
281
+ }
282
+ for (const specifier of specifiers) {
283
+ const name = packageOfSpecifier(specifier);
284
+ if (name == null || APP_PROVIDED.has(name) || name in declared) continue;
285
+ if (typeof ranges[name] === 'string') needed[name] = ranges[name];
286
+ }
287
+ }
288
+ return needed;
289
+ }
290
+
217
291
  /**
218
292
  * Build the write plans for a forked theme.
219
293
  * @param {import('../../foundation/discovery/theme-discovery.mjs').DiscoveredTheme} base
@@ -467,11 +541,27 @@ export async function integrationAddTheme(name, options = {}) {
467
541
  packageUpdate?.expectedOriginal ?? fs.readFileSync(packageFile);
468
542
  const text = packageUpdate?.contents ?? expectedOriginal.toString('utf-8');
469
543
  const current = JSON.parse(text);
470
- if (themesCliProblem(current) != null) {
544
+ let next =
545
+ themesCliProblem(current) != null
546
+ ? withCliPeer(current, THEMES_CLI)
547
+ : current;
548
+ // A fork runs the base's imports: declare the packages they come from.
549
+ const forked = baseTheme ? forkDependencies(baseTheme, next) : {};
550
+ if (Object.keys(forked).length > 0) {
551
+ const dependencies = {...next.dependencies, ...forked};
552
+ next = {
553
+ ...next,
554
+ dependencies: Object.fromEntries(
555
+ Object.keys(dependencies)
556
+ .sort()
557
+ .map(name => [name, dependencies[name]]),
558
+ ),
559
+ };
560
+ }
561
+ if (next !== current) {
471
562
  packageUpdate = {
472
563
  contents:
473
- JSON.stringify(withCliPeer(current, THEMES_CLI), null, 2) +
474
- (text.endsWith('\n') ? '\n' : ''),
564
+ JSON.stringify(next, null, 2) + (text.endsWith('\n') ? '\n' : ''),
475
565
  expectedOriginal,
476
566
  };
477
567
  }