create-kerf-component 5.0.0-beta.125 → 5.0.0-beta.127
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/README.md +12 -1
- package/catalog/export-discovery.js +112 -0
- package/catalog/generate.js +308 -0
- package/catalog/schema-validate.js +140 -0
- package/catalog/schemas.js +19 -0
- package/catalog/selection.js +243 -0
- package/catalog/validate-component.js +524 -0
- package/catalog/workspace.js +62 -0
- package/catalog.js +21 -1183
- package/index.js +85 -16
- package/package.json +15 -2
- package/template/package.json +2 -2
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
// The selection catalog (the @kerfjs/ui component-catalog extension) shares
|
|
2
|
+
// identity, purpose, boundaries, and geometry with the composition metadata,
|
|
3
|
+
// so only the selection-only facts live in each component's `selection` block.
|
|
4
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
5
|
+
import { dirname, join, relative, resolve, sep } from 'node:path';
|
|
6
|
+
|
|
7
|
+
import { validateDocumentAgainstSchema } from './schema-validate.js';
|
|
8
|
+
import { selectionSchema } from './schemas.js';
|
|
9
|
+
import { CONFIG_KEY } from './workspace.js';
|
|
10
|
+
|
|
11
|
+
function selectionGeometry(component) {
|
|
12
|
+
if (component.selection?.geometry) return component.selection.geometry;
|
|
13
|
+
const { notes, ...owners } = component.composition.layout.geometry;
|
|
14
|
+
return notes?.length ? { ...owners, notes } : owners;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function toSelectionEntry(component) {
|
|
18
|
+
const { selection, boundaries } = component;
|
|
19
|
+
return {
|
|
20
|
+
id: component.id,
|
|
21
|
+
name: component.name,
|
|
22
|
+
kind: component.kind,
|
|
23
|
+
purpose: component.purpose,
|
|
24
|
+
useWhen: selection.useWhen,
|
|
25
|
+
avoidWhen: selection.avoidWhen,
|
|
26
|
+
publicClasses: boundaries.publicClasses,
|
|
27
|
+
publicTokens: boundaries.publicTokens,
|
|
28
|
+
...(selection.uses ? { uses: selection.uses } : {}),
|
|
29
|
+
source: component.source,
|
|
30
|
+
...(selection.styleSources ? { styleSources: selection.styleSources } : {}),
|
|
31
|
+
...(selection.demoSource ? { demoSource: selection.demoSource } : {}),
|
|
32
|
+
geometry: selectionGeometry(component),
|
|
33
|
+
...(selection.documentation
|
|
34
|
+
? { documentation: selection.documentation }
|
|
35
|
+
: {}),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// The @kerfjs/ui selection catalog ids an installed copy ships, which a
|
|
40
|
+
// selection entry's `uses` may name alongside the package's own ids.
|
|
41
|
+
function kerfSelectionIds(packageRoot) {
|
|
42
|
+
for (let dir = packageRoot; ; dir = dirname(dir)) {
|
|
43
|
+
const catalogPath = join(
|
|
44
|
+
dir,
|
|
45
|
+
'node_modules',
|
|
46
|
+
'@kerfjs',
|
|
47
|
+
'ui',
|
|
48
|
+
'ai',
|
|
49
|
+
'component-catalog.json',
|
|
50
|
+
);
|
|
51
|
+
if (existsSync(catalogPath)) {
|
|
52
|
+
try {
|
|
53
|
+
const catalog = JSON.parse(readFileSync(catalogPath, 'utf8'));
|
|
54
|
+
return new Set((catalog.entries ?? []).map((entry) => entry.id));
|
|
55
|
+
} catch {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (dirname(dir) === dir) return null;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function localPathExists(packageRoot, link) {
|
|
64
|
+
if (/^[a-z]+:/i.test(link)) return true;
|
|
65
|
+
const localPath = link.split('#')[0];
|
|
66
|
+
return !localPath || existsSync(resolve(packageRoot, localPath));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** `to` relative to `from`, POSIX-separated and always `./`-prefixed. */
|
|
70
|
+
export function posixRelative(from, to) {
|
|
71
|
+
const path = relative(from, to).split(sep).join('/');
|
|
72
|
+
return path.startsWith('.') ? path : `./${path}`;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Without a configured selectionOutput, selection-only metadata has nowhere to
|
|
76
|
+
// go, so it is an error rather than silently dropped.
|
|
77
|
+
function rejectUnusedSelection(metadata, at, diagnostics) {
|
|
78
|
+
if (metadata.description !== undefined)
|
|
79
|
+
diagnostics.push(
|
|
80
|
+
`${at}: description is emitted only to a selection catalog; configure package.json#${CONFIG_KEY}.selectionOutput or remove it`,
|
|
81
|
+
);
|
|
82
|
+
for (const component of metadata.components)
|
|
83
|
+
if (component.selection)
|
|
84
|
+
diagnostics.push(
|
|
85
|
+
`${at}: components.${component.id}.selection is emitted only to a selection catalog; configure package.json#${CONFIG_KEY}.selectionOutput or remove it`,
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function validatePackageSelection(
|
|
90
|
+
metadata,
|
|
91
|
+
at,
|
|
92
|
+
outputPath,
|
|
93
|
+
selectionOutputPath,
|
|
94
|
+
diagnostics,
|
|
95
|
+
) {
|
|
96
|
+
if (!metadata.description)
|
|
97
|
+
diagnostics.push(
|
|
98
|
+
`${at}.description: author decision required; the selection catalog needs a package description`,
|
|
99
|
+
);
|
|
100
|
+
const derived = posixRelative(dirname(outputPath), selectionOutputPath);
|
|
101
|
+
if (
|
|
102
|
+
metadata.componentCatalog !== undefined &&
|
|
103
|
+
metadata.componentCatalog !== 'not-applicable' &&
|
|
104
|
+
metadata.componentCatalog !== derived
|
|
105
|
+
)
|
|
106
|
+
diagnostics.push(
|
|
107
|
+
`${at}.componentCatalog: ${metadata.componentCatalog} disagrees with the configured selectionOutput (${derived}); remove it or set it to "not-applicable"`,
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// One component's `selection` block. `kerfIds` lazily resolves the installed
|
|
112
|
+
// @kerfjs/ui ids only when a `uses` entry is not one of the package's own.
|
|
113
|
+
function validateComponentSelection(
|
|
114
|
+
component,
|
|
115
|
+
at,
|
|
116
|
+
packageRoot,
|
|
117
|
+
ownIds,
|
|
118
|
+
kerfIds,
|
|
119
|
+
diagnostics,
|
|
120
|
+
) {
|
|
121
|
+
const componentAt = `${at}: components.${component.id}.selection`;
|
|
122
|
+
if (component.kind === 'recipe') {
|
|
123
|
+
if (component.selection)
|
|
124
|
+
diagnostics.push(
|
|
125
|
+
`${componentAt}: recipes are not selection catalog entries; remove it`,
|
|
126
|
+
);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
if (!component.selection) {
|
|
130
|
+
diagnostics.push(
|
|
131
|
+
`${componentAt}: author decision required; selectionOutput is configured, so every component and composition needs useWhen and avoidWhen`,
|
|
132
|
+
);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
const { selection } = component;
|
|
136
|
+
if (
|
|
137
|
+
!selection.geometry &&
|
|
138
|
+
Object.values(component.composition.layout.geometry).includes('composed')
|
|
139
|
+
)
|
|
140
|
+
diagnostics.push(
|
|
141
|
+
`${componentAt}.geometry: required because composition.layout.geometry uses "composed", which the selection catalog cannot express`,
|
|
142
|
+
);
|
|
143
|
+
for (const path of [
|
|
144
|
+
...(selection.styleSources ?? []),
|
|
145
|
+
...(selection.demoSource ? [selection.demoSource] : []),
|
|
146
|
+
...(selection.documentation ? [selection.documentation] : []),
|
|
147
|
+
])
|
|
148
|
+
if (!localPathExists(packageRoot, path))
|
|
149
|
+
diagnostics.push(`${componentAt}: local path does not exist: ${path}`);
|
|
150
|
+
for (const use of selection.uses ?? []) {
|
|
151
|
+
if (use === component.id)
|
|
152
|
+
diagnostics.push(
|
|
153
|
+
`${componentAt}.uses: ${use} names the component itself`,
|
|
154
|
+
);
|
|
155
|
+
else if (!ownIds.has(use)) {
|
|
156
|
+
const ids = kerfIds();
|
|
157
|
+
if (!ids)
|
|
158
|
+
diagnostics.push(
|
|
159
|
+
`${componentAt}.uses: cannot resolve ${use}; install @kerfjs/ui so its selection catalog ids can be checked`,
|
|
160
|
+
);
|
|
161
|
+
else if (!ids.has(use))
|
|
162
|
+
diagnostics.push(
|
|
163
|
+
`${componentAt}.uses: ${use} is neither a component in this package nor a @kerfjs/ui catalog entry`,
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* The package's selection catalog, or null when no selectionOutput is
|
|
171
|
+
* configured. Problems are pushed onto `diagnostics`.
|
|
172
|
+
*/
|
|
173
|
+
export function buildSelectionCatalog({
|
|
174
|
+
packageRoot,
|
|
175
|
+
packageJson,
|
|
176
|
+
metadata,
|
|
177
|
+
metadataPath,
|
|
178
|
+
outputPath,
|
|
179
|
+
selectionOutputPath,
|
|
180
|
+
diagnostics,
|
|
181
|
+
}) {
|
|
182
|
+
const at = metadataPath;
|
|
183
|
+
if (!selectionOutputPath) {
|
|
184
|
+
rejectUnusedSelection(metadata, at, diagnostics);
|
|
185
|
+
return null;
|
|
186
|
+
}
|
|
187
|
+
validatePackageSelection(
|
|
188
|
+
metadata,
|
|
189
|
+
at,
|
|
190
|
+
outputPath,
|
|
191
|
+
selectionOutputPath,
|
|
192
|
+
diagnostics,
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
const selected = metadata.components.filter(
|
|
196
|
+
(component) => component.kind !== 'recipe',
|
|
197
|
+
);
|
|
198
|
+
const ownIds = new Set(selected.map((component) => component.id));
|
|
199
|
+
let resolvedKerfIds;
|
|
200
|
+
let resolved = false;
|
|
201
|
+
const kerfIds = () => {
|
|
202
|
+
if (!resolved) {
|
|
203
|
+
resolvedKerfIds = kerfSelectionIds(packageRoot);
|
|
204
|
+
resolved = true;
|
|
205
|
+
}
|
|
206
|
+
return resolvedKerfIds;
|
|
207
|
+
};
|
|
208
|
+
for (const component of metadata.components)
|
|
209
|
+
validateComponentSelection(
|
|
210
|
+
component,
|
|
211
|
+
at,
|
|
212
|
+
packageRoot,
|
|
213
|
+
ownIds,
|
|
214
|
+
kerfIds,
|
|
215
|
+
diagnostics,
|
|
216
|
+
);
|
|
217
|
+
if (ownIds.size)
|
|
218
|
+
for (const id of ownIds)
|
|
219
|
+
if (kerfIds()?.has(id))
|
|
220
|
+
diagnostics.push(
|
|
221
|
+
`${at}: components.${id}.id duplicates a @kerfjs/ui catalog id, which the selection catalog cannot disambiguate`,
|
|
222
|
+
);
|
|
223
|
+
|
|
224
|
+
const catalog = {
|
|
225
|
+
$schema:
|
|
226
|
+
'https://raw.githubusercontent.com/brianwestphal/kerf/main/ui/ai/component-catalog-extension.schema.json',
|
|
227
|
+
schemaVersion: 1,
|
|
228
|
+
package: packageJson.name,
|
|
229
|
+
description: metadata.description,
|
|
230
|
+
entries: selected
|
|
231
|
+
.filter((component) => component.selection)
|
|
232
|
+
.sort((a, b) => a.id.localeCompare(b.id))
|
|
233
|
+
.map(toSelectionEntry),
|
|
234
|
+
};
|
|
235
|
+
diagnostics.push(
|
|
236
|
+
...validateDocumentAgainstSchema(
|
|
237
|
+
catalog,
|
|
238
|
+
selectionSchema,
|
|
239
|
+
selectionOutputPath,
|
|
240
|
+
),
|
|
241
|
+
);
|
|
242
|
+
return catalog;
|
|
243
|
+
}
|