create-kerf-component 5.0.0-beta.5 → 5.0.0-beta.50
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 +29 -1
- package/catalog.js +773 -0
- package/component-catalog-v2.schema.json +298 -0
- package/component-metadata.schema.json +229 -0
- package/index.js +29 -3
- package/package.json +9 -2
- package/template/README.md +35 -9
- package/template/component-catalog-v2.json +142 -0
- package/template/kerf.components.json +71 -0
- package/template/package.json +10 -2
- package/template/src/counter.tsx +4 -1
package/catalog.js
ADDED
|
@@ -0,0 +1,773 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
existsSync,
|
|
5
|
+
readFileSync,
|
|
6
|
+
realpathSync,
|
|
7
|
+
readdirSync,
|
|
8
|
+
statSync,
|
|
9
|
+
writeFileSync,
|
|
10
|
+
} from 'node:fs';
|
|
11
|
+
import { dirname, join, relative, resolve, sep } from 'node:path';
|
|
12
|
+
import process from 'node:process';
|
|
13
|
+
import { createRequire } from 'node:module';
|
|
14
|
+
import { fileURLToPath } from 'node:url';
|
|
15
|
+
|
|
16
|
+
const CONFIG_KEY = 'kerfComponentCatalog';
|
|
17
|
+
const ignoredDirectories = new Set(['.git', 'dist', 'node_modules']);
|
|
18
|
+
const scriptRoot = dirname(fileURLToPath(import.meta.url));
|
|
19
|
+
const metadataSchema = JSON.parse(
|
|
20
|
+
readFileSync(join(scriptRoot, 'component-metadata.schema.json'), 'utf8'),
|
|
21
|
+
);
|
|
22
|
+
const catalogSchema = JSON.parse(
|
|
23
|
+
readFileSync(join(scriptRoot, 'component-catalog-v2.schema.json'), 'utf8'),
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
export class CatalogError extends Error {
|
|
27
|
+
constructor(diagnostics) {
|
|
28
|
+
super(diagnostics.join('\n'));
|
|
29
|
+
this.name = 'CatalogError';
|
|
30
|
+
this.diagnostics = diagnostics;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function readJson(path, label, diagnostics) {
|
|
35
|
+
try {
|
|
36
|
+
return JSON.parse(readFileSync(path, 'utf8'));
|
|
37
|
+
} catch (error) {
|
|
38
|
+
diagnostics.push(`${label}: ${error.message}`);
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function schemaReference(rootSchema, reference) {
|
|
44
|
+
if (!reference.startsWith('#/'))
|
|
45
|
+
throw new Error(`unsupported schema reference ${reference}`);
|
|
46
|
+
return reference
|
|
47
|
+
.slice(2)
|
|
48
|
+
.split('/')
|
|
49
|
+
.map((part) => part.replaceAll('~1', '/').replaceAll('~0', '~'))
|
|
50
|
+
.reduce((value, part) => value?.[part], rootSchema);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function schemaTypeMatches(value, type) {
|
|
54
|
+
if (type === 'array') return Array.isArray(value);
|
|
55
|
+
if (type === 'object')
|
|
56
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
57
|
+
if (type === 'integer') return Number.isInteger(value);
|
|
58
|
+
return typeof value === type;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function validateSchemaValue(value, rule, path, rootSchema, diagnostics) {
|
|
62
|
+
if (rule.$ref) {
|
|
63
|
+
const referenced = schemaReference(rootSchema, rule.$ref);
|
|
64
|
+
if (!referenced) {
|
|
65
|
+
diagnostics.push(`${path}: schema reference ${rule.$ref} does not exist`);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
validateSchemaValue(value, referenced, path, rootSchema, diagnostics);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (rule.allOf) {
|
|
72
|
+
for (const candidate of rule.allOf)
|
|
73
|
+
validateSchemaValue(value, candidate, path, rootSchema, diagnostics);
|
|
74
|
+
}
|
|
75
|
+
if (rule.anyOf) {
|
|
76
|
+
const alternatives = rule.anyOf.map((candidate) => {
|
|
77
|
+
const failures = [];
|
|
78
|
+
validateSchemaValue(value, candidate, path, rootSchema, failures);
|
|
79
|
+
return failures;
|
|
80
|
+
});
|
|
81
|
+
if (alternatives.every((failures) => failures.length))
|
|
82
|
+
diagnostics.push(`${path}: does not match any allowed schema variant`);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
if (rule.const !== undefined && value !== rule.const)
|
|
86
|
+
diagnostics.push(`${path}: expected ${JSON.stringify(rule.const)}`);
|
|
87
|
+
if (rule.enum && !rule.enum.includes(value))
|
|
88
|
+
diagnostics.push(
|
|
89
|
+
`${path}: expected one of ${rule.enum.map((item) => JSON.stringify(item)).join(', ')}`,
|
|
90
|
+
);
|
|
91
|
+
if (rule.type && !schemaTypeMatches(value, rule.type)) {
|
|
92
|
+
diagnostics.push(`${path}: expected ${rule.type}`);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
if (typeof value === 'string') {
|
|
96
|
+
if (rule.minLength !== undefined && value.length < rule.minLength)
|
|
97
|
+
diagnostics.push(
|
|
98
|
+
`${path}: expected at least ${rule.minLength} character(s)`,
|
|
99
|
+
);
|
|
100
|
+
if (rule.pattern && !new RegExp(rule.pattern).test(value))
|
|
101
|
+
diagnostics.push(`${path}: must match ${rule.pattern}`);
|
|
102
|
+
}
|
|
103
|
+
if (typeof value === 'number' && rule.minimum !== undefined) {
|
|
104
|
+
if (value < rule.minimum)
|
|
105
|
+
diagnostics.push(`${path}: expected a value >= ${rule.minimum}`);
|
|
106
|
+
}
|
|
107
|
+
if (Array.isArray(value)) {
|
|
108
|
+
if (rule.minItems !== undefined && value.length < rule.minItems)
|
|
109
|
+
diagnostics.push(`${path}: expected at least ${rule.minItems} item(s)`);
|
|
110
|
+
if (rule.uniqueItems) {
|
|
111
|
+
const serialized = value.map((item) => JSON.stringify(item));
|
|
112
|
+
if (new Set(serialized).size !== serialized.length)
|
|
113
|
+
diagnostics.push(`${path}: expected unique items`);
|
|
114
|
+
}
|
|
115
|
+
if (rule.items)
|
|
116
|
+
value.forEach((item, index) =>
|
|
117
|
+
validateSchemaValue(
|
|
118
|
+
item,
|
|
119
|
+
rule.items,
|
|
120
|
+
`${path}[${index}]`,
|
|
121
|
+
rootSchema,
|
|
122
|
+
diagnostics,
|
|
123
|
+
),
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
if (value !== null && typeof value === 'object' && !Array.isArray(value)) {
|
|
127
|
+
for (const key of rule.required ?? [])
|
|
128
|
+
if (!(key in value)) diagnostics.push(`${path}.${key}: is required`);
|
|
129
|
+
const properties = rule.properties ?? {};
|
|
130
|
+
for (const [key, item] of Object.entries(value)) {
|
|
131
|
+
if (key in properties)
|
|
132
|
+
validateSchemaValue(
|
|
133
|
+
item,
|
|
134
|
+
properties[key],
|
|
135
|
+
`${path}.${key}`,
|
|
136
|
+
rootSchema,
|
|
137
|
+
diagnostics,
|
|
138
|
+
);
|
|
139
|
+
else if (rule.additionalProperties === false)
|
|
140
|
+
diagnostics.push(`${path}.${key}: additional property is not allowed`);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function validateDocumentAgainstSchema(value, schema, path) {
|
|
146
|
+
const diagnostics = [];
|
|
147
|
+
validateSchemaValue(value, schema, path, schema, diagnostics);
|
|
148
|
+
return diagnostics;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function expandWorkspace(root, pattern) {
|
|
152
|
+
const normalized = pattern.replaceAll('/', sep);
|
|
153
|
+
if (!normalized.includes('*')) return [resolve(root, normalized)];
|
|
154
|
+
const star = normalized.indexOf('*');
|
|
155
|
+
const parent = resolve(root, normalized.slice(0, star));
|
|
156
|
+
const suffix = normalized.slice(star + 1);
|
|
157
|
+
if (!existsSync(parent)) return [];
|
|
158
|
+
return readdirSync(parent, { withFileTypes: true })
|
|
159
|
+
.filter(
|
|
160
|
+
(entry) => entry.isDirectory() && !ignoredDirectories.has(entry.name),
|
|
161
|
+
)
|
|
162
|
+
.map((entry) => resolve(parent, entry.name + suffix))
|
|
163
|
+
.filter((path) => existsSync(path) && statSync(path).isDirectory());
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function configuredPackages(root, diagnostics) {
|
|
167
|
+
const rootPackagePath = join(root, 'package.json');
|
|
168
|
+
const rootPackage = readJson(rootPackagePath, rootPackagePath, diagnostics);
|
|
169
|
+
if (!rootPackage) return [];
|
|
170
|
+
const packages = rootPackage[CONFIG_KEY] ? [root] : [];
|
|
171
|
+
const workspacePatterns = Array.isArray(rootPackage.workspaces)
|
|
172
|
+
? rootPackage.workspaces
|
|
173
|
+
: (rootPackage.workspaces?.packages ?? []);
|
|
174
|
+
for (const pattern of workspacePatterns) {
|
|
175
|
+
for (const packageRoot of expandWorkspace(root, pattern)) {
|
|
176
|
+
const packagePath = join(packageRoot, 'package.json');
|
|
177
|
+
if (!existsSync(packagePath)) continue;
|
|
178
|
+
const packageJson = readJson(packagePath, packagePath, diagnostics);
|
|
179
|
+
if (packageJson?.[CONFIG_KEY]) packages.push(packageRoot);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return [...new Set(packages)].sort();
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function valueAt(value, path) {
|
|
186
|
+
return path.split('.').reduce((current, part) => current?.[part], value);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function requireDecision(component, path, at, diagnostics) {
|
|
190
|
+
const value = valueAt(component, path);
|
|
191
|
+
if (value === undefined || value === null || value === '') {
|
|
192
|
+
diagnostics.push(
|
|
193
|
+
`${at}.${path}: author decision required; the generator does not infer semantics or geometry from rendered appearance`,
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
let typescript;
|
|
199
|
+
|
|
200
|
+
function typescriptApi() {
|
|
201
|
+
if (typescript) return typescript;
|
|
202
|
+
try {
|
|
203
|
+
typescript = createRequire(import.meta.url)('typescript');
|
|
204
|
+
return typescript;
|
|
205
|
+
} catch {
|
|
206
|
+
throw new Error(
|
|
207
|
+
'TypeScript is required for syntax-aware export verification; run npm install before catalog generation.',
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function hasModifier(ts, node, kind) {
|
|
213
|
+
return Boolean(node.modifiers?.some((modifier) => modifier.kind === kind));
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function bindingNames(ts, name, names) {
|
|
217
|
+
if (ts.isIdentifier(name)) names.add(name.text);
|
|
218
|
+
else
|
|
219
|
+
for (const element of name.elements ?? [])
|
|
220
|
+
if (!ts.isOmittedExpression(element))
|
|
221
|
+
bindingNames(ts, element.name, names);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export function exportedNamesFromSource(source, fileName = 'component.tsx') {
|
|
225
|
+
const ts = typescriptApi();
|
|
226
|
+
const sourceFile = ts.createSourceFile(
|
|
227
|
+
fileName,
|
|
228
|
+
source,
|
|
229
|
+
ts.ScriptTarget.Latest,
|
|
230
|
+
true,
|
|
231
|
+
fileName.endsWith('.tsx') || fileName.endsWith('.jsx')
|
|
232
|
+
? ts.ScriptKind.TSX
|
|
233
|
+
: ts.ScriptKind.TS,
|
|
234
|
+
);
|
|
235
|
+
if (sourceFile.parseDiagnostics.length) {
|
|
236
|
+
const diagnostic = sourceFile.parseDiagnostics[0];
|
|
237
|
+
const point = sourceFile.getLineAndCharacterOfPosition(
|
|
238
|
+
diagnostic.start ?? 0,
|
|
239
|
+
);
|
|
240
|
+
throw new Error(
|
|
241
|
+
`${fileName}:${point.line + 1}:${point.character + 1}: ${ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')}`,
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
const names = new Set();
|
|
245
|
+
for (const statement of sourceFile.statements) {
|
|
246
|
+
if (ts.isExportDeclaration(statement)) {
|
|
247
|
+
if (!statement.exportClause) continue;
|
|
248
|
+
if (ts.isNamedExports(statement.exportClause))
|
|
249
|
+
for (const element of statement.exportClause.elements)
|
|
250
|
+
names.add(element.name.text);
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
if (
|
|
254
|
+
!hasModifier(ts, statement, ts.SyntaxKind.ExportKeyword) ||
|
|
255
|
+
hasModifier(ts, statement, ts.SyntaxKind.DefaultKeyword)
|
|
256
|
+
)
|
|
257
|
+
continue;
|
|
258
|
+
if (ts.isVariableStatement(statement)) {
|
|
259
|
+
for (const declaration of statement.declarationList.declarations)
|
|
260
|
+
bindingNames(ts, declaration.name, names);
|
|
261
|
+
} else if (
|
|
262
|
+
(ts.isFunctionDeclaration(statement) ||
|
|
263
|
+
ts.isClassDeclaration(statement) ||
|
|
264
|
+
ts.isEnumDeclaration(statement) ||
|
|
265
|
+
ts.isModuleDeclaration(statement) ||
|
|
266
|
+
ts.isInterfaceDeclaration(statement) ||
|
|
267
|
+
ts.isTypeAliasDeclaration(statement)) &&
|
|
268
|
+
statement.name
|
|
269
|
+
)
|
|
270
|
+
names.add(statement.name.text);
|
|
271
|
+
}
|
|
272
|
+
return names;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function exportedNamesFromFile(path) {
|
|
276
|
+
return exportedNamesFromSource(readFileSync(path, 'utf8'), path);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function sourceEntryForExport(packageRoot, definition) {
|
|
280
|
+
const target =
|
|
281
|
+
typeof definition === 'string'
|
|
282
|
+
? definition
|
|
283
|
+
: (definition?.import ?? definition?.default);
|
|
284
|
+
if (typeof target !== 'string' || !target.startsWith('./dist/')) return null;
|
|
285
|
+
const stem = target.slice('./dist/'.length).replace(/\.(?:mjs|cjs|js)$/, '');
|
|
286
|
+
for (const extension of ['.ts', '.tsx', '.mts', '.js', '.jsx', '.mjs']) {
|
|
287
|
+
const candidate = join(packageRoot, 'src', `${stem}${extension}`);
|
|
288
|
+
if (existsSync(candidate)) return candidate;
|
|
289
|
+
}
|
|
290
|
+
return null;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
function validateStringList(value, at, diagnostics, { nonempty = false } = {}) {
|
|
294
|
+
if (
|
|
295
|
+
!Array.isArray(value) ||
|
|
296
|
+
(nonempty && value.length === 0) ||
|
|
297
|
+
value.some((item) => typeof item !== 'string' || item.length === 0) ||
|
|
298
|
+
new Set(value).size !== value.length
|
|
299
|
+
)
|
|
300
|
+
diagnostics.push(
|
|
301
|
+
`${at}: expected a${nonempty ? ' non-empty' : ''} unique string list`,
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function validateComposition(component, at, diagnostics) {
|
|
306
|
+
const composition = component.composition ?? {};
|
|
307
|
+
if (!['any', 'root', 'listed'].includes(composition.parents?.mode))
|
|
308
|
+
diagnostics.push(`${at}.composition.parents.mode: invalid parent mode`);
|
|
309
|
+
validateStringList(
|
|
310
|
+
composition.parents?.entries,
|
|
311
|
+
`${at}.composition.parents.entries`,
|
|
312
|
+
diagnostics,
|
|
313
|
+
{ nonempty: composition.parents?.mode === 'listed' },
|
|
314
|
+
);
|
|
315
|
+
validateStringList(
|
|
316
|
+
composition.contexts,
|
|
317
|
+
`${at}.composition.contexts`,
|
|
318
|
+
diagnostics,
|
|
319
|
+
);
|
|
320
|
+
if (!Array.isArray(composition.zones))
|
|
321
|
+
diagnostics.push(`${at}.composition.zones: expected an array`);
|
|
322
|
+
const zoneIds = new Set();
|
|
323
|
+
for (const zone of composition.zones ?? []) {
|
|
324
|
+
const zoneAt = `${at}.composition.zones.${zone?.id ?? '<unknown>'}`;
|
|
325
|
+
if (!zone?.id || zoneIds.has(zone.id))
|
|
326
|
+
diagnostics.push(`${zoneAt}.id: expected a unique non-empty id`);
|
|
327
|
+
zoneIds.add(zone?.id);
|
|
328
|
+
validateStringList(zone?.accepts, `${zoneAt}.accepts`, diagnostics, {
|
|
329
|
+
nonempty: true,
|
|
330
|
+
});
|
|
331
|
+
const { min, max } = zone?.cardinality ?? {};
|
|
332
|
+
if (!Number.isInteger(min) || min < 0)
|
|
333
|
+
diagnostics.push(
|
|
334
|
+
`${zoneAt}.cardinality.min: expected a non-negative integer`,
|
|
335
|
+
);
|
|
336
|
+
if (max !== 'unbounded' && (!Number.isInteger(max) || max < 1 || max < min))
|
|
337
|
+
diagnostics.push(
|
|
338
|
+
`${zoneAt}.cardinality.max: expected unbounded or an integer >= min`,
|
|
339
|
+
);
|
|
340
|
+
validateStringList(
|
|
341
|
+
zone?.exclusiveWith,
|
|
342
|
+
`${zoneAt}.exclusiveWith`,
|
|
343
|
+
diagnostics,
|
|
344
|
+
);
|
|
345
|
+
}
|
|
346
|
+
for (const zone of composition.zones ?? [])
|
|
347
|
+
for (const other of zone?.exclusiveWith ?? [])
|
|
348
|
+
if (other === zone.id || !zoneIds.has(other))
|
|
349
|
+
diagnostics.push(
|
|
350
|
+
`${at}.composition.zones.${zone.id}.exclusiveWith: unknown or self-referential zone ${other}`,
|
|
351
|
+
);
|
|
352
|
+
|
|
353
|
+
if (!['any', 'none', 'listed'].includes(composition.children?.mode))
|
|
354
|
+
diagnostics.push(`${at}.composition.children.mode: invalid child mode`);
|
|
355
|
+
validateStringList(
|
|
356
|
+
composition.children?.concepts,
|
|
357
|
+
`${at}.composition.children.concepts`,
|
|
358
|
+
diagnostics,
|
|
359
|
+
{ nonempty: composition.children?.mode === 'listed' },
|
|
360
|
+
);
|
|
361
|
+
validateStringList(
|
|
362
|
+
composition.children?.requiredConcepts,
|
|
363
|
+
`${at}.composition.children.requiredConcepts`,
|
|
364
|
+
diagnostics,
|
|
365
|
+
);
|
|
366
|
+
for (const required of composition.children?.requiredConcepts ?? [])
|
|
367
|
+
if (!composition.children.concepts?.includes(required))
|
|
368
|
+
diagnostics.push(
|
|
369
|
+
`${at}.composition.children.requiredConcepts: ${required} is not an allowed concept`,
|
|
370
|
+
);
|
|
371
|
+
|
|
372
|
+
if (!Array.isArray(composition.state))
|
|
373
|
+
diagnostics.push(`${at}.composition.state: expected an array`);
|
|
374
|
+
const stateIds = new Set();
|
|
375
|
+
for (const state of composition.state ?? []) {
|
|
376
|
+
if (!state?.id || stateIds.has(state.id))
|
|
377
|
+
diagnostics.push(
|
|
378
|
+
`${at}.composition.state: state ids must be unique and non-empty`,
|
|
379
|
+
);
|
|
380
|
+
stateIds.add(state?.id);
|
|
381
|
+
if (!['application', 'controlled', 'component'].includes(state?.owner))
|
|
382
|
+
diagnostics.push(
|
|
383
|
+
`${at}.composition.state.${state?.id}.owner: invalid owner`,
|
|
384
|
+
);
|
|
385
|
+
if (typeof state?.required !== 'boolean')
|
|
386
|
+
diagnostics.push(
|
|
387
|
+
`${at}.composition.state.${state?.id}.required: expected boolean`,
|
|
388
|
+
);
|
|
389
|
+
}
|
|
390
|
+
if (typeof composition.wiring?.required !== 'boolean')
|
|
391
|
+
diagnostics.push(`${at}.composition.wiring.required: expected boolean`);
|
|
392
|
+
validateStringList(
|
|
393
|
+
composition.wiring?.helpers,
|
|
394
|
+
`${at}.composition.wiring.helpers`,
|
|
395
|
+
diagnostics,
|
|
396
|
+
{ nonempty: composition.wiring?.required === true },
|
|
397
|
+
);
|
|
398
|
+
validateStringList(
|
|
399
|
+
composition.wiring?.obligations,
|
|
400
|
+
`${at}.composition.wiring.obligations`,
|
|
401
|
+
diagnostics,
|
|
402
|
+
);
|
|
403
|
+
if (
|
|
404
|
+
!['application', 'component', 'shared', 'not-applicable'].includes(
|
|
405
|
+
composition.responsive?.owner,
|
|
406
|
+
)
|
|
407
|
+
)
|
|
408
|
+
diagnostics.push(`${at}.composition.responsive.owner: invalid owner`);
|
|
409
|
+
validateStringList(
|
|
410
|
+
composition.responsive?.behaviors,
|
|
411
|
+
`${at}.composition.responsive.behaviors`,
|
|
412
|
+
diagnostics,
|
|
413
|
+
);
|
|
414
|
+
validateStringList(
|
|
415
|
+
composition.layout?.roles,
|
|
416
|
+
`${at}.composition.layout.roles`,
|
|
417
|
+
diagnostics,
|
|
418
|
+
{ nonempty: true },
|
|
419
|
+
);
|
|
420
|
+
const geometryOwners = [
|
|
421
|
+
'self',
|
|
422
|
+
'parent',
|
|
423
|
+
'child',
|
|
424
|
+
'none',
|
|
425
|
+
'conditional',
|
|
426
|
+
'composed',
|
|
427
|
+
];
|
|
428
|
+
for (const dimension of ['margin', 'border', 'padding'])
|
|
429
|
+
if (!geometryOwners.includes(composition.layout?.geometry?.[dimension]))
|
|
430
|
+
diagnostics.push(
|
|
431
|
+
`${at}.composition.layout.geometry.${dimension}: invalid geometry owner`,
|
|
432
|
+
);
|
|
433
|
+
if ('notes' in (composition.layout?.geometry ?? {}))
|
|
434
|
+
validateStringList(
|
|
435
|
+
composition.layout.geometry.notes,
|
|
436
|
+
`${at}.composition.layout.geometry.notes`,
|
|
437
|
+
diagnostics,
|
|
438
|
+
);
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
function validateComponent(
|
|
442
|
+
component,
|
|
443
|
+
packageRoot,
|
|
444
|
+
packageJson,
|
|
445
|
+
ids,
|
|
446
|
+
diagnosticIds,
|
|
447
|
+
diagnostics,
|
|
448
|
+
) {
|
|
449
|
+
const at = `${packageJson.name}:${component?.id ?? '<unknown>'}`;
|
|
450
|
+
for (const path of [
|
|
451
|
+
'id',
|
|
452
|
+
'name',
|
|
453
|
+
'kind',
|
|
454
|
+
'purpose',
|
|
455
|
+
'source',
|
|
456
|
+
'publicExports',
|
|
457
|
+
'sourceLinks',
|
|
458
|
+
'composition.parents.mode',
|
|
459
|
+
'composition.parents.entries',
|
|
460
|
+
'composition.contexts',
|
|
461
|
+
'composition.zones',
|
|
462
|
+
'composition.children.mode',
|
|
463
|
+
'composition.children.concepts',
|
|
464
|
+
'composition.children.requiredConcepts',
|
|
465
|
+
'composition.state',
|
|
466
|
+
'composition.wiring.required',
|
|
467
|
+
'composition.wiring.helpers',
|
|
468
|
+
'composition.wiring.obligations',
|
|
469
|
+
'composition.responsive.owner',
|
|
470
|
+
'composition.responsive.behaviors',
|
|
471
|
+
'composition.layout.roles',
|
|
472
|
+
'composition.layout.geometry.margin',
|
|
473
|
+
'composition.layout.geometry.border',
|
|
474
|
+
'composition.layout.geometry.padding',
|
|
475
|
+
'boundaries.rootClass',
|
|
476
|
+
'boundaries.publicClasses',
|
|
477
|
+
'boundaries.publicTokens',
|
|
478
|
+
'accessibility.obligations',
|
|
479
|
+
'diagnostics',
|
|
480
|
+
'provenance.selection',
|
|
481
|
+
'provenance.composition',
|
|
482
|
+
])
|
|
483
|
+
requireDecision(component, path, at, diagnostics);
|
|
484
|
+
|
|
485
|
+
if (!component?.id) return;
|
|
486
|
+
if (ids.has(component.id))
|
|
487
|
+
diagnostics.push(`${at}.id: duplicate component id`);
|
|
488
|
+
ids.add(component.id);
|
|
489
|
+
if (!['component', 'composition', 'recipe'].includes(component.kind))
|
|
490
|
+
diagnostics.push(`${at}.kind: expected component, composition, or recipe`);
|
|
491
|
+
const sourcePath = resolve(packageRoot, component.source ?? '');
|
|
492
|
+
if (!component.source || !existsSync(sourcePath))
|
|
493
|
+
diagnostics.push(`${at}.source: file does not exist: ${component.source}`);
|
|
494
|
+
|
|
495
|
+
const exports = packageJson.exports ?? {};
|
|
496
|
+
if (
|
|
497
|
+
!Array.isArray(component.publicExports) ||
|
|
498
|
+
!component.publicExports.length
|
|
499
|
+
)
|
|
500
|
+
diagnostics.push(`${at}.publicExports: expected a non-empty array`);
|
|
501
|
+
else {
|
|
502
|
+
const seen = new Set();
|
|
503
|
+
for (const item of component.publicExports) {
|
|
504
|
+
const exportAt = `${at}.publicExports`;
|
|
505
|
+
if (!item?.name || typeof item?.subpath !== 'string') {
|
|
506
|
+
diagnostics.push(`${exportAt}: every export requires name and subpath`);
|
|
507
|
+
continue;
|
|
508
|
+
}
|
|
509
|
+
const key = `${item.subpath}:${item.name}`;
|
|
510
|
+
if (seen.has(key)) diagnostics.push(`${exportAt}: duplicate ${key}`);
|
|
511
|
+
seen.add(key);
|
|
512
|
+
if (!(item.subpath in exports))
|
|
513
|
+
diagnostics.push(
|
|
514
|
+
`${exportAt}: package.json does not export subpath ${item.subpath}`,
|
|
515
|
+
);
|
|
516
|
+
const sourceEntry = sourceEntryForExport(
|
|
517
|
+
packageRoot,
|
|
518
|
+
exports[item.subpath],
|
|
519
|
+
);
|
|
520
|
+
if (!sourceEntry)
|
|
521
|
+
diagnostics.push(
|
|
522
|
+
`${exportAt}: cannot map ${item.subpath} to an existing src/ entry`,
|
|
523
|
+
);
|
|
524
|
+
else if (!exportedNamesFromFile(sourceEntry).has(item.name))
|
|
525
|
+
diagnostics.push(
|
|
526
|
+
`${exportAt}: ${item.name} is not exported by ${relative(packageRoot, sourceEntry)}`,
|
|
527
|
+
);
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
validateStringList(component.sourceLinks, `${at}.sourceLinks`, diagnostics, {
|
|
531
|
+
nonempty: true,
|
|
532
|
+
});
|
|
533
|
+
for (const link of component.sourceLinks ?? []) {
|
|
534
|
+
if (/^[a-z]+:/i.test(link)) continue;
|
|
535
|
+
const localPath = link.split('#')[0];
|
|
536
|
+
if (localPath && !existsSync(resolve(packageRoot, localPath)))
|
|
537
|
+
diagnostics.push(
|
|
538
|
+
`${at}.sourceLinks: local source link does not exist: ${link}`,
|
|
539
|
+
);
|
|
540
|
+
}
|
|
541
|
+
validateComposition(component, at, diagnostics);
|
|
542
|
+
validateStringList(
|
|
543
|
+
component.boundaries?.publicClasses,
|
|
544
|
+
`${at}.boundaries.publicClasses`,
|
|
545
|
+
diagnostics,
|
|
546
|
+
);
|
|
547
|
+
if (
|
|
548
|
+
component.boundaries?.rootClass !== null &&
|
|
549
|
+
(typeof component.boundaries?.rootClass !== 'string' ||
|
|
550
|
+
!component.boundaries.publicClasses?.includes(
|
|
551
|
+
component.boundaries.rootClass,
|
|
552
|
+
))
|
|
553
|
+
)
|
|
554
|
+
diagnostics.push(
|
|
555
|
+
`${at}.boundaries.rootClass: expected null or one publicClasses entry`,
|
|
556
|
+
);
|
|
557
|
+
validateStringList(
|
|
558
|
+
component.boundaries?.publicTokens,
|
|
559
|
+
`${at}.boundaries.publicTokens`,
|
|
560
|
+
diagnostics,
|
|
561
|
+
);
|
|
562
|
+
validateStringList(
|
|
563
|
+
component.accessibility?.obligations,
|
|
564
|
+
`${at}.accessibility.obligations`,
|
|
565
|
+
diagnostics,
|
|
566
|
+
);
|
|
567
|
+
if (!Array.isArray(component.diagnostics))
|
|
568
|
+
diagnostics.push(`${at}.diagnostics: expected an array`);
|
|
569
|
+
for (const diagnostic of component.diagnostics ?? []) {
|
|
570
|
+
if (
|
|
571
|
+
!/^KUI-C[0-9]{3}$/.test(diagnostic?.id ?? '') ||
|
|
572
|
+
diagnosticIds.has(diagnostic.id)
|
|
573
|
+
)
|
|
574
|
+
diagnostics.push(`${at}.diagnostics: ids must be unique KUI-C### values`);
|
|
575
|
+
diagnosticIds.add(diagnostic?.id);
|
|
576
|
+
if (!['error', 'warning'].includes(diagnostic?.severity))
|
|
577
|
+
diagnostics.push(
|
|
578
|
+
`${at}.diagnostics.${diagnostic?.id}.severity: invalid severity`,
|
|
579
|
+
);
|
|
580
|
+
if (!diagnostic?.when || !diagnostic?.message)
|
|
581
|
+
diagnostics.push(
|
|
582
|
+
`${at}.diagnostics.${diagnostic?.id}: when and message are required`,
|
|
583
|
+
);
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
function toEntry(packageName, component) {
|
|
588
|
+
return {
|
|
589
|
+
key: `${packageName}:${component.id}`,
|
|
590
|
+
package: packageName,
|
|
591
|
+
id: component.id,
|
|
592
|
+
name: component.name,
|
|
593
|
+
kind: component.kind,
|
|
594
|
+
purpose: component.purpose,
|
|
595
|
+
publicExports: component.publicExports,
|
|
596
|
+
sourceLinks: component.sourceLinks,
|
|
597
|
+
source: component.source,
|
|
598
|
+
parents: component.composition.parents,
|
|
599
|
+
contexts: component.composition.contexts,
|
|
600
|
+
zones: component.composition.zones,
|
|
601
|
+
children: component.composition.children,
|
|
602
|
+
state: component.composition.state,
|
|
603
|
+
wiring: component.composition.wiring,
|
|
604
|
+
responsive: component.composition.responsive,
|
|
605
|
+
layout: component.composition.layout,
|
|
606
|
+
accessibility: component.accessibility,
|
|
607
|
+
boundaries: component.boundaries,
|
|
608
|
+
diagnostics: component.diagnostics,
|
|
609
|
+
provenance: component.provenance,
|
|
610
|
+
};
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
export function generateCatalogs(root = process.cwd()) {
|
|
614
|
+
const diagnostics = [];
|
|
615
|
+
const packages = configuredPackages(resolve(root), diagnostics);
|
|
616
|
+
if (!packages.length && !diagnostics.length)
|
|
617
|
+
diagnostics.push(
|
|
618
|
+
`${relative(process.cwd(), resolve(root)) || '.'}: no package declares package.json#${CONFIG_KEY}`,
|
|
619
|
+
);
|
|
620
|
+
const results = [];
|
|
621
|
+
const packageNames = new Set();
|
|
622
|
+
for (const packageRoot of packages) {
|
|
623
|
+
const packagePath = join(packageRoot, 'package.json');
|
|
624
|
+
const packageJson = readJson(packagePath, packagePath, diagnostics);
|
|
625
|
+
if (!packageJson) continue;
|
|
626
|
+
const config = packageJson[CONFIG_KEY];
|
|
627
|
+
if (!packageJson.name)
|
|
628
|
+
diagnostics.push(`${packagePath}: package name is required`);
|
|
629
|
+
if (packageNames.has(packageJson.name))
|
|
630
|
+
diagnostics.push(
|
|
631
|
+
`${packagePath}: duplicate configured package name ${packageJson.name}`,
|
|
632
|
+
);
|
|
633
|
+
packageNames.add(packageJson.name);
|
|
634
|
+
for (const key of ['source', 'output'])
|
|
635
|
+
if (!config?.[key])
|
|
636
|
+
diagnostics.push(`${packagePath}#${CONFIG_KEY}.${key}: is required`);
|
|
637
|
+
if (!config?.source || !config?.output) continue;
|
|
638
|
+
const metadataPath = resolve(packageRoot, config.source);
|
|
639
|
+
const metadata = readJson(metadataPath, metadataPath, diagnostics);
|
|
640
|
+
if (!metadata) continue;
|
|
641
|
+
const metadataDiagnostics = validateDocumentAgainstSchema(
|
|
642
|
+
metadata,
|
|
643
|
+
metadataSchema,
|
|
644
|
+
metadataPath,
|
|
645
|
+
);
|
|
646
|
+
diagnostics.push(...metadataDiagnostics);
|
|
647
|
+
if (metadataDiagnostics.length) {
|
|
648
|
+
continue;
|
|
649
|
+
}
|
|
650
|
+
const ids = new Set();
|
|
651
|
+
const diagnosticIds = new Set();
|
|
652
|
+
for (const component of metadata.components)
|
|
653
|
+
validateComponent(
|
|
654
|
+
component,
|
|
655
|
+
packageRoot,
|
|
656
|
+
packageJson,
|
|
657
|
+
ids,
|
|
658
|
+
diagnosticIds,
|
|
659
|
+
diagnostics,
|
|
660
|
+
);
|
|
661
|
+
const entries = [...metadata.components]
|
|
662
|
+
.sort((a, b) => a.id.localeCompare(b.id))
|
|
663
|
+
.map((component) => toEntry(packageJson.name, component));
|
|
664
|
+
const catalog = {
|
|
665
|
+
$schema:
|
|
666
|
+
'https://raw.githubusercontent.com/brianwestphal/kerf/main/ui/ai/component-catalog-extension-v2.schema.json',
|
|
667
|
+
schemaVersion: 2,
|
|
668
|
+
package: packageJson.name,
|
|
669
|
+
compatibility: {
|
|
670
|
+
v1Catalog: metadata.v1Catalog ?? 'not-applicable',
|
|
671
|
+
identity: 'package:id',
|
|
672
|
+
},
|
|
673
|
+
entries,
|
|
674
|
+
};
|
|
675
|
+
diagnostics.push(
|
|
676
|
+
...validateDocumentAgainstSchema(
|
|
677
|
+
catalog,
|
|
678
|
+
catalogSchema,
|
|
679
|
+
resolve(packageRoot, config.output),
|
|
680
|
+
),
|
|
681
|
+
);
|
|
682
|
+
results.push({
|
|
683
|
+
packageRoot,
|
|
684
|
+
outputPath: resolve(packageRoot, config.output),
|
|
685
|
+
catalog,
|
|
686
|
+
});
|
|
687
|
+
}
|
|
688
|
+
if (diagnostics.length) throw new CatalogError(diagnostics.sort());
|
|
689
|
+
return results;
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
export function formatCatalog(catalog) {
|
|
693
|
+
return `${JSON.stringify(catalog, null, 2)}\n`;
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
export function runCatalogCommand({
|
|
697
|
+
root = process.cwd(),
|
|
698
|
+
check = false,
|
|
699
|
+
} = {}) {
|
|
700
|
+
const results = generateCatalogs(root);
|
|
701
|
+
const diagnostics = [];
|
|
702
|
+
for (const result of results) {
|
|
703
|
+
const expected = formatCatalog(result.catalog);
|
|
704
|
+
if (check) {
|
|
705
|
+
if (!existsSync(result.outputPath))
|
|
706
|
+
diagnostics.push(`${result.outputPath}: generated catalog is missing`);
|
|
707
|
+
else {
|
|
708
|
+
const actualSource = readFileSync(result.outputPath, 'utf8');
|
|
709
|
+
const actual = readJson(
|
|
710
|
+
result.outputPath,
|
|
711
|
+
result.outputPath,
|
|
712
|
+
diagnostics,
|
|
713
|
+
);
|
|
714
|
+
if (actual)
|
|
715
|
+
diagnostics.push(
|
|
716
|
+
...validateDocumentAgainstSchema(
|
|
717
|
+
actual,
|
|
718
|
+
catalogSchema,
|
|
719
|
+
result.outputPath,
|
|
720
|
+
),
|
|
721
|
+
);
|
|
722
|
+
if (actualSource !== expected)
|
|
723
|
+
diagnostics.push(
|
|
724
|
+
`${result.outputPath}: generated catalog is stale; run catalog:generate`,
|
|
725
|
+
);
|
|
726
|
+
}
|
|
727
|
+
} else {
|
|
728
|
+
writeFileSync(result.outputPath, expected);
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
if (diagnostics.length) throw new CatalogError(diagnostics);
|
|
732
|
+
return results;
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
function parseArgs(argv) {
|
|
736
|
+
let root = process.cwd();
|
|
737
|
+
let check = false;
|
|
738
|
+
for (let index = 2; index < argv.length; index += 1) {
|
|
739
|
+
const arg = argv[index];
|
|
740
|
+
if (arg === '--check') check = true;
|
|
741
|
+
else if (arg === '--write') check = false;
|
|
742
|
+
else if (arg === '--root' && argv[index + 1]) root = resolve(argv[++index]);
|
|
743
|
+
else if (arg === '--help' || arg === '-h') {
|
|
744
|
+
process.stdout.write(
|
|
745
|
+
'Usage: kerf-component-catalog [--write|--check] [--root <package-or-workspace>]\n',
|
|
746
|
+
);
|
|
747
|
+
process.exit(0);
|
|
748
|
+
} else throw new CatalogError([`unknown argument: ${arg}`]);
|
|
749
|
+
}
|
|
750
|
+
return { root, check };
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
const isMain =
|
|
754
|
+
process.argv[1] &&
|
|
755
|
+
realpathSync(process.argv[1]) ===
|
|
756
|
+
realpathSync(fileURLToPath(import.meta.url));
|
|
757
|
+
if (isMain) {
|
|
758
|
+
try {
|
|
759
|
+
const options = parseArgs(process.argv);
|
|
760
|
+
const results = runCatalogCommand(options);
|
|
761
|
+
const verb = options.check ? 'verified' : 'wrote';
|
|
762
|
+
for (const result of results)
|
|
763
|
+
process.stdout.write(
|
|
764
|
+
`${verb} ${relative(options.root, result.outputPath)}\n`,
|
|
765
|
+
);
|
|
766
|
+
} catch (error) {
|
|
767
|
+
const diagnostics =
|
|
768
|
+
error instanceof CatalogError ? error.diagnostics : [error.message];
|
|
769
|
+
for (const diagnostic of diagnostics)
|
|
770
|
+
process.stderr.write(`kerf-component-catalog: ${diagnostic}\n`);
|
|
771
|
+
process.exitCode = 1;
|
|
772
|
+
}
|
|
773
|
+
}
|