@astryxdesign/cli 0.7.2-canary.198a7e3 → 0.7.2-canary.2ec83d7
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.
- package/api/component/component.test.mjs +80 -0
- package/api/component/list/list.mjs +42 -4
- package/api/search/search.mjs +8 -3
- package/api/search/search.test.mjs +36 -0
- package/api/template/template.test.mjs +120 -3
- package/assets/codemods/transforms/next/__tests__/migrate-list-edge-compensation-to-edge.test.mjs +120 -0
- package/assets/codemods/transforms/next/index.mjs +11 -1
- package/assets/codemods/transforms/next/migrate-list-edge-compensation-to-edge.mjs +198 -0
- package/assets/docs/working-with-ai.doc.mjs +43 -4
- package/foundation/discovery/component-discovery.d.mts +12 -0
- package/foundation/discovery/component-discovery.mjs +211 -0
- package/foundation/discovery/component-discovery.test.mjs +96 -0
- package/foundation/discovery/template-adapter.mjs +74 -5
- package/foundation/doc-compiler/doc-loads.test.mjs +5 -1
- package/foundation/doc-compiler/inputs.mjs +23 -0
- package/foundation/doc-compiler/inputs.test.mjs +0 -1
- package/foundation/xle/registry.mjs +4 -0
- package/foundation/xle/registry.test.mjs +28 -0
- package/package.json +9 -9
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file Codemod: migrate List edgeCompensation to edge
|
|
5
|
+
*
|
|
6
|
+
* `edgeCompensation="inline"` on an imported `List` becomes `edge="outset"`,
|
|
7
|
+
* the value it already resolves to. List resolves
|
|
8
|
+
* `edge ?? (edgeCompensation === 'inline' ? 'outset' : 'padded')`, so `edge`
|
|
9
|
+
* wins only when it has a value. The deprecated prop is removed beside an
|
|
10
|
+
* `edge` string that no later spread can override; beside a computed `edge`,
|
|
11
|
+
* which may be undefined at runtime, it gets a TODO instead.
|
|
12
|
+
*
|
|
13
|
+
* Built on the shared `transformProp` helper, so it does not follow
|
|
14
|
+
* variables, helpers, wrappers, or types. Where a List writes
|
|
15
|
+
* `edgeCompensation` but the codemod cannot prove the result, it keeps the
|
|
16
|
+
* old prop and leaves a TODO instead of guessing: on a dynamic value, and
|
|
17
|
+
* wherever the List also spreads props. A spread before the old prop may
|
|
18
|
+
* carry an `edge` that wins today and that a new `edge` would override; one
|
|
19
|
+
* after it may override either prop. A spread onto a List that never writes
|
|
20
|
+
* `edgeCompensation` is not evidence of use, so it stays unchanged
|
|
21
|
+
* (`spec:AST-040` FR1): the deprecated prop keeps working through it, and
|
|
22
|
+
* its `@deprecated` type reports a typed use where the props are written.
|
|
23
|
+
* Comments are the codemod's only report channel (`api.report` is a stub),
|
|
24
|
+
* and each is added once, so a second run changes nothing.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import {transformProp} from '../../transform-prop.mjs';
|
|
28
|
+
|
|
29
|
+
export const meta = {
|
|
30
|
+
title: 'Migrate List edgeCompensation to edge',
|
|
31
|
+
description:
|
|
32
|
+
'Renames `edgeCompensation="inline"` to `edge="outset"` on List, and ' +
|
|
33
|
+
'drops `edgeCompensation` beside an `edge` string. Keeps the old prop ' +
|
|
34
|
+
'with a TODO where `edge` is computed or a later spread may override ' +
|
|
35
|
+
'it, on a dynamic value, and where the List also spreads props; the ' +
|
|
36
|
+
'deprecated prop still works there.',
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const OLD_NAME = 'edgeCompensation';
|
|
40
|
+
const NEW_NAME = 'edge';
|
|
41
|
+
const CORE_IMPORT_RE = /^@(astryxdesign|xds)\/core(?:\/|$)/;
|
|
42
|
+
|
|
43
|
+
const COMPONENT_PROPS = new Map([['List', new Set([OLD_NAME])]]);
|
|
44
|
+
|
|
45
|
+
const DYNAMIC_COMMENT =
|
|
46
|
+
' TODO(astryx upgrade): edgeCompensation is deprecated. Replace ' +
|
|
47
|
+
'edgeCompensation="inline" with edge="outset" by hand; this value is ' +
|
|
48
|
+
'computed. ';
|
|
49
|
+
const BOTH_SET_COMMENT =
|
|
50
|
+
' TODO(astryx upgrade): edgeCompensation is deprecated. edge is also set ' +
|
|
51
|
+
'here, but it may be undefined, and then edgeCompensation still applies. ' +
|
|
52
|
+
'Make edge default to "outset" where edgeCompensation is "inline", then ' +
|
|
53
|
+
'remove edgeCompensation. ';
|
|
54
|
+
const SPREAD_COMMENT =
|
|
55
|
+
' TODO(astryx upgrade): edgeCompensation is deprecated, but this List ' +
|
|
56
|
+
'also spreads props that may set edge, so it was left unchanged. Replace ' +
|
|
57
|
+
'edgeCompensation="inline" with edge="outset" by hand, after checking ' +
|
|
58
|
+
'which edge the spread passes. ';
|
|
59
|
+
|
|
60
|
+
const TYPE_WRAPPERS = new Set([
|
|
61
|
+
'TSAsExpression',
|
|
62
|
+
'TSSatisfiesExpression',
|
|
63
|
+
'TSTypeAssertion',
|
|
64
|
+
'TSNonNullExpression',
|
|
65
|
+
'TypeCastExpression',
|
|
66
|
+
'ParenthesizedExpression',
|
|
67
|
+
]);
|
|
68
|
+
|
|
69
|
+
/** @param {any} node */
|
|
70
|
+
function unwrap(node) {
|
|
71
|
+
let current = node;
|
|
72
|
+
while (current && TYPE_WRAPPERS.has(current.type)) {
|
|
73
|
+
current = current.expression;
|
|
74
|
+
}
|
|
75
|
+
return current;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** @param {any} node */
|
|
79
|
+
function isStringLiteral(node) {
|
|
80
|
+
const value = unwrap(node);
|
|
81
|
+
return (
|
|
82
|
+
value != null &&
|
|
83
|
+
(value.type === 'StringLiteral' || value.type === 'Literal') &&
|
|
84
|
+
typeof value.value === 'string'
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The value of a JSX attribute: the string of `edge="flush"`, or the
|
|
90
|
+
* expression inside `edge={...}`.
|
|
91
|
+
* @param {any} attribute
|
|
92
|
+
*/
|
|
93
|
+
function attributeValue(attribute) {
|
|
94
|
+
return attribute.value?.type === 'JSXExpressionContainer'
|
|
95
|
+
? attribute.value.expression
|
|
96
|
+
: attribute.value;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** @param {any} node */
|
|
100
|
+
function isInlineLiteral(node) {
|
|
101
|
+
const value = unwrap(node);
|
|
102
|
+
return (
|
|
103
|
+
value != null &&
|
|
104
|
+
(value.type === 'StringLiteral' || value.type === 'Literal') &&
|
|
105
|
+
value.value === 'inline'
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Attach a comment to a node once, so re-running the codemod adds nothing.
|
|
111
|
+
* @param {any} j @param {any} node @param {string} text
|
|
112
|
+
*/
|
|
113
|
+
function addComment(j, node, text) {
|
|
114
|
+
node.comments ??= [];
|
|
115
|
+
if (node.comments.some((/** @type {any} */ c) => c.value === text)) {
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
node.comments.push(j.commentBlock(text, true, false));
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** @param {any} attributes */
|
|
123
|
+
function firstSpread(attributes) {
|
|
124
|
+
return attributes.find(
|
|
125
|
+
(/** @type {any} */ attribute) => attribute.type === 'JSXSpreadAttribute',
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* @param {import('../../../../authoring/codemod/type').AstryxCodemodFile} file
|
|
131
|
+
* @param {import('../../../../authoring/codemod/type').CodemodTransformApi} api
|
|
132
|
+
* @returns {string | null | undefined}
|
|
133
|
+
*/
|
|
134
|
+
export default function transformer(file, api) {
|
|
135
|
+
const j = api.jscodeshift;
|
|
136
|
+
const root = j(file.source);
|
|
137
|
+
let hasChanges = false;
|
|
138
|
+
|
|
139
|
+
transformProp(
|
|
140
|
+
root,
|
|
141
|
+
j,
|
|
142
|
+
{
|
|
143
|
+
matchesImport: source => CORE_IMPORT_RE.test(source),
|
|
144
|
+
components: COMPONENT_PROPS,
|
|
145
|
+
},
|
|
146
|
+
propPath => {
|
|
147
|
+
const opening = propPath.parent?.node;
|
|
148
|
+
const attributes = opening?.attributes ?? [];
|
|
149
|
+
const prop = propPath.node;
|
|
150
|
+
const edgeIndex = attributes.findIndex(
|
|
151
|
+
(/** @type {any} */ attribute) =>
|
|
152
|
+
attribute.type === 'JSXAttribute' &&
|
|
153
|
+
attribute.name?.name === NEW_NAME,
|
|
154
|
+
);
|
|
155
|
+
if (edgeIndex !== -1) {
|
|
156
|
+
// `edge` wins only when it has a value. A string that no later
|
|
157
|
+
// spread can override always does, so the old prop is inert and
|
|
158
|
+
// removing it changes nothing. A computed `edge` may be undefined,
|
|
159
|
+
// and then the old prop still decides.
|
|
160
|
+
const isProven =
|
|
161
|
+
isStringLiteral(attributeValue(attributes[edgeIndex])) &&
|
|
162
|
+
!attributes
|
|
163
|
+
.slice(edgeIndex + 1)
|
|
164
|
+
.some(
|
|
165
|
+
(/** @type {any} */ attribute) =>
|
|
166
|
+
attribute.type === 'JSXSpreadAttribute',
|
|
167
|
+
);
|
|
168
|
+
if (isProven) {
|
|
169
|
+
opening.attributes = attributes.filter(
|
|
170
|
+
(/** @type {any} */ attribute) => attribute !== prop,
|
|
171
|
+
);
|
|
172
|
+
hasChanges = true;
|
|
173
|
+
} else {
|
|
174
|
+
hasChanges = addComment(j, prop, BOTH_SET_COMMENT) || hasChanges;
|
|
175
|
+
}
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
// With a spread on the element the rename cannot be proven: a spread
|
|
179
|
+
// before the old prop may carry an `edge` that wins today and that a
|
|
180
|
+
// new `edge` would override, and one after it may override either
|
|
181
|
+
// prop. The old prop keeps working, so it stays, with a TODO.
|
|
182
|
+
if (firstSpread(attributes)) {
|
|
183
|
+
hasChanges = addComment(j, prop, SPREAD_COMMENT) || hasChanges;
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
if (isInlineLiteral(attributeValue(prop))) {
|
|
187
|
+
prop.name.name = NEW_NAME;
|
|
188
|
+
prop.value = j.stringLiteral('outset');
|
|
189
|
+
hasChanges = true;
|
|
190
|
+
} else {
|
|
191
|
+
hasChanges = addComment(j, prop, DYNAMIC_COMMENT) || hasChanges;
|
|
192
|
+
}
|
|
193
|
+
},
|
|
194
|
+
);
|
|
195
|
+
|
|
196
|
+
if (!hasChanges) return undefined;
|
|
197
|
+
return root.toSource({quote: 'single'});
|
|
198
|
+
}
|
|
@@ -44,7 +44,7 @@ export const docs = {
|
|
|
44
44
|
},
|
|
45
45
|
{
|
|
46
46
|
type: 'prose',
|
|
47
|
-
text:
|
|
47
|
+
text: "By default this creates `AGENTS.md` (the tool-agnostic standard most agents read). To target a specific tool's file instead:",
|
|
48
48
|
},
|
|
49
49
|
{
|
|
50
50
|
type: 'code',
|
|
@@ -182,21 +182,60 @@ astryx docs principles --dense`,
|
|
|
182
182
|
},
|
|
183
183
|
{
|
|
184
184
|
type: 'prose',
|
|
185
|
-
text: 'Add the server to your MCP config file.
|
|
185
|
+
text: 'Add the server to your MCP config file. The entry is a remote HTTP server, and the exact shape differs per client, so use the snippet for the tool you run. An entry with an unrecognized transport is skipped with a warning rather than an error, which reads as a server that connects but exposes no tools.',
|
|
186
186
|
},
|
|
187
187
|
{
|
|
188
188
|
type: 'code',
|
|
189
189
|
lang: 'json',
|
|
190
|
-
label: '
|
|
190
|
+
label: 'Claude Code (.mcp.json)',
|
|
191
191
|
code: `{
|
|
192
192
|
"mcpServers": {
|
|
193
193
|
"astryx": {
|
|
194
|
-
"type": "
|
|
194
|
+
"type": "http",
|
|
195
195
|
"url": "https://astryx.atmeta.com/mcp"
|
|
196
196
|
}
|
|
197
197
|
}
|
|
198
198
|
}`,
|
|
199
199
|
},
|
|
200
|
+
{
|
|
201
|
+
type: 'prose',
|
|
202
|
+
text: 'Claude Code can also write that entry from the terminal:',
|
|
203
|
+
},
|
|
204
|
+
{
|
|
205
|
+
type: 'code',
|
|
206
|
+
lang: 'bash',
|
|
207
|
+
code: `claude mcp add --transport http astryx https://astryx.atmeta.com/mcp`,
|
|
208
|
+
},
|
|
209
|
+
{
|
|
210
|
+
type: 'code',
|
|
211
|
+
lang: 'json',
|
|
212
|
+
label: 'Cline (cline_mcp_settings.json) - type is streamableHttp',
|
|
213
|
+
code: `{
|
|
214
|
+
"mcpServers": {
|
|
215
|
+
"astryx": {
|
|
216
|
+
"type": "streamableHttp",
|
|
217
|
+
"url": "https://astryx.atmeta.com/mcp"
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}`,
|
|
221
|
+
},
|
|
222
|
+
{
|
|
223
|
+
type: 'code',
|
|
224
|
+
lang: 'json',
|
|
225
|
+
label:
|
|
226
|
+
'Cursor (.cursor/mcp.json) - remote servers take no type field',
|
|
227
|
+
code: `{
|
|
228
|
+
"mcpServers": {
|
|
229
|
+
"astryx": {
|
|
230
|
+
"url": "https://astryx.atmeta.com/mcp"
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}`,
|
|
234
|
+
},
|
|
235
|
+
{
|
|
236
|
+
type: 'prose',
|
|
237
|
+
text: 'Claude Desktop is the exception: claude_desktop_config.json holds local stdio servers, so a remote server is added through Settings, then Connectors, then Add custom connector, pasting the URL above.',
|
|
238
|
+
},
|
|
200
239
|
{
|
|
201
240
|
type: 'prose',
|
|
202
241
|
text: 'Once connected, your AI tool can search for components by natural language (e.g. "dropdown menu", "success message") and retrieve full documentation without any manual CLI invocation. The server uses the same keyword index from component docs, so search quality improves automatically as component documentation is updated.',
|
|
@@ -1,8 +1,20 @@
|
|
|
1
1
|
// @generated by scripts/sync-api-types.mjs from the JSDoc in foundation/**/*.mjs.
|
|
2
2
|
// DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* The entry a family doc gives `name` in its `components`, when the doc is not
|
|
6
|
+
* the component's own (Stack's doc for VStack); null otherwise.
|
|
7
|
+
* @param {any} docs - a loaded component doc
|
|
8
|
+
* @param {string} name - bare component name
|
|
9
|
+
* @returns {any}
|
|
10
|
+
*/
|
|
11
|
+
export function familyMember(docs: any, name: string): any;
|
|
4
12
|
/**
|
|
5
13
|
* Auto-discover components by scanning for Astryx*.tsx files in core/src/.
|
|
14
|
+
* A component is listed when a doc documents it: a doc in its folder, or, for a
|
|
15
|
+
* component folder with no doc of its own, the doc `findComponentReadme` finds
|
|
16
|
+
* elsewhere (Heading's doc in the Text folder; HStack and VStack as members of
|
|
17
|
+
* Stack's doc).
|
|
6
18
|
*
|
|
7
19
|
* Returns an ordered Record where:
|
|
8
20
|
* - Grouped components use the group name as key: `'Buttons': ['Button', 'IconButton']`
|
|
@@ -5,12 +5,15 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import * as fs from 'node:fs';
|
|
8
|
+
import {createRequire} from 'node:module';
|
|
8
9
|
import * as path from 'node:path';
|
|
9
10
|
import {listDir, listDirEntries, pathExists} from '../fs/listing-cache.mjs';
|
|
10
11
|
import {existsCaseExact} from '../fs/paths.mjs';
|
|
11
12
|
import {CORE_PROVIDER_ID} from '../identity/providers.mjs';
|
|
12
13
|
import {loadComponentDoc} from './component-loader.mjs';
|
|
13
14
|
|
|
15
|
+
const require = createRequire(import.meta.url);
|
|
16
|
+
|
|
14
17
|
const SKIP_DIRS = new Set(['hooks', 'utils', '__tests__', 'node_modules']);
|
|
15
18
|
|
|
16
19
|
/** The owner package name for built-in (core) components. */
|
|
@@ -109,8 +112,193 @@ function readDocMeta(docPath) {
|
|
|
109
112
|
}
|
|
110
113
|
}
|
|
111
114
|
|
|
115
|
+
/** @type {typeof import('@babel/parser') | undefined} */
|
|
116
|
+
let babelParser;
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* A property of an object literal, by its plain key.
|
|
120
|
+
* @param {any} object - an ObjectExpression node
|
|
121
|
+
* @param {string} key
|
|
122
|
+
* @returns {any}
|
|
123
|
+
*/
|
|
124
|
+
function propertyValue(object, key) {
|
|
125
|
+
for (const property of object.properties) {
|
|
126
|
+
if (property.type !== 'ObjectProperty' || property.computed) continue;
|
|
127
|
+
const name =
|
|
128
|
+
property.key.type === 'Identifier'
|
|
129
|
+
? property.key.name
|
|
130
|
+
: property.key.type === 'StringLiteral'
|
|
131
|
+
? property.key.value
|
|
132
|
+
: null;
|
|
133
|
+
if (name === key) return property.value;
|
|
134
|
+
}
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* The names a component doc lists as `components` members, read from its
|
|
140
|
+
* source without running it. Only a static `{name: '<Name>'}` entry in the doc
|
|
141
|
+
* object the loader reads (the `default` export, else `docs`) counts: a name in
|
|
142
|
+
* prose, an example, or `relatedComponents` is not a member.
|
|
143
|
+
* @param {string} source - the doc file's text
|
|
144
|
+
* @returns {string[]}
|
|
145
|
+
*/
|
|
146
|
+
function docMemberNames(source) {
|
|
147
|
+
/** @type {any} */
|
|
148
|
+
let ast;
|
|
149
|
+
try {
|
|
150
|
+
babelParser ??= require('@babel/parser');
|
|
151
|
+
ast = /** @type {typeof import('@babel/parser')} */ (babelParser).parse(source, {
|
|
152
|
+
sourceType: 'module',
|
|
153
|
+
});
|
|
154
|
+
} catch {
|
|
155
|
+
return [];
|
|
156
|
+
}
|
|
157
|
+
/** @type {Map<string, any>} */
|
|
158
|
+
const constants = new Map();
|
|
159
|
+
let defaultExport = null;
|
|
160
|
+
let docsExport = null;
|
|
161
|
+
for (const node of ast.program.body) {
|
|
162
|
+
const declaration =
|
|
163
|
+
node.type === 'ExportNamedDeclaration' ? node.declaration : node;
|
|
164
|
+
if (declaration?.type === 'VariableDeclaration') {
|
|
165
|
+
for (const declarator of declaration.declarations) {
|
|
166
|
+
if (declarator.id.type !== 'Identifier' || !declarator.init) continue;
|
|
167
|
+
constants.set(declarator.id.name, declarator.init);
|
|
168
|
+
if (node.type === 'ExportNamedDeclaration' && declarator.id.name === 'docs') {
|
|
169
|
+
docsExport = declarator.init;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (node.type === 'ExportDefaultDeclaration') defaultExport = node.declaration;
|
|
174
|
+
}
|
|
175
|
+
let doc = defaultExport ?? docsExport;
|
|
176
|
+
if (doc?.type === 'Identifier') doc = constants.get(doc.name);
|
|
177
|
+
if (doc?.type !== 'ObjectExpression') return [];
|
|
178
|
+
const members = propertyValue(doc, 'components');
|
|
179
|
+
if (members?.type !== 'ArrayExpression') return [];
|
|
180
|
+
return members.elements.flatMap((/** @type {any} */ element) => {
|
|
181
|
+
if (element?.type !== 'ObjectExpression') return [];
|
|
182
|
+
const name = propertyValue(element, 'name');
|
|
183
|
+
return name?.type === 'StringLiteral' ? [name.value] : [];
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Whether `name` has its own component folder in Core: `src/<Name>/<Name>.tsx`
|
|
189
|
+
* (or the `XDS`-prefixed form).
|
|
190
|
+
* @param {string} srcDir
|
|
191
|
+
* @param {string} name
|
|
192
|
+
* @returns {boolean}
|
|
193
|
+
*/
|
|
194
|
+
function hasOwnFolder(srcDir, name) {
|
|
195
|
+
return [`${name}.tsx`, `XDS${name}.tsx`].some(file =>
|
|
196
|
+
existsCaseExact(path.join(srcDir, name, file), srcDir),
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Member names read in this process, by doc path, reused while the file keeps
|
|
202
|
+
* its size and mtime.
|
|
203
|
+
* @type {Map<string, {size: number, mtimeMs: number, members: string[]}>}
|
|
204
|
+
*/
|
|
205
|
+
const memberReads = new Map();
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* The names a doc lists as `components` members (see docMemberNames).
|
|
209
|
+
* @param {string} docPath
|
|
210
|
+
* @returns {string[]}
|
|
211
|
+
*/
|
|
212
|
+
function readDocMembers(docPath) {
|
|
213
|
+
let stat;
|
|
214
|
+
try {
|
|
215
|
+
stat = fs.statSync(docPath);
|
|
216
|
+
} catch {
|
|
217
|
+
return [];
|
|
218
|
+
}
|
|
219
|
+
const cached = memberReads.get(docPath);
|
|
220
|
+
if (cached && cached.size === stat.size && cached.mtimeMs === stat.mtimeMs) {
|
|
221
|
+
return cached.members;
|
|
222
|
+
}
|
|
223
|
+
/** @type {string[]} */
|
|
224
|
+
let members;
|
|
225
|
+
try {
|
|
226
|
+
members = docMemberNames(fs.readFileSync(docPath, 'utf-8'));
|
|
227
|
+
} catch {
|
|
228
|
+
members = [];
|
|
229
|
+
}
|
|
230
|
+
memberReads.set(docPath, {size: stat.size, mtimeMs: stat.mtimeMs, members});
|
|
231
|
+
return members;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// A sibling component folder named by a relative import or re-export.
|
|
235
|
+
const SIBLING_IMPORT_RE =
|
|
236
|
+
/(?:\bfrom|\bimport\s*\()\s*['"]\.\.\/([A-Za-z0-9_-]+)(?:\/[^'"]*)?['"]/g;
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* The Core doc that documents `name` as one of its `components` members: the
|
|
240
|
+
* doc of a component it builds on. HStack and VStack import Stack, and Stack's
|
|
241
|
+
* doc lists them, so each has its own folder and import path but no doc of its
|
|
242
|
+
* own. Only the folders the component's own source imports from are read.
|
|
243
|
+
* @param {string} srcDir
|
|
244
|
+
* @param {string} name
|
|
245
|
+
* @returns {string | null}
|
|
246
|
+
*/
|
|
247
|
+
function findFamilyDoc(srcDir, name) {
|
|
248
|
+
const sourcePath = [`${name}.tsx`, `XDS${name}.tsx`]
|
|
249
|
+
.map(file => path.join(srcDir, name, file))
|
|
250
|
+
.find(file => existsCaseExact(file, srcDir));
|
|
251
|
+
if (!sourcePath) return null;
|
|
252
|
+
let source;
|
|
253
|
+
try {
|
|
254
|
+
source = fs.readFileSync(sourcePath, 'utf-8');
|
|
255
|
+
} catch {
|
|
256
|
+
return null;
|
|
257
|
+
}
|
|
258
|
+
const families = [
|
|
259
|
+
...new Set([...source.matchAll(SIBLING_IMPORT_RE)].map(match => match[1])),
|
|
260
|
+
]
|
|
261
|
+
.filter(dir => dir !== name && !SKIP_DIRS.has(dir))
|
|
262
|
+
.sort();
|
|
263
|
+
for (const dir of families) {
|
|
264
|
+
const dirPath = path.join(srcDir, dir);
|
|
265
|
+
if (!existsCaseExact(dirPath, srcDir)) continue;
|
|
266
|
+
for (const file of [...listDir(dirPath)].sort()) {
|
|
267
|
+
if (!file.endsWith('.doc.mjs')) continue;
|
|
268
|
+
const docPath = path.join(dirPath, file);
|
|
269
|
+
const members = readDocMembers(docPath);
|
|
270
|
+
if (members.includes(name) || members.includes(`XDS${name}`)) {
|
|
271
|
+
return docPath;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return null;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* The entry a family doc gives `name` in its `components`, when the doc is not
|
|
280
|
+
* the component's own (Stack's doc for VStack); null otherwise.
|
|
281
|
+
* @param {any} docs - a loaded component doc
|
|
282
|
+
* @param {string} name - bare component name
|
|
283
|
+
* @returns {any}
|
|
284
|
+
*/
|
|
285
|
+
export function familyMember(docs, name) {
|
|
286
|
+
if (!docs || docs.name === name || docs.name === `XDS${name}`) return null;
|
|
287
|
+
const members = Array.isArray(docs.components) ? docs.components : [];
|
|
288
|
+
return (
|
|
289
|
+
members.find(
|
|
290
|
+
(/** @type {any} */ member) =>
|
|
291
|
+
member?.name === name || member?.name === `XDS${name}`,
|
|
292
|
+
) ?? null
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
|
|
112
296
|
/**
|
|
113
297
|
* Auto-discover components by scanning for Astryx*.tsx files in core/src/.
|
|
298
|
+
* A component is listed when a doc documents it: a doc in its folder, or, for a
|
|
299
|
+
* component folder with no doc of its own, the doc `findComponentReadme` finds
|
|
300
|
+
* elsewhere (Heading's doc in the Text folder; HStack and VStack as members of
|
|
301
|
+
* Stack's doc).
|
|
114
302
|
*
|
|
115
303
|
* Returns an ordered Record where:
|
|
116
304
|
* - Grouped components use the group name as key: `'Buttons': ['Button', 'IconButton']`
|
|
@@ -192,6 +380,25 @@ export function discoverComponents(coreDir) {
|
|
|
192
380
|
}
|
|
193
381
|
}
|
|
194
382
|
|
|
383
|
+
// A component folder with no doc in it is still listed when a doc elsewhere
|
|
384
|
+
// documents it, the doc `component <Name>` opens: its own same-stem doc in a
|
|
385
|
+
// family folder (Text/Heading.doc.mjs), or a member entry in the family's doc
|
|
386
|
+
// (HStack and VStack in Stack's doc).
|
|
387
|
+
for (const entry of topEntries) {
|
|
388
|
+
if (!entry.isDirectory() || SKIP_DIRS.has(entry.name)) continue;
|
|
389
|
+
const name = entry.name;
|
|
390
|
+
if (componentGroups.has(name) || name.endsWith('Context')) continue;
|
|
391
|
+
if (listDir(path.join(srcDir, name)).some(file => file.endsWith('.doc.mjs'))) {
|
|
392
|
+
continue;
|
|
393
|
+
}
|
|
394
|
+
if (!hasOwnFolder(srcDir, name)) continue;
|
|
395
|
+
const docPath = findComponentReadme(coreDir, name);
|
|
396
|
+
if (!docPath?.endsWith('.doc.mjs')) continue;
|
|
397
|
+
const meta = readDocMeta(docPath);
|
|
398
|
+
if (meta.hidden || meta.hiddenComponents.has(name)) continue;
|
|
399
|
+
componentGroups.set(name, meta.group);
|
|
400
|
+
}
|
|
401
|
+
|
|
195
402
|
// Build the result: group name → sorted members, or component name → [self]
|
|
196
403
|
/** @type {Map<string, string[]>} */
|
|
197
404
|
const groups = new Map();
|
|
@@ -284,6 +491,10 @@ export function findComponentReadme(coreDir, name) {
|
|
|
284
491
|
}
|
|
285
492
|
}
|
|
286
493
|
|
|
494
|
+
// A component with its own folder, documented as a member of its family's
|
|
495
|
+
// doc (HStack and VStack in Stack's doc).
|
|
496
|
+
if (hasOwnFolder(srcDir, name)) return findFamilyDoc(srcDir, name);
|
|
497
|
+
|
|
287
498
|
return null;
|
|
288
499
|
}
|
|
289
500
|
|
|
@@ -16,6 +16,7 @@ import {fileURLToPath} from 'node:url';
|
|
|
16
16
|
import {
|
|
17
17
|
CORE_PACKAGE,
|
|
18
18
|
discoverComponents,
|
|
19
|
+
familyMember,
|
|
19
20
|
findComponentReadme,
|
|
20
21
|
findComponentSource,
|
|
21
22
|
resolveImportPath,
|
|
@@ -121,6 +122,91 @@ describe('discoverComponents (real core)', () => {
|
|
|
121
122
|
expect(discoverComponents(core)).toEqual({});
|
|
122
123
|
});
|
|
123
124
|
|
|
125
|
+
it('lists HStack, VStack and Heading, whose docs live in their family folders', () => {
|
|
126
|
+
// Each has its own folder and import path; Stack's doc documents HStack and
|
|
127
|
+
// VStack as members, and Heading's own doc sits in the Text folder.
|
|
128
|
+
const comps = discoverComponents(CORE);
|
|
129
|
+
expect(comps.Layout).toEqual(expect.arrayContaining(['HStack', 'Stack', 'VStack']));
|
|
130
|
+
expect(comps.Heading).toEqual(['Heading']);
|
|
131
|
+
expect(findComponentReadme(CORE, 'VStack')).toMatch(/[\\/]Stack[\\/]Stack\.doc\.mjs$/);
|
|
132
|
+
expect(findComponentReadme(CORE, 'HStack')).toMatch(/[\\/]Stack[\\/]Stack\.doc\.mjs$/);
|
|
133
|
+
expect(findComponentReadme(CORE, 'Heading')).toMatch(/[\\/]Text[\\/]Heading\.doc\.mjs$/);
|
|
134
|
+
expect(resolveImportPath(CORE, 'VStack')).toBe('@astryxdesign/core/VStack');
|
|
135
|
+
expect(resolveImportPath(CORE, 'Heading')).toBe('@astryxdesign/core/Heading');
|
|
136
|
+
}, SLOW);
|
|
137
|
+
|
|
138
|
+
it('lists a component folder with no doc when its family doc lists it as a member', () => {
|
|
139
|
+
const core = mkTmp('as-cd-family-');
|
|
140
|
+
const write = (rel, text) => {
|
|
141
|
+
fs.mkdirSync(path.dirname(path.join(core, 'src', rel)), {recursive: true});
|
|
142
|
+
fs.writeFileSync(path.join(core, 'src', rel), text);
|
|
143
|
+
};
|
|
144
|
+
write('Stack/Stack.tsx', 'x');
|
|
145
|
+
write('VStack/VStack.tsx', "import {Stack} from '../Stack/Stack';\n");
|
|
146
|
+
write('Prose/Prose.tsx', "import {Stack} from '../Stack/Stack';\n");
|
|
147
|
+
// Listed by Stack's doc, but builds on nothing in Stack's folder.
|
|
148
|
+
write('Orphan/Orphan.tsx', "import {x} from '../utils';\n");
|
|
149
|
+
write(
|
|
150
|
+
'Stack/Stack.doc.mjs',
|
|
151
|
+
'export const docs = {\n' +
|
|
152
|
+
" name: 'Stack',\n" +
|
|
153
|
+
" group: 'Layout',\n" +
|
|
154
|
+
" relatedComponents: ['Prose'],\n" +
|
|
155
|
+
" usage: {description: 'Pair with Prose.', examples: [{code: \"<Prose label='Prose' />\"}]},\n" +
|
|
156
|
+
" components: [{name: 'Stack'}, {name: 'VStack', description: 'Vertical.'}, {name: 'Orphan'}],\n" +
|
|
157
|
+
'};\n',
|
|
158
|
+
);
|
|
159
|
+
expect(discoverComponents(core)).toEqual({Layout: ['Stack', 'VStack']});
|
|
160
|
+
expect(findComponentReadme(core, 'VStack')).toBe(
|
|
161
|
+
path.join(core, 'src', 'Stack', 'Stack.doc.mjs'),
|
|
162
|
+
);
|
|
163
|
+
// Named only in relatedComponents, prose, and an example: not documented.
|
|
164
|
+
expect(findComponentReadme(core, 'Prose')).toBeNull();
|
|
165
|
+
// A family doc is read only from the folders the component imports.
|
|
166
|
+
expect(findComponentReadme(core, 'Orphan')).toBeNull();
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it('lists a component folder with no doc when its own doc sits in a family folder', () => {
|
|
170
|
+
const core = mkTmp('as-cd-sibling-');
|
|
171
|
+
for (const dir of ['Text', 'Heading']) {
|
|
172
|
+
fs.mkdirSync(path.join(core, 'src', dir), {recursive: true});
|
|
173
|
+
}
|
|
174
|
+
fs.writeFileSync(path.join(core, 'src', 'Text', 'Text.tsx'), 'x');
|
|
175
|
+
fs.writeFileSync(path.join(core, 'src', 'Heading', 'Heading.tsx'), 'x');
|
|
176
|
+
fs.writeFileSync(
|
|
177
|
+
path.join(core, 'src', 'Text', 'Text.doc.mjs'),
|
|
178
|
+
"export const docs = {name: 'Text', components: [{name: 'Heading'}]};\n",
|
|
179
|
+
);
|
|
180
|
+
fs.writeFileSync(
|
|
181
|
+
path.join(core, 'src', 'Text', 'Heading.doc.mjs'),
|
|
182
|
+
"export const docs = {name: 'Heading'};\n",
|
|
183
|
+
);
|
|
184
|
+
expect(discoverComponents(core)).toEqual({Heading: ['Heading'], Text: ['Text']});
|
|
185
|
+
expect(findComponentReadme(core, 'Heading')).toBe(
|
|
186
|
+
path.join(core, 'src', 'Text', 'Heading.doc.mjs'),
|
|
187
|
+
);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it('keeps a member its family hides, and a context folder, unlisted', () => {
|
|
191
|
+
const core = mkTmp('as-cd-family-hidden-');
|
|
192
|
+
for (const dir of ['Stack', 'VStack', 'SizeContext']) {
|
|
193
|
+
fs.mkdirSync(path.join(core, 'src', dir), {recursive: true});
|
|
194
|
+
fs.writeFileSync(
|
|
195
|
+
path.join(core, 'src', dir, `${dir}.tsx`),
|
|
196
|
+
"import {Stack} from '../Stack/Stack';\n",
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
fs.writeFileSync(
|
|
200
|
+
path.join(core, 'src', 'Stack', 'Stack.doc.mjs'),
|
|
201
|
+
'export const docs = {\n' +
|
|
202
|
+
" name: 'Stack',\n" +
|
|
203
|
+
" hiddenComponents: ['VStack'],\n" +
|
|
204
|
+
" components: [{name: 'Stack'}, {name: 'VStack'}, {name: 'SizeContext'}],\n" +
|
|
205
|
+
'};\n',
|
|
206
|
+
);
|
|
207
|
+
expect(discoverComponents(core)).toEqual({Stack: ['Stack']});
|
|
208
|
+
});
|
|
209
|
+
|
|
124
210
|
it('returns {} for an empty src/ dir', () => {
|
|
125
211
|
const core = mkTmp('as-cd-emptysrc-');
|
|
126
212
|
fs.mkdirSync(path.join(core, 'src'));
|
|
@@ -133,6 +219,16 @@ describe('discoverComponents (real core)', () => {
|
|
|
133
219
|
});
|
|
134
220
|
});
|
|
135
221
|
|
|
222
|
+
describe('familyMember', () => {
|
|
223
|
+
it("returns a family doc's entry for a member, and null for a component's own doc", () => {
|
|
224
|
+
const stack = {name: 'Stack', components: [{name: 'Stack'}, {name: 'VStack', description: 'v'}]};
|
|
225
|
+
expect(familyMember(stack, 'VStack')).toEqual({name: 'VStack', description: 'v'});
|
|
226
|
+
expect(familyMember(stack, 'Stack')).toBeNull();
|
|
227
|
+
expect(familyMember({name: 'Button'}, 'Button')).toBeNull();
|
|
228
|
+
expect(familyMember(stack, 'HStack')).toBeNull();
|
|
229
|
+
});
|
|
230
|
+
});
|
|
231
|
+
|
|
136
232
|
describe('findComponentReadme / findComponentSource / resolveImportPath', () => {
|
|
137
233
|
it('finds the doc + source for a top-level component', () => {
|
|
138
234
|
expect(findComponentReadme(CORE, 'Button')).toMatch(/Button\.doc\.mjs$/);
|