@astryxdesign/cli 0.6.3-canary.b1da7d2 → 0.6.3-canary.b6f9ee7

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 (185) hide show
  1. package/README.md +19 -17
  2. package/api/component/_adapter.d.mts +6 -12
  3. package/api/component/_adapter.mjs +20 -10
  4. package/api/component/component.mjs +60 -12
  5. package/api/component/list/list.mjs +3 -2
  6. package/api/docs/_adapter.d.mts +209 -28
  7. package/api/docs/_adapter.mjs +667 -35
  8. package/api/docs/detail/detail.mjs +11 -3
  9. package/api/docs/detail/section/section.mjs +24 -13
  10. package/api/docs/detail/section/section.test.mjs +15 -6
  11. package/api/docs/docs.d.mts +5 -3
  12. package/api/docs/docs.doc.mjs +35 -15
  13. package/api/docs/docs.mjs +44 -8
  14. package/api/docs/docs.test.mjs +158 -4
  15. package/api/docs/docs.type.d.mts +165 -2
  16. package/api/docs/docs.type.mjs +101 -3
  17. package/api/docs/index/index.mjs +11 -3
  18. package/api/docs/index/index.test.mjs +1 -1
  19. package/api/docs/integration-tree.test.mjs +547 -0
  20. package/api/docs/integrationDocs.test.mjs +14 -14
  21. package/api/docs/list/list.mjs +25 -5
  22. package/api/docs/node/node.d.mts +43 -0
  23. package/api/docs/node/node.mjs +192 -0
  24. package/api/doctor/doctor.d.mts +30 -2
  25. package/api/doctor/doctor.mjs +147 -8
  26. package/api/doctor/doctor.test.mjs +95 -7
  27. package/api/index.d.mts +1 -1
  28. package/api/index.mjs +1 -0
  29. package/api/init/init.doc.mjs +1 -1
  30. package/api/integration/add-contribution.d.mts +2 -1
  31. package/api/integration/add-contribution.mjs +107 -5
  32. package/api/integration/add-contribution.test.mjs +137 -0
  33. package/api/integration/authoring-checks.mjs +108 -31
  34. package/api/integration/authoring-checks.test.mjs +163 -2
  35. package/api/integration/authoring-checks.type.d.mts +3 -1
  36. package/api/integration/authoring-checks.type.mjs +3 -1
  37. package/api/integration/integration-authoring.type.d.mts +2 -0
  38. package/api/integration/integration-authoring.type.mjs +2 -0
  39. package/api/integration/integrationAdd.doc.mjs +6 -0
  40. package/api/integration/integrationAddDoc.doc.mjs +7 -1
  41. package/api/integration/integrationDocConflicts.doc.mjs +1 -1
  42. package/api/integration/integrationPackCheck.doc.mjs +1 -1
  43. package/api/integration/integrationTemplateConflicts.doc.mjs +6 -5
  44. package/api/integration/pack-check.mjs +34 -0
  45. package/api/integration/pack-check.test.mjs +121 -0
  46. package/api/integration/pack-check.type.d.mts +26 -2
  47. package/api/integration/pack-check.type.mjs +14 -1
  48. package/api/integration/validate-integration.test.mjs +3 -3
  49. package/api/integration/validateIntegration.doc.mjs +1 -1
  50. package/api/json/index.ts +2 -0
  51. package/api/layout/_adapter.mjs +20 -5
  52. package/api/search/search.d.mts +26 -1
  53. package/api/search/search.doc.mjs +4 -3
  54. package/api/search/search.mjs +308 -29
  55. package/api/search/search.test.mjs +91 -2
  56. package/api/search/search.type.d.mts +13 -1
  57. package/api/search/search.type.mjs +4 -1
  58. package/api/template/list/list.mjs +1 -0
  59. package/api/template/template-integration.test.mjs +1022 -3
  60. package/api/template/template.doc.mjs +27 -7
  61. package/api/template/template.mjs +45 -8
  62. package/api/template/template.type.d.mts +6 -8
  63. package/api/template/template.type.mjs +3 -2
  64. package/api/theme/template/template.test.mjs +5 -0
  65. package/api/theme/themeTemplate.doc.mjs +1 -1
  66. package/assets/docs/getting-started.doc.mjs +2 -2
  67. package/assets/docs/principles.doc.mjs +6 -6
  68. package/assets/docs/styling-libraries.doc.mjs +2 -2
  69. package/assets/docs/styling.doc.mjs +4 -4
  70. package/assets/docs/theme.doc.mjs +3 -3
  71. package/assets/docs/tokens.doc.mjs +1 -1
  72. package/assets/docs/tree/api.doc.mjs +30 -0
  73. package/assets/docs/tree/cli.doc.mjs +23 -0
  74. package/assets/docs/tree/commands.doc.mjs +25 -0
  75. package/assets/docs/{cli-integrations.doc.mjs → tree/integrations.doc.mjs} +37 -9
  76. package/assets/docs/tree/integrations.test.mjs +62 -0
  77. package/assets/docs/tree/writing-docs.doc.mjs +286 -0
  78. package/authoring/debug/parse.d.mts +3 -3
  79. package/authoring/doctypes/_schema.d.mts +71 -141
  80. package/authoring/doctypes/_schema.mjs +29 -2
  81. package/authoring/doctypes/base/graph-fields.doc.mjs +7 -5
  82. package/authoring/doctypes/base/type.ts +6 -5
  83. package/authoring/doctypes/command/command.doc.mjs +1 -1
  84. package/authoring/doctypes/command/type.ts +2 -2
  85. package/authoring/doctypes/enum/enum.doc.mjs +1 -1
  86. package/authoring/doctypes/enum/type.ts +1 -1
  87. package/authoring/doctypes/function/function.doc.mjs +3 -2
  88. package/authoring/doctypes/function/type.ts +3 -2
  89. package/authoring/doctypes/load-contract.test.mjs +1 -1
  90. package/authoring/doctypes/namespace/namespace.doc.mjs +6 -10
  91. package/authoring/doctypes/namespace/parse.test.mjs +23 -25
  92. package/authoring/doctypes/namespace/type.ts +5 -2
  93. package/authoring/doctypes/parse.mjs +3 -2
  94. package/authoring/doctypes/reference/reference.doc.mjs +6 -4
  95. package/authoring/doctypes/reference/type.ts +10 -8
  96. package/authoring/doctypes/schema/schema.doc.mjs +1 -1
  97. package/authoring/doctypes/schema/type.ts +1 -1
  98. package/authoring/doctypes/template/parse.d.mts +2 -0
  99. package/authoring/doctypes/template/parse.mjs +4 -0
  100. package/authoring/doctypes/template/parse.test.mjs +18 -0
  101. package/authoring/doctypes/template/template.doc.mjs +6 -0
  102. package/authoring/doctypes/template/type.ts +8 -0
  103. package/authoring/identity/identity.doc.mjs +2 -2
  104. package/authoring/index.d.ts +1 -0
  105. package/authoring/integration/type.ts +1 -1
  106. package/clients/cli/commands/build-theme.adaptations.test.mjs +100 -1
  107. package/clients/cli/commands/build-theme.mjs +1 -1
  108. package/clients/cli/commands/build.mjs +5 -2
  109. package/clients/cli/commands/component/index.mjs +1 -1
  110. package/clients/cli/commands/component-ownership.test.mjs +3 -3
  111. package/clients/cli/commands/discover.broken-integration.test.mjs +7 -8
  112. package/clients/cli/commands/discover.mjs +1 -1
  113. package/clients/cli/commands/docs.doc.mjs +20 -8
  114. package/clients/cli/commands/docs.mjs +181 -67
  115. package/clients/cli/commands/docs.test.mjs +120 -16
  116. package/clients/cli/commands/doctor-integration-docs.doc.mjs +2 -2
  117. package/clients/cli/commands/doctor-integration-templates.doc.mjs +14 -7
  118. package/clients/cli/commands/doctor-integration.test.mjs +68 -1
  119. package/clients/cli/commands/doctor.mjs +24 -9
  120. package/clients/cli/commands/integration-add.doc.mjs +10 -0
  121. package/clients/cli/commands/integration.mjs +1 -0
  122. package/clients/cli/commands/search.mjs +11 -2
  123. package/clients/cli/commands/setup-nudge.test.mjs +6 -0
  124. package/clients/cli/commands/template.doc.mjs +23 -6
  125. package/clients/cli/commands/template.mjs +4 -91
  126. package/clients/cli/index.mjs +8 -0
  127. package/clients/cli/lib/manifest.mjs +9 -2
  128. package/foundation/agent-docs/agent-docs.mjs +1 -0
  129. package/foundation/agent-docs/agent-docs.test.mjs +10 -1
  130. package/foundation/config/project.d.mts +12 -11
  131. package/foundation/config/project.mjs +73 -59
  132. package/foundation/config/project.test.mjs +141 -5
  133. package/foundation/discovery/authoring-self-docs.test.mjs +20 -9
  134. package/foundation/discovery/cli-self-docs.d.mts +40 -34
  135. package/foundation/discovery/cli-self-docs.mjs +91 -115
  136. package/foundation/discovery/cli-self-docs.test.mjs +104 -166
  137. package/foundation/discovery/component-discovery.d.mts +38 -0
  138. package/foundation/discovery/component-discovery.mjs +48 -0
  139. package/foundation/discovery/docs-discovery.d.mts +105 -8
  140. package/foundation/discovery/docs-discovery.mjs +183 -18
  141. package/foundation/discovery/docs-discovery.test.mjs +63 -27
  142. package/foundation/discovery/docs-output-budget.d.mts +2 -2
  143. package/foundation/discovery/docs-output-budget.mjs +1 -1
  144. package/foundation/discovery/docs-section-key.d.mts +28 -10
  145. package/foundation/discovery/docs-section-key.mjs +134 -33
  146. package/foundation/discovery/docs-section-key.test.mjs +41 -19
  147. package/foundation/discovery/template-adapter.d.mts +92 -6
  148. package/foundation/discovery/template-adapter.mjs +453 -17
  149. package/foundation/discovery/template-adapter.test.mjs +42 -0
  150. package/foundation/doc-compiler/bundle.mjs +61 -1
  151. package/foundation/doc-compiler/bundle.test.mjs +21 -10
  152. package/foundation/doc-compiler/compile.d.mts +113 -132
  153. package/foundation/doc-compiler/compile.mjs +57 -10
  154. package/foundation/doc-compiler/diagnostics.mjs +28 -0
  155. package/foundation/doc-compiler/doc-loads.test.mjs +2 -2
  156. package/foundation/doc-compiler/inputs.d.mts +2 -2
  157. package/foundation/doc-compiler/inputs.mjs +6 -1
  158. package/foundation/doc-compiler/ir.mjs +7 -0
  159. package/foundation/doc-compiler/lenses.d.mts +3 -2
  160. package/foundation/doc-compiler/lenses.mjs +2 -1
  161. package/foundation/doc-compiler/links.d.mts +135 -0
  162. package/foundation/doc-compiler/links.mjs +253 -0
  163. package/foundation/doc-compiler/links.test.mjs +139 -0
  164. package/foundation/doc-compiler/lower-doc.test.mjs +5 -2
  165. package/foundation/doc-compiler/read.d.mts +3 -2
  166. package/foundation/doc-compiler/read.mjs +6 -1
  167. package/foundation/doc-compiler/tree.d.mts +288 -0
  168. package/foundation/doc-compiler/tree.mjs +876 -0
  169. package/foundation/doc-compiler/tree.test.mjs +598 -0
  170. package/foundation/integrations/autolink.test.mjs +1 -1
  171. package/foundation/integrations/cli-requirement.d.mts +45 -0
  172. package/foundation/integrations/cli-requirement.mjs +154 -0
  173. package/foundation/integrations/cli-requirement.test.mjs +109 -0
  174. package/foundation/integrations/contribution-inventory.d.mts +2 -1
  175. package/foundation/integrations/contribution-inventory.mjs +10 -4
  176. package/foundation/integrations/contribution-inventory.test.mjs +19 -0
  177. package/foundation/integrations/integration-warnings.d.mts +9 -2
  178. package/foundation/integrations/integration-warnings.mjs +51 -26
  179. package/foundation/integrations/integration-warnings.test.mjs +74 -1
  180. package/foundation/integrations/validate-contributions.mjs +25 -27
  181. package/foundation/response/response-types.doc.mjs +13 -8
  182. package/foundation/xle/expand.mjs +1 -1
  183. package/foundation/xle/xle.test.mjs +13 -0
  184. package/package.json +9 -9
  185. package/assets/docs/cli.doc.mjs +0 -15
@@ -23,9 +23,11 @@ import {
23
23
  doctor,
24
24
  checkAuthoringDocs,
25
25
  checkCliDocs,
26
+ checkDocsTree,
26
27
  checkDocsProgressiveDisclosure,
27
28
  checkImplicitIntegrations,
28
29
  checkProviderIdentity,
30
+ checkIntegrationIssues,
29
31
  checkVersionAlignment,
30
32
  checkPackageManager,
31
33
  } from './doctor.mjs';
@@ -84,6 +86,27 @@ describe('doctor leaf', () => {
84
86
  }, SLOW);
85
87
  });
86
88
 
89
+ describe('integration issue check', () => {
90
+ it('surfaces cross-package replacement warnings', () => {
91
+ const check = checkIntegrationIssues({
92
+ integrationIssues: [
93
+ {
94
+ package: '@acme/later',
95
+ code: 'ambiguous_template_replacement',
96
+ severity: 'warning',
97
+ message: 'Later configured replacement wins.',
98
+ },
99
+ ],
100
+ });
101
+
102
+ expect(check).toMatchObject({
103
+ id: 'integration-issues',
104
+ status: 'warn',
105
+ message: expect.stringContaining('Later configured replacement wins.'),
106
+ });
107
+ });
108
+ });
109
+
87
110
  describe('doctor leaf — degradation & error paths', () => {
88
111
  it('does not crash on multiple config files; reports a config FAIL', async () => {
89
112
  const dir = mkProject({
@@ -391,7 +414,7 @@ describe('checkDocsProgressiveDisclosure', () => {
391
414
  expect(c.message).toMatch(/^\d+ topics: /);
392
415
  }, SLOW);
393
416
 
394
- it('fails on an invalid doc an integration contributed', async () => {
417
+ it('warns on an invalid doc an integration contributed', async () => {
395
418
  const c = await checkDocsProgressiveDisclosure({
396
419
  docsCatalogIssues: [
397
420
  {
@@ -402,13 +425,13 @@ describe('checkDocsProgressiveDisclosure', () => {
402
425
  },
403
426
  ],
404
427
  });
405
- expect(c.status).toBe('fail');
428
+ expect(c.status).toBe('warn');
406
429
  expect(c.message).toBe('@acme/widgets: bad.doc.mjs exports no doc');
407
430
  });
408
431
 
409
- it('fails when the docs catalog cannot be built', async () => {
432
+ it('warns when the docs catalog cannot be built', async () => {
410
433
  const c = await checkDocsProgressiveDisclosure({docsCatalogError: 'boom'});
411
- expect(c.status).toBe('fail');
434
+ expect(c.status).toBe('warn');
412
435
  expect(c.message).toContain('boom');
413
436
  });
414
437
 
@@ -436,7 +459,7 @@ describe('checkDocsProgressiveDisclosure', () => {
436
459
  }),
437
460
  docsCatalogIssues: [],
438
461
  });
439
- expect(c.status).toBe('fail');
462
+ expect(c.status).toBe('warn');
440
463
  expect(c.message).toMatch(/^2 problems: /);
441
464
  expect(c.message).toContain('huge everything: 41 KB, over the 32 KB one read may return');
442
465
  expect(c.message).toContain('broken: ');
@@ -476,7 +499,7 @@ describe('checkCliDocs', () => {
476
499
  id: 'cli-docs',
477
500
  label: 'CLI docs',
478
501
  status: 'pass',
479
- message: `All ${audit.docs} CLI docs are readable: ${audit.sections} in \`astryx docs cli\` and ${audit.authoring} in \`astryx docs authoring\`.`,
502
+ message: `All ${audit.docs} CLI docs are readable: ${audit.tree} in the \`astryx docs cli\` tree and ${audit.authoring} in \`astryx docs authoring\`.`,
480
503
  });
481
504
  },
482
505
  SLOW,
@@ -518,6 +541,71 @@ describe('checkCliDocs', () => {
518
541
  );
519
542
  });
520
543
 
544
+ describe('checkDocsTree', () => {
545
+ it(
546
+ 'passes on this repo: every CLI doc in a cli group has one route',
547
+ async () => {
548
+ const {loadDocsTree} = await import(
549
+ '../../foundation/doc-compiler/tree.mjs'
550
+ );
551
+ const tree = await loadDocsTree({fresh: true});
552
+ const nodes = [...tree.nodes.values()];
553
+ const namespaces = nodes.filter(node => node.kind === 'namespace').length;
554
+ expect(await checkDocsTree()).toEqual({
555
+ id: 'docs-tree',
556
+ label: 'Docs tree',
557
+ status: 'pass',
558
+ message: expect.stringMatching(
559
+ /^The docs tree has \d+ docs in \d+ sections \(\d+ at the top\), each at one route\.$/,
560
+ ),
561
+ });
562
+ },
563
+ SLOW,
564
+ );
565
+
566
+ it('fails on a tree with a broken placement, and names the fix', async () => {
567
+ const {buildDocsTree} = await import(
568
+ '../../foundation/doc-compiler/tree.mjs'
569
+ );
570
+ const tree = buildDocsTree({
571
+ namespaces: [
572
+ {
573
+ provider: '@acme/kit',
574
+ providerId: '@acme/kit',
575
+ source: '@acme/kit/tree/guides.doc.mjs',
576
+ doc: {
577
+ type: 'namespace',
578
+ name: 'guides',
579
+ title: 'Guides',
580
+ summary: 'Every guide.',
581
+ slots: {all: {title: 'All', accepts: {kinds: ['generic']}}},
582
+ },
583
+ },
584
+ ],
585
+ docs: [
586
+ {
587
+ provider: '@acme/kit',
588
+ providerId: '@acme/kit',
589
+ source: '@acme/kit/tree/setup.doc.mjs',
590
+ kind: 'generic',
591
+ name: 'setup',
592
+ title: 'Setup',
593
+ summary: 'Set it up.',
594
+ group: null,
595
+ placement: {parent: 'namespace:guidez'},
596
+ },
597
+ ],
598
+ });
599
+ const c = await checkDocsTree(undefined, {tree});
600
+ // New findings warn (0.6 compatibility): a broken tree is named, not fatal.
601
+ expect(c).toMatchObject({id: 'docs-tree', status: 'warn'});
602
+ expect(c.message).toContain(
603
+ '@acme/kit/tree/setup.doc.mjs: placement.parent "namespace:guidez" names no namespace; @acme/kit declares "guides".',
604
+ );
605
+ expect(c.fix).toMatch(/a placement names a namespace of its own package/);
606
+ });
607
+ });
608
+
521
609
  describe('checkAuthoringDocs against the public authoring surface', () => {
522
610
  const NAMESPACE = 'doctypes/namespace/namespace.doc.mjs';
523
611
  const NAMESPACE_TYPES =
@@ -812,7 +900,7 @@ describe('checkDocsProgressiveDisclosure languages', () => {
812
900
  docsCatalog: DocsCatalog.fromBuiltins({deploying: path.join(dir, 'deploying.doc.mjs')}),
813
901
  docsCatalogIssues: [],
814
902
  });
815
- expect(c.status).toBe('fail');
903
+ expect(c.status).toBe('warn');
816
904
  expect(c.message).toContain('deploying [zh]: zh overlay broken');
817
905
  expect(c.message).toContain('deploying [dense] overview: 41 KB');
818
906
  expect(c.message).not.toMatch(/deploying overview:/);
package/api/index.d.mts CHANGED
@@ -38,7 +38,7 @@ export * from "./integration/pack-check.type.mjs";
38
38
  export * from "./integration/validate-integration.type.mjs";
39
39
  export * from "./integration/authoring-checks.type.mjs";
40
40
  export type Logger = import("./logger.mjs").Logger;
41
- export { themeBuild, themeAdd, themeList, themeListAvailable, themeTargets, themePaletteGenerate, generateTonalPalette, listThemes } from "./theme/theme.mjs";
41
+ export { themeBuild, themeAdd, themeTemplate, themeList, themeListAvailable, themeTargets, themePaletteGenerate, generateTonalPalette, listThemes } from "./theme/theme.mjs";
42
42
  export { layoutExpand, layoutCheck, layoutGrammar } from "./layout/layout.mjs";
43
43
  export { integrationAdd, integrationAddAgentDoc, integrationAddCodemod, integrationAddComponent, integrationAddDoc, integrationAddTemplate } from "./integration/add-contribution.mjs";
44
44
  export { validateIntegration, summarizeIssues } from "./integration/validate-integration.mjs";
package/api/index.mjs CHANGED
@@ -28,6 +28,7 @@ export {template} from './template/template.mjs';
28
28
  export {
29
29
  themeBuild,
30
30
  themeAdd,
31
+ themeTemplate,
31
32
  themeList,
32
33
  themeListAvailable,
33
34
  themeTargets,
@@ -102,5 +102,5 @@ export const doc = {
102
102
  {label: 'Remove agent docs', code: 'await init({removeAgents: true});'},
103
103
  ],
104
104
  command: 'init',
105
- related: ['doctor', 'upgrade', 'build', 'theme'],
105
+ related: ['doctor', 'upgrade', 'build', 'themeTemplate'],
106
106
  };
@@ -31,7 +31,7 @@ export function integrationAddAgentDoc(line: string, options?: import("./integra
31
31
  *
32
32
  * @param {'component'|'doc'|'template'|'codemod'|'agent-doc'|'theme'} kind
33
33
  * @param {string} name
34
- * @param {{cwd?: string, dryRun?: boolean, templateType?: 'page'|'block', to?: string, replaces?: string, extends?: string}} [options]
34
+ * @param {{cwd?: string, dryRun?: boolean, templateType?: 'page'|'block', to?: string, replaces?: string, extends?: string, parent?: string}} [options]
35
35
  * @returns {Promise<import('./integration-authoring.type.mjs').IntegrationAddResponse>}
36
36
  */
37
37
  export function integrationAdd(kind: "component" | "doc" | "template" | "codemod" | "agent-doc" | "theme", name: string, options?: {
@@ -41,4 +41,5 @@ export function integrationAdd(kind: "component" | "doc" | "template" | "codemod
41
41
  to?: string;
42
42
  replaces?: string;
43
43
  extends?: string;
44
+ parent?: string;
44
45
  }): Promise<import("./integration-authoring.type.mjs").IntegrationAddResponse>;
@@ -34,6 +34,11 @@ import {
34
34
  } from '../../foundation/integrations/integrations.mjs';
35
35
  import {isValidSemver} from '../../foundation/env/semver.mjs';
36
36
  import {assertContributionVisible} from '../../foundation/integrations/contribution-inventory.mjs';
37
+ import {
38
+ docsTreeCliProblem,
39
+ withDocsTreeCli,
40
+ } from '../../foundation/integrations/cli-requirement.mjs';
41
+ import {discoverIntegrationDocs} from '../../foundation/discovery/docs-discovery.mjs';
37
42
  import {findIntegrationComponentDoc} from '../../foundation/discovery/component-discovery.mjs';
38
43
  import {parseAgentDocsField} from '../../authoring/integration/schema.mjs';
39
44
  import {integrationAddTheme} from './add-theme.mjs';
@@ -83,6 +88,17 @@ function resolvePackage(cwd) {
83
88
  }
84
89
  const owner = typeof pkg.name === 'string' ? pkg.name : '(local integration)';
85
90
  const existingManifest = findLocalIntegrationManifestOrNull(packageDir);
91
+ // The nearest package.json above the cwd may belong to another package (an
92
+ // app that contains the folder). Only a package that is already an
93
+ // integration is found from a subfolder; the first add runs in the package.
94
+ const here = path.resolve(cwd ?? process.cwd());
95
+ if (existingManifest == null && path.resolve(packageDir) !== here) {
96
+ throw new AstryxError(
97
+ `${here} has no package.json. The nearest is ${packageFile} (${owner}), which is not an Astryx integration yet. Run this in your integration package's own directory, next to its package.json.`,
98
+ undefined,
99
+ ERROR_CODES.ERR_INVALID_ARGUMENT,
100
+ );
101
+ }
86
102
  const manifestFile =
87
103
  existingManifest ?? path.join(packageDir, 'astryx.integration.mjs');
88
104
  return {
@@ -353,7 +369,7 @@ async function addComponent(name, options) {
353
369
 
354
370
  /**
355
371
  * @param {string} name topic name (must match [\w-]+)
356
- * @param {{cwd?: string, dryRun?: boolean, replaces?: string, extends?: string}} options
372
+ * @param {{cwd?: string, dryRun?: boolean, replaces?: string, extends?: string, parent?: string}} options
357
373
  * @returns {Promise<import('./integration-authoring.type.mjs').IntegrationAddResponse>}
358
374
  */
359
375
  async function addDoc(name, options) {
@@ -372,6 +388,20 @@ async function addDoc(name, options) {
372
388
  ERROR_CODES.ERR_INVALID_ARGUMENT,
373
389
  );
374
390
  }
391
+ if (options.parent != null && (options.replaces != null || options.extends != null)) {
392
+ throw new AstryxError(
393
+ 'A guide placed in a namespace has its own route: it cannot also replace or extend a topic.',
394
+ undefined,
395
+ ERROR_CODES.ERR_INVALID_ARGUMENT,
396
+ );
397
+ }
398
+ if (options.parent != null && !/^[a-z0-9]+(?:-[a-z0-9]+)*$/u.test(options.parent)) {
399
+ throw new AstryxError(
400
+ '--parent must name a namespace in lowercase letters and digits joined by single hyphens, such as "acme".',
401
+ undefined,
402
+ ERROR_CODES.ERR_INVALID_ARGUMENT,
403
+ );
404
+ }
375
405
  for (const [option, value] of [
376
406
  ['--replaces', options.replaces],
377
407
  ['--extends', options.extends],
@@ -432,20 +462,92 @@ async function addDoc(name, options) {
432
462
  }
433
463
 
434
464
  const title = kebabToTitle(name);
465
+ // --parent names a namespace of this package by its name, wherever its file
466
+ // is. An existing one is used as it is: its `guides` slot, or else its only
467
+ // slot that takes a guide. Otherwise the namespace doc is written, unless a
468
+ // file of that name is already something else (spec:AST-046 FR11).
469
+ const namespaceFile =
470
+ options.parent == null
471
+ ? null
472
+ : assertWithin(`${options.parent}.doc.mjs`, root, {
473
+ label: 'namespace doc file',
474
+ });
475
+ let parentSlot = 'guides';
476
+ let writeNamespace = false;
477
+ if (options.parent != null && namespaceFile != null) {
478
+ const {namespaces} = await discoverIntegrationDocs({
479
+ name: owner,
480
+ docs: root,
481
+ });
482
+ const existing = namespaces.find(ns => ns.doc.name === options.parent);
483
+ if (existing != null) {
484
+ const takesGuides = Object.entries(existing.doc.slots ?? {})
485
+ .filter(([, slot]) => slot?.accepts?.kinds?.includes('generic'))
486
+ .map(([slotName]) => slotName);
487
+ const slot = takesGuides.includes('guides')
488
+ ? 'guides'
489
+ : takesGuides.length === 1
490
+ ? takesGuides[0]
491
+ : null;
492
+ if (slot == null) {
493
+ throw new AstryxError(
494
+ takesGuides.length === 0
495
+ ? `Namespace "${options.parent}" (${existing.source}) has no slot that takes a guide. Add one, such as guides: {title: 'Guides', accepts: {kinds: ['generic']}}.`
496
+ : `Namespace "${options.parent}" (${existing.source}) has more than one slot that takes a guide (${takesGuides.join(', ')}). Write the guide yourself, and name its slot in its placement.`,
497
+ undefined,
498
+ ERROR_CODES.ERR_INVALID_ARGUMENT,
499
+ );
500
+ }
501
+ parentSlot = slot;
502
+ } else if (fs.existsSync(namespaceFile)) {
503
+ throw new AstryxError(
504
+ `${projectPath(path.relative(packageDir, namespaceFile))} exists but does not load as the namespace "${options.parent}". Fix or rename that file, or pass another --parent.`,
505
+ undefined,
506
+ ERROR_CODES.ERR_FILE_EXISTS,
507
+ );
508
+ } else {
509
+ writeNamespace = true;
510
+ }
511
+ }
435
512
  const relationship = options.replaces
436
513
  ? `\n replaces: '${options.replaces}',`
437
514
  : options.extends
438
515
  ? `\n extends: '${options.extends}',`
439
- : '';
516
+ : options.parent
517
+ ? `\n placement: {parent: 'namespace:${options.parent}', slot: '${parentSlot}'},`
518
+ : '';
519
+ const namespaceTitle =
520
+ options.parent == null ? '' : kebabToTitle(options.parent);
521
+ const namespaceContents = `/** @type {import('@astryxdesign/cli/authoring').NamespaceDoc} */\nexport default {\n type: 'namespace',\n name: '${options.parent}',\n title: '${namespaceTitle}',\n summary: 'Guides for ${namespaceTitle}.',\n slots: {\n guides: {title: 'Guides', accepts: {kinds: ['generic']}},\n },\n};\n`;
440
522
  const docContents = `/** @type {import('@astryxdesign/cli/authoring').ReferenceDoc} */\nexport default {\n type: 'generic',\n name: '${name}',\n title: '${title}',\n description: '${title} documentation.',${relationship}\n sections: [\n {\n title: 'Overview',\n content: [\n { type: 'prose', text: '${title} documentation.' },\n ],\n },\n ],\n};\n`;
441
523
 
442
524
  /** @type {import('./add-helpers.mjs').WritePlan[]} */
443
525
  const plans = [{path: docFile, contents: docContents, createOnly: true}];
444
- const pkgUpdate = packageJsonUpdate(
526
+ if (namespaceFile != null && writeNamespace) {
527
+ plans.push({path: namespaceFile, contents: namespaceContents, createOnly: true});
528
+ }
529
+ let pkgUpdate = packageJsonUpdate(
445
530
  packageFile,
446
531
  rootPath,
447
532
  path.basename(manifestFile),
448
533
  );
534
+ // A namespace doc needs a CLI that reads it: an older one hides every doc
535
+ // topic the package ships. Declare that CLI as a peer, so an older one fails
536
+ // at install instead (spec:AST-046 FR11).
537
+ if (options.parent != null) {
538
+ const expectedOriginal =
539
+ pkgUpdate?.expectedOriginal ?? fs.readFileSync(packageFile);
540
+ const text = pkgUpdate?.contents ?? expectedOriginal.toString('utf-8');
541
+ const current = JSON.parse(text);
542
+ if (docsTreeCliProblem(current) != null) {
543
+ pkgUpdate = {
544
+ contents:
545
+ JSON.stringify(withDocsTreeCli(current), null, 2) +
546
+ (text.endsWith('\n') ? '\n' : ''),
547
+ expectedOriginal,
548
+ };
549
+ }
550
+ }
449
551
  if (pkgUpdate != null) {
450
552
  plans.push({
451
553
  path: packageFile,
@@ -1105,7 +1207,7 @@ export function integrationAddAgentDoc(line, options = {}) {
1105
1207
 
1106
1208
  const KIND_OPTIONS = {
1107
1209
  component: new Set(['cwd', 'dryRun']),
1108
- doc: new Set(['cwd', 'dryRun', 'replaces', 'extends']),
1210
+ doc: new Set(['cwd', 'dryRun', 'replaces', 'extends', 'parent']),
1109
1211
  template: new Set(['cwd', 'dryRun', 'templateType']),
1110
1212
  codemod: new Set(['cwd', 'dryRun', 'to']),
1111
1213
  'agent-doc': new Set(['cwd', 'dryRun']),
@@ -1136,7 +1238,7 @@ function validateKindOptions(kind, options) {
1136
1238
  *
1137
1239
  * @param {'component'|'doc'|'template'|'codemod'|'agent-doc'|'theme'} kind
1138
1240
  * @param {string} name
1139
- * @param {{cwd?: string, dryRun?: boolean, templateType?: 'page'|'block', to?: string, replaces?: string, extends?: string}} [options]
1241
+ * @param {{cwd?: string, dryRun?: boolean, templateType?: 'page'|'block', to?: string, replaces?: string, extends?: string, parent?: string}} [options]
1140
1242
  * @returns {Promise<import('./integration-authoring.type.mjs').IntegrationAddResponse>}
1141
1243
  */
1142
1244
  export async function integrationAdd(kind, name, options = {}) {
@@ -12,6 +12,7 @@ import {
12
12
  integrationAddTemplate,
13
13
  } from './add-contribution.mjs';
14
14
  import {validateLocalIntegration} from './validate-integration.mjs';
15
+ import {DOCS_TREE_CLI} from '../../foundation/integrations/cli-requirement.mjs';
15
16
 
16
17
  let tmpDir;
17
18
 
@@ -314,6 +315,142 @@ describe('integrationAdd doc', () => {
314
315
  expect(docErrors).toEqual([]);
315
316
  });
316
317
 
318
+ it('refuses to write from a folder with no package.json into the package above it', async () => {
319
+ setupBare();
320
+ const inside = path.join(tmpDir, 'kit');
321
+ fs.mkdirSync(inside);
322
+ await expect(
323
+ integrationAdd('doc', 'my-guide', {cwd: inside, parent: 'acme'}),
324
+ ).rejects.toMatchObject({
325
+ code: 'ERR_INVALID_ARGUMENT',
326
+ message: expect.stringContaining('has no package.json'),
327
+ });
328
+ expect(fs.existsSync(path.join(tmpDir, 'docs'))).toBe(false);
329
+ expect(fs.existsSync(path.join(tmpDir, 'astryx.integration.mjs'))).toBe(false);
330
+ });
331
+
332
+ it('finds an integration from a folder inside it', async () => {
333
+ setup();
334
+ const inside = path.join(tmpDir, 'src');
335
+ fs.mkdirSync(inside);
336
+ const result = await integrationAdd('doc', 'my-guide', {cwd: inside});
337
+ expect(result.data.files).toContain('docs/my-guide.doc.mjs');
338
+ expect(fs.existsSync(path.join(tmpDir, 'docs/my-guide.doc.mjs'))).toBe(true);
339
+ });
340
+
341
+ it('places a guide in a namespace of the package, writing the namespace once', async () => {
342
+ setup();
343
+ const first = await integrationAdd('doc', 'deploying', {
344
+ cwd: tmpDir,
345
+ parent: 'acme',
346
+ });
347
+ expect(first.data.files).toEqual(
348
+ expect.arrayContaining(['docs/deploying.doc.mjs', 'docs/acme.doc.mjs']),
349
+ );
350
+ const guide = fs.readFileSync(path.join(tmpDir, 'docs/deploying.doc.mjs'), 'utf-8');
351
+ expect(guide).toContain("placement: {parent: 'namespace:acme', slot: 'guides'}");
352
+ const namespace = fs.readFileSync(path.join(tmpDir, 'docs/acme.doc.mjs'), 'utf-8');
353
+ expect(namespace).toContain("type: 'namespace'");
354
+ expect(namespace).toContain("guides: {title: 'Guides', accepts: {kinds: ['generic']}}");
355
+
356
+ // The namespace doc needs a CLI that reads it, declared as an optional peer.
357
+ const pkg = JSON.parse(fs.readFileSync(path.join(tmpDir, 'package.json'), 'utf-8'));
358
+ expect(pkg.peerDependencies).toEqual({'@astryxdesign/cli': `>=${DOCS_TREE_CLI}`});
359
+ expect(pkg.peerDependenciesMeta).toEqual({'@astryxdesign/cli': {optional: true}});
360
+
361
+ const second = await integrationAdd('doc', 'upgrading', {
362
+ cwd: tmpDir,
363
+ parent: 'acme',
364
+ });
365
+ expect(second.data.files).not.toContain('docs/acme.doc.mjs');
366
+
367
+ const validation = await validateLocalIntegration(tmpDir);
368
+ expect(validation.issues.filter(i => i.code === 'invalid_doc')).toEqual([]);
369
+ });
370
+
371
+ it('finds the --parent namespace by its name, wherever its file is, and uses its slot', async () => {
372
+ setup({manifest: "export default {docs: './docs'};\n"});
373
+ fs.mkdirSync(path.join(tmpDir, 'docs', 'ns'), {recursive: true});
374
+ fs.writeFileSync(
375
+ path.join(tmpDir, 'docs', 'ns', 'acme.doc.mjs'),
376
+ "export default {type: 'namespace', name: 'acme', title: 'Acme', summary: 'Acme.', slots: {items: {title: 'Items', accepts: {kinds: ['generic']}}}};\n",
377
+ );
378
+ const result = await integrationAdd('doc', 'deploying', {cwd: tmpDir, parent: 'acme'});
379
+ expect(result.data.files).not.toContain('docs/acme.doc.mjs');
380
+ expect(fs.existsSync(path.join(tmpDir, 'docs', 'acme.doc.mjs'))).toBe(false);
381
+ expect(fs.readFileSync(path.join(tmpDir, 'docs', 'deploying.doc.mjs'), 'utf-8')).toContain(
382
+ "placement: {parent: 'namespace:acme', slot: 'items'}",
383
+ );
384
+ });
385
+
386
+ it('refuses --parent when its file is not that namespace, or the namespace has no slot for a guide', async () => {
387
+ setup({manifest: "export default {docs: './docs'};\n"});
388
+ fs.mkdirSync(path.join(tmpDir, 'docs'), {recursive: true});
389
+ const nsFile = path.join(tmpDir, 'docs', 'acme.doc.mjs');
390
+ fs.writeFileSync(
391
+ nsFile,
392
+ "export default {type: 'generic', name: 'acme', title: 'Acme', description: 'A topic.', sections: [{title: 'Only', content: [{type: 'prose', text: 'x'}]}]};\n",
393
+ );
394
+ await expect(
395
+ integrationAdd('doc', 'deploying', {cwd: tmpDir, parent: 'acme'}),
396
+ ).rejects.toMatchObject({code: 'ERR_FILE_EXISTS'});
397
+ // A fresh file: the module loader keeps the first import of a path.
398
+ fs.writeFileSync(
399
+ path.join(tmpDir, 'docs', 'beta.doc.mjs'),
400
+ "export default {type: 'namespace', name: 'beta', title: 'Beta', summary: 'Beta.', slots: {items: {title: 'Items', accepts: {kinds: ['function']}}}};\n",
401
+ );
402
+ await expect(
403
+ integrationAdd('doc', 'deploying', {cwd: tmpDir, parent: 'beta'}),
404
+ ).rejects.toMatchObject({
405
+ code: 'ERR_INVALID_ARGUMENT',
406
+ message: expect.stringContaining('no slot that takes a guide'),
407
+ });
408
+ fs.writeFileSync(
409
+ path.join(tmpDir, 'docs', 'gamma.doc.mjs'),
410
+ "export default {type: 'namespace', name: 'gamma', title: 'Gamma', summary: 'Gamma.', slots: {guides: {title: 'Guides', accepts: {kinds: ['function']}}, more: {title: 'More', accepts: {kinds: ['generic']}}, extra: {title: 'Extra', accepts: {kinds: ['generic']}}}};\n",
411
+ );
412
+ await expect(
413
+ integrationAdd('doc', 'deploying', {cwd: tmpDir, parent: 'gamma'}),
414
+ ).rejects.toMatchObject({
415
+ code: 'ERR_INVALID_ARGUMENT',
416
+ message: expect.stringContaining('more than one slot that takes a guide (more, extra)'),
417
+ });
418
+ expect(fs.existsSync(path.join(tmpDir, 'docs', 'deploying.doc.mjs'))).toBe(false);
419
+ });
420
+
421
+ it('raises a CLI peer that admits a CLI too old for namespace docs, and keeps one that does not', async () => {
422
+ setup();
423
+ const file = path.join(tmpDir, 'package.json');
424
+ const write = (/** @type {string} */ range) => {
425
+ const pkg = JSON.parse(fs.readFileSync(file, 'utf-8'));
426
+ pkg.peerDependencies = {'@astryxdesign/cli': range};
427
+ fs.writeFileSync(file, `${JSON.stringify(pkg, null, 2)}\n`);
428
+ };
429
+ write('^0.6.0');
430
+ await integrationAdd('doc', 'deploying', {cwd: tmpDir, parent: 'acme'});
431
+ expect(JSON.parse(fs.readFileSync(file, 'utf-8')).peerDependencies).toEqual({
432
+ '@astryxdesign/cli': `>=${DOCS_TREE_CLI}`,
433
+ });
434
+ write('^9.1.0');
435
+ await integrationAdd('doc', 'upgrading', {cwd: tmpDir, parent: 'acme'});
436
+ expect(JSON.parse(fs.readFileSync(file, 'utf-8')).peerDependencies).toEqual({
437
+ '@astryxdesign/cli': '^9.1.0',
438
+ });
439
+ });
440
+
441
+ it('refuses --parent with a relationship, or a namespace name that is not a route segment', async () => {
442
+ setup();
443
+ await expect(
444
+ integrationAdd('doc', 'x', {cwd: tmpDir, parent: 'acme', replaces: 'getting-started'}),
445
+ ).rejects.toMatchObject({code: 'ERR_INVALID_ARGUMENT'});
446
+ await expect(
447
+ integrationAdd('doc', 'x', {cwd: tmpDir, parent: 'Acme Kit'}),
448
+ ).rejects.toMatchObject({code: 'ERR_INVALID_ARGUMENT'});
449
+ await expect(
450
+ integrationAdd('template', 'x', {cwd: tmpDir, parent: 'acme'}),
451
+ ).rejects.toMatchObject({code: 'ERR_INVALID_ARGUMENT'});
452
+ });
453
+
317
454
  it('includes replaces in the generated doc', async () => {
318
455
  setup();
319
456
  const result = await integrationAdd('doc', 'my-tokens', {