create-kerf-component 5.0.0-beta.13 → 5.0.0-beta.130
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 +114 -4
- 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 +65 -0
- package/component-catalog-extension.schema.json +104 -0
- package/component-composition.schema.json +749 -0
- package/component-metadata.schema.json +453 -0
- package/index.js +117 -20
- package/package.json +23 -2
- package/template/README.md +66 -10
- package/template/component-composition.json +144 -0
- package/template/kerf.components.json +71 -0
- package/template/package.json +19 -3
- package/template/src/counter.css +38 -0
- package/template/src/counter.tsx +18 -3
|
@@ -0,0 +1,524 @@
|
|
|
1
|
+
// Semantic checks on one component's author metadata, beyond what its JSON
|
|
2
|
+
// schema can express: explicit author decisions, unique ids, files and exports
|
|
3
|
+
// that actually exist, and composition facts that agree with each other.
|
|
4
|
+
import { existsSync } from 'node:fs';
|
|
5
|
+
import { relative, resolve } from 'node:path';
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
exportedNamesFromFile,
|
|
9
|
+
sourceEntryForExport,
|
|
10
|
+
} from './export-discovery.js';
|
|
11
|
+
|
|
12
|
+
function valueAt(value, path) {
|
|
13
|
+
return path.split('.').reduce((current, part) => current?.[part], value);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// Paths where an explicit `null` is itself the author's decision: a component
|
|
17
|
+
// whose root element belongs to another catalog (a `rendersAs` wrapper) or that
|
|
18
|
+
// exposes no public root class.
|
|
19
|
+
const NULLABLE_DECISIONS = new Set(['boundaries.rootClass']);
|
|
20
|
+
|
|
21
|
+
// The facts the generator never infers from rendered appearance.
|
|
22
|
+
const DECISION_PATHS = [
|
|
23
|
+
'id',
|
|
24
|
+
'name',
|
|
25
|
+
'kind',
|
|
26
|
+
'purpose',
|
|
27
|
+
'source',
|
|
28
|
+
'publicExports',
|
|
29
|
+
'sourceLinks',
|
|
30
|
+
'composition.parents.mode',
|
|
31
|
+
'composition.parents.entries',
|
|
32
|
+
'composition.contexts',
|
|
33
|
+
'composition.zones',
|
|
34
|
+
'composition.children.mode',
|
|
35
|
+
'composition.children.concepts',
|
|
36
|
+
'composition.children.requiredConcepts',
|
|
37
|
+
'composition.state',
|
|
38
|
+
'composition.wiring.required',
|
|
39
|
+
'composition.wiring.helpers',
|
|
40
|
+
'composition.wiring.obligations',
|
|
41
|
+
'composition.responsive.owner',
|
|
42
|
+
'composition.responsive.behaviors',
|
|
43
|
+
'composition.layout.roles',
|
|
44
|
+
'composition.layout.geometry.margin',
|
|
45
|
+
'composition.layout.geometry.border',
|
|
46
|
+
'composition.layout.geometry.padding',
|
|
47
|
+
'boundaries.rootClass',
|
|
48
|
+
'boundaries.publicClasses',
|
|
49
|
+
'boundaries.publicTokens',
|
|
50
|
+
'accessibility.obligations',
|
|
51
|
+
'diagnostics',
|
|
52
|
+
'provenance.selection',
|
|
53
|
+
'provenance.composition',
|
|
54
|
+
];
|
|
55
|
+
|
|
56
|
+
function requireDecision(component, path, at, diagnostics) {
|
|
57
|
+
const value = valueAt(component, path);
|
|
58
|
+
const explicitNull =
|
|
59
|
+
value === null &&
|
|
60
|
+
NULLABLE_DECISIONS.has(path) &&
|
|
61
|
+
Object.hasOwn(
|
|
62
|
+
valueAt(component, path.slice(0, path.lastIndexOf('.'))) ?? {},
|
|
63
|
+
path.slice(path.lastIndexOf('.') + 1),
|
|
64
|
+
);
|
|
65
|
+
if (explicitNull) return;
|
|
66
|
+
if (value === undefined || value === null || value === '') {
|
|
67
|
+
diagnostics.push(
|
|
68
|
+
`${at}.${path}: author decision required; the generator does not infer semantics or geometry from rendered appearance`,
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function validateStringList(
|
|
74
|
+
value,
|
|
75
|
+
at,
|
|
76
|
+
diagnostics,
|
|
77
|
+
{ nonempty = false } = {},
|
|
78
|
+
) {
|
|
79
|
+
if (
|
|
80
|
+
!Array.isArray(value) ||
|
|
81
|
+
(nonempty && value.length === 0) ||
|
|
82
|
+
value.some((item) => typeof item !== 'string' || item.length === 0) ||
|
|
83
|
+
new Set(value).size !== value.length
|
|
84
|
+
)
|
|
85
|
+
diagnostics.push(
|
|
86
|
+
`${at}: expected a${nonempty ? ' non-empty' : ''} unique string list`,
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function validateZone(zone, at, zoneIds, zoneProps, diagnostics) {
|
|
91
|
+
const zoneAt = `${at}.composition.zones.${zone?.id ?? '<unknown>'}`;
|
|
92
|
+
if (!zone?.id || zoneIds.has(zone.id))
|
|
93
|
+
diagnostics.push(`${zoneAt}.id: expected a unique non-empty id`);
|
|
94
|
+
zoneIds.add(zone?.id);
|
|
95
|
+
const prop = zone?.jsx?.prop;
|
|
96
|
+
if (prop !== undefined) {
|
|
97
|
+
if (zoneProps.has(prop))
|
|
98
|
+
diagnostics.push(
|
|
99
|
+
`${zoneAt}.jsx.prop: ${prop} already fills another zone`,
|
|
100
|
+
);
|
|
101
|
+
zoneProps.add(prop);
|
|
102
|
+
}
|
|
103
|
+
validateStringList(zone?.accepts, `${zoneAt}.accepts`, diagnostics, {
|
|
104
|
+
nonempty: true,
|
|
105
|
+
});
|
|
106
|
+
const { min, max } = zone?.cardinality ?? {};
|
|
107
|
+
if (!Number.isInteger(min) || min < 0)
|
|
108
|
+
diagnostics.push(
|
|
109
|
+
`${zoneAt}.cardinality.min: expected a non-negative integer`,
|
|
110
|
+
);
|
|
111
|
+
if (max !== 'unbounded' && (!Number.isInteger(max) || max < 1 || max < min))
|
|
112
|
+
diagnostics.push(
|
|
113
|
+
`${zoneAt}.cardinality.max: expected unbounded or an integer >= min`,
|
|
114
|
+
);
|
|
115
|
+
validateStringList(
|
|
116
|
+
zone?.exclusiveWith,
|
|
117
|
+
`${zoneAt}.exclusiveWith`,
|
|
118
|
+
diagnostics,
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function validateParentsAndZones(composition, at, diagnostics) {
|
|
123
|
+
if (!['any', 'root', 'listed'].includes(composition.parents?.mode))
|
|
124
|
+
diagnostics.push(`${at}.composition.parents.mode: invalid parent mode`);
|
|
125
|
+
validateStringList(
|
|
126
|
+
composition.parents?.entries,
|
|
127
|
+
`${at}.composition.parents.entries`,
|
|
128
|
+
diagnostics,
|
|
129
|
+
{ nonempty: composition.parents?.mode === 'listed' },
|
|
130
|
+
);
|
|
131
|
+
validateStringList(
|
|
132
|
+
composition.contexts,
|
|
133
|
+
`${at}.composition.contexts`,
|
|
134
|
+
diagnostics,
|
|
135
|
+
);
|
|
136
|
+
if (!Array.isArray(composition.zones))
|
|
137
|
+
diagnostics.push(`${at}.composition.zones: expected an array`);
|
|
138
|
+
const zoneIds = new Set();
|
|
139
|
+
const zoneProps = new Set();
|
|
140
|
+
for (const zone of composition.zones ?? [])
|
|
141
|
+
validateZone(zone, at, zoneIds, zoneProps, diagnostics);
|
|
142
|
+
for (const zone of composition.zones ?? [])
|
|
143
|
+
for (const other of zone?.exclusiveWith ?? [])
|
|
144
|
+
if (other === zone.id || !zoneIds.has(other))
|
|
145
|
+
diagnostics.push(
|
|
146
|
+
`${at}.composition.zones.${zone.id}.exclusiveWith: unknown or self-referential zone ${other}`,
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function validateChildren(composition, at, diagnostics) {
|
|
151
|
+
if (!['any', 'none', 'listed'].includes(composition.children?.mode))
|
|
152
|
+
diagnostics.push(`${at}.composition.children.mode: invalid child mode`);
|
|
153
|
+
validateStringList(
|
|
154
|
+
composition.children?.concepts,
|
|
155
|
+
`${at}.composition.children.concepts`,
|
|
156
|
+
diagnostics,
|
|
157
|
+
{ nonempty: composition.children?.mode === 'listed' },
|
|
158
|
+
);
|
|
159
|
+
validateStringList(
|
|
160
|
+
composition.children?.requiredConcepts,
|
|
161
|
+
`${at}.composition.children.requiredConcepts`,
|
|
162
|
+
diagnostics,
|
|
163
|
+
);
|
|
164
|
+
for (const required of composition.children?.requiredConcepts ?? [])
|
|
165
|
+
if (!composition.children.concepts?.includes(required))
|
|
166
|
+
diagnostics.push(
|
|
167
|
+
`${at}.composition.children.requiredConcepts: ${required} is not an allowed concept`,
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function validateState(composition, at, diagnostics) {
|
|
172
|
+
if (!Array.isArray(composition.state))
|
|
173
|
+
diagnostics.push(`${at}.composition.state: expected an array`);
|
|
174
|
+
const stateIds = new Set();
|
|
175
|
+
for (const state of composition.state ?? []) {
|
|
176
|
+
if (!state?.id || stateIds.has(state.id))
|
|
177
|
+
diagnostics.push(
|
|
178
|
+
`${at}.composition.state: state ids must be unique and non-empty`,
|
|
179
|
+
);
|
|
180
|
+
stateIds.add(state?.id);
|
|
181
|
+
if (!['application', 'controlled', 'component'].includes(state?.owner))
|
|
182
|
+
diagnostics.push(
|
|
183
|
+
`${at}.composition.state.${state?.id}.owner: invalid owner`,
|
|
184
|
+
);
|
|
185
|
+
if (typeof state?.required !== 'boolean')
|
|
186
|
+
diagnostics.push(
|
|
187
|
+
`${at}.composition.state.${state?.id}.required: expected boolean`,
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// Each wiring helper source names a listed helper that its file exports.
|
|
193
|
+
function validateWiringSources(wiring, at, packageRoot, diagnostics) {
|
|
194
|
+
const seen = new Set();
|
|
195
|
+
for (const helperSource of wiring?.sources ?? []) {
|
|
196
|
+
const sourceAt = `${at}.composition.wiring.sources.${helperSource?.export}`;
|
|
197
|
+
if (seen.has(helperSource?.export))
|
|
198
|
+
diagnostics.push(`${sourceAt}: declared more than once`);
|
|
199
|
+
seen.add(helperSource?.export);
|
|
200
|
+
if (!wiring.helpers?.includes(helperSource?.export))
|
|
201
|
+
diagnostics.push(
|
|
202
|
+
`${sourceAt}: ${helperSource?.export} is not listed in composition.wiring.helpers`,
|
|
203
|
+
);
|
|
204
|
+
if (
|
|
205
|
+
helperSource?.returnsDisposer !== undefined &&
|
|
206
|
+
typeof helperSource.returnsDisposer !== 'boolean'
|
|
207
|
+
)
|
|
208
|
+
diagnostics.push(`${sourceAt}.returnsDisposer: expected boolean`);
|
|
209
|
+
const file = resolve(packageRoot, helperSource?.source ?? '');
|
|
210
|
+
if (!helperSource?.source || !existsSync(file))
|
|
211
|
+
diagnostics.push(
|
|
212
|
+
`${sourceAt}.source: file does not exist: ${helperSource?.source}`,
|
|
213
|
+
);
|
|
214
|
+
else if (!exportedNamesFromFile(file).has(helperSource.export))
|
|
215
|
+
diagnostics.push(
|
|
216
|
+
`${sourceAt}: ${helperSource.export} is not exported by ${helperSource.source}`,
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// Each `data-*` state attribute a wiring helper maintains is declared once.
|
|
222
|
+
function validateStateAttributes(wiring, at, diagnostics) {
|
|
223
|
+
const stateAttributes = wiring?.stateAttributes;
|
|
224
|
+
if (stateAttributes !== undefined && !Array.isArray(stateAttributes))
|
|
225
|
+
diagnostics.push(
|
|
226
|
+
`${at}.composition.wiring.stateAttributes: expected an array`,
|
|
227
|
+
);
|
|
228
|
+
const names = new Set();
|
|
229
|
+
for (const attribute of Array.isArray(stateAttributes)
|
|
230
|
+
? stateAttributes
|
|
231
|
+
: []) {
|
|
232
|
+
const name = attribute?.name;
|
|
233
|
+
const attributeAt = `${at}.composition.wiring.stateAttributes.${name}`;
|
|
234
|
+
if (typeof name !== 'string' || !/^data-[a-z0-9]+(-[a-z0-9]+)*$/.test(name))
|
|
235
|
+
diagnostics.push(`${attributeAt}.name: expected a data-* attribute name`);
|
|
236
|
+
if (names.has(name))
|
|
237
|
+
diagnostics.push(`${attributeAt}.name: declared more than once`);
|
|
238
|
+
names.add(name);
|
|
239
|
+
for (const field of ['on', 'meaning'])
|
|
240
|
+
if (typeof attribute?.[field] !== 'string' || !attribute[field])
|
|
241
|
+
diagnostics.push(
|
|
242
|
+
`${attributeAt}.${field}: expected a non-empty string`,
|
|
243
|
+
);
|
|
244
|
+
if (!wiring.helpers?.includes(attribute?.helper))
|
|
245
|
+
diagnostics.push(
|
|
246
|
+
`${attributeAt}.helper: ${attribute?.helper} is not listed in composition.wiring.helpers`,
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function validateWiring(composition, at, packageRoot, diagnostics) {
|
|
252
|
+
const wiring = composition.wiring;
|
|
253
|
+
if (typeof wiring?.required !== 'boolean')
|
|
254
|
+
diagnostics.push(`${at}.composition.wiring.required: expected boolean`);
|
|
255
|
+
validateStringList(
|
|
256
|
+
wiring?.helpers,
|
|
257
|
+
`${at}.composition.wiring.helpers`,
|
|
258
|
+
diagnostics,
|
|
259
|
+
);
|
|
260
|
+
if ('actions' in (wiring ?? {}))
|
|
261
|
+
validateStringList(
|
|
262
|
+
wiring.actions,
|
|
263
|
+
`${at}.composition.wiring.actions`,
|
|
264
|
+
diagnostics,
|
|
265
|
+
);
|
|
266
|
+
// Required wiring names something the app must do: a helper to call, or a
|
|
267
|
+
// root-delegated data-action to handle.
|
|
268
|
+
if (
|
|
269
|
+
wiring?.required === true &&
|
|
270
|
+
Array.isArray(wiring.helpers) &&
|
|
271
|
+
!wiring.helpers.length &&
|
|
272
|
+
!wiring.actions?.length
|
|
273
|
+
)
|
|
274
|
+
diagnostics.push(
|
|
275
|
+
`${at}.composition.wiring: required wiring names at least one helper or action`,
|
|
276
|
+
);
|
|
277
|
+
validateStringList(
|
|
278
|
+
wiring?.obligations,
|
|
279
|
+
`${at}.composition.wiring.obligations`,
|
|
280
|
+
diagnostics,
|
|
281
|
+
);
|
|
282
|
+
validateWiringSources(wiring, at, packageRoot, diagnostics);
|
|
283
|
+
validateStateAttributes(wiring, at, diagnostics);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const GEOMETRY_OWNERS = [
|
|
287
|
+
'self',
|
|
288
|
+
'parent',
|
|
289
|
+
'child',
|
|
290
|
+
'none',
|
|
291
|
+
'conditional',
|
|
292
|
+
'composed',
|
|
293
|
+
];
|
|
294
|
+
|
|
295
|
+
function validateResponsiveAndLayout(composition, at, diagnostics) {
|
|
296
|
+
if (
|
|
297
|
+
!['application', 'component', 'shared', 'not-applicable'].includes(
|
|
298
|
+
composition.responsive?.owner,
|
|
299
|
+
)
|
|
300
|
+
)
|
|
301
|
+
diagnostics.push(`${at}.composition.responsive.owner: invalid owner`);
|
|
302
|
+
validateStringList(
|
|
303
|
+
composition.responsive?.behaviors,
|
|
304
|
+
`${at}.composition.responsive.behaviors`,
|
|
305
|
+
diagnostics,
|
|
306
|
+
);
|
|
307
|
+
validateStringList(
|
|
308
|
+
composition.layout?.roles,
|
|
309
|
+
`${at}.composition.layout.roles`,
|
|
310
|
+
diagnostics,
|
|
311
|
+
{ nonempty: true },
|
|
312
|
+
);
|
|
313
|
+
for (const dimension of ['margin', 'border', 'padding'])
|
|
314
|
+
if (!GEOMETRY_OWNERS.includes(composition.layout?.geometry?.[dimension]))
|
|
315
|
+
diagnostics.push(
|
|
316
|
+
`${at}.composition.layout.geometry.${dimension}: invalid geometry owner`,
|
|
317
|
+
);
|
|
318
|
+
if ('notes' in (composition.layout?.geometry ?? {}))
|
|
319
|
+
validateStringList(
|
|
320
|
+
composition.layout.geometry.notes,
|
|
321
|
+
`${at}.composition.layout.geometry.notes`,
|
|
322
|
+
diagnostics,
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/** Check a component's `composition` block; `at` prefixes each diagnostic. */
|
|
327
|
+
export function validateComposition(component, at, diagnostics, packageRoot) {
|
|
328
|
+
const composition = component.composition ?? {};
|
|
329
|
+
validateParentsAndZones(composition, at, diagnostics);
|
|
330
|
+
validateChildren(composition, at, diagnostics);
|
|
331
|
+
validateState(composition, at, diagnostics);
|
|
332
|
+
validateWiring(composition, at, packageRoot, diagnostics);
|
|
333
|
+
validateResponsiveAndLayout(composition, at, diagnostics);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// One `publicExports` item of a published package must be a package.json
|
|
337
|
+
// subpath whose src/ entry exports the name. A private application
|
|
338
|
+
// (package.json#private) is bundled, never imported by package name, and has
|
|
339
|
+
// no per-file dist entries: its items may omit `subpath`, and each name is
|
|
340
|
+
// then verified against the component's own `source` file, which is how the
|
|
341
|
+
// Kerf UI rules resolve its wrappers.
|
|
342
|
+
function validatePublicExport(item, context, seen, diagnostics) {
|
|
343
|
+
const { at, packageRoot, packageJson, component, sourcePath } = context;
|
|
344
|
+
const exportAt = `${at}.publicExports`;
|
|
345
|
+
const exports = packageJson.exports ?? {};
|
|
346
|
+
const sourceOnly =
|
|
347
|
+
packageJson.private === true &&
|
|
348
|
+
Boolean(item?.name) &&
|
|
349
|
+
!Object.hasOwn(item, 'subpath');
|
|
350
|
+
if (!item?.name || (!sourceOnly && typeof item?.subpath !== 'string')) {
|
|
351
|
+
diagnostics.push(
|
|
352
|
+
`${exportAt}: every export requires name and subpath (only a private application, package.json#private, may omit subpath)`,
|
|
353
|
+
);
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
const key = sourceOnly
|
|
357
|
+
? `source:${item.name}`
|
|
358
|
+
: `${item.subpath}:${item.name}`;
|
|
359
|
+
if (seen.has(key))
|
|
360
|
+
diagnostics.push(`${exportAt}: duplicate ${sourceOnly ? item.name : key}`);
|
|
361
|
+
seen.add(key);
|
|
362
|
+
if (sourceOnly) {
|
|
363
|
+
if (
|
|
364
|
+
existsSync(sourcePath) &&
|
|
365
|
+
!exportedNamesFromFile(sourcePath).has(item.name)
|
|
366
|
+
)
|
|
367
|
+
diagnostics.push(
|
|
368
|
+
`${exportAt}: ${item.name} is not exported by ${component.source}`,
|
|
369
|
+
);
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
if (!(item.subpath in exports))
|
|
373
|
+
diagnostics.push(
|
|
374
|
+
`${exportAt}: package.json does not export subpath ${item.subpath}`,
|
|
375
|
+
);
|
|
376
|
+
const sourceEntry = sourceEntryForExport(packageRoot, exports[item.subpath]);
|
|
377
|
+
if (!sourceEntry)
|
|
378
|
+
diagnostics.push(
|
|
379
|
+
`${exportAt}: cannot map ${item.subpath} to an existing src/ entry`,
|
|
380
|
+
);
|
|
381
|
+
else if (!exportedNamesFromFile(sourceEntry).has(item.name))
|
|
382
|
+
diagnostics.push(
|
|
383
|
+
`${exportAt}: ${item.name} is not exported by ${relative(packageRoot, sourceEntry)}`,
|
|
384
|
+
);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
function validatePublicExports(context, diagnostics) {
|
|
388
|
+
const { component, at } = context;
|
|
389
|
+
if (
|
|
390
|
+
!Array.isArray(component.publicExports) ||
|
|
391
|
+
!component.publicExports.length
|
|
392
|
+
) {
|
|
393
|
+
diagnostics.push(`${at}.publicExports: expected a non-empty array`);
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
const seen = new Set();
|
|
397
|
+
for (const item of component.publicExports)
|
|
398
|
+
validatePublicExport(item, context, seen, diagnostics);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
function validateSourceLinks(component, at, packageRoot, diagnostics) {
|
|
402
|
+
validateStringList(component.sourceLinks, `${at}.sourceLinks`, diagnostics, {
|
|
403
|
+
nonempty: true,
|
|
404
|
+
});
|
|
405
|
+
for (const link of component.sourceLinks ?? []) {
|
|
406
|
+
if (/^[a-z]+:/i.test(link)) continue;
|
|
407
|
+
const localPath = link.split('#')[0];
|
|
408
|
+
if (localPath && !existsSync(resolve(packageRoot, localPath)))
|
|
409
|
+
diagnostics.push(
|
|
410
|
+
`${at}.sourceLinks: local source link does not exist: ${link}`,
|
|
411
|
+
);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
function validateBoundaries(component, at, diagnostics) {
|
|
416
|
+
const boundaries = component.boundaries;
|
|
417
|
+
validateStringList(
|
|
418
|
+
boundaries?.publicClasses,
|
|
419
|
+
`${at}.boundaries.publicClasses`,
|
|
420
|
+
diagnostics,
|
|
421
|
+
);
|
|
422
|
+
if (
|
|
423
|
+
boundaries?.rootClass !== null &&
|
|
424
|
+
(typeof boundaries?.rootClass !== 'string' ||
|
|
425
|
+
!boundaries.publicClasses?.includes(boundaries.rootClass))
|
|
426
|
+
)
|
|
427
|
+
diagnostics.push(
|
|
428
|
+
`${at}.boundaries.rootClass: expected null or one publicClasses entry`,
|
|
429
|
+
);
|
|
430
|
+
// An application may place only the declared subset of public classes on
|
|
431
|
+
// its own elements; every other public class is the component's rendered
|
|
432
|
+
// anatomy (eslint-plugin-kerfjs KUI-L103). `rootElement` names the element
|
|
433
|
+
// the component renders around its placeable classes.
|
|
434
|
+
if (boundaries && 'placeableClasses' in boundaries) {
|
|
435
|
+
validateStringList(
|
|
436
|
+
boundaries.placeableClasses,
|
|
437
|
+
`${at}.boundaries.placeableClasses`,
|
|
438
|
+
diagnostics,
|
|
439
|
+
);
|
|
440
|
+
for (const className of Array.isArray(boundaries.placeableClasses)
|
|
441
|
+
? boundaries.placeableClasses
|
|
442
|
+
: [])
|
|
443
|
+
if (!boundaries.publicClasses?.includes(className))
|
|
444
|
+
diagnostics.push(
|
|
445
|
+
`${at}.boundaries.placeableClasses: ${className} is not one of publicClasses`,
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
if (
|
|
449
|
+
boundaries?.rootElement !== undefined &&
|
|
450
|
+
!boundaries.placeableClasses?.length
|
|
451
|
+
)
|
|
452
|
+
diagnostics.push(
|
|
453
|
+
`${at}.boundaries.rootElement: requires a non-empty placeableClasses`,
|
|
454
|
+
);
|
|
455
|
+
validateStringList(
|
|
456
|
+
boundaries?.publicTokens,
|
|
457
|
+
`${at}.boundaries.publicTokens`,
|
|
458
|
+
diagnostics,
|
|
459
|
+
);
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
// Diagnostic ids are unique across the whole package, not per component.
|
|
463
|
+
function validateDiagnostics(component, at, diagnosticIds, diagnostics) {
|
|
464
|
+
if (!Array.isArray(component.diagnostics))
|
|
465
|
+
diagnostics.push(`${at}.diagnostics: expected an array`);
|
|
466
|
+
for (const diagnostic of component.diagnostics ?? []) {
|
|
467
|
+
if (
|
|
468
|
+
!/^KUI-C[0-9]{3}$/.test(diagnostic?.id ?? '') ||
|
|
469
|
+
diagnosticIds.has(diagnostic.id)
|
|
470
|
+
)
|
|
471
|
+
diagnostics.push(`${at}.diagnostics: ids must be unique KUI-C### values`);
|
|
472
|
+
diagnosticIds.add(diagnostic?.id);
|
|
473
|
+
if (!['error', 'warning'].includes(diagnostic?.severity))
|
|
474
|
+
diagnostics.push(
|
|
475
|
+
`${at}.diagnostics.${diagnostic?.id}.severity: invalid severity`,
|
|
476
|
+
);
|
|
477
|
+
if (!diagnostic?.when || !diagnostic?.message)
|
|
478
|
+
diagnostics.push(
|
|
479
|
+
`${at}.diagnostics.${diagnostic?.id}: when and message are required`,
|
|
480
|
+
);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* Check one component of `packageJson`'s author metadata. `ids` and
|
|
486
|
+
* `diagnosticIds` accumulate across the package's components so duplicates
|
|
487
|
+
* are reported.
|
|
488
|
+
*/
|
|
489
|
+
export function validateComponent(
|
|
490
|
+
component,
|
|
491
|
+
packageRoot,
|
|
492
|
+
packageJson,
|
|
493
|
+
ids,
|
|
494
|
+
diagnosticIds,
|
|
495
|
+
diagnostics,
|
|
496
|
+
) {
|
|
497
|
+
const at = `${packageJson.name}:${component?.id ?? '<unknown>'}`;
|
|
498
|
+
for (const path of DECISION_PATHS)
|
|
499
|
+
requireDecision(component, path, at, diagnostics);
|
|
500
|
+
|
|
501
|
+
if (!component?.id) return;
|
|
502
|
+
if (ids.has(component.id))
|
|
503
|
+
diagnostics.push(`${at}.id: duplicate component id`);
|
|
504
|
+
ids.add(component.id);
|
|
505
|
+
if (!['component', 'composition', 'recipe'].includes(component.kind))
|
|
506
|
+
diagnostics.push(`${at}.kind: expected component, composition, or recipe`);
|
|
507
|
+
const sourcePath = resolve(packageRoot, component.source ?? '');
|
|
508
|
+
if (!component.source || !existsSync(sourcePath))
|
|
509
|
+
diagnostics.push(`${at}.source: file does not exist: ${component.source}`);
|
|
510
|
+
|
|
511
|
+
validatePublicExports(
|
|
512
|
+
{ at, packageRoot, packageJson, component, sourcePath },
|
|
513
|
+
diagnostics,
|
|
514
|
+
);
|
|
515
|
+
validateSourceLinks(component, at, packageRoot, diagnostics);
|
|
516
|
+
validateComposition(component, at, diagnostics, packageRoot);
|
|
517
|
+
validateBoundaries(component, at, diagnostics);
|
|
518
|
+
validateStringList(
|
|
519
|
+
component.accessibility?.obligations,
|
|
520
|
+
`${at}.accessibility.obligations`,
|
|
521
|
+
diagnostics,
|
|
522
|
+
);
|
|
523
|
+
validateDiagnostics(component, at, diagnosticIds, diagnostics);
|
|
524
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// Which packages the catalog command generates: the root package and every
|
|
2
|
+
// npm workspace package that declares `package.json#kerfComponentCatalog`.
|
|
3
|
+
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
|
|
4
|
+
import { join, resolve, sep } from 'node:path';
|
|
5
|
+
|
|
6
|
+
export const CONFIG_KEY = 'kerfComponentCatalog';
|
|
7
|
+
const ignoredDirectories = new Set(['.git', 'dist', 'node_modules']);
|
|
8
|
+
|
|
9
|
+
/** Every diagnostic a run found, thrown together. */
|
|
10
|
+
export class CatalogError extends Error {
|
|
11
|
+
constructor(diagnostics) {
|
|
12
|
+
super(diagnostics.join('\n'));
|
|
13
|
+
this.name = 'CatalogError';
|
|
14
|
+
this.diagnostics = diagnostics;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Parse a JSON file, or record why it could not be read and return null. */
|
|
19
|
+
export function readJson(path, label, diagnostics) {
|
|
20
|
+
try {
|
|
21
|
+
return JSON.parse(readFileSync(path, 'utf8'));
|
|
22
|
+
} catch (error) {
|
|
23
|
+
diagnostics.push(`${label}: ${error.message}`);
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// A workspace pattern supports one `*` segment (`packages/*`), as npm does.
|
|
29
|
+
function expandWorkspace(root, pattern) {
|
|
30
|
+
const normalized = pattern.replaceAll('/', sep);
|
|
31
|
+
if (!normalized.includes('*')) return [resolve(root, normalized)];
|
|
32
|
+
const star = normalized.indexOf('*');
|
|
33
|
+
const parent = resolve(root, normalized.slice(0, star));
|
|
34
|
+
const suffix = normalized.slice(star + 1);
|
|
35
|
+
if (!existsSync(parent)) return [];
|
|
36
|
+
return readdirSync(parent, { withFileTypes: true })
|
|
37
|
+
.filter(
|
|
38
|
+
(entry) => entry.isDirectory() && !ignoredDirectories.has(entry.name),
|
|
39
|
+
)
|
|
40
|
+
.map((entry) => resolve(parent, entry.name + suffix))
|
|
41
|
+
.filter((path) => existsSync(path) && statSync(path).isDirectory());
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The configured package roots under `root`, sorted and deduplicated. */
|
|
45
|
+
export function configuredPackages(root, diagnostics) {
|
|
46
|
+
const rootPackagePath = join(root, 'package.json');
|
|
47
|
+
const rootPackage = readJson(rootPackagePath, rootPackagePath, diagnostics);
|
|
48
|
+
if (!rootPackage) return [];
|
|
49
|
+
const packages = rootPackage[CONFIG_KEY] ? [root] : [];
|
|
50
|
+
const workspacePatterns = Array.isArray(rootPackage.workspaces)
|
|
51
|
+
? rootPackage.workspaces
|
|
52
|
+
: (rootPackage.workspaces?.packages ?? []);
|
|
53
|
+
for (const pattern of workspacePatterns) {
|
|
54
|
+
for (const packageRoot of expandWorkspace(root, pattern)) {
|
|
55
|
+
const packagePath = join(packageRoot, 'package.json');
|
|
56
|
+
if (!existsSync(packagePath)) continue;
|
|
57
|
+
const packageJson = readJson(packagePath, packagePath, diagnostics);
|
|
58
|
+
if (packageJson?.[CONFIG_KEY]) packages.push(packageRoot);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return [...new Set(packages)].sort();
|
|
62
|
+
}
|
package/catalog.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// kerf-component-catalog: generate (or with --check, verify) a component
|
|
3
|
+
// package's composition and selection catalogs from its author-owned
|
|
4
|
+
// `kerf.components.json`. This file is the command and the public API; the
|
|
5
|
+
// work lives in ./catalog/ (schema validation, workspace discovery, export
|
|
6
|
+
// discovery, component validation, selection catalog, generation). A
|
|
7
|
+
// scaffolded package receives this file as scripts/kerf-component-catalog.mjs
|
|
8
|
+
// with ./catalog/ and the three schemas beside it.
|
|
9
|
+
|
|
10
|
+
import { realpathSync } from 'node:fs';
|
|
11
|
+
import { relative, resolve } from 'node:path';
|
|
12
|
+
import process from 'node:process';
|
|
13
|
+
import { fileURLToPath } from 'node:url';
|
|
14
|
+
|
|
15
|
+
import { runCatalogCommand } from './catalog/generate.js';
|
|
16
|
+
import { CatalogError } from './catalog/workspace.js';
|
|
17
|
+
|
|
18
|
+
export { exportedNamesFromSource } from './catalog/export-discovery.js';
|
|
19
|
+
export {
|
|
20
|
+
formatCatalog,
|
|
21
|
+
generateCatalogs,
|
|
22
|
+
runCatalogCommand,
|
|
23
|
+
} from './catalog/generate.js';
|
|
24
|
+
export { validateDocumentAgainstSchema } from './catalog/schema-validate.js';
|
|
25
|
+
export { CatalogError } from './catalog/workspace.js';
|
|
26
|
+
|
|
27
|
+
function parseArgs(argv) {
|
|
28
|
+
let root = process.cwd();
|
|
29
|
+
let check = false;
|
|
30
|
+
for (let index = 2; index < argv.length; index += 1) {
|
|
31
|
+
const arg = argv[index];
|
|
32
|
+
if (arg === '--check') check = true;
|
|
33
|
+
else if (arg === '--write') check = false;
|
|
34
|
+
else if (arg === '--root' && argv[index + 1]) root = resolve(argv[++index]);
|
|
35
|
+
else if (arg === '--help' || arg === '-h') {
|
|
36
|
+
process.stdout.write(
|
|
37
|
+
'Usage: kerf-component-catalog [--write|--check] [--root <package-or-workspace>]\n',
|
|
38
|
+
);
|
|
39
|
+
process.exit(0);
|
|
40
|
+
} else throw new CatalogError([`unknown argument: ${arg}`]);
|
|
41
|
+
}
|
|
42
|
+
return { root, check };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const isMain =
|
|
46
|
+
process.argv[1] &&
|
|
47
|
+
realpathSync(process.argv[1]) ===
|
|
48
|
+
realpathSync(fileURLToPath(import.meta.url));
|
|
49
|
+
if (isMain) {
|
|
50
|
+
try {
|
|
51
|
+
const options = parseArgs(process.argv);
|
|
52
|
+
const results = runCatalogCommand(options);
|
|
53
|
+
const verb = options.check ? 'verified' : 'wrote';
|
|
54
|
+
for (const result of results)
|
|
55
|
+
for (const path of [result.outputPath, result.selectionOutputPath])
|
|
56
|
+
if (path)
|
|
57
|
+
process.stdout.write(`${verb} ${relative(options.root, path)}\n`);
|
|
58
|
+
} catch (error) {
|
|
59
|
+
const diagnostics =
|
|
60
|
+
error instanceof CatalogError ? error.diagnostics : [error.message];
|
|
61
|
+
for (const diagnostic of diagnostics)
|
|
62
|
+
process.stderr.write(`kerf-component-catalog: ${diagnostic}\n`);
|
|
63
|
+
process.exitCode = 1;
|
|
64
|
+
}
|
|
65
|
+
}
|