@astryxdesign/cli 0.6.6-canary.fcf255b → 0.6.6-canary.ffa4f72

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 (184) hide show
  1. package/README.md +83 -83
  2. package/api/build/_adapter.d.mts +15 -0
  3. package/api/build/_adapter.mjs +22 -0
  4. package/api/build/build.doc.mjs +1 -1
  5. package/api/build/build.type.d.mts +4 -0
  6. package/api/build/build.type.mjs +1 -0
  7. package/api/build/help/help.mjs +1 -1
  8. package/api/build/kit/kit.mjs +12 -1
  9. package/api/component/component-replacement.test.mjs +12 -0
  10. package/api/component/component.doc.mjs +5 -2
  11. package/api/docs/_adapter.mjs +48 -1
  12. package/api/docs/compiled-topics.test.mjs +1 -1
  13. package/api/docs/detail/section/section.d.mts +44 -0
  14. package/api/docs/detail/section/section.mjs +164 -13
  15. package/api/docs/detail/section/section.test.mjs +4 -4
  16. package/api/docs/docOverlays.test.mjs +28 -17
  17. package/api/docs/docs.doc.mjs +3 -3
  18. package/api/docs/docs.mjs +11 -16
  19. package/api/docs/docs.test.mjs +9 -10
  20. package/api/docs/integration-tree.test.mjs +26 -26
  21. package/api/docs/integrationDocs.test.mjs +2 -2
  22. package/api/docs/namespace-section.test.mjs +260 -0
  23. package/api/docs/token-tables.test.mjs +93 -0
  24. package/api/doctor/_adapter.d.mts +7 -0
  25. package/api/doctor/_adapter.mjs +48 -0
  26. package/api/doctor/doctor.d.mts +6 -0
  27. package/api/doctor/doctor.mjs +154 -43
  28. package/api/doctor/doctor.test.mjs +104 -0
  29. package/api/doctor/theme-checks.d.mts +12 -0
  30. package/api/doctor/theme-checks.mjs +115 -1
  31. package/api/doctor/theme-checks.test.mjs +142 -1
  32. package/api/gap-report/gap-report.doc.mjs +2 -1
  33. package/api/init/run/run.mjs +1 -0
  34. package/api/integration/validate-integration.mjs +20 -4
  35. package/api/integration/validate-integration.test.mjs +73 -0
  36. package/api/layout/check/check.d.mts +1 -0
  37. package/api/layout/check/check.mjs +1 -0
  38. package/api/layout/expand/expand.d.mts +1 -0
  39. package/api/layout/expand/expand.mjs +1 -0
  40. package/api/layout/grammar/grammar.d.mts +1 -0
  41. package/api/layout/grammar/grammar.mjs +1 -0
  42. package/api/layout/layoutCheck.doc.mjs +1 -1
  43. package/api/layout/layoutExpand.doc.mjs +1 -1
  44. package/api/layout/layoutGrammar.doc.mjs +1 -1
  45. package/api/search/search.d.mts +44 -0
  46. package/api/search/search.domain-priority.test.mjs +261 -0
  47. package/api/search/search.mjs +191 -29
  48. package/api/swizzle/swizzle.doc.mjs +5 -2
  49. package/api/template/copy/copy.mjs +12 -0
  50. package/api/template/template.doc.mjs +1 -1
  51. package/api/template/template.type.d.mts +3 -0
  52. package/api/template/template.type.mjs +3 -0
  53. package/api/theme/build/build.d.mts +70 -0
  54. package/api/theme/build/build.mjs +205 -41
  55. package/api/theme/build/font-warning.d.mts +11 -0
  56. package/api/theme/build/font-warning.mjs +28 -3
  57. package/assets/docs/author-a-theme.doc.mjs +19 -14
  58. package/assets/docs/getting-started.doc.mjs +2 -2
  59. package/assets/docs/principles.doc.dense.mjs +3 -3
  60. package/assets/docs/principles.doc.mjs +4 -4
  61. package/assets/docs/principles.doc.zh.mjs +3 -3
  62. package/assets/docs/styling-overview.doc.mjs +211 -0
  63. package/assets/docs/theme.doc.dense.mjs +1 -1
  64. package/assets/docs/theme.doc.mjs +5 -5
  65. package/assets/docs/theme.doc.zh.mjs +1 -1
  66. package/assets/docs/tree/component-migration.doc.mjs +107 -0
  67. package/assets/docs/tree/css-and-stylex.doc.mjs +150 -0
  68. package/assets/docs/tree/font-setup.doc.mjs +122 -0
  69. package/assets/docs/tree/headers-and-footers.doc.mjs +0 -1
  70. package/assets/docs/tree/i18n-library-integration.doc.mjs +167 -0
  71. package/assets/docs/tree/i18n-quick-start.doc.mjs +101 -0
  72. package/assets/docs/{internationalization.doc.mjs → tree/i18n-rtl-and-testing.doc.mjs} +17 -223
  73. package/assets/docs/tree/internationalization.doc.mjs +29 -0
  74. package/assets/docs/tree/layout.doc.mjs +23 -0
  75. package/assets/docs/{migration.doc.mjs → tree/migration-setup.doc.mjs} +24 -129
  76. package/assets/docs/tree/migration-verification.doc.mjs +67 -0
  77. package/assets/docs/tree/migration.doc.mjs +28 -0
  78. package/assets/docs/tree/non-css-processing.doc.mjs +112 -0
  79. package/assets/docs/tree/other-libraries.doc.mjs +218 -0
  80. package/assets/docs/tree/responsive.doc.mjs +0 -4
  81. package/assets/docs/tree/scaffold.doc.mjs +0 -5
  82. package/assets/docs/tree/side-panels.doc.mjs +0 -1
  83. package/assets/docs/tree/structure.doc.mjs +0 -4
  84. package/assets/docs/tree/styling-advanced.doc.mjs +159 -0
  85. package/assets/docs/tree/styling-basics.doc.mjs +181 -0
  86. package/assets/docs/tree/styling-libraries.doc.mjs +30 -0
  87. package/assets/docs/tree/styling.doc.mjs +29 -0
  88. package/assets/docs/tree/tailwind.doc.mjs +65 -0
  89. package/assets/docs/tree/tokens-and-setup.doc.mjs +128 -0
  90. package/assets/docs/tree/tokens-border.doc.mjs +46 -0
  91. package/assets/docs/tree/tokens-color.doc.dense.mjs +10 -0
  92. package/assets/docs/tree/tokens-color.doc.mjs +438 -0
  93. package/assets/docs/tree/tokens-color.doc.zh.mjs +10 -0
  94. package/assets/docs/tree/tokens-data.doc.mjs +323 -0
  95. package/assets/docs/tree/tokens-duration.doc.mjs +78 -0
  96. package/assets/docs/tree/tokens-ease.doc.mjs +46 -0
  97. package/assets/docs/tree/tokens-focus.doc.mjs +57 -0
  98. package/assets/docs/tree/tokens-font-weight.doc.mjs +58 -0
  99. package/assets/docs/tree/tokens-radius.doc.dense.mjs +10 -0
  100. package/assets/docs/tree/tokens-radius.doc.mjs +70 -0
  101. package/assets/docs/tree/tokens-radius.doc.zh.mjs +10 -0
  102. package/assets/docs/tree/tokens-shadow.doc.dense.mjs +10 -0
  103. package/assets/docs/tree/tokens-shadow.doc.mjs +74 -0
  104. package/assets/docs/tree/tokens-shadow.doc.zh.mjs +10 -0
  105. package/assets/docs/tree/tokens-size.doc.dense.mjs +10 -0
  106. package/assets/docs/tree/tokens-size.doc.mjs +54 -0
  107. package/assets/docs/tree/tokens-size.doc.zh.mjs +10 -0
  108. package/assets/docs/tree/tokens-spacing.doc.dense.mjs +10 -0
  109. package/assets/docs/tree/tokens-spacing.doc.mjs +102 -0
  110. package/assets/docs/tree/tokens-spacing.doc.zh.mjs +10 -0
  111. package/assets/docs/tree/tokens-syntax.doc.mjs +98 -0
  112. package/assets/docs/tree/tokens-text-size.doc.mjs +90 -0
  113. package/assets/docs/tree/tokens-type-scale.doc.mjs +210 -0
  114. package/assets/docs/tree/tokens-typography.doc.mjs +54 -0
  115. package/assets/docs/tree/tokens-usage.doc.dense.mjs +10 -0
  116. package/assets/docs/tree/tokens-usage.doc.mjs +36 -0
  117. package/assets/docs/tree/tokens-usage.doc.zh.mjs +10 -0
  118. package/assets/docs/tree/tokens.doc.mjs +28 -0
  119. package/assets/docs/tree/type-scale.doc.mjs +147 -0
  120. package/assets/docs/tree/typography-best-practices.doc.mjs +55 -0
  121. package/assets/docs/tree/typography.doc.mjs +29 -0
  122. package/assets/docs/use-a-theme.doc.mjs +162 -47
  123. package/assets/templates/pages/incident-console/page.tsx +1 -1
  124. package/assets/templates/pages/table-comparison/template.doc.mjs +1 -1
  125. package/assets/theme.template.ts +4 -3
  126. package/authoring/doctypes/template/type.ts +1 -0
  127. package/clients/cli/commands/blog.mjs +3 -3
  128. package/clients/cli/commands/build-theme.adaptations.test.mjs +1 -1
  129. package/clients/cli/commands/build-theme.ascii-output.test.mjs +11 -1
  130. package/clients/cli/commands/build-theme.batch-report.test.mjs +335 -0
  131. package/clients/cli/commands/build-theme.font-warning.test.mjs +36 -7
  132. package/clients/cli/commands/build-theme.import-path.test.mjs +3 -3
  133. package/clients/cli/commands/build-theme.mjs +68 -18
  134. package/clients/cli/commands/build-theme.watch.test.mjs +33 -0
  135. package/clients/cli/commands/build.mjs +1 -0
  136. package/clients/cli/commands/component/index.mjs +4 -1
  137. package/clients/cli/commands/component-replacement.text.test.mjs +45 -0
  138. package/clients/cli/commands/docs.doc.mjs +2 -1
  139. package/clients/cli/commands/docs.test.mjs +16 -13
  140. package/clients/cli/commands/doctor-integration-components.doc.mjs +1 -1
  141. package/clients/cli/commands/doctor.doc.mjs +1 -1
  142. package/clients/cli/commands/doctor.test.mjs +50 -1
  143. package/clients/cli/commands/import-hint-correctness.test.mjs +23 -0
  144. package/clients/cli/commands/layout-check.doc.mjs +1 -0
  145. package/clients/cli/commands/layout-expand.doc.mjs +1 -0
  146. package/clients/cli/commands/layout-grammar.doc.mjs +1 -0
  147. package/clients/cli/commands/layout.deprecation-lifecycle.test.mjs +140 -0
  148. package/clients/cli/commands/layout.doc.mjs +1 -0
  149. package/clients/cli/commands/layout.mjs +6 -11
  150. package/clients/cli/commands/search.test.mjs +6 -2
  151. package/clients/cli/commands/swizzle.routing.test.mjs +2 -1
  152. package/clients/cli/commands/template.mjs +4 -3
  153. package/clients/cli/commands/theme-add-lifecycle.test.mjs +9 -6
  154. package/clients/cli/commands/theme-build.doc.mjs +3 -1
  155. package/clients/cli/commands/upgrade.config-ordering.test.mjs +2 -1
  156. package/clients/cli/index.mjs +1 -1
  157. package/clients/cli/lib/component-format.mjs +13 -3
  158. package/clients/cli/lib/component-format.test.mjs +1 -1
  159. package/clients/cli/lib/define-command.mjs +4 -0
  160. package/foundation/agent-docs/agent-docs.mjs +18 -2
  161. package/foundation/agent-docs/agent-docs.test.mjs +18 -0
  162. package/foundation/config/project.test.mjs +1 -1
  163. package/foundation/discovery/docs-discovery.test.mjs +1 -1
  164. package/foundation/discovery/template-needs.d.mts +31 -0
  165. package/foundation/discovery/template-needs.mjs +156 -0
  166. package/foundation/discovery/template-needs.test.mjs +203 -0
  167. package/foundation/doc-compiler/bundle.mjs +52 -1
  168. package/foundation/doc-compiler/split-topic-keywords.test.mjs +73 -0
  169. package/foundation/doc-compiler/tree.d.mts +10 -0
  170. package/foundation/doc-compiler/tree.mjs +22 -0
  171. package/foundation/doc-compiler/tree.test.mjs +3 -2
  172. package/foundation/env/package-manager.d.mts +31 -8
  173. package/foundation/env/package-manager.mjs +120 -13
  174. package/foundation/env/package-manager.test.mjs +174 -1
  175. package/foundation/integrations/validate-contributions.d.mts +16 -0
  176. package/foundation/integrations/validate-contributions.mjs +56 -8
  177. package/foundation/response/response-types.doc.mjs +4 -4
  178. package/package.json +9 -9
  179. package/assets/docs/styling-libraries.doc.mjs +0 -469
  180. package/assets/docs/styling.doc.mjs +0 -412
  181. package/assets/docs/tokens.doc.dense.mjs +0 -15
  182. package/assets/docs/tokens.doc.mjs +0 -1491
  183. package/assets/docs/tokens.doc.zh.mjs +0 -15
  184. package/assets/docs/typography.doc.mjs +0 -286
@@ -0,0 +1,203 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file Tests for template-needs analysis: external package detection, StyleX
5
+ * compiler warning, and install command generation.
6
+ */
7
+
8
+ import {describe, it, expect, beforeEach, afterEach} from 'vitest';
9
+ import * as fs from 'node:fs';
10
+ import * as os from 'node:os';
11
+ import * as path from 'node:path';
12
+ import {
13
+ extractImportedPackages,
14
+ analyzeTemplateNeeds,
15
+ } from './template-needs.mjs';
16
+
17
+ // ── extractImportedPackages ─────────────────────────────────────────────────
18
+
19
+ describe('extractImportedPackages', () => {
20
+ it('extracts bare package names from static imports', () => {
21
+ const source = `
22
+ import {Button} from '@astryxdesign/core';
23
+ import {ArrowIcon} from '@heroicons/react/24/outline';
24
+ import * as stylex from '@stylexjs/stylex';
25
+ import {useState} from 'react';
26
+ import recharts from 'recharts';
27
+ `;
28
+ const pkgs = extractImportedPackages(source);
29
+ expect(pkgs).toEqual(
30
+ new Set([
31
+ '@astryxdesign/core',
32
+ '@heroicons/react',
33
+ '@stylexjs/stylex',
34
+ 'react',
35
+ 'recharts',
36
+ ]),
37
+ );
38
+ });
39
+
40
+ it('ignores relative and absolute imports', () => {
41
+ const source = `
42
+ import {foo} from './bar';
43
+ import {baz} from '../qux';
44
+ import {thing} from '/absolute/path';
45
+ `;
46
+ expect(extractImportedPackages(source).size).toBe(0);
47
+ });
48
+
49
+ it('ignores Node builtins with the node: prefix', () => {
50
+ const source = `
51
+ import * as fs from 'node:fs';
52
+ import * as path from 'node:path';
53
+ import {createRequire} from 'node:module';
54
+ `;
55
+ expect(extractImportedPackages(source).size).toBe(0);
56
+ });
57
+
58
+ it('ignores bare Node builtins and their subpaths', () => {
59
+ const source = `
60
+ import * as fs from 'fs';
61
+ import {join} from 'path';
62
+ import {createReadStream} from 'fs/promises';
63
+ `;
64
+ expect(extractImportedPackages(source).size).toBe(0);
65
+ });
66
+
67
+ it('handles type imports', () => {
68
+ const source = `import type {Props} from 'some-package';`;
69
+ expect(extractImportedPackages(source)).toEqual(new Set(['some-package']));
70
+ });
71
+
72
+ it('deduplicates multiple imports from the same package', () => {
73
+ const source = `
74
+ import {A} from '@heroicons/react/24/outline';
75
+ import {B} from '@heroicons/react/24/solid';
76
+ `;
77
+ expect(extractImportedPackages(source)).toEqual(
78
+ new Set(['@heroicons/react']),
79
+ );
80
+ });
81
+
82
+ it('maps subpath imports to the package root', () => {
83
+ const source = `
84
+ import debounce from 'lodash/debounce';
85
+ import {Chart} from 'recharts/es6';
86
+ `;
87
+ const pkgs = extractImportedPackages(source);
88
+ expect(pkgs).toEqual(new Set(['lodash', 'recharts']));
89
+ });
90
+ });
91
+
92
+ // ── analyzeTemplateNeeds ────────────────────────────────────────────────────
93
+
94
+ describe('analyzeTemplateNeeds', () => {
95
+ let dir;
96
+ beforeEach(() => {
97
+ dir = fs.mkdtempSync(path.join(os.tmpdir(), 'tmpl-needs-'));
98
+ });
99
+ afterEach(() => fs.rmSync(dir, {recursive: true, force: true}));
100
+
101
+ it('reports missing external packages with an install command', () => {
102
+ fs.writeFileSync(
103
+ path.join(dir, 'package.json'),
104
+ JSON.stringify({dependencies: {'react': '^19.0.0', '@astryxdesign/core': '^0.6.0'}}),
105
+ );
106
+ const source = `
107
+ import {Button} from '@astryxdesign/core';
108
+ import {ArrowIcon} from '@heroicons/react/24/outline';
109
+ import {useState} from 'react';
110
+ `;
111
+ const result = analyzeTemplateNeeds(source, dir);
112
+ expect(result.missingPackages).toEqual(['@heroicons/react']);
113
+ expect(result.installCommand).toMatch(/npm install @heroicons\/react/);
114
+ expect(result.notes.length).toBeGreaterThanOrEqual(1);
115
+ expect(result.notes[0]).toMatch(/Install missing dependencies/);
116
+ });
117
+
118
+ it('reports nothing when all packages are installed', () => {
119
+ fs.writeFileSync(
120
+ path.join(dir, 'package.json'),
121
+ JSON.stringify({
122
+ dependencies: {
123
+ 'react': '^19.0.0',
124
+ '@astryxdesign/core': '^0.6.0',
125
+ '@heroicons/react': '^2.2.0',
126
+ },
127
+ }),
128
+ );
129
+ const source = `
130
+ import {Button} from '@astryxdesign/core';
131
+ import {ArrowIcon} from '@heroicons/react/24/outline';
132
+ `;
133
+ const result = analyzeTemplateNeeds(source, dir);
134
+ expect(result.missingPackages).toEqual([]);
135
+ expect(result.installCommand).toBeNull();
136
+ expect(result.notes).toEqual([]);
137
+ });
138
+
139
+ it('warns about StyleX compiler when template uses it and project lacks one', () => {
140
+ fs.writeFileSync(
141
+ path.join(dir, 'package.json'),
142
+ JSON.stringify({dependencies: {'@stylexjs/stylex': '^0.10.0'}}),
143
+ );
144
+ const source = `import * as stylex from '@stylexjs/stylex';`;
145
+ const result = analyzeTemplateNeeds(source, dir);
146
+ expect(result.notes).toEqual(
147
+ expect.arrayContaining([
148
+ expect.stringContaining('StyleX'),
149
+ expect.stringContaining('styling-overview'),
150
+ ]),
151
+ );
152
+ });
153
+
154
+ it('does NOT warn about StyleX when the project has a compiler', () => {
155
+ fs.writeFileSync(
156
+ path.join(dir, 'package.json'),
157
+ JSON.stringify({
158
+ devDependencies: {
159
+ '@stylexjs/stylex': '^0.10.0',
160
+ '@stylexjs/rollup-plugin': '^0.10.0',
161
+ },
162
+ }),
163
+ );
164
+ const source = `import * as stylex from '@stylexjs/stylex';`;
165
+ const result = analyzeTemplateNeeds(source, dir);
166
+ const stylexNotes = result.notes.filter(n => n.includes('StyleX'));
167
+ expect(stylexNotes).toEqual([]);
168
+ });
169
+
170
+ it('detects pnpm from lockfile', () => {
171
+ fs.writeFileSync(path.join(dir, 'package.json'), '{}');
172
+ fs.writeFileSync(path.join(dir, 'pnpm-lock.yaml'), '');
173
+ const source = `import {Chart} from 'recharts';`;
174
+ const result = analyzeTemplateNeeds(source, dir);
175
+ expect(result.installCommand).toMatch(/^pnpm add /);
176
+ });
177
+
178
+ it('detects yarn from lockfile', () => {
179
+ fs.writeFileSync(path.join(dir, 'package.json'), '{}');
180
+ fs.writeFileSync(path.join(dir, 'yarn.lock'), '');
181
+ const source = `import {Chart} from 'recharts';`;
182
+ const result = analyzeTemplateNeeds(source, dir);
183
+ expect(result.installCommand).toMatch(/^yarn add /);
184
+ });
185
+
186
+ it('excludes @stylexjs/stylex from missing packages (it is a Core peer)', () => {
187
+ fs.writeFileSync(path.join(dir, 'package.json'), '{}');
188
+ const source = `import * as stylex from '@stylexjs/stylex';`;
189
+ const result = analyzeTemplateNeeds(source, dir);
190
+ expect(result.missingPackages).not.toContain('@stylexjs/stylex');
191
+ });
192
+
193
+ it('handles a template with no external imports', () => {
194
+ fs.writeFileSync(path.join(dir, 'package.json'), '{}');
195
+ const source = `
196
+ import {useState} from 'react';
197
+ import {Button} from '@astryxdesign/core';
198
+ `;
199
+ const result = analyzeTemplateNeeds(source, dir);
200
+ expect(result.notes).toEqual([]);
201
+ expect(result.missingPackages).toEqual([]);
202
+ });
203
+ });
@@ -118,10 +118,61 @@ export async function compileDocs(project, {lang = null} = {}) {
118
118
  );
119
119
  }
120
120
  }
121
+ // Guides placed in a namespace, lowered, so a token reference to the
122
+ // namespace (a flat topic split into guides, such as `tokens`) reads them
123
+ // in placement order as one topic, as the docs API does.
124
+ /** @type {Map<string, Array<{order: number, node: CompiledReferenceNode}>>} */
125
+ const placedGuides = new Map();
126
+ for (const input of inputs) {
127
+ if (
128
+ input.root !== 'tree' ||
129
+ (await treeDocType(input.file)) !== 'generic'
130
+ ) {
131
+ continue;
132
+ }
133
+ const topic = await loadTopicInput(
134
+ {
135
+ name: input.name,
136
+ package: input.owner,
137
+ path: input.file,
138
+ extensions: [],
139
+ tree: true,
140
+ },
141
+ lang,
142
+ );
143
+ if ('error' in topic.base || 'overlayError' in topic.base) continue;
144
+ const parent = /^namespace:(.+)$/u.exec(
145
+ topic.base.doc?.placement?.parent ?? '',
146
+ )?.[1];
147
+ if (!parent) continue;
148
+ try {
149
+ const node = lowerReferenceTopic(topic);
150
+ const list = placedGuides.get(parent.toLowerCase()) ?? [];
151
+ list.push({order: topic.base.doc.placement.order ?? 0, node});
152
+ placedGuides.set(parent.toLowerCase(), list);
153
+ } catch {
154
+ // Reported where the guide itself is compiled below.
155
+ }
156
+ }
121
157
  /** @param {string} name */
122
158
  const lowerTarget = async name => {
123
159
  const target = catalog.resolve(name);
124
- return target ? (topics.get(target.name.toLowerCase()) ?? null) : null;
160
+ if (target) return topics.get(target.name.toLowerCase()) ?? null;
161
+ const guides = placedGuides.get(name.toLowerCase());
162
+ if (!guides) return null;
163
+ const ordered = [...guides].sort((a, b) => a.order - b.order);
164
+ /** @type {Record<string, string>} */
165
+ const sourceTitles = {};
166
+ for (const {node} of ordered) {
167
+ for (const [key, title] of Object.entries(node.sourceTitles)) {
168
+ sourceTitles[key] ??= title;
169
+ }
170
+ }
171
+ return /** @type {any} */ ({
172
+ id: name,
173
+ doc: {sections: ordered.flatMap(({node}) => node.doc.sections)},
174
+ sourceTitles,
175
+ });
125
176
  };
126
177
 
127
178
  /** @type {Array<CompiledDocNode | CompiledReferenceNode>} */
@@ -0,0 +1,73 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file A topic split into a namespace of guides indexes each term once, a
5
+ * policy of the docs split itself (no spec clause states it). Below the CLI's own
6
+ * root namespaces (every one but `cli`, its command and API reference), a
7
+ * guide's search keywords are its own: none repeats a keyword its namespace,
8
+ * a sibling, or a guide of another split topic declares. A term several
9
+ * guides share belongs to their namespace, or to none.
10
+ */
11
+
12
+ import fs from 'node:fs';
13
+ import path from 'node:path';
14
+ import {fileURLToPath} from 'node:url';
15
+ import {describe, expect, it} from 'vitest';
16
+
17
+ const TREE_DIR = path.resolve(
18
+ path.dirname(fileURLToPath(import.meta.url)),
19
+ '../../assets/docs/tree',
20
+ );
21
+
22
+ /** @returns {Promise<Map<string, any>>} every CLI tree doc, by name */
23
+ async function treeDocs() {
24
+ /** @type {Map<string, any>} */
25
+ const docs = new Map();
26
+ for (const file of fs.readdirSync(TREE_DIR)) {
27
+ if (!/^[\w-]+\.doc\.mjs$/.test(file)) continue;
28
+ const {docs: doc} = await import(path.join(TREE_DIR, file));
29
+ docs.set(doc.name, doc);
30
+ }
31
+ return docs;
32
+ }
33
+
34
+ /** @param {any} doc @returns {string | null} */
35
+ const parentOf = doc =>
36
+ /^namespace:(.+)$/.exec(doc.placement?.parent ?? '')?.[1] ?? null;
37
+
38
+ describe('the keywords of a split topic', () => {
39
+ it("are each declared once, and a guide's are its own", async () => {
40
+ const docs = await treeDocs();
41
+ /** @param {string} name */
42
+ const rootOf = name => {
43
+ let doc = docs.get(name);
44
+ while (doc && parentOf(doc)) doc = docs.get(parentOf(doc));
45
+ return doc?.name;
46
+ };
47
+ const roots = [...docs.values()]
48
+ .filter(doc => doc.type === 'namespace' && !parentOf(doc) && doc.name !== 'cli')
49
+ .map(doc => doc.name);
50
+ expect(roots).toContain('layout');
51
+
52
+ /** @type {Map<string, any[]>} */
53
+ const owners = new Map();
54
+ for (const doc of docs.values()) {
55
+ if (!roots.includes(rootOf(doc.name))) continue;
56
+ const own = new Set((doc.keywords ?? []).map(k => String(k).toLowerCase()));
57
+ for (const keyword of own) {
58
+ owners.set(keyword, [...(owners.get(keyword) ?? []), doc]);
59
+ }
60
+ }
61
+ const repeats = [...owners]
62
+ .filter(([, holders]) => holders.length > 1 && holders.some(d => d.type !== 'namespace'))
63
+ .map(([keyword, holders]) => `${keyword}: ${holders.map(d => d.name).join(', ')}`);
64
+ expect(repeats).toEqual([]);
65
+ });
66
+
67
+ it('are not repeated inside one doc', async () => {
68
+ for (const doc of (await treeDocs()).values()) {
69
+ const keywords = (doc.keywords ?? []).map(k => String(k).toLowerCase());
70
+ expect(new Set(keywords).size, doc.name).toBe(keywords.length);
71
+ }
72
+ });
73
+ });
@@ -26,6 +26,16 @@ export function buildDocsTree({ namespaces, docs, topics }: {
26
26
  * @returns {string[]}
27
27
  */
28
28
  export function treeDocFiles(dir?: string): string[];
29
+ /**
30
+ * The names of the CLI's own root namespaces (spec:AST-046 FR7): each is read
31
+ * with `astryx docs <name>`, as a flat topic is, though no topic file has its
32
+ * name. Read from the namespace files' text, without loading them, so a
33
+ * synchronous caller (the agent docs block) lists them beside the flat topics.
34
+ * A topic split into a namespace keeps its name in every such list.
35
+ * @param {string} [dir]
36
+ * @returns {string[]}
37
+ */
38
+ export function cliRootNamespaceNames(dir?: string): string[];
29
39
  /**
30
40
  * The CLI's own tree inputs: the namespace docs and guides under
31
41
  * assets/docs/tree, and, unless left out, every typed self-doc with its
@@ -744,6 +744,28 @@ export function treeDocFiles(dir = TREE_DOCS_DIR) {
744
744
  .map(file => path.join(dir, file));
745
745
  }
746
746
 
747
+ /**
748
+ * The names of the CLI's own root namespaces (spec:AST-046 FR7): each is read
749
+ * with `astryx docs <name>`, as a flat topic is, though no topic file has its
750
+ * name. Read from the namespace files' text, without loading them, so a
751
+ * synchronous caller (the agent docs block) lists them beside the flat topics.
752
+ * A topic split into a namespace keeps its name in every such list.
753
+ * @param {string} [dir]
754
+ * @returns {string[]}
755
+ */
756
+ export function cliRootNamespaceNames(dir = TREE_DOCS_DIR) {
757
+ /** @type {string[]} */
758
+ const names = [];
759
+ for (const file of treeDocFiles(dir)) {
760
+ const text = fs.readFileSync(file, 'utf8');
761
+ if (!/["']?type["']?\s*:\s*["']namespace["']/u.test(text)) continue;
762
+ if (/["']?placement["']?\s*:/u.test(text)) continue;
763
+ const name = /["']?name["']?\s*:\s*["']([\w-]+)["']/u.exec(text)?.[1];
764
+ if (name) names.push(name);
765
+ }
766
+ return names.sort(byText);
767
+ }
768
+
747
769
  /**
748
770
  * The CLI's own tree inputs: the namespace docs and guides under
749
771
  * assets/docs/tree, and, unless left out, every typed self-doc with its
@@ -495,7 +495,7 @@ describe("the CLI's own docs tree", () => {
495
495
  async () => {
496
496
  const tree = await loadDocsTree({fresh: true});
497
497
  expect(tree.diagnostics).toEqual([]);
498
- expect(tree.roots().map(node => node.route)).toEqual(['cli', 'layout']);
498
+ expect(tree.roots().map(node => node.route)).toEqual(['cli', 'internationalization', 'layout', 'migration', 'styling', 'styling-libraries', 'tokens', 'typography']);
499
499
  // Every slot child is a node one level below the namespace that places
500
500
  // it, and every node but the root is placed. Read from the tree, so a
501
501
  // restructure of the guides does not need this test edited.
@@ -518,10 +518,11 @@ describe("the CLI's own docs tree", () => {
518
518
  }
519
519
  }
520
520
  expect(misplaced).toEqual([]);
521
+ const roots = new Set(tree.roots().map(node => node.route));
521
522
  expect(
522
523
  nodes
523
524
  .map(node => node.route)
524
- .filter(route => route !== 'cli' && route !== 'layout' && !placed.has(route)),
525
+ .filter(route => !roots.has(route) && !placed.has(route)),
525
526
  ).toEqual([]);
526
527
  // The reference groups the CLI generates from its own typed docs.
527
528
  expect(
@@ -80,6 +80,9 @@ export function getRunPrefix(targetDir?: string): string;
80
80
  * {@link CLI_PACKAGE}, never the bare `astryx` bin. Running bare `npx astryx`
81
81
  * without the CLI installed resolves to an unrelated package on the registry.
82
82
  *
83
+ * Classic Yarn (1.x) has no `dlx`, so its projects get `npx`, which every
84
+ * Node install carries.
85
+ *
83
86
  * @param {string} [targetDir]
84
87
  * @returns {string} e.g. 'npx', 'pnpm dlx', 'yarn dlx', 'bunx'
85
88
  */
@@ -90,22 +93,42 @@ export function getDlxPrefix(targetDir?: string): string;
90
93
  *
91
94
  * We sniff the entry path (`process.argv[1]`) for well-known runner-cache
92
95
  * markers. This errs safe in both directions: a false negative falls back to
93
- * the installed form (`<prefix> astryx`, the historical behavior), and a false
94
- * positive emits the always-valid scoped form (`<dlx> @astryxdesign/cli`).
96
+ * {@link getCliInvocation}'s on-disk check, and a false positive emits the
97
+ * always-valid scoped form (`<dlx> @astryxdesign/cli`).
95
98
  *
96
99
  * @returns {boolean}
97
100
  */
98
101
  export function isCliOneOff(): boolean;
102
+ /**
103
+ * Does the bare `astryx` bin resolve from `targetDir`?
104
+ *
105
+ * A package runner finds an installed bin in `node_modules/.bin`, in the
106
+ * project or in a folder above it (a workspace root). When no such link
107
+ * exists, `npx astryx` and `bunx astryx` fetch the unrelated registry package
108
+ * named `astryx` instead of running this CLI, and `pnpm exec astryx` or
109
+ * `yarn astryx` fail. A global install, or a workspace install running in a
110
+ * fresh package, is exactly that case.
111
+ *
112
+ * A false negative (a layout without `node_modules/.bin`, such as Yarn Plug'n'Play)
113
+ * is safe: the caller suggests the scoped package, which always resolves.
114
+ *
115
+ * @param {string} [targetDir=process.cwd()]
116
+ * @returns {boolean}
117
+ */
118
+ export function cliBinResolves(targetDir?: string): boolean;
99
119
  /**
100
120
  * The safe, install-aware CLI invocation stem to suggest to users.
101
121
  *
102
- * - Installed / global / dev: `<run-prefix> astryx` (e.g. `pnpm exec astryx`).
103
- * Bare `astryx` resolves to the local (or global) binary.
104
- * - One-off (npx/dlx cache): `<dlx-prefix> @astryxdesign/cli` — the bare
105
- * `astryx` name isn't on disk, so npm would fetch an unrelated registry
106
- * package; the scoped package always resolves to us.
122
+ * - The `astryx` bin resolves from `targetDir`: `<run-prefix> astryx`
123
+ * (e.g. `pnpm exec astryx`).
124
+ * - Launched one-off (npx/dlx cache), or the bin does not resolve from
125
+ * `targetDir` (a global install, a fresh package in a workspace):
126
+ * `<dlx-prefix> @astryxdesign/cli`. The bare `astryx` name would make npm
127
+ * fetch an unrelated registry package; the scoped package always resolves
128
+ * to this CLI.
107
129
  *
108
- * @param {string} [targetDir]
130
+ * @param {string} [targetDir] the project the printed command runs in;
131
+ * defaults to the working directory
109
132
  * @returns {string}
110
133
  */
111
134
  export function getCliInvocation(targetDir?: string): string;
@@ -6,7 +6,9 @@
6
6
  * The order is: the `packageManager` field a project DECLARES, then the
7
7
  * lockfiles it happens to have, then a committed package-manager config, then
8
8
  * the runner that launched us. Returns the correct command prefix for running
9
- * package binaries (e.g. 'npx astryx', 'yarn astryx', 'pnpm exec astryx').
9
+ * package binaries (e.g. 'npx astryx', 'yarn astryx', 'pnpm exec astryx'), and
10
+ * the scoped one-off form ('npx @astryxdesign/cli') when the `astryx` bin does
11
+ * not resolve from the project.
10
12
  */
11
13
 
12
14
  import * as fs from 'node:fs';
@@ -299,13 +301,16 @@ export const CLI_BIN = 'astryx';
299
301
  * {@link CLI_PACKAGE}, never the bare `astryx` bin. Running bare `npx astryx`
300
302
  * without the CLI installed resolves to an unrelated package on the registry.
301
303
  *
304
+ * Classic Yarn (1.x) has no `dlx`, so its projects get `npx`, which every
305
+ * Node install carries.
306
+ *
302
307
  * @param {string} [targetDir]
303
308
  * @returns {string} e.g. 'npx', 'pnpm dlx', 'yarn dlx', 'bunx'
304
309
  */
305
310
  export function getDlxPrefix(targetDir) {
306
- const pm = detectPackageManager(targetDir);
307
- switch (pm) {
308
- case 'yarn': return 'yarn dlx';
311
+ const resolution = explainPackageManager(targetDir);
312
+ switch (resolution.pm) {
313
+ case 'yarn': return isYarnClassic(resolution) ? 'npx' : 'yarn dlx';
309
314
  case 'pnpm': return 'pnpm dlx';
310
315
  case 'bun': return 'bunx';
311
316
  case 'npm':
@@ -313,14 +318,79 @@ export function getDlxPrefix(targetDir) {
313
318
  }
314
319
  }
315
320
 
321
+ /**
322
+ * Is the project's Yarn the classic line (1.x), which has no `dlx`?
323
+ *
324
+ * Read from the signals detection trusts, in the same order: the declared
325
+ * `packageManager` version, then the lockfile header (`# yarn lockfile v1`
326
+ * is classic; Berry's lockfile carries `__metadata:`), then a committed
327
+ * config (`.yarnrc.yml` is Berry, a lone `.yarnrc` classic), then the
328
+ * runner's user agent (`yarn/1.22.21 …`). With none of them it is Berry.
329
+ *
330
+ * @param {PackageManagerResolution} resolution
331
+ * @returns {boolean}
332
+ */
333
+ function isYarnClassic({dir}) {
334
+ if (dir) {
335
+ const declared = declaredYarnMajor(dir);
336
+ if (declared !== null) return declared === 1;
337
+ const lockfile = path.join(dir, 'yarn.lock');
338
+ if (fs.existsSync(lockfile)) {
339
+ const head = readHead(lockfile);
340
+ if (/^# yarn lockfile v1\b/m.test(head)) return true;
341
+ if (/^__metadata:/m.test(head)) return false;
342
+ }
343
+ if (fs.existsSync(path.join(dir, '.yarnrc.yml'))) return false;
344
+ if (fs.existsSync(path.join(dir, '.yarnrc'))) return true;
345
+ }
346
+ const agent = /^yarn\/(\d+)\./.exec(String(process.env.npm_config_user_agent ?? ''));
347
+ return agent ? agent[1] === '1' : false;
348
+ }
349
+
350
+ /**
351
+ * The major version in a directory's `packageManager: "yarn@<version>"`.
352
+ * @param {string} dir
353
+ * @returns {number | null}
354
+ */
355
+ function declaredYarnMajor(dir) {
356
+ try {
357
+ const pkg = JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf-8'));
358
+ const match = /^yarn@(\d+)(?:[.+]|$)/.exec(String(pkg.packageManager ?? ''));
359
+ return match ? Number(match[1]) : null;
360
+ } catch {
361
+ // Best-effort: no package.json, or an unreadable one.
362
+ return null;
363
+ }
364
+ }
365
+
366
+ /**
367
+ * The first bytes of a file — enough for a lockfile's header.
368
+ * @param {string} file
369
+ * @returns {string}
370
+ */
371
+ function readHead(file) {
372
+ try {
373
+ const fd = fs.openSync(file, 'r');
374
+ try {
375
+ const buffer = Buffer.alloc(512);
376
+ const bytes = fs.readSync(fd, buffer, 0, buffer.length, 0);
377
+ return buffer.toString('utf-8', 0, bytes);
378
+ } finally {
379
+ fs.closeSync(fd);
380
+ }
381
+ } catch {
382
+ return '';
383
+ }
384
+ }
385
+
316
386
  /**
317
387
  * Heuristic: was the running CLI launched one-off via a package runner
318
388
  * (npx / pnpm dlx / yarn dlx / bunx) rather than from an installed dependency?
319
389
  *
320
390
  * We sniff the entry path (`process.argv[1]`) for well-known runner-cache
321
391
  * markers. This errs safe in both directions: a false negative falls back to
322
- * the installed form (`<prefix> astryx`, the historical behavior), and a false
323
- * positive emits the always-valid scoped form (`<dlx> @astryxdesign/cli`).
392
+ * {@link getCliInvocation}'s on-disk check, and a false positive emits the
393
+ * always-valid scoped form (`<dlx> @astryxdesign/cli`).
324
394
  *
325
395
  * @returns {boolean}
326
396
  */
@@ -329,20 +399,57 @@ export function isCliOneOff() {
329
399
  return /\/_npx\/|\/dlx[-/]|\/\.bun\/install\/cache\/|\/bunx-/.test(entry);
330
400
  }
331
401
 
402
+ /** The `astryx` bin as package managers link it, including the Windows shims. */
403
+ const CLI_BIN_LINKS = [CLI_BIN, `${CLI_BIN}.cmd`, `${CLI_BIN}.ps1`];
404
+
405
+ /**
406
+ * Does the bare `astryx` bin resolve from `targetDir`?
407
+ *
408
+ * A package runner finds an installed bin in `node_modules/.bin`, in the
409
+ * project or in a folder above it (a workspace root). When no such link
410
+ * exists, `npx astryx` and `bunx astryx` fetch the unrelated registry package
411
+ * named `astryx` instead of running this CLI, and `pnpm exec astryx` or
412
+ * `yarn astryx` fail. A global install, or a workspace install running in a
413
+ * fresh package, is exactly that case.
414
+ *
415
+ * A false negative (a layout without `node_modules/.bin`, such as Yarn Plug'n'Play)
416
+ * is safe: the caller suggests the scoped package, which always resolves.
417
+ *
418
+ * @param {string} [targetDir=process.cwd()]
419
+ * @returns {boolean}
420
+ */
421
+ export function cliBinResolves(targetDir = process.cwd()) {
422
+ let dir = path.resolve(targetDir);
423
+ for (;;) {
424
+ const binDir = path.join(dir, 'node_modules', '.bin');
425
+ if (CLI_BIN_LINKS.some(name => fs.existsSync(path.join(binDir, name)))) {
426
+ return true;
427
+ }
428
+ const parent = path.dirname(dir);
429
+ if (parent === dir) return false;
430
+ dir = parent;
431
+ }
432
+ }
433
+
332
434
  /**
333
435
  * The safe, install-aware CLI invocation stem to suggest to users.
334
436
  *
335
- * - Installed / global / dev: `<run-prefix> astryx` (e.g. `pnpm exec astryx`).
336
- * Bare `astryx` resolves to the local (or global) binary.
337
- * - One-off (npx/dlx cache): `<dlx-prefix> @astryxdesign/cli` — the bare
338
- * `astryx` name isn't on disk, so npm would fetch an unrelated registry
339
- * package; the scoped package always resolves to us.
437
+ * - The `astryx` bin resolves from `targetDir`: `<run-prefix> astryx`
438
+ * (e.g. `pnpm exec astryx`).
439
+ * - Launched one-off (npx/dlx cache), or the bin does not resolve from
440
+ * `targetDir` (a global install, a fresh package in a workspace):
441
+ * `<dlx-prefix> @astryxdesign/cli`. The bare `astryx` name would make npm
442
+ * fetch an unrelated registry package; the scoped package always resolves
443
+ * to this CLI.
340
444
  *
341
- * @param {string} [targetDir]
445
+ * @param {string} [targetDir] the project the printed command runs in;
446
+ * defaults to the working directory
342
447
  * @returns {string}
343
448
  */
344
449
  export function getCliInvocation(targetDir) {
345
- if (isCliOneOff()) return `${getDlxPrefix(targetDir)} ${CLI_PACKAGE}`;
450
+ if (isCliOneOff() || !cliBinResolves(targetDir)) {
451
+ return `${getDlxPrefix(targetDir)} ${CLI_PACKAGE}`;
452
+ }
346
453
  return `${getRunPrefix(targetDir)} ${CLI_BIN}`;
347
454
  }
348
455