create-kerf-component 5.0.0-beta.125 → 5.0.0-beta.126
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
package/catalog.js
CHANGED
|
@@ -1,1190 +1,28 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
} from 'node:fs';
|
|
11
|
-
import {
|
|
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
12
|
import process from 'node:process';
|
|
13
|
-
import { createRequire } from 'node:module';
|
|
14
13
|
import { fileURLToPath } from 'node:url';
|
|
15
14
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
join(scriptRoot, 'component-catalog-extension.schema.json'),
|
|
28
|
-
'utf8',
|
|
29
|
-
),
|
|
30
|
-
);
|
|
31
|
-
|
|
32
|
-
export class CatalogError extends Error {
|
|
33
|
-
constructor(diagnostics) {
|
|
34
|
-
super(diagnostics.join('\n'));
|
|
35
|
-
this.name = 'CatalogError';
|
|
36
|
-
this.diagnostics = diagnostics;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function readJson(path, label, diagnostics) {
|
|
41
|
-
try {
|
|
42
|
-
return JSON.parse(readFileSync(path, 'utf8'));
|
|
43
|
-
} catch (error) {
|
|
44
|
-
diagnostics.push(`${label}: ${error.message}`);
|
|
45
|
-
return null;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function schemaReference(rootSchema, reference) {
|
|
50
|
-
if (!reference.startsWith('#/'))
|
|
51
|
-
throw new Error(`unsupported schema reference ${reference}`);
|
|
52
|
-
return reference
|
|
53
|
-
.slice(2)
|
|
54
|
-
.split('/')
|
|
55
|
-
.map((part) => part.replaceAll('~1', '/').replaceAll('~0', '~'))
|
|
56
|
-
.reduce((value, part) => value?.[part], rootSchema);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function schemaTypeMatches(value, type) {
|
|
60
|
-
if (type === 'array') return Array.isArray(value);
|
|
61
|
-
if (type === 'object')
|
|
62
|
-
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
63
|
-
if (type === 'integer') return Number.isInteger(value);
|
|
64
|
-
return typeof value === type;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function validateSchemaValue(value, rule, path, rootSchema, diagnostics) {
|
|
68
|
-
if (rule.$ref) {
|
|
69
|
-
const referenced = schemaReference(rootSchema, rule.$ref);
|
|
70
|
-
if (!referenced) {
|
|
71
|
-
diagnostics.push(`${path}: schema reference ${rule.$ref} does not exist`);
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
validateSchemaValue(value, referenced, path, rootSchema, diagnostics);
|
|
75
|
-
return;
|
|
76
|
-
}
|
|
77
|
-
if (rule.allOf) {
|
|
78
|
-
for (const candidate of rule.allOf)
|
|
79
|
-
validateSchemaValue(value, candidate, path, rootSchema, diagnostics);
|
|
80
|
-
}
|
|
81
|
-
if (rule.anyOf) {
|
|
82
|
-
const alternatives = rule.anyOf.map((candidate) => {
|
|
83
|
-
const failures = [];
|
|
84
|
-
validateSchemaValue(value, candidate, path, rootSchema, failures);
|
|
85
|
-
return failures;
|
|
86
|
-
});
|
|
87
|
-
if (alternatives.every((failures) => failures.length)) {
|
|
88
|
-
// A union of bare types (a prop condition's `equals`) names the types.
|
|
89
|
-
const types = rule.anyOf.every(
|
|
90
|
-
(candidate) =>
|
|
91
|
-
Object.keys(candidate).length === 1 &&
|
|
92
|
-
typeof candidate.type === 'string',
|
|
93
|
-
)
|
|
94
|
-
? rule.anyOf.map((candidate) => candidate.type)
|
|
95
|
-
: null;
|
|
96
|
-
diagnostics.push(
|
|
97
|
-
types
|
|
98
|
-
? `${path}: expected ${types.join(' or ')}`
|
|
99
|
-
: `${path}: does not match any allowed schema variant`,
|
|
100
|
-
);
|
|
101
|
-
}
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
if (rule.const !== undefined && value !== rule.const)
|
|
105
|
-
diagnostics.push(`${path}: expected ${JSON.stringify(rule.const)}`);
|
|
106
|
-
if (rule.enum && !rule.enum.includes(value))
|
|
107
|
-
diagnostics.push(
|
|
108
|
-
`${path}: expected one of ${rule.enum.map((item) => JSON.stringify(item)).join(', ')}`,
|
|
109
|
-
);
|
|
110
|
-
if (rule.type && !schemaTypeMatches(value, rule.type)) {
|
|
111
|
-
diagnostics.push(`${path}: expected ${rule.type}`);
|
|
112
|
-
return;
|
|
113
|
-
}
|
|
114
|
-
if (typeof value === 'string') {
|
|
115
|
-
if (rule.minLength !== undefined && value.length < rule.minLength)
|
|
116
|
-
diagnostics.push(
|
|
117
|
-
`${path}: expected at least ${rule.minLength} character(s)`,
|
|
118
|
-
);
|
|
119
|
-
if (rule.pattern && !new RegExp(rule.pattern).test(value))
|
|
120
|
-
diagnostics.push(`${path}: must match ${rule.pattern}`);
|
|
121
|
-
}
|
|
122
|
-
if (typeof value === 'number' && rule.minimum !== undefined) {
|
|
123
|
-
if (value < rule.minimum)
|
|
124
|
-
diagnostics.push(`${path}: expected a value >= ${rule.minimum}`);
|
|
125
|
-
}
|
|
126
|
-
if (Array.isArray(value)) {
|
|
127
|
-
if (rule.minItems !== undefined && value.length < rule.minItems)
|
|
128
|
-
diagnostics.push(`${path}: expected at least ${rule.minItems} item(s)`);
|
|
129
|
-
if (rule.uniqueItems) {
|
|
130
|
-
const serialized = value.map((item) => JSON.stringify(item));
|
|
131
|
-
if (new Set(serialized).size !== serialized.length)
|
|
132
|
-
diagnostics.push(`${path}: expected unique items`);
|
|
133
|
-
}
|
|
134
|
-
if (rule.items)
|
|
135
|
-
value.forEach((item, index) =>
|
|
136
|
-
validateSchemaValue(
|
|
137
|
-
item,
|
|
138
|
-
rule.items,
|
|
139
|
-
`${path}[${index}]`,
|
|
140
|
-
rootSchema,
|
|
141
|
-
diagnostics,
|
|
142
|
-
),
|
|
143
|
-
);
|
|
144
|
-
}
|
|
145
|
-
if (value !== null && typeof value === 'object' && !Array.isArray(value)) {
|
|
146
|
-
for (const key of rule.required ?? [])
|
|
147
|
-
if (!(key in value)) diagnostics.push(`${path}.${key}: is required`);
|
|
148
|
-
const properties = rule.properties ?? {};
|
|
149
|
-
for (const [key, item] of Object.entries(value)) {
|
|
150
|
-
if (key in properties)
|
|
151
|
-
validateSchemaValue(
|
|
152
|
-
item,
|
|
153
|
-
properties[key],
|
|
154
|
-
`${path}.${key}`,
|
|
155
|
-
rootSchema,
|
|
156
|
-
diagnostics,
|
|
157
|
-
);
|
|
158
|
-
else if (rule.additionalProperties === false)
|
|
159
|
-
diagnostics.push(`${path}.${key}: additional property is not allowed`);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
export function validateDocumentAgainstSchema(value, schema, path) {
|
|
165
|
-
const diagnostics = [];
|
|
166
|
-
validateSchemaValue(value, schema, path, schema, diagnostics);
|
|
167
|
-
return diagnostics;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
function expandWorkspace(root, pattern) {
|
|
171
|
-
const normalized = pattern.replaceAll('/', sep);
|
|
172
|
-
if (!normalized.includes('*')) return [resolve(root, normalized)];
|
|
173
|
-
const star = normalized.indexOf('*');
|
|
174
|
-
const parent = resolve(root, normalized.slice(0, star));
|
|
175
|
-
const suffix = normalized.slice(star + 1);
|
|
176
|
-
if (!existsSync(parent)) return [];
|
|
177
|
-
return readdirSync(parent, { withFileTypes: true })
|
|
178
|
-
.filter(
|
|
179
|
-
(entry) => entry.isDirectory() && !ignoredDirectories.has(entry.name),
|
|
180
|
-
)
|
|
181
|
-
.map((entry) => resolve(parent, entry.name + suffix))
|
|
182
|
-
.filter((path) => existsSync(path) && statSync(path).isDirectory());
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
function configuredPackages(root, diagnostics) {
|
|
186
|
-
const rootPackagePath = join(root, 'package.json');
|
|
187
|
-
const rootPackage = readJson(rootPackagePath, rootPackagePath, diagnostics);
|
|
188
|
-
if (!rootPackage) return [];
|
|
189
|
-
const packages = rootPackage[CONFIG_KEY] ? [root] : [];
|
|
190
|
-
const workspacePatterns = Array.isArray(rootPackage.workspaces)
|
|
191
|
-
? rootPackage.workspaces
|
|
192
|
-
: (rootPackage.workspaces?.packages ?? []);
|
|
193
|
-
for (const pattern of workspacePatterns) {
|
|
194
|
-
for (const packageRoot of expandWorkspace(root, pattern)) {
|
|
195
|
-
const packagePath = join(packageRoot, 'package.json');
|
|
196
|
-
if (!existsSync(packagePath)) continue;
|
|
197
|
-
const packageJson = readJson(packagePath, packagePath, diagnostics);
|
|
198
|
-
if (packageJson?.[CONFIG_KEY]) packages.push(packageRoot);
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
return [...new Set(packages)].sort();
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
function valueAt(value, path) {
|
|
205
|
-
return path.split('.').reduce((current, part) => current?.[part], value);
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
// Paths where an explicit `null` is itself the author's decision: a component
|
|
209
|
-
// whose root element belongs to another catalog (a `rendersAs` wrapper) or that
|
|
210
|
-
// exposes no public root class.
|
|
211
|
-
const NULLABLE_DECISIONS = new Set(['boundaries.rootClass']);
|
|
212
|
-
|
|
213
|
-
function requireDecision(component, path, at, diagnostics) {
|
|
214
|
-
const value = valueAt(component, path);
|
|
215
|
-
const explicitNull =
|
|
216
|
-
value === null &&
|
|
217
|
-
NULLABLE_DECISIONS.has(path) &&
|
|
218
|
-
Object.hasOwn(
|
|
219
|
-
valueAt(component, path.slice(0, path.lastIndexOf('.'))) ?? {},
|
|
220
|
-
path.slice(path.lastIndexOf('.') + 1),
|
|
221
|
-
);
|
|
222
|
-
if (explicitNull) return;
|
|
223
|
-
if (value === undefined || value === null || value === '') {
|
|
224
|
-
diagnostics.push(
|
|
225
|
-
`${at}.${path}: author decision required; the generator does not infer semantics or geometry from rendered appearance`,
|
|
226
|
-
);
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
let typescript;
|
|
231
|
-
|
|
232
|
-
function typescriptApi() {
|
|
233
|
-
if (typescript) return typescript;
|
|
234
|
-
try {
|
|
235
|
-
typescript = createRequire(import.meta.url)('typescript');
|
|
236
|
-
return typescript;
|
|
237
|
-
} catch {
|
|
238
|
-
throw new Error(
|
|
239
|
-
'TypeScript is required for syntax-aware export verification; run npm install before catalog generation.',
|
|
240
|
-
);
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
function hasModifier(ts, node, kind) {
|
|
245
|
-
return Boolean(node.modifiers?.some((modifier) => modifier.kind === kind));
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
function bindingNames(ts, name, names) {
|
|
249
|
-
if (ts.isIdentifier(name)) names.add(name.text);
|
|
250
|
-
else
|
|
251
|
-
for (const element of name.elements ?? [])
|
|
252
|
-
if (!ts.isOmittedExpression(element))
|
|
253
|
-
bindingNames(ts, element.name, names);
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
export function exportedNamesFromSource(source, fileName = 'component.tsx') {
|
|
257
|
-
const ts = typescriptApi();
|
|
258
|
-
const sourceFile = ts.createSourceFile(
|
|
259
|
-
fileName,
|
|
260
|
-
source,
|
|
261
|
-
ts.ScriptTarget.Latest,
|
|
262
|
-
true,
|
|
263
|
-
fileName.endsWith('.tsx') || fileName.endsWith('.jsx')
|
|
264
|
-
? ts.ScriptKind.TSX
|
|
265
|
-
: ts.ScriptKind.TS,
|
|
266
|
-
);
|
|
267
|
-
if (sourceFile.parseDiagnostics.length) {
|
|
268
|
-
const diagnostic = sourceFile.parseDiagnostics[0];
|
|
269
|
-
const point = sourceFile.getLineAndCharacterOfPosition(
|
|
270
|
-
diagnostic.start ?? 0,
|
|
271
|
-
);
|
|
272
|
-
throw new Error(
|
|
273
|
-
`${fileName}:${point.line + 1}:${point.character + 1}: ${ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')}`,
|
|
274
|
-
);
|
|
275
|
-
}
|
|
276
|
-
const names = new Set();
|
|
277
|
-
for (const statement of sourceFile.statements) {
|
|
278
|
-
if (ts.isExportDeclaration(statement)) {
|
|
279
|
-
if (!statement.exportClause) continue;
|
|
280
|
-
if (ts.isNamedExports(statement.exportClause))
|
|
281
|
-
for (const element of statement.exportClause.elements)
|
|
282
|
-
names.add(element.name.text);
|
|
283
|
-
continue;
|
|
284
|
-
}
|
|
285
|
-
if (
|
|
286
|
-
!hasModifier(ts, statement, ts.SyntaxKind.ExportKeyword) ||
|
|
287
|
-
hasModifier(ts, statement, ts.SyntaxKind.DefaultKeyword)
|
|
288
|
-
)
|
|
289
|
-
continue;
|
|
290
|
-
if (ts.isVariableStatement(statement)) {
|
|
291
|
-
for (const declaration of statement.declarationList.declarations)
|
|
292
|
-
bindingNames(ts, declaration.name, names);
|
|
293
|
-
} else if (
|
|
294
|
-
(ts.isFunctionDeclaration(statement) ||
|
|
295
|
-
ts.isClassDeclaration(statement) ||
|
|
296
|
-
ts.isEnumDeclaration(statement) ||
|
|
297
|
-
ts.isModuleDeclaration(statement) ||
|
|
298
|
-
ts.isInterfaceDeclaration(statement) ||
|
|
299
|
-
ts.isTypeAliasDeclaration(statement)) &&
|
|
300
|
-
statement.name
|
|
301
|
-
)
|
|
302
|
-
names.add(statement.name.text);
|
|
303
|
-
}
|
|
304
|
-
return names;
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
function exportedNamesFromFile(path) {
|
|
308
|
-
return exportedNamesFromSource(readFileSync(path, 'utf8'), path);
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
function sourceEntryForExport(packageRoot, definition) {
|
|
312
|
-
const target =
|
|
313
|
-
typeof definition === 'string'
|
|
314
|
-
? definition
|
|
315
|
-
: (definition?.import ?? definition?.default);
|
|
316
|
-
if (typeof target !== 'string' || !target.startsWith('./dist/')) return null;
|
|
317
|
-
const stem = target.slice('./dist/'.length).replace(/\.(?:mjs|cjs|js)$/, '');
|
|
318
|
-
for (const extension of ['.ts', '.tsx', '.mts', '.js', '.jsx', '.mjs']) {
|
|
319
|
-
const candidate = join(packageRoot, 'src', `${stem}${extension}`);
|
|
320
|
-
if (existsSync(candidate)) return candidate;
|
|
321
|
-
}
|
|
322
|
-
return null;
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
function validateStringList(value, at, diagnostics, { nonempty = false } = {}) {
|
|
326
|
-
if (
|
|
327
|
-
!Array.isArray(value) ||
|
|
328
|
-
(nonempty && value.length === 0) ||
|
|
329
|
-
value.some((item) => typeof item !== 'string' || item.length === 0) ||
|
|
330
|
-
new Set(value).size !== value.length
|
|
331
|
-
)
|
|
332
|
-
diagnostics.push(
|
|
333
|
-
`${at}: expected a${nonempty ? ' non-empty' : ''} unique string list`,
|
|
334
|
-
);
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
function validateComposition(component, at, diagnostics, packageRoot) {
|
|
338
|
-
const composition = component.composition ?? {};
|
|
339
|
-
if (!['any', 'root', 'listed'].includes(composition.parents?.mode))
|
|
340
|
-
diagnostics.push(`${at}.composition.parents.mode: invalid parent mode`);
|
|
341
|
-
validateStringList(
|
|
342
|
-
composition.parents?.entries,
|
|
343
|
-
`${at}.composition.parents.entries`,
|
|
344
|
-
diagnostics,
|
|
345
|
-
{ nonempty: composition.parents?.mode === 'listed' },
|
|
346
|
-
);
|
|
347
|
-
validateStringList(
|
|
348
|
-
composition.contexts,
|
|
349
|
-
`${at}.composition.contexts`,
|
|
350
|
-
diagnostics,
|
|
351
|
-
);
|
|
352
|
-
if (!Array.isArray(composition.zones))
|
|
353
|
-
diagnostics.push(`${at}.composition.zones: expected an array`);
|
|
354
|
-
const zoneIds = new Set();
|
|
355
|
-
const zoneProps = new Set();
|
|
356
|
-
for (const zone of composition.zones ?? []) {
|
|
357
|
-
const zoneAt = `${at}.composition.zones.${zone?.id ?? '<unknown>'}`;
|
|
358
|
-
if (!zone?.id || zoneIds.has(zone.id))
|
|
359
|
-
diagnostics.push(`${zoneAt}.id: expected a unique non-empty id`);
|
|
360
|
-
zoneIds.add(zone?.id);
|
|
361
|
-
const prop = zone?.jsx?.prop;
|
|
362
|
-
if (prop !== undefined) {
|
|
363
|
-
if (zoneProps.has(prop))
|
|
364
|
-
diagnostics.push(
|
|
365
|
-
`${zoneAt}.jsx.prop: ${prop} already fills another zone`,
|
|
366
|
-
);
|
|
367
|
-
zoneProps.add(prop);
|
|
368
|
-
}
|
|
369
|
-
validateStringList(zone?.accepts, `${zoneAt}.accepts`, diagnostics, {
|
|
370
|
-
nonempty: true,
|
|
371
|
-
});
|
|
372
|
-
const { min, max } = zone?.cardinality ?? {};
|
|
373
|
-
if (!Number.isInteger(min) || min < 0)
|
|
374
|
-
diagnostics.push(
|
|
375
|
-
`${zoneAt}.cardinality.min: expected a non-negative integer`,
|
|
376
|
-
);
|
|
377
|
-
if (max !== 'unbounded' && (!Number.isInteger(max) || max < 1 || max < min))
|
|
378
|
-
diagnostics.push(
|
|
379
|
-
`${zoneAt}.cardinality.max: expected unbounded or an integer >= min`,
|
|
380
|
-
);
|
|
381
|
-
validateStringList(
|
|
382
|
-
zone?.exclusiveWith,
|
|
383
|
-
`${zoneAt}.exclusiveWith`,
|
|
384
|
-
diagnostics,
|
|
385
|
-
);
|
|
386
|
-
}
|
|
387
|
-
for (const zone of composition.zones ?? [])
|
|
388
|
-
for (const other of zone?.exclusiveWith ?? [])
|
|
389
|
-
if (other === zone.id || !zoneIds.has(other))
|
|
390
|
-
diagnostics.push(
|
|
391
|
-
`${at}.composition.zones.${zone.id}.exclusiveWith: unknown or self-referential zone ${other}`,
|
|
392
|
-
);
|
|
393
|
-
|
|
394
|
-
if (!['any', 'none', 'listed'].includes(composition.children?.mode))
|
|
395
|
-
diagnostics.push(`${at}.composition.children.mode: invalid child mode`);
|
|
396
|
-
validateStringList(
|
|
397
|
-
composition.children?.concepts,
|
|
398
|
-
`${at}.composition.children.concepts`,
|
|
399
|
-
diagnostics,
|
|
400
|
-
{ nonempty: composition.children?.mode === 'listed' },
|
|
401
|
-
);
|
|
402
|
-
validateStringList(
|
|
403
|
-
composition.children?.requiredConcepts,
|
|
404
|
-
`${at}.composition.children.requiredConcepts`,
|
|
405
|
-
diagnostics,
|
|
406
|
-
);
|
|
407
|
-
for (const required of composition.children?.requiredConcepts ?? [])
|
|
408
|
-
if (!composition.children.concepts?.includes(required))
|
|
409
|
-
diagnostics.push(
|
|
410
|
-
`${at}.composition.children.requiredConcepts: ${required} is not an allowed concept`,
|
|
411
|
-
);
|
|
412
|
-
|
|
413
|
-
if (!Array.isArray(composition.state))
|
|
414
|
-
diagnostics.push(`${at}.composition.state: expected an array`);
|
|
415
|
-
const stateIds = new Set();
|
|
416
|
-
for (const state of composition.state ?? []) {
|
|
417
|
-
if (!state?.id || stateIds.has(state.id))
|
|
418
|
-
diagnostics.push(
|
|
419
|
-
`${at}.composition.state: state ids must be unique and non-empty`,
|
|
420
|
-
);
|
|
421
|
-
stateIds.add(state?.id);
|
|
422
|
-
if (!['application', 'controlled', 'component'].includes(state?.owner))
|
|
423
|
-
diagnostics.push(
|
|
424
|
-
`${at}.composition.state.${state?.id}.owner: invalid owner`,
|
|
425
|
-
);
|
|
426
|
-
if (typeof state?.required !== 'boolean')
|
|
427
|
-
diagnostics.push(
|
|
428
|
-
`${at}.composition.state.${state?.id}.required: expected boolean`,
|
|
429
|
-
);
|
|
430
|
-
}
|
|
431
|
-
if (typeof composition.wiring?.required !== 'boolean')
|
|
432
|
-
diagnostics.push(`${at}.composition.wiring.required: expected boolean`);
|
|
433
|
-
validateStringList(
|
|
434
|
-
composition.wiring?.helpers,
|
|
435
|
-
`${at}.composition.wiring.helpers`,
|
|
436
|
-
diagnostics,
|
|
437
|
-
);
|
|
438
|
-
if ('actions' in (composition.wiring ?? {}))
|
|
439
|
-
validateStringList(
|
|
440
|
-
composition.wiring.actions,
|
|
441
|
-
`${at}.composition.wiring.actions`,
|
|
442
|
-
diagnostics,
|
|
443
|
-
);
|
|
444
|
-
// Required wiring names something the app must do: a helper to call, or a
|
|
445
|
-
// root-delegated data-action to handle.
|
|
446
|
-
if (
|
|
447
|
-
composition.wiring?.required === true &&
|
|
448
|
-
Array.isArray(composition.wiring.helpers) &&
|
|
449
|
-
!composition.wiring.helpers.length &&
|
|
450
|
-
!composition.wiring.actions?.length
|
|
451
|
-
)
|
|
452
|
-
diagnostics.push(
|
|
453
|
-
`${at}.composition.wiring: required wiring names at least one helper or action`,
|
|
454
|
-
);
|
|
455
|
-
validateStringList(
|
|
456
|
-
composition.wiring?.obligations,
|
|
457
|
-
`${at}.composition.wiring.obligations`,
|
|
458
|
-
diagnostics,
|
|
459
|
-
);
|
|
460
|
-
const helperSourcesSeen = new Set();
|
|
461
|
-
for (const helperSource of composition.wiring?.sources ?? []) {
|
|
462
|
-
const sourceAt = `${at}.composition.wiring.sources.${helperSource?.export}`;
|
|
463
|
-
if (helperSourcesSeen.has(helperSource?.export))
|
|
464
|
-
diagnostics.push(`${sourceAt}: declared more than once`);
|
|
465
|
-
helperSourcesSeen.add(helperSource?.export);
|
|
466
|
-
if (!composition.wiring.helpers?.includes(helperSource?.export))
|
|
467
|
-
diagnostics.push(
|
|
468
|
-
`${sourceAt}: ${helperSource?.export} is not listed in composition.wiring.helpers`,
|
|
469
|
-
);
|
|
470
|
-
if (
|
|
471
|
-
helperSource?.returnsDisposer !== undefined &&
|
|
472
|
-
typeof helperSource.returnsDisposer !== 'boolean'
|
|
473
|
-
)
|
|
474
|
-
diagnostics.push(`${sourceAt}.returnsDisposer: expected boolean`);
|
|
475
|
-
const file = resolve(packageRoot, helperSource?.source ?? '');
|
|
476
|
-
if (!helperSource?.source || !existsSync(file))
|
|
477
|
-
diagnostics.push(
|
|
478
|
-
`${sourceAt}.source: file does not exist: ${helperSource?.source}`,
|
|
479
|
-
);
|
|
480
|
-
else if (!exportedNamesFromFile(file).has(helperSource.export))
|
|
481
|
-
diagnostics.push(
|
|
482
|
-
`${sourceAt}: ${helperSource.export} is not exported by ${helperSource.source}`,
|
|
483
|
-
);
|
|
484
|
-
}
|
|
485
|
-
const stateAttributes = composition.wiring?.stateAttributes;
|
|
486
|
-
if (stateAttributes !== undefined && !Array.isArray(stateAttributes))
|
|
487
|
-
diagnostics.push(
|
|
488
|
-
`${at}.composition.wiring.stateAttributes: expected an array`,
|
|
489
|
-
);
|
|
490
|
-
const stateAttributeNames = new Set();
|
|
491
|
-
for (const attribute of Array.isArray(stateAttributes)
|
|
492
|
-
? stateAttributes
|
|
493
|
-
: []) {
|
|
494
|
-
const name = attribute?.name;
|
|
495
|
-
const attributeAt = `${at}.composition.wiring.stateAttributes.${name}`;
|
|
496
|
-
if (typeof name !== 'string' || !/^data-[a-z0-9]+(-[a-z0-9]+)*$/.test(name))
|
|
497
|
-
diagnostics.push(`${attributeAt}.name: expected a data-* attribute name`);
|
|
498
|
-
if (stateAttributeNames.has(name))
|
|
499
|
-
diagnostics.push(`${attributeAt}.name: declared more than once`);
|
|
500
|
-
stateAttributeNames.add(name);
|
|
501
|
-
for (const field of ['on', 'meaning'])
|
|
502
|
-
if (typeof attribute?.[field] !== 'string' || !attribute[field])
|
|
503
|
-
diagnostics.push(
|
|
504
|
-
`${attributeAt}.${field}: expected a non-empty string`,
|
|
505
|
-
);
|
|
506
|
-
if (!composition.wiring.helpers?.includes(attribute?.helper))
|
|
507
|
-
diagnostics.push(
|
|
508
|
-
`${attributeAt}.helper: ${attribute?.helper} is not listed in composition.wiring.helpers`,
|
|
509
|
-
);
|
|
510
|
-
}
|
|
511
|
-
if (
|
|
512
|
-
!['application', 'component', 'shared', 'not-applicable'].includes(
|
|
513
|
-
composition.responsive?.owner,
|
|
514
|
-
)
|
|
515
|
-
)
|
|
516
|
-
diagnostics.push(`${at}.composition.responsive.owner: invalid owner`);
|
|
517
|
-
validateStringList(
|
|
518
|
-
composition.responsive?.behaviors,
|
|
519
|
-
`${at}.composition.responsive.behaviors`,
|
|
520
|
-
diagnostics,
|
|
521
|
-
);
|
|
522
|
-
validateStringList(
|
|
523
|
-
composition.layout?.roles,
|
|
524
|
-
`${at}.composition.layout.roles`,
|
|
525
|
-
diagnostics,
|
|
526
|
-
{ nonempty: true },
|
|
527
|
-
);
|
|
528
|
-
const geometryOwners = [
|
|
529
|
-
'self',
|
|
530
|
-
'parent',
|
|
531
|
-
'child',
|
|
532
|
-
'none',
|
|
533
|
-
'conditional',
|
|
534
|
-
'composed',
|
|
535
|
-
];
|
|
536
|
-
for (const dimension of ['margin', 'border', 'padding'])
|
|
537
|
-
if (!geometryOwners.includes(composition.layout?.geometry?.[dimension]))
|
|
538
|
-
diagnostics.push(
|
|
539
|
-
`${at}.composition.layout.geometry.${dimension}: invalid geometry owner`,
|
|
540
|
-
);
|
|
541
|
-
if ('notes' in (composition.layout?.geometry ?? {}))
|
|
542
|
-
validateStringList(
|
|
543
|
-
composition.layout.geometry.notes,
|
|
544
|
-
`${at}.composition.layout.geometry.notes`,
|
|
545
|
-
diagnostics,
|
|
546
|
-
);
|
|
547
|
-
}
|
|
548
|
-
|
|
549
|
-
function validateComponent(
|
|
550
|
-
component,
|
|
551
|
-
packageRoot,
|
|
552
|
-
packageJson,
|
|
553
|
-
ids,
|
|
554
|
-
diagnosticIds,
|
|
555
|
-
diagnostics,
|
|
556
|
-
) {
|
|
557
|
-
const at = `${packageJson.name}:${component?.id ?? '<unknown>'}`;
|
|
558
|
-
for (const path of [
|
|
559
|
-
'id',
|
|
560
|
-
'name',
|
|
561
|
-
'kind',
|
|
562
|
-
'purpose',
|
|
563
|
-
'source',
|
|
564
|
-
'publicExports',
|
|
565
|
-
'sourceLinks',
|
|
566
|
-
'composition.parents.mode',
|
|
567
|
-
'composition.parents.entries',
|
|
568
|
-
'composition.contexts',
|
|
569
|
-
'composition.zones',
|
|
570
|
-
'composition.children.mode',
|
|
571
|
-
'composition.children.concepts',
|
|
572
|
-
'composition.children.requiredConcepts',
|
|
573
|
-
'composition.state',
|
|
574
|
-
'composition.wiring.required',
|
|
575
|
-
'composition.wiring.helpers',
|
|
576
|
-
'composition.wiring.obligations',
|
|
577
|
-
'composition.responsive.owner',
|
|
578
|
-
'composition.responsive.behaviors',
|
|
579
|
-
'composition.layout.roles',
|
|
580
|
-
'composition.layout.geometry.margin',
|
|
581
|
-
'composition.layout.geometry.border',
|
|
582
|
-
'composition.layout.geometry.padding',
|
|
583
|
-
'boundaries.rootClass',
|
|
584
|
-
'boundaries.publicClasses',
|
|
585
|
-
'boundaries.publicTokens',
|
|
586
|
-
'accessibility.obligations',
|
|
587
|
-
'diagnostics',
|
|
588
|
-
'provenance.selection',
|
|
589
|
-
'provenance.composition',
|
|
590
|
-
])
|
|
591
|
-
requireDecision(component, path, at, diagnostics);
|
|
592
|
-
|
|
593
|
-
if (!component?.id) return;
|
|
594
|
-
if (ids.has(component.id))
|
|
595
|
-
diagnostics.push(`${at}.id: duplicate component id`);
|
|
596
|
-
ids.add(component.id);
|
|
597
|
-
if (!['component', 'composition', 'recipe'].includes(component.kind))
|
|
598
|
-
diagnostics.push(`${at}.kind: expected component, composition, or recipe`);
|
|
599
|
-
const sourcePath = resolve(packageRoot, component.source ?? '');
|
|
600
|
-
if (!component.source || !existsSync(sourcePath))
|
|
601
|
-
diagnostics.push(`${at}.source: file does not exist: ${component.source}`);
|
|
602
|
-
|
|
603
|
-
const exports = packageJson.exports ?? {};
|
|
604
|
-
// A private application (package.json#private) is bundled, never imported
|
|
605
|
-
// by package name, and has no per-file dist entries. Its exports may omit
|
|
606
|
-
// `subpath`; each name is then verified against the component's own
|
|
607
|
-
// `source` file, which is how the Kerf UI rules resolve its wrappers.
|
|
608
|
-
const privateApplication = packageJson.private === true;
|
|
609
|
-
if (
|
|
610
|
-
!Array.isArray(component.publicExports) ||
|
|
611
|
-
!component.publicExports.length
|
|
612
|
-
)
|
|
613
|
-
diagnostics.push(`${at}.publicExports: expected a non-empty array`);
|
|
614
|
-
else {
|
|
615
|
-
const seen = new Set();
|
|
616
|
-
for (const item of component.publicExports) {
|
|
617
|
-
const exportAt = `${at}.publicExports`;
|
|
618
|
-
const sourceOnly =
|
|
619
|
-
privateApplication &&
|
|
620
|
-
Boolean(item?.name) &&
|
|
621
|
-
!Object.hasOwn(item, 'subpath');
|
|
622
|
-
if (!item?.name || (!sourceOnly && typeof item?.subpath !== 'string')) {
|
|
623
|
-
diagnostics.push(
|
|
624
|
-
`${exportAt}: every export requires name and subpath (only a private application, package.json#private, may omit subpath)`,
|
|
625
|
-
);
|
|
626
|
-
continue;
|
|
627
|
-
}
|
|
628
|
-
const key = sourceOnly
|
|
629
|
-
? `source:${item.name}`
|
|
630
|
-
: `${item.subpath}:${item.name}`;
|
|
631
|
-
if (seen.has(key))
|
|
632
|
-
diagnostics.push(
|
|
633
|
-
`${exportAt}: duplicate ${sourceOnly ? item.name : key}`,
|
|
634
|
-
);
|
|
635
|
-
seen.add(key);
|
|
636
|
-
if (sourceOnly) {
|
|
637
|
-
if (
|
|
638
|
-
existsSync(sourcePath) &&
|
|
639
|
-
!exportedNamesFromFile(sourcePath).has(item.name)
|
|
640
|
-
)
|
|
641
|
-
diagnostics.push(
|
|
642
|
-
`${exportAt}: ${item.name} is not exported by ${component.source}`,
|
|
643
|
-
);
|
|
644
|
-
continue;
|
|
645
|
-
}
|
|
646
|
-
if (!(item.subpath in exports))
|
|
647
|
-
diagnostics.push(
|
|
648
|
-
`${exportAt}: package.json does not export subpath ${item.subpath}`,
|
|
649
|
-
);
|
|
650
|
-
const sourceEntry = sourceEntryForExport(
|
|
651
|
-
packageRoot,
|
|
652
|
-
exports[item.subpath],
|
|
653
|
-
);
|
|
654
|
-
if (!sourceEntry)
|
|
655
|
-
diagnostics.push(
|
|
656
|
-
`${exportAt}: cannot map ${item.subpath} to an existing src/ entry`,
|
|
657
|
-
);
|
|
658
|
-
else if (!exportedNamesFromFile(sourceEntry).has(item.name))
|
|
659
|
-
diagnostics.push(
|
|
660
|
-
`${exportAt}: ${item.name} is not exported by ${relative(packageRoot, sourceEntry)}`,
|
|
661
|
-
);
|
|
662
|
-
}
|
|
663
|
-
}
|
|
664
|
-
validateStringList(component.sourceLinks, `${at}.sourceLinks`, diagnostics, {
|
|
665
|
-
nonempty: true,
|
|
666
|
-
});
|
|
667
|
-
for (const link of component.sourceLinks ?? []) {
|
|
668
|
-
if (/^[a-z]+:/i.test(link)) continue;
|
|
669
|
-
const localPath = link.split('#')[0];
|
|
670
|
-
if (localPath && !existsSync(resolve(packageRoot, localPath)))
|
|
671
|
-
diagnostics.push(
|
|
672
|
-
`${at}.sourceLinks: local source link does not exist: ${link}`,
|
|
673
|
-
);
|
|
674
|
-
}
|
|
675
|
-
validateComposition(component, at, diagnostics, packageRoot);
|
|
676
|
-
validateStringList(
|
|
677
|
-
component.boundaries?.publicClasses,
|
|
678
|
-
`${at}.boundaries.publicClasses`,
|
|
679
|
-
diagnostics,
|
|
680
|
-
);
|
|
681
|
-
if (
|
|
682
|
-
component.boundaries?.rootClass !== null &&
|
|
683
|
-
(typeof component.boundaries?.rootClass !== 'string' ||
|
|
684
|
-
!component.boundaries.publicClasses?.includes(
|
|
685
|
-
component.boundaries.rootClass,
|
|
686
|
-
))
|
|
687
|
-
)
|
|
688
|
-
diagnostics.push(
|
|
689
|
-
`${at}.boundaries.rootClass: expected null or one publicClasses entry`,
|
|
690
|
-
);
|
|
691
|
-
// An application may place only the declared subset of public classes on
|
|
692
|
-
// its own elements; every other public class is the component's rendered
|
|
693
|
-
// anatomy (eslint-plugin-kerfjs KUI-L103). `rootElement` names the element
|
|
694
|
-
// the component renders around its placeable classes.
|
|
695
|
-
if (component.boundaries && 'placeableClasses' in component.boundaries) {
|
|
696
|
-
validateStringList(
|
|
697
|
-
component.boundaries.placeableClasses,
|
|
698
|
-
`${at}.boundaries.placeableClasses`,
|
|
699
|
-
diagnostics,
|
|
700
|
-
);
|
|
701
|
-
for (const className of Array.isArray(component.boundaries.placeableClasses)
|
|
702
|
-
? component.boundaries.placeableClasses
|
|
703
|
-
: [])
|
|
704
|
-
if (!component.boundaries.publicClasses?.includes(className))
|
|
705
|
-
diagnostics.push(
|
|
706
|
-
`${at}.boundaries.placeableClasses: ${className} is not one of publicClasses`,
|
|
707
|
-
);
|
|
708
|
-
}
|
|
709
|
-
if (
|
|
710
|
-
component.boundaries?.rootElement !== undefined &&
|
|
711
|
-
!component.boundaries.placeableClasses?.length
|
|
712
|
-
)
|
|
713
|
-
diagnostics.push(
|
|
714
|
-
`${at}.boundaries.rootElement: requires a non-empty placeableClasses`,
|
|
715
|
-
);
|
|
716
|
-
validateStringList(
|
|
717
|
-
component.boundaries?.publicTokens,
|
|
718
|
-
`${at}.boundaries.publicTokens`,
|
|
719
|
-
diagnostics,
|
|
720
|
-
);
|
|
721
|
-
validateStringList(
|
|
722
|
-
component.accessibility?.obligations,
|
|
723
|
-
`${at}.accessibility.obligations`,
|
|
724
|
-
diagnostics,
|
|
725
|
-
);
|
|
726
|
-
if (!Array.isArray(component.diagnostics))
|
|
727
|
-
diagnostics.push(`${at}.diagnostics: expected an array`);
|
|
728
|
-
for (const diagnostic of component.diagnostics ?? []) {
|
|
729
|
-
if (
|
|
730
|
-
!/^KUI-C[0-9]{3}$/.test(diagnostic?.id ?? '') ||
|
|
731
|
-
diagnosticIds.has(diagnostic.id)
|
|
732
|
-
)
|
|
733
|
-
diagnostics.push(`${at}.diagnostics: ids must be unique KUI-C### values`);
|
|
734
|
-
diagnosticIds.add(diagnostic?.id);
|
|
735
|
-
if (!['error', 'warning'].includes(diagnostic?.severity))
|
|
736
|
-
diagnostics.push(
|
|
737
|
-
`${at}.diagnostics.${diagnostic?.id}.severity: invalid severity`,
|
|
738
|
-
);
|
|
739
|
-
if (!diagnostic?.when || !diagnostic?.message)
|
|
740
|
-
diagnostics.push(
|
|
741
|
-
`${at}.diagnostics.${diagnostic?.id}: when and message are required`,
|
|
742
|
-
);
|
|
743
|
-
}
|
|
744
|
-
}
|
|
745
|
-
|
|
746
|
-
function toEntry(packageName, component) {
|
|
747
|
-
return {
|
|
748
|
-
key: `${packageName}:${component.id}`,
|
|
749
|
-
package: packageName,
|
|
750
|
-
id: component.id,
|
|
751
|
-
name: component.name,
|
|
752
|
-
kind: component.kind,
|
|
753
|
-
purpose: component.purpose,
|
|
754
|
-
publicExports: component.publicExports,
|
|
755
|
-
sourceLinks: component.sourceLinks,
|
|
756
|
-
source: component.source,
|
|
757
|
-
...(component.composition.rendersAs
|
|
758
|
-
? { rendersAs: component.composition.rendersAs }
|
|
759
|
-
: {}),
|
|
760
|
-
...(component.composition.insetFreeChildren
|
|
761
|
-
? { insetFreeChildren: component.composition.insetFreeChildren }
|
|
762
|
-
: {}),
|
|
763
|
-
...(component.composition.insetFreeWhen
|
|
764
|
-
? { insetFreeWhen: component.composition.insetFreeWhen }
|
|
765
|
-
: {}),
|
|
766
|
-
...(component.composition.content
|
|
767
|
-
? { content: component.composition.content }
|
|
768
|
-
: {}),
|
|
769
|
-
parents: component.composition.parents,
|
|
770
|
-
contexts: component.composition.contexts,
|
|
771
|
-
zones: component.composition.zones,
|
|
772
|
-
children: component.composition.children,
|
|
773
|
-
state: component.composition.state,
|
|
774
|
-
wiring: component.composition.wiring,
|
|
775
|
-
responsive: component.composition.responsive,
|
|
776
|
-
layout: component.composition.layout,
|
|
777
|
-
accessibility: component.accessibility,
|
|
778
|
-
boundaries: component.boundaries,
|
|
779
|
-
diagnostics: component.diagnostics,
|
|
780
|
-
provenance: component.provenance,
|
|
781
|
-
};
|
|
782
|
-
}
|
|
783
|
-
|
|
784
|
-
// The catalog keys a package's `rendersAs` may name: entries generated in this
|
|
785
|
-
// run, or those of an installed dependency's shipped catalog (its
|
|
786
|
-
// package.json#kerfComponentCatalog.output, or @kerfjs/ui's ai/ catalog).
|
|
787
|
-
function dependencyCatalogKeys(packageRoot, packageName, cache) {
|
|
788
|
-
const cacheKey = `${packageRoot}\0${packageName}`;
|
|
789
|
-
if (cache.has(cacheKey)) return cache.get(cacheKey);
|
|
790
|
-
let keys;
|
|
791
|
-
for (let dir = packageRoot; ; dir = dirname(dir)) {
|
|
792
|
-
const manifestPath = join(dir, 'node_modules', packageName, 'package.json');
|
|
793
|
-
if (existsSync(manifestPath)) {
|
|
794
|
-
const dependencyRoot = dirname(manifestPath);
|
|
795
|
-
let catalogPath;
|
|
796
|
-
try {
|
|
797
|
-
const manifest = JSON.parse(readFileSync(manifestPath, 'utf8'));
|
|
798
|
-
if (manifest[CONFIG_KEY]?.output)
|
|
799
|
-
catalogPath = resolve(dependencyRoot, manifest[CONFIG_KEY].output);
|
|
800
|
-
} catch {
|
|
801
|
-
// An unreadable manifest leaves the package unresolved below.
|
|
802
|
-
}
|
|
803
|
-
catalogPath ??= join(dependencyRoot, 'ai', 'component-composition.json');
|
|
804
|
-
try {
|
|
805
|
-
const catalog = JSON.parse(readFileSync(catalogPath, 'utf8'));
|
|
806
|
-
keys = new Set(
|
|
807
|
-
(catalog.entries ?? []).map(
|
|
808
|
-
(entry) => entry.key ?? `${catalog.package}:${entry.id}`,
|
|
809
|
-
),
|
|
810
|
-
);
|
|
811
|
-
} catch {
|
|
812
|
-
// No readable catalog: the package stays unresolved.
|
|
813
|
-
}
|
|
814
|
-
break;
|
|
815
|
-
}
|
|
816
|
-
if (dirname(dir) === dir) break;
|
|
817
|
-
}
|
|
818
|
-
cache.set(cacheKey, keys);
|
|
819
|
-
return keys;
|
|
820
|
-
}
|
|
821
|
-
|
|
822
|
-
function validateRendersAs(results, diagnostics) {
|
|
823
|
-
const generated = new Map(
|
|
824
|
-
results.map(({ catalog }) => [
|
|
825
|
-
catalog.package,
|
|
826
|
-
new Set(catalog.entries.map((entry) => entry.key)),
|
|
827
|
-
]),
|
|
828
|
-
);
|
|
829
|
-
const cache = new Map();
|
|
830
|
-
for (const { packageRoot, metadataPath, catalog } of results)
|
|
831
|
-
for (const entry of catalog.entries)
|
|
832
|
-
for (const root of entry.rendersAs ?? []) {
|
|
833
|
-
const at = `${metadataPath}: components.${entry.id}.composition.rendersAs`;
|
|
834
|
-
const rootPackage = root.slice(0, root.lastIndexOf(':'));
|
|
835
|
-
if (root === entry.key) {
|
|
836
|
-
diagnostics.push(`${at}: ${root} names the component itself`);
|
|
837
|
-
continue;
|
|
838
|
-
}
|
|
839
|
-
const keys =
|
|
840
|
-
generated.get(rootPackage) ??
|
|
841
|
-
dependencyCatalogKeys(packageRoot, rootPackage, cache);
|
|
842
|
-
if (!keys)
|
|
843
|
-
diagnostics.push(
|
|
844
|
-
`${at}: cannot resolve a component catalog for package ${rootPackage}; install it or generate its catalog`,
|
|
845
|
-
);
|
|
846
|
-
else if (!keys.has(root))
|
|
847
|
-
diagnostics.push(
|
|
848
|
-
`${at}: ${root} is not an entry in ${rootPackage}'s catalog`,
|
|
849
|
-
);
|
|
850
|
-
}
|
|
851
|
-
}
|
|
852
|
-
|
|
853
|
-
// The selection catalog (the @kerfjs/ui component-catalog extension) shares
|
|
854
|
-
// identity, purpose, boundaries, and geometry with the composition metadata,
|
|
855
|
-
// so only the selection-only facts live in each component's `selection` block.
|
|
856
|
-
function selectionGeometry(component) {
|
|
857
|
-
if (component.selection?.geometry) return component.selection.geometry;
|
|
858
|
-
const { notes, ...owners } = component.composition.layout.geometry;
|
|
859
|
-
return notes?.length ? { ...owners, notes } : owners;
|
|
860
|
-
}
|
|
861
|
-
|
|
862
|
-
function toSelectionEntry(component) {
|
|
863
|
-
const { selection, boundaries } = component;
|
|
864
|
-
return {
|
|
865
|
-
id: component.id,
|
|
866
|
-
name: component.name,
|
|
867
|
-
kind: component.kind,
|
|
868
|
-
purpose: component.purpose,
|
|
869
|
-
useWhen: selection.useWhen,
|
|
870
|
-
avoidWhen: selection.avoidWhen,
|
|
871
|
-
publicClasses: boundaries.publicClasses,
|
|
872
|
-
publicTokens: boundaries.publicTokens,
|
|
873
|
-
...(selection.uses ? { uses: selection.uses } : {}),
|
|
874
|
-
source: component.source,
|
|
875
|
-
...(selection.styleSources ? { styleSources: selection.styleSources } : {}),
|
|
876
|
-
...(selection.demoSource ? { demoSource: selection.demoSource } : {}),
|
|
877
|
-
geometry: selectionGeometry(component),
|
|
878
|
-
...(selection.documentation
|
|
879
|
-
? { documentation: selection.documentation }
|
|
880
|
-
: {}),
|
|
881
|
-
};
|
|
882
|
-
}
|
|
883
|
-
|
|
884
|
-
// The @kerfjs/ui selection catalog ids an installed copy ships, which a
|
|
885
|
-
// selection entry's `uses` may name alongside the package's own ids.
|
|
886
|
-
function kerfSelectionIds(packageRoot) {
|
|
887
|
-
for (let dir = packageRoot; ; dir = dirname(dir)) {
|
|
888
|
-
const catalogPath = join(
|
|
889
|
-
dir,
|
|
890
|
-
'node_modules',
|
|
891
|
-
'@kerfjs',
|
|
892
|
-
'ui',
|
|
893
|
-
'ai',
|
|
894
|
-
'component-catalog.json',
|
|
895
|
-
);
|
|
896
|
-
if (existsSync(catalogPath)) {
|
|
897
|
-
try {
|
|
898
|
-
const catalog = JSON.parse(readFileSync(catalogPath, 'utf8'));
|
|
899
|
-
return new Set((catalog.entries ?? []).map((entry) => entry.id));
|
|
900
|
-
} catch {
|
|
901
|
-
return null;
|
|
902
|
-
}
|
|
903
|
-
}
|
|
904
|
-
if (dirname(dir) === dir) return null;
|
|
905
|
-
}
|
|
906
|
-
}
|
|
907
|
-
|
|
908
|
-
function localPathExists(packageRoot, link) {
|
|
909
|
-
if (/^[a-z]+:/i.test(link)) return true;
|
|
910
|
-
const localPath = link.split('#')[0];
|
|
911
|
-
return !localPath || existsSync(resolve(packageRoot, localPath));
|
|
912
|
-
}
|
|
913
|
-
|
|
914
|
-
function posixRelative(from, to) {
|
|
915
|
-
const path = relative(from, to).split(sep).join('/');
|
|
916
|
-
return path.startsWith('.') ? path : `./${path}`;
|
|
917
|
-
}
|
|
918
|
-
|
|
919
|
-
function buildSelectionCatalog({
|
|
920
|
-
packageRoot,
|
|
921
|
-
packageJson,
|
|
922
|
-
metadata,
|
|
923
|
-
metadataPath,
|
|
924
|
-
outputPath,
|
|
925
|
-
selectionOutputPath,
|
|
926
|
-
diagnostics,
|
|
927
|
-
}) {
|
|
928
|
-
const at = metadataPath;
|
|
929
|
-
const selected = metadata.components.filter(
|
|
930
|
-
(component) => component.kind !== 'recipe',
|
|
931
|
-
);
|
|
932
|
-
if (!selectionOutputPath) {
|
|
933
|
-
if (metadata.description !== undefined)
|
|
934
|
-
diagnostics.push(
|
|
935
|
-
`${at}: description is emitted only to a selection catalog; configure package.json#${CONFIG_KEY}.selectionOutput or remove it`,
|
|
936
|
-
);
|
|
937
|
-
for (const component of metadata.components)
|
|
938
|
-
if (component.selection)
|
|
939
|
-
diagnostics.push(
|
|
940
|
-
`${at}: components.${component.id}.selection is emitted only to a selection catalog; configure package.json#${CONFIG_KEY}.selectionOutput or remove it`,
|
|
941
|
-
);
|
|
942
|
-
return null;
|
|
943
|
-
}
|
|
944
|
-
|
|
945
|
-
if (!metadata.description)
|
|
946
|
-
diagnostics.push(
|
|
947
|
-
`${at}.description: author decision required; the selection catalog needs a package description`,
|
|
948
|
-
);
|
|
949
|
-
const derived = posixRelative(dirname(outputPath), selectionOutputPath);
|
|
950
|
-
if (
|
|
951
|
-
metadata.componentCatalog !== undefined &&
|
|
952
|
-
metadata.componentCatalog !== 'not-applicable' &&
|
|
953
|
-
metadata.componentCatalog !== derived
|
|
954
|
-
)
|
|
955
|
-
diagnostics.push(
|
|
956
|
-
`${at}.componentCatalog: ${metadata.componentCatalog} disagrees with the configured selectionOutput (${derived}); remove it or set it to "not-applicable"`,
|
|
957
|
-
);
|
|
958
|
-
|
|
959
|
-
const ownIds = new Set(selected.map((component) => component.id));
|
|
960
|
-
let kerfIds;
|
|
961
|
-
for (const component of metadata.components) {
|
|
962
|
-
const componentAt = `${at}: components.${component.id}.selection`;
|
|
963
|
-
if (component.kind === 'recipe') {
|
|
964
|
-
if (component.selection)
|
|
965
|
-
diagnostics.push(
|
|
966
|
-
`${componentAt}: recipes are not selection catalog entries; remove it`,
|
|
967
|
-
);
|
|
968
|
-
continue;
|
|
969
|
-
}
|
|
970
|
-
if (!component.selection) {
|
|
971
|
-
diagnostics.push(
|
|
972
|
-
`${componentAt}: author decision required; selectionOutput is configured, so every component and composition needs useWhen and avoidWhen`,
|
|
973
|
-
);
|
|
974
|
-
continue;
|
|
975
|
-
}
|
|
976
|
-
const { selection } = component;
|
|
977
|
-
if (
|
|
978
|
-
!selection.geometry &&
|
|
979
|
-
Object.values(component.composition.layout.geometry).includes('composed')
|
|
980
|
-
)
|
|
981
|
-
diagnostics.push(
|
|
982
|
-
`${componentAt}.geometry: required because composition.layout.geometry uses "composed", which the selection catalog cannot express`,
|
|
983
|
-
);
|
|
984
|
-
for (const path of [
|
|
985
|
-
...(selection.styleSources ?? []),
|
|
986
|
-
...(selection.demoSource ? [selection.demoSource] : []),
|
|
987
|
-
...(selection.documentation ? [selection.documentation] : []),
|
|
988
|
-
])
|
|
989
|
-
if (!localPathExists(packageRoot, path))
|
|
990
|
-
diagnostics.push(`${componentAt}: local path does not exist: ${path}`);
|
|
991
|
-
for (const use of selection.uses ?? []) {
|
|
992
|
-
if (use === component.id)
|
|
993
|
-
diagnostics.push(
|
|
994
|
-
`${componentAt}.uses: ${use} names the component itself`,
|
|
995
|
-
);
|
|
996
|
-
else if (!ownIds.has(use)) {
|
|
997
|
-
kerfIds ??= kerfSelectionIds(packageRoot);
|
|
998
|
-
if (!kerfIds)
|
|
999
|
-
diagnostics.push(
|
|
1000
|
-
`${componentAt}.uses: cannot resolve ${use}; install @kerfjs/ui so its selection catalog ids can be checked`,
|
|
1001
|
-
);
|
|
1002
|
-
else if (!kerfIds.has(use))
|
|
1003
|
-
diagnostics.push(
|
|
1004
|
-
`${componentAt}.uses: ${use} is neither a component in this package nor a @kerfjs/ui catalog entry`,
|
|
1005
|
-
);
|
|
1006
|
-
}
|
|
1007
|
-
}
|
|
1008
|
-
}
|
|
1009
|
-
if (ownIds.size) {
|
|
1010
|
-
kerfIds ??= kerfSelectionIds(packageRoot);
|
|
1011
|
-
for (const id of ownIds)
|
|
1012
|
-
if (kerfIds?.has(id))
|
|
1013
|
-
diagnostics.push(
|
|
1014
|
-
`${at}: components.${id}.id duplicates a @kerfjs/ui catalog id, which the selection catalog cannot disambiguate`,
|
|
1015
|
-
);
|
|
1016
|
-
}
|
|
1017
|
-
|
|
1018
|
-
const catalog = {
|
|
1019
|
-
$schema:
|
|
1020
|
-
'https://raw.githubusercontent.com/brianwestphal/kerf/main/ui/ai/component-catalog-extension.schema.json',
|
|
1021
|
-
schemaVersion: 1,
|
|
1022
|
-
package: packageJson.name,
|
|
1023
|
-
description: metadata.description,
|
|
1024
|
-
entries: selected
|
|
1025
|
-
.filter((component) => component.selection)
|
|
1026
|
-
.sort((a, b) => a.id.localeCompare(b.id))
|
|
1027
|
-
.map(toSelectionEntry),
|
|
1028
|
-
};
|
|
1029
|
-
diagnostics.push(
|
|
1030
|
-
...validateDocumentAgainstSchema(
|
|
1031
|
-
catalog,
|
|
1032
|
-
selectionSchema,
|
|
1033
|
-
selectionOutputPath,
|
|
1034
|
-
),
|
|
1035
|
-
);
|
|
1036
|
-
return catalog;
|
|
1037
|
-
}
|
|
1038
|
-
|
|
1039
|
-
export function generateCatalogs(root = process.cwd()) {
|
|
1040
|
-
const diagnostics = [];
|
|
1041
|
-
const packages = configuredPackages(resolve(root), diagnostics);
|
|
1042
|
-
if (!packages.length && !diagnostics.length)
|
|
1043
|
-
diagnostics.push(
|
|
1044
|
-
`${relative(process.cwd(), resolve(root)) || '.'}: no package declares package.json#${CONFIG_KEY}`,
|
|
1045
|
-
);
|
|
1046
|
-
const results = [];
|
|
1047
|
-
const packageNames = new Set();
|
|
1048
|
-
for (const packageRoot of packages) {
|
|
1049
|
-
const packagePath = join(packageRoot, 'package.json');
|
|
1050
|
-
const packageJson = readJson(packagePath, packagePath, diagnostics);
|
|
1051
|
-
if (!packageJson) continue;
|
|
1052
|
-
const config = packageJson[CONFIG_KEY];
|
|
1053
|
-
if (!packageJson.name)
|
|
1054
|
-
diagnostics.push(`${packagePath}: package name is required`);
|
|
1055
|
-
if (packageNames.has(packageJson.name))
|
|
1056
|
-
diagnostics.push(
|
|
1057
|
-
`${packagePath}: duplicate configured package name ${packageJson.name}`,
|
|
1058
|
-
);
|
|
1059
|
-
packageNames.add(packageJson.name);
|
|
1060
|
-
for (const key of ['source', 'output'])
|
|
1061
|
-
if (!config?.[key])
|
|
1062
|
-
diagnostics.push(`${packagePath}#${CONFIG_KEY}.${key}: is required`);
|
|
1063
|
-
if (
|
|
1064
|
-
config?.selectionOutput !== undefined &&
|
|
1065
|
-
(typeof config.selectionOutput !== 'string' ||
|
|
1066
|
-
!config.selectionOutput ||
|
|
1067
|
-
resolve(packageRoot, config.selectionOutput) ===
|
|
1068
|
-
resolve(packageRoot, config.output ?? ''))
|
|
1069
|
-
)
|
|
1070
|
-
diagnostics.push(
|
|
1071
|
-
`${packagePath}#${CONFIG_KEY}.selectionOutput: expected a path distinct from output`,
|
|
1072
|
-
);
|
|
1073
|
-
if (!config?.source || !config?.output) continue;
|
|
1074
|
-
const metadataPath = resolve(packageRoot, config.source);
|
|
1075
|
-
const metadata = readJson(metadataPath, metadataPath, diagnostics);
|
|
1076
|
-
if (!metadata) continue;
|
|
1077
|
-
const metadataDiagnostics = validateDocumentAgainstSchema(
|
|
1078
|
-
metadata,
|
|
1079
|
-
metadataSchema,
|
|
1080
|
-
metadataPath,
|
|
1081
|
-
);
|
|
1082
|
-
diagnostics.push(...metadataDiagnostics);
|
|
1083
|
-
if (metadataDiagnostics.length) {
|
|
1084
|
-
continue;
|
|
1085
|
-
}
|
|
1086
|
-
const ids = new Set();
|
|
1087
|
-
const diagnosticIds = new Set();
|
|
1088
|
-
for (const component of metadata.components)
|
|
1089
|
-
validateComponent(
|
|
1090
|
-
component,
|
|
1091
|
-
packageRoot,
|
|
1092
|
-
packageJson,
|
|
1093
|
-
ids,
|
|
1094
|
-
diagnosticIds,
|
|
1095
|
-
diagnostics,
|
|
1096
|
-
);
|
|
1097
|
-
const entries = [...metadata.components]
|
|
1098
|
-
.sort((a, b) => a.id.localeCompare(b.id))
|
|
1099
|
-
.map((component) => toEntry(packageJson.name, component));
|
|
1100
|
-
const outputPath = resolve(packageRoot, config.output);
|
|
1101
|
-
const selectionOutputPath =
|
|
1102
|
-
typeof config.selectionOutput === 'string' && config.selectionOutput
|
|
1103
|
-
? resolve(packageRoot, config.selectionOutput)
|
|
1104
|
-
: null;
|
|
1105
|
-
const selection = buildSelectionCatalog({
|
|
1106
|
-
packageRoot,
|
|
1107
|
-
packageJson,
|
|
1108
|
-
metadata,
|
|
1109
|
-
metadataPath,
|
|
1110
|
-
outputPath,
|
|
1111
|
-
selectionOutputPath,
|
|
1112
|
-
diagnostics,
|
|
1113
|
-
});
|
|
1114
|
-
const componentCatalog = selectionOutputPath
|
|
1115
|
-
? posixRelative(dirname(outputPath), selectionOutputPath)
|
|
1116
|
-
: (metadata.componentCatalog ?? 'not-applicable');
|
|
1117
|
-
const catalog = {
|
|
1118
|
-
$schema:
|
|
1119
|
-
'https://raw.githubusercontent.com/brianwestphal/kerf/main/ui/ai/component-composition-extension.schema.json',
|
|
1120
|
-
schemaVersion: 1,
|
|
1121
|
-
package: packageJson.name,
|
|
1122
|
-
compatibility: {
|
|
1123
|
-
componentCatalog,
|
|
1124
|
-
identity: 'package:id',
|
|
1125
|
-
},
|
|
1126
|
-
entries,
|
|
1127
|
-
};
|
|
1128
|
-
diagnostics.push(
|
|
1129
|
-
...validateDocumentAgainstSchema(catalog, catalogSchema, outputPath),
|
|
1130
|
-
);
|
|
1131
|
-
results.push({
|
|
1132
|
-
packageRoot,
|
|
1133
|
-
metadataPath,
|
|
1134
|
-
outputPath,
|
|
1135
|
-
catalog,
|
|
1136
|
-
...(selection ? { selectionOutputPath, selection } : {}),
|
|
1137
|
-
});
|
|
1138
|
-
}
|
|
1139
|
-
validateRendersAs(results, diagnostics);
|
|
1140
|
-
if (diagnostics.length) throw new CatalogError(diagnostics.sort());
|
|
1141
|
-
return results;
|
|
1142
|
-
}
|
|
1143
|
-
|
|
1144
|
-
export function formatCatalog(catalog) {
|
|
1145
|
-
return `${JSON.stringify(catalog, null, 2)}\n`;
|
|
1146
|
-
}
|
|
1147
|
-
|
|
1148
|
-
function checkOutput(path, catalog, schema, label, diagnostics) {
|
|
1149
|
-
if (!existsSync(path)) {
|
|
1150
|
-
diagnostics.push(`${path}: generated ${label} is missing`);
|
|
1151
|
-
return;
|
|
1152
|
-
}
|
|
1153
|
-
const actualSource = readFileSync(path, 'utf8');
|
|
1154
|
-
const actual = readJson(path, path, diagnostics);
|
|
1155
|
-
if (actual)
|
|
1156
|
-
diagnostics.push(...validateDocumentAgainstSchema(actual, schema, path));
|
|
1157
|
-
if (actualSource !== formatCatalog(catalog))
|
|
1158
|
-
diagnostics.push(
|
|
1159
|
-
`${path}: generated ${label} is stale; run catalog:generate`,
|
|
1160
|
-
);
|
|
1161
|
-
}
|
|
1162
|
-
|
|
1163
|
-
export function runCatalogCommand({
|
|
1164
|
-
root = process.cwd(),
|
|
1165
|
-
check = false,
|
|
1166
|
-
} = {}) {
|
|
1167
|
-
const results = generateCatalogs(root);
|
|
1168
|
-
const diagnostics = [];
|
|
1169
|
-
for (const result of results) {
|
|
1170
|
-
const outputs = [
|
|
1171
|
-
[result.outputPath, result.catalog, catalogSchema, 'catalog'],
|
|
1172
|
-
];
|
|
1173
|
-
if (result.selection)
|
|
1174
|
-
outputs.push([
|
|
1175
|
-
result.selectionOutputPath,
|
|
1176
|
-
result.selection,
|
|
1177
|
-
selectionSchema,
|
|
1178
|
-
'selection catalog',
|
|
1179
|
-
]);
|
|
1180
|
-
for (const [path, catalog, schema, label] of outputs) {
|
|
1181
|
-
if (check) checkOutput(path, catalog, schema, label, diagnostics);
|
|
1182
|
-
else writeFileSync(path, formatCatalog(catalog));
|
|
1183
|
-
}
|
|
1184
|
-
}
|
|
1185
|
-
if (diagnostics.length) throw new CatalogError(diagnostics);
|
|
1186
|
-
return results;
|
|
1187
|
-
}
|
|
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';
|
|
1188
26
|
|
|
1189
27
|
function parseArgs(argv) {
|
|
1190
28
|
let root = process.cwd();
|