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