@eldrajs/vite-plugin-theme 0.0.0 → 0.2.0
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/CHANGELOG.md +270 -0
- package/LICENSE +21 -0
- package/README.md +30 -1
- package/dist/index.d.ts +74 -0
- package/dist/index.js +452 -0
- package/dist/scan-DmmAzjZz.js +2184 -0
- package/dist/scan.d.ts +6 -0
- package/dist/scan.js +2 -0
- package/dist/types-kcnroZCz.d.ts +434 -0
- package/package.json +53 -2
|
@@ -0,0 +1,2184 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, statSync } from "node:fs";
|
|
3
|
+
import { dirname, join, relative } from "node:path";
|
|
4
|
+
import { DesignTokenValidationError, normalizeThemeDesignTokens } from "@eldrajs/theme-core/design-tokens";
|
|
5
|
+
import { resolveLayoutBreakpoints } from "@eldrajs/theme-core/layout";
|
|
6
|
+
import Ajv from "ajv";
|
|
7
|
+
import { RESERVED_THEME_IDS, RICH_TEXT_TOOLBAR_CONTROLS, THEME_ID_PATTERN, parseDynamicRoutePattern } from "@eldrajs/theme-core";
|
|
8
|
+
import { safeLinkHref } from "@eldrajs/theme-core/links";
|
|
9
|
+
//#region src/blockSchema.ts
|
|
10
|
+
const API_ID_PATTERN = "^[a-z][a-z0-9-]{1,48}$";
|
|
11
|
+
const FIELD_ID_PATTERN = "^[a-z][a-zA-Z0-9]{0,48}$";
|
|
12
|
+
const blockJsonSchema = {
|
|
13
|
+
$schema: "http://json-schema.org/draft-07/schema#",
|
|
14
|
+
type: "object",
|
|
15
|
+
additionalProperties: false,
|
|
16
|
+
required: [
|
|
17
|
+
"apiId",
|
|
18
|
+
"name",
|
|
19
|
+
"version",
|
|
20
|
+
"fields"
|
|
21
|
+
],
|
|
22
|
+
properties: {
|
|
23
|
+
apiId: {
|
|
24
|
+
type: "string",
|
|
25
|
+
pattern: API_ID_PATTERN
|
|
26
|
+
},
|
|
27
|
+
name: {
|
|
28
|
+
type: "string",
|
|
29
|
+
minLength: 1,
|
|
30
|
+
maxLength: 80
|
|
31
|
+
},
|
|
32
|
+
description: {
|
|
33
|
+
type: "string",
|
|
34
|
+
maxLength: 300
|
|
35
|
+
},
|
|
36
|
+
icon: {
|
|
37
|
+
type: "string",
|
|
38
|
+
minLength: 1
|
|
39
|
+
},
|
|
40
|
+
category: {
|
|
41
|
+
type: "string",
|
|
42
|
+
pattern: "^[a-z][a-z0-9-]{1,30}$"
|
|
43
|
+
},
|
|
44
|
+
version: {
|
|
45
|
+
type: "integer",
|
|
46
|
+
minimum: 1
|
|
47
|
+
},
|
|
48
|
+
fields: {
|
|
49
|
+
type: "array",
|
|
50
|
+
minItems: 1,
|
|
51
|
+
maxItems: 100,
|
|
52
|
+
items: { $ref: "#/definitions/blockField" }
|
|
53
|
+
},
|
|
54
|
+
groups: {
|
|
55
|
+
type: "array",
|
|
56
|
+
items: {
|
|
57
|
+
type: "object",
|
|
58
|
+
additionalProperties: false,
|
|
59
|
+
required: ["groupId", "name"],
|
|
60
|
+
properties: {
|
|
61
|
+
groupId: {
|
|
62
|
+
type: "string",
|
|
63
|
+
pattern: FIELD_ID_PATTERN
|
|
64
|
+
},
|
|
65
|
+
name: {
|
|
66
|
+
type: "string",
|
|
67
|
+
minLength: 1,
|
|
68
|
+
maxLength: 80
|
|
69
|
+
},
|
|
70
|
+
defaultOpen: { type: "boolean" }
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
migrations: {
|
|
75
|
+
type: "array",
|
|
76
|
+
items: { $ref: "#/definitions/blockMigration" }
|
|
77
|
+
},
|
|
78
|
+
slots: {
|
|
79
|
+
type: "array",
|
|
80
|
+
maxItems: 12,
|
|
81
|
+
items: { $ref: "#/definitions/blockSlot" }
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
definitions: {
|
|
85
|
+
blockMigration: {
|
|
86
|
+
type: "object",
|
|
87
|
+
additionalProperties: false,
|
|
88
|
+
required: ["version"],
|
|
89
|
+
properties: {
|
|
90
|
+
version: {
|
|
91
|
+
type: "integer",
|
|
92
|
+
minimum: 2
|
|
93
|
+
},
|
|
94
|
+
renames: {
|
|
95
|
+
type: "array",
|
|
96
|
+
maxItems: 32,
|
|
97
|
+
items: {
|
|
98
|
+
type: "object",
|
|
99
|
+
additionalProperties: false,
|
|
100
|
+
required: ["from", "to"],
|
|
101
|
+
properties: {
|
|
102
|
+
from: {
|
|
103
|
+
type: "string",
|
|
104
|
+
pattern: FIELD_ID_PATTERN
|
|
105
|
+
},
|
|
106
|
+
to: {
|
|
107
|
+
type: "string",
|
|
108
|
+
pattern: FIELD_ID_PATTERN
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
convertToLink: {
|
|
114
|
+
type: "array",
|
|
115
|
+
maxItems: 16,
|
|
116
|
+
items: {
|
|
117
|
+
type: "object",
|
|
118
|
+
additionalProperties: false,
|
|
119
|
+
required: [
|
|
120
|
+
"from",
|
|
121
|
+
"to",
|
|
122
|
+
"shape"
|
|
123
|
+
],
|
|
124
|
+
properties: {
|
|
125
|
+
from: {
|
|
126
|
+
type: "string",
|
|
127
|
+
pattern: FIELD_ID_PATTERN
|
|
128
|
+
},
|
|
129
|
+
to: {
|
|
130
|
+
type: "string",
|
|
131
|
+
pattern: FIELD_ID_PATTERN
|
|
132
|
+
},
|
|
133
|
+
shape: {
|
|
134
|
+
type: "string",
|
|
135
|
+
enum: ["string", "list"]
|
|
136
|
+
},
|
|
137
|
+
label: {
|
|
138
|
+
type: "string",
|
|
139
|
+
pattern: FIELD_ID_PATTERN
|
|
140
|
+
},
|
|
141
|
+
url: {
|
|
142
|
+
type: "string",
|
|
143
|
+
pattern: FIELD_ID_PATTERN
|
|
144
|
+
},
|
|
145
|
+
group: {
|
|
146
|
+
type: "string",
|
|
147
|
+
pattern: FIELD_ID_PATTERN
|
|
148
|
+
},
|
|
149
|
+
children: {
|
|
150
|
+
type: "object",
|
|
151
|
+
additionalProperties: false,
|
|
152
|
+
required: ["from"],
|
|
153
|
+
properties: {
|
|
154
|
+
from: {
|
|
155
|
+
type: "string",
|
|
156
|
+
pattern: FIELD_ID_PATTERN
|
|
157
|
+
},
|
|
158
|
+
label: {
|
|
159
|
+
type: "string",
|
|
160
|
+
pattern: FIELD_ID_PATTERN
|
|
161
|
+
},
|
|
162
|
+
url: {
|
|
163
|
+
type: "string",
|
|
164
|
+
pattern: FIELD_ID_PATTERN
|
|
165
|
+
},
|
|
166
|
+
group: {
|
|
167
|
+
type: "string",
|
|
168
|
+
pattern: FIELD_ID_PATTERN
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
},
|
|
177
|
+
blockField: {
|
|
178
|
+
type: "object",
|
|
179
|
+
additionalProperties: false,
|
|
180
|
+
required: [
|
|
181
|
+
"fieldId",
|
|
182
|
+
"name",
|
|
183
|
+
"type"
|
|
184
|
+
],
|
|
185
|
+
properties: {
|
|
186
|
+
fieldId: {
|
|
187
|
+
type: "string",
|
|
188
|
+
pattern: FIELD_ID_PATTERN
|
|
189
|
+
},
|
|
190
|
+
name: {
|
|
191
|
+
type: "string",
|
|
192
|
+
minLength: 1
|
|
193
|
+
},
|
|
194
|
+
type: {
|
|
195
|
+
type: "string",
|
|
196
|
+
minLength: 1
|
|
197
|
+
},
|
|
198
|
+
groupId: { type: "string" },
|
|
199
|
+
isTitle: { type: "boolean" },
|
|
200
|
+
localized: { type: "boolean" },
|
|
201
|
+
default: {},
|
|
202
|
+
description: { type: "string" },
|
|
203
|
+
helpText: { type: "string" },
|
|
204
|
+
validators: { type: "object" },
|
|
205
|
+
metadata: { type: "object" },
|
|
206
|
+
showWhen: {
|
|
207
|
+
type: "object",
|
|
208
|
+
additionalProperties: false,
|
|
209
|
+
required: ["field"],
|
|
210
|
+
properties: {
|
|
211
|
+
field: {
|
|
212
|
+
type: "string",
|
|
213
|
+
pattern: FIELD_ID_PATTERN
|
|
214
|
+
},
|
|
215
|
+
in: {
|
|
216
|
+
type: "array",
|
|
217
|
+
maxItems: 50,
|
|
218
|
+
items: { type: "string" }
|
|
219
|
+
},
|
|
220
|
+
equals: { type: "string" }
|
|
221
|
+
}
|
|
222
|
+
},
|
|
223
|
+
relation: {
|
|
224
|
+
type: "object",
|
|
225
|
+
additionalProperties: false,
|
|
226
|
+
properties: {
|
|
227
|
+
allowedTagIds: {
|
|
228
|
+
type: "array",
|
|
229
|
+
minItems: 1,
|
|
230
|
+
items: { type: "string" }
|
|
231
|
+
},
|
|
232
|
+
allowProducts: { type: "boolean" },
|
|
233
|
+
allowCollections: { type: "boolean" },
|
|
234
|
+
multiple: { type: "boolean" }
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
},
|
|
239
|
+
blockSlot: {
|
|
240
|
+
type: "object",
|
|
241
|
+
additionalProperties: false,
|
|
242
|
+
required: [
|
|
243
|
+
"id",
|
|
244
|
+
"label",
|
|
245
|
+
"maxItems"
|
|
246
|
+
],
|
|
247
|
+
properties: {
|
|
248
|
+
id: {
|
|
249
|
+
type: "string",
|
|
250
|
+
pattern: "^[a-z][a-z0-9-]{0,47}$"
|
|
251
|
+
},
|
|
252
|
+
label: {
|
|
253
|
+
type: "string",
|
|
254
|
+
minLength: 1,
|
|
255
|
+
maxLength: 80
|
|
256
|
+
},
|
|
257
|
+
description: {
|
|
258
|
+
type: "string",
|
|
259
|
+
maxLength: 240
|
|
260
|
+
},
|
|
261
|
+
minItems: {
|
|
262
|
+
type: "integer",
|
|
263
|
+
enum: [0]
|
|
264
|
+
},
|
|
265
|
+
maxItems: {
|
|
266
|
+
type: "integer",
|
|
267
|
+
minimum: 1,
|
|
268
|
+
maximum: 20
|
|
269
|
+
},
|
|
270
|
+
allowedBlockApiIds: {
|
|
271
|
+
type: "array",
|
|
272
|
+
maxItems: 50,
|
|
273
|
+
uniqueItems: true,
|
|
274
|
+
items: { type: "string" }
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
//#endregion
|
|
281
|
+
//#region src/migrations.ts
|
|
282
|
+
const STORED_FIELD_TYPES = /* @__PURE__ */ new Set([
|
|
283
|
+
"bool",
|
|
284
|
+
"color",
|
|
285
|
+
"composite",
|
|
286
|
+
"datetime",
|
|
287
|
+
"float",
|
|
288
|
+
"int",
|
|
289
|
+
"json",
|
|
290
|
+
"layout",
|
|
291
|
+
"link",
|
|
292
|
+
"list",
|
|
293
|
+
"lucide-icon",
|
|
294
|
+
"markdown",
|
|
295
|
+
"media",
|
|
296
|
+
"reference",
|
|
297
|
+
"rich-text",
|
|
298
|
+
"select",
|
|
299
|
+
"seo",
|
|
300
|
+
"slug",
|
|
301
|
+
"string",
|
|
302
|
+
"styling",
|
|
303
|
+
"tabler-icon",
|
|
304
|
+
"text",
|
|
305
|
+
"uuid"
|
|
306
|
+
]);
|
|
307
|
+
const STORED_FIELD_KEYS = [
|
|
308
|
+
"fieldId",
|
|
309
|
+
"groupId",
|
|
310
|
+
"presetId",
|
|
311
|
+
"name",
|
|
312
|
+
"isTitle",
|
|
313
|
+
"type",
|
|
314
|
+
"description",
|
|
315
|
+
"default",
|
|
316
|
+
"localized",
|
|
317
|
+
"helpText",
|
|
318
|
+
"validators",
|
|
319
|
+
"metadata",
|
|
320
|
+
"relation"
|
|
321
|
+
];
|
|
322
|
+
const IGNORED_FIELD_KEYS = ["showWhen"];
|
|
323
|
+
const STORED_RELATION_KEYS = [
|
|
324
|
+
"multiple",
|
|
325
|
+
"allowProducts",
|
|
326
|
+
"allowCollections",
|
|
327
|
+
"allowedTagIds",
|
|
328
|
+
"allowedSchemaIds"
|
|
329
|
+
];
|
|
330
|
+
function migrationChecks(file, block, previous, errors) {
|
|
331
|
+
const next = new Map(block.fields.map((field) => [field.fieldId, field]));
|
|
332
|
+
const versions = /* @__PURE__ */ new Set();
|
|
333
|
+
for (const [index, step] of (block.migrations ?? []).entries()) {
|
|
334
|
+
const at = `${file}: migrations[${index}]`;
|
|
335
|
+
if (step.version > block.version) errors.push(`${at}.version — must be a block version bump`);
|
|
336
|
+
if (versions.has(step.version)) errors.push(`${at}.version — migration versions must be unique`);
|
|
337
|
+
versions.add(step.version);
|
|
338
|
+
const renames = step.renames ?? [];
|
|
339
|
+
const conversions = step.convertToLink ?? [];
|
|
340
|
+
if (step.renames === void 0 && step.convertToLink === void 0) errors.push(`${at} — a migration step must declare renames or convertToLink`);
|
|
341
|
+
const conversionTargets = /* @__PURE__ */ new Set();
|
|
342
|
+
for (const conversion of conversions) {
|
|
343
|
+
checkConversionTarget(at, conversion, next, errors);
|
|
344
|
+
if (conversionTargets.has(conversion.to)) errors.push(`${at} — conversion destinations must be unique; two conversions cannot write one field`);
|
|
345
|
+
conversionTargets.add(conversion.to);
|
|
346
|
+
}
|
|
347
|
+
const sources = /* @__PURE__ */ new Set();
|
|
348
|
+
const targets = /* @__PURE__ */ new Set();
|
|
349
|
+
for (const rename of renames) {
|
|
350
|
+
if (rename.from === rename.to) errors.push(`${at} — renames require distinct top-level field IDs`);
|
|
351
|
+
if (!next.has(rename.to)) errors.push(`${at} — migration destination does not exist in incoming fields`);
|
|
352
|
+
if (sources.has(rename.from) || targets.has(rename.to)) errors.push(`${at} — rename pairs must be unique; splitting and merging are unsupported`);
|
|
353
|
+
sources.add(rename.from);
|
|
354
|
+
targets.add(rename.to);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
if (previous === void 0) return;
|
|
358
|
+
const old = new Map(previous.fields.map((field) => [field.fieldId, field]));
|
|
359
|
+
const sources = /* @__PURE__ */ new Set();
|
|
360
|
+
const targets = /* @__PURE__ */ new Set();
|
|
361
|
+
for (const step of [...block.migrations ?? []].sort((a, b) => a.version - b.version)) {
|
|
362
|
+
for (const conversion of step.convertToLink ?? []) {
|
|
363
|
+
if (step.version <= previous.version && alreadyConverted(conversion, old)) continue;
|
|
364
|
+
checkConversionSource(`${file}: migrations (version ${step.version})`, conversion, old, errors);
|
|
365
|
+
}
|
|
366
|
+
for (const rename of step.renames ?? []) {
|
|
367
|
+
const prior = old.get(rename.from);
|
|
368
|
+
const at = `${file}: migrations (version ${step.version})`;
|
|
369
|
+
if (step.version <= previous.version) {
|
|
370
|
+
if (prior !== void 0 && !next.has(rename.from)) errors.push(`${at} — destructive rename requires a new version bump`);
|
|
371
|
+
continue;
|
|
372
|
+
}
|
|
373
|
+
if (prior === void 0) {
|
|
374
|
+
errors.push(`${at} — migration source does not exist in the previous local schema`);
|
|
375
|
+
continue;
|
|
376
|
+
}
|
|
377
|
+
if (sources.has(rename.from) || targets.has(rename.to) || targets.has(rename.from) || sources.has(rename.to)) errors.push(`${at} — pending steps require unavailable intermediate schema history`);
|
|
378
|
+
const destination = next.get(rename.to);
|
|
379
|
+
if (destination !== void 0 && !storageCompatible(prior, destination)) errors.push(`${at} — field type, cardinality, and storage shape changes are unsupported`);
|
|
380
|
+
sources.add(rename.from);
|
|
381
|
+
targets.add(rename.to);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
requireVersionBumpForRetirement(file, block, previous, old, next, errors);
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* What a conversion must be true of in the *incoming* fields: it writes a link,
|
|
388
|
+
* so `to` has to be one — or, for `shape: "list"`, a list whose item is one.
|
|
389
|
+
* Checked without any history, because it is a statement about the block being
|
|
390
|
+
* deployed rather than about what came before it.
|
|
391
|
+
*/
|
|
392
|
+
function checkConversionTarget(at, conversion, next, errors) {
|
|
393
|
+
const destination = next.get(conversion.to);
|
|
394
|
+
if (destination === void 0) {
|
|
395
|
+
errors.push(`${at} — conversion destination "${conversion.to}" does not exist in incoming fields`);
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
if (conversion.shape !== "list") {
|
|
399
|
+
if (destination.type !== "link") errors.push(`${at} — conversion destination "${conversion.to}" must be a link field`);
|
|
400
|
+
for (const key of ["url", "group"]) if (conversion[key] !== void 0) errors.push(`${at} — conversion ${key} is not allowed on shape "string"`);
|
|
401
|
+
if (conversion.children !== void 0) errors.push(`${at} — conversion children is not allowed on shape "string"`);
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
if (conversion.children !== void 0 && (conversion.children.url ?? conversion.url) === void 0) errors.push(`${at} — conversion children requires url, or a step-level url to inherit`);
|
|
405
|
+
if (listItem(destination)?.type !== "link") errors.push(`${at} — conversion destination "${conversion.to}" must be a list whose item is a link field`);
|
|
406
|
+
}
|
|
407
|
+
/**
|
|
408
|
+
* What a conversion must be true of in the *previous local manifest*: the field
|
|
409
|
+
* it reads has to exist, in the shape the step says it has, carrying the
|
|
410
|
+
* children it names. Advisory, like every other history check here — Core owns
|
|
411
|
+
* the installed schema and re-validates the same rules at ingest.
|
|
412
|
+
*/
|
|
413
|
+
/** Whether the previous local manifest already describes `from` as the link the
|
|
414
|
+
* conversion writes — the state a deploy leaves behind when `from` and `to`
|
|
415
|
+
* share an id. An absent `from` counts too: it has been retired. */
|
|
416
|
+
function alreadyConverted(conversion, old) {
|
|
417
|
+
const prior = old.get(conversion.from);
|
|
418
|
+
if (prior === void 0) return true;
|
|
419
|
+
return conversion.shape === "string" ? prior.type === "link" : listItem(prior)?.type === "link";
|
|
420
|
+
}
|
|
421
|
+
function checkConversionSource(at, conversion, old, errors) {
|
|
422
|
+
const prior = old.get(conversion.from);
|
|
423
|
+
if (prior === void 0) {
|
|
424
|
+
errors.push(`${at} — conversion source does not exist in the previous local schema`);
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
if (conversion.shape === "string") {
|
|
428
|
+
if (prior.type !== "string") errors.push(`${at} — conversion source "${conversion.from}" must be a string field`);
|
|
429
|
+
if (conversion.label !== void 0) {
|
|
430
|
+
const label = old.get(conversion.label);
|
|
431
|
+
if (conversion.label === conversion.from || label === void 0 || label.type !== "string") errors.push(`${at} — conversion label "${conversion.label}" must name another string field of the previous block`);
|
|
432
|
+
}
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
435
|
+
const item = listItem(prior);
|
|
436
|
+
if (prior.type !== "list" || item === null || item.type !== "composite") {
|
|
437
|
+
errors.push(`${at} — conversion source "${conversion.from}" must be a list of composites`);
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
const own = new Set(compositeFields(item).map((child) => child.fieldId));
|
|
441
|
+
for (const key of [
|
|
442
|
+
"label",
|
|
443
|
+
"url",
|
|
444
|
+
"group"
|
|
445
|
+
]) {
|
|
446
|
+
const named = conversion[key];
|
|
447
|
+
if (named !== void 0 && !own.has(named)) errors.push(`${at} — conversion ${key} "${named}" is not a child of "${conversion.from}"'s item`);
|
|
448
|
+
}
|
|
449
|
+
const children = conversion.children;
|
|
450
|
+
if (children === void 0) return;
|
|
451
|
+
const nestedItem = nestedListItem(item, children.from);
|
|
452
|
+
if (nestedItem === null) {
|
|
453
|
+
errors.push(`${at} — conversion children.from "${children.from}" is not a list of composites under "${conversion.from}"'s item`);
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
const nestedIds = new Set(compositeFields(nestedItem).map((child) => child.fieldId));
|
|
457
|
+
for (const key of [
|
|
458
|
+
"label",
|
|
459
|
+
"url",
|
|
460
|
+
"group"
|
|
461
|
+
]) {
|
|
462
|
+
const named = children[key] ?? conversion[key];
|
|
463
|
+
if (named !== void 0 && !nestedIds.has(named)) errors.push(`${at} — conversion children.${key} "${named}" is not a child of "${children.from}"'s item`);
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
/** The composite inside the named child list, when the child is a list of
|
|
467
|
+
* composites at all. */
|
|
468
|
+
function nestedListItem(item, childId) {
|
|
469
|
+
const child = compositeFields(item).find((field) => field.fieldId === childId);
|
|
470
|
+
if (child === void 0 || child.type !== "list") return null;
|
|
471
|
+
const nested = listItem(child);
|
|
472
|
+
return nested === null || nested.type !== "composite" ? null : nested;
|
|
473
|
+
}
|
|
474
|
+
function listItem(field) {
|
|
475
|
+
const item = field.metadata?.item;
|
|
476
|
+
return isRecord$1(item) ? item : null;
|
|
477
|
+
}
|
|
478
|
+
function compositeFields(field) {
|
|
479
|
+
const fields = field.metadata?.fields;
|
|
480
|
+
return Array.isArray(fields) ? fields.filter(isRecord$1) : [];
|
|
481
|
+
}
|
|
482
|
+
function requireVersionBumpForRetirement(file, block, previous, old, next, errors) {
|
|
483
|
+
const bumped = block.version > previous.version;
|
|
484
|
+
const renamedFrom = /* @__PURE__ */ new Set();
|
|
485
|
+
for (const step of block.migrations ?? []) for (const rename of step.renames ?? []) renamedFrom.add(rename.from);
|
|
486
|
+
for (const [fieldId, prior] of old) {
|
|
487
|
+
if (renamedFrom.has(fieldId)) continue;
|
|
488
|
+
const current = next.get(fieldId);
|
|
489
|
+
if (bumped) continue;
|
|
490
|
+
if (current === void 0) {
|
|
491
|
+
errors.push(`${file}: field ${fieldId} was removed; bump "version" to ${previous.version + 1} so Core retires the previous content`);
|
|
492
|
+
continue;
|
|
493
|
+
}
|
|
494
|
+
if (!storageCompatible(prior, current)) {
|
|
495
|
+
const reason = prior.type !== current.type ? `changed type (${prior.type} → ${current.type})` : prior.localized === true !== (current.localized === true) ? "changed localization" : "changed storage shape (media multiplicity / nested field)";
|
|
496
|
+
errors.push(`${file}: field ${fieldId} ${reason}; bump "version" to ${previous.version + 1} so Core retires the previous content`);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
function storageCompatible(oldValue, nextValue) {
|
|
501
|
+
const old = storedMigrationField(oldValue);
|
|
502
|
+
const next = storedMigrationField(nextValue);
|
|
503
|
+
if (old === null || next === null) return false;
|
|
504
|
+
if (old.type !== next.type || old.localized === true !== (next.localized === true)) return false;
|
|
505
|
+
switch (old.type) {
|
|
506
|
+
case "media": return multiple(old, true) === multiple(next, true);
|
|
507
|
+
case "select": return multiple(old, false) === multiple(next, false);
|
|
508
|
+
case "reference": return old.relation?.multiple === true === (next.relation?.multiple === true);
|
|
509
|
+
case "composite":
|
|
510
|
+
case "list": {
|
|
511
|
+
const oldChildren = childFields(old);
|
|
512
|
+
const nextChildren = childFields(next);
|
|
513
|
+
if (oldChildren.length !== nextChildren.length) return false;
|
|
514
|
+
const byId = new Map(nextChildren.map((field) => [field.fieldId, field]));
|
|
515
|
+
return oldChildren.every((field) => {
|
|
516
|
+
const other = byId.get(field.fieldId);
|
|
517
|
+
return other !== void 0 && storageCompatible(field, other);
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
default: return true;
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
function multiple(field, fallback) {
|
|
524
|
+
return typeof field.metadata?.multiple === "boolean" ? field.metadata.multiple : fallback;
|
|
525
|
+
}
|
|
526
|
+
function childFields(field) {
|
|
527
|
+
if (field.type === "list") {
|
|
528
|
+
const item = field.metadata?.item;
|
|
529
|
+
return isRecord$1(item) ? [item] : null;
|
|
530
|
+
}
|
|
531
|
+
const fields = field.metadata?.fields;
|
|
532
|
+
return Array.isArray(fields) && fields.length > 0 ? fields : null;
|
|
533
|
+
}
|
|
534
|
+
function validMigrationFieldShape(value, depth = 1) {
|
|
535
|
+
return storedMigrationField(value, depth) !== null;
|
|
536
|
+
}
|
|
537
|
+
function storedMigrationField(raw, depth = 1) {
|
|
538
|
+
const value = storedObject(raw, STORED_FIELD_KEYS);
|
|
539
|
+
if (value !== null) for (const key of IGNORED_FIELD_KEYS) delete value[key];
|
|
540
|
+
if (value === null || typeof value.fieldId !== "string" || !/^[a-zA-Z0-9_]+$/.test(value.fieldId) || typeof value.name !== "string" || value.name === "" || typeof value.type !== "string" || !STORED_FIELD_TYPES.has(value.type) || value.localized != null && typeof value.localized !== "boolean" || depth > 5) return null;
|
|
541
|
+
if ([
|
|
542
|
+
"groupId",
|
|
543
|
+
"presetId",
|
|
544
|
+
"description",
|
|
545
|
+
"helpText"
|
|
546
|
+
].some((key) => value[key] != null && typeof value[key] !== "string") || value.isTitle != null && typeof value.isTitle !== "boolean" || value.metadata != null && !isRecord$1(value.metadata) || !validStoredValidators(value.validators)) return null;
|
|
547
|
+
if (value.relation != null) {
|
|
548
|
+
const relation = storedObject(value.relation, STORED_RELATION_KEYS);
|
|
549
|
+
if (relation === null || !validStoredRelation(relation)) return null;
|
|
550
|
+
value.relation = relation;
|
|
551
|
+
}
|
|
552
|
+
if (value.type !== "composite" && value.type !== "list") return value;
|
|
553
|
+
if (!isRecord$1(value.metadata)) return null;
|
|
554
|
+
const children = childFields(value);
|
|
555
|
+
if (children === null) return null;
|
|
556
|
+
const normalized = children.map((child) => storedMigrationField(child, depth + 1));
|
|
557
|
+
if (normalized.some((child) => child === null)) return null;
|
|
558
|
+
const fields = normalized;
|
|
559
|
+
if (new Set(fields.map((child) => child.fieldId)).size !== fields.length) return null;
|
|
560
|
+
value.metadata = {
|
|
561
|
+
...value.metadata,
|
|
562
|
+
...value.type === "list" ? { item: fields[0] } : { fields }
|
|
563
|
+
};
|
|
564
|
+
return value;
|
|
565
|
+
}
|
|
566
|
+
function validStoredRelation(value) {
|
|
567
|
+
if (value == null) return true;
|
|
568
|
+
if (!isRecord$1(value)) return false;
|
|
569
|
+
return [
|
|
570
|
+
"multiple",
|
|
571
|
+
"allowProducts",
|
|
572
|
+
"allowCollections"
|
|
573
|
+
].every((key) => value[key] == null || typeof value[key] === "boolean") && ["allowedTagIds", "allowedSchemaIds"].every((key) => value[key] == null || Array.isArray(value[key]) && value[key].every((id) => id === null || typeof id === "string"));
|
|
574
|
+
}
|
|
575
|
+
function validStoredValidators(value) {
|
|
576
|
+
if (value == null) return true;
|
|
577
|
+
if (!isRecord$1(value)) return false;
|
|
578
|
+
return Object.entries(value).every(([key, item]) => {
|
|
579
|
+
switch (key) {
|
|
580
|
+
case "min":
|
|
581
|
+
case "max":
|
|
582
|
+
case "minLength":
|
|
583
|
+
case "maxLength": return item == null || typeof item === "number" && Number.isFinite(item);
|
|
584
|
+
case "unique":
|
|
585
|
+
case "required": return item == null || typeof item === "boolean";
|
|
586
|
+
case "match":
|
|
587
|
+
case "prohibit": {
|
|
588
|
+
if (item == null) return true;
|
|
589
|
+
const pattern = storedObject(item, ["preset", "custom"]);
|
|
590
|
+
return pattern !== null && ["preset", "custom"].every((name) => pattern[name] == null || typeof pattern[name] === "string");
|
|
591
|
+
}
|
|
592
|
+
default: return false;
|
|
593
|
+
}
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
function storedObject(value, keys) {
|
|
597
|
+
if (!isRecord$1(value)) return null;
|
|
598
|
+
const normalized = { ...value };
|
|
599
|
+
const seen = /* @__PURE__ */ new Set();
|
|
600
|
+
for (const [key, item] of Object.entries(value)) {
|
|
601
|
+
const folded = key.toLowerCase().replace(/ſ/g, "s");
|
|
602
|
+
const canonical = keys.find((name) => name.toLowerCase() === folded);
|
|
603
|
+
if (canonical === void 0) continue;
|
|
604
|
+
if (seen.has(canonical)) return null;
|
|
605
|
+
seen.add(canonical);
|
|
606
|
+
delete normalized[key];
|
|
607
|
+
normalized[canonical] = item;
|
|
608
|
+
}
|
|
609
|
+
return normalized;
|
|
610
|
+
}
|
|
611
|
+
function isRecord$1(value) {
|
|
612
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
613
|
+
}
|
|
614
|
+
//#endregion
|
|
615
|
+
//#region src/util.ts
|
|
616
|
+
/** Helpers shared by the scanner and the validators it calls. Kept in their own
|
|
617
|
+
* module so nothing has to import back out of `scan.ts`, which would make the
|
|
618
|
+
* graph circular. */
|
|
619
|
+
function isRecord(value) {
|
|
620
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
621
|
+
}
|
|
622
|
+
/** Drops Unicode Cc (control) and Cf (format) runes, mirroring Core's
|
|
623
|
+
* stripControlRunes, so a title cannot smuggle invisible characters past a
|
|
624
|
+
* length check. */
|
|
625
|
+
function stripPlainTextControls(value) {
|
|
626
|
+
return typeof value === "string" ? value.replace(/[\p{Cc}\p{Cf}]/gu, "") : "";
|
|
627
|
+
}
|
|
628
|
+
/** Length in code points, not UTF-16 units: an emoji counts once. */
|
|
629
|
+
function codePointLength(value) {
|
|
630
|
+
return Array.from(value).length;
|
|
631
|
+
}
|
|
632
|
+
//#endregion
|
|
633
|
+
//#region src/seedData.ts
|
|
634
|
+
const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
635
|
+
const MEDIA_VALUE_KEYS = /* @__PURE__ */ new Set(["assetId", "framing"]);
|
|
636
|
+
/** The two shapes a seed may name a referenced object by: one that already
|
|
637
|
+
* exists (`id`), or a catalog collection Core looks up by slug on deploy. */
|
|
638
|
+
const REFERENCE_ID_KEYS = /* @__PURE__ */ new Set(["_type", "id"]);
|
|
639
|
+
const REFERENCE_SLUG_KEYS = /* @__PURE__ */ new Set(["_type", "slug"]);
|
|
640
|
+
const REFERENCE_MESSAGE = "reference values must be absent, {_type, id: uuid} or {_type: \"collection\", slug} — Core resolves the slug at seed time";
|
|
641
|
+
const REFERENCE_RELATION_MESSAGE = "a {_type: \"collection\", slug} seed reference needs a relation with allowCollections";
|
|
642
|
+
/** A `link` target named by handle. Wider than `REFERENCE_SLUG_KEYS` on
|
|
643
|
+
* purpose: Core resolves a link target's slug against both catalogs, while the
|
|
644
|
+
* `reference` field's own seed grammar stays collection-only. */
|
|
645
|
+
const LINK_SLUG_KEYS = /* @__PURE__ */ new Set(["_type", "slug"]);
|
|
646
|
+
const LINK_SLUG_TYPES = /* @__PURE__ */ new Set(["product", "collection"]);
|
|
647
|
+
const LINK_KINDS = /* @__PURE__ */ new Set([
|
|
648
|
+
"product",
|
|
649
|
+
"collection",
|
|
650
|
+
"category",
|
|
651
|
+
"entry",
|
|
652
|
+
"page",
|
|
653
|
+
"url",
|
|
654
|
+
"none"
|
|
655
|
+
]);
|
|
656
|
+
const LINK_MESSAGE = "link values must be {kind, …}: a target is absent, {_type, id: uuid} or {_type: \"product\"|\"collection\", slug} — Core resolves the slug at seed time";
|
|
657
|
+
const LINK_NONE_MESSAGE = "a link with kind \"none\" is a heading: it needs a label and children, and carries no target or url";
|
|
658
|
+
/** The url allowlist is the platform write side's, applied here through the
|
|
659
|
+
* kit's single implementation (`safeLinkHref`) so a scan refuses exactly what a
|
|
660
|
+
* deploy refuses: a seed carrying `javascript:` or a bare `#` used to pass the
|
|
661
|
+
* scan and fail at the deploy, which is the one place an author cannot fix it
|
|
662
|
+
* offline. */
|
|
663
|
+
const LINK_URL_MESSAGE = "a link with kind \"url\" needs a url the platform stores: a rooted /path that is not //, an #anchor with something after it, an absolute http(s):// url with a host, or a mailto:/tel: with an address — at most 2048 bytes, no backslash and no control characters";
|
|
664
|
+
/**
|
|
665
|
+
* Validates one seed's data against the block's own fields: the two value
|
|
666
|
+
* shapes Core's write side refuses outright are media and reference values.
|
|
667
|
+
*
|
|
668
|
+
* **Media.** Studio seeds a freshly-inserted block's CMS entry from `mock.json`
|
|
669
|
+
* verbatim, and the CMS's write-side media validator only accepts
|
|
670
|
+
* `{ assetId: <uuid>, framing? }` — the starter's old convention of
|
|
671
|
+
* embedding a Storybook fixture
|
|
672
|
+
* (`{ assetId: "demo-<name>", url, altText }`) in a media field 400s every
|
|
673
|
+
* such insert. Seed data must therefore either omit a media field entirely
|
|
674
|
+
* or carry a write-valid value; demo imagery belongs in the sibling
|
|
675
|
+
* `preview.json` overlay instead. Walks the block's field tree (including
|
|
676
|
+
* `list`/`composite` nesting) alongside the parsed data object so a media
|
|
677
|
+
* field buried inside `feature-grid`'s `items` or `gallery`'s multi-value
|
|
678
|
+
* `images` is checked too, not just top-level fields. A theme's template
|
|
679
|
+
* seeds are the same kind of write, so they go through the same walk.
|
|
680
|
+
*
|
|
681
|
+
* **References.** A `reference` field's seed value either names an object that
|
|
682
|
+
* already exists (`{_type, id: <uuid>}`) or, for a relation that allows catalog
|
|
683
|
+
* collections, names one by slug (`{_type: "collection", slug}`) — the form a
|
|
684
|
+
* theme ships, because a theme cannot know the organisation's collection ids.
|
|
685
|
+
* Core resolves that slug against the organisation's own catalog at seed time
|
|
686
|
+
* and leaves the field empty when nothing matches, so a deploy still succeeds.
|
|
687
|
+
* Anything else — a bare handle string, a resolved read's full object, a slug
|
|
688
|
+
* on a relation that allows no collection — would be written verbatim into an
|
|
689
|
+
* entry Core then refuses, so it is refused here with the path that carries it.
|
|
690
|
+
*
|
|
691
|
+
* **Links.** A `link` field's seed value carries a `kind` and, for every kind
|
|
692
|
+
* but `url`, an optional `target`. A product or a collection may be named by
|
|
693
|
+
* handle (`{_type: "product", slug}`) because a theme cannot know an
|
|
694
|
+
* organisation's catalog ids and Core resolves the handle at seed time; the
|
|
695
|
+
* other kinds address organisation-owned objects a theme has no portable name
|
|
696
|
+
* for, so a seed leaves their target out and an author fills it in. Children
|
|
697
|
+
* are walked too, and one of them may not carry children of its own — which
|
|
698
|
+
* also means `kind: "none"`, the heading that must carry children, can only
|
|
699
|
+
* ever be a top-level item. A `kind: "url"` value goes through the same href
|
|
700
|
+
* allowlist the platform's write side applies (`safeLinkHref`), not merely a
|
|
701
|
+
* non-blank check, so a scan refuses what a deploy would refuse rather than
|
|
702
|
+
* leaving an author to discover it there.
|
|
703
|
+
*/
|
|
704
|
+
function checkSeedData(format, fields, data, errors, pathPrefix = "") {
|
|
705
|
+
for (const field of fields) {
|
|
706
|
+
const fieldId = String(field.fieldId ?? "");
|
|
707
|
+
if (fieldId === "" || !Object.hasOwn(data, fieldId)) continue;
|
|
708
|
+
const value = data[fieldId];
|
|
709
|
+
const path = pathPrefix === "" ? fieldId : `${pathPrefix}.${fieldId}`;
|
|
710
|
+
const metadata = field.metadata !== null && typeof field.metadata === "object" ? field.metadata : {};
|
|
711
|
+
if (field.type === "media") {
|
|
712
|
+
if (metadata.multiple === true) {
|
|
713
|
+
if (Array.isArray(value)) value.forEach((item, index) => checkMediaValue(format, `${path}[${index}]`, item, errors));
|
|
714
|
+
else checkMediaValue(format, path, value, errors);
|
|
715
|
+
} else checkMediaValue(format, path, value, errors);
|
|
716
|
+
continue;
|
|
717
|
+
}
|
|
718
|
+
if (field.type === "reference") {
|
|
719
|
+
const relation = isRecord(field.relation) ? field.relation : {};
|
|
720
|
+
if (relation.multiple === true) {
|
|
721
|
+
if (Array.isArray(value)) value.forEach((item, index) => checkReferenceValue(format, `${path}[${index}]`, item, relation, errors));
|
|
722
|
+
else checkReferenceValue(format, path, value, relation, errors);
|
|
723
|
+
} else checkReferenceValue(format, path, value, relation, errors);
|
|
724
|
+
continue;
|
|
725
|
+
}
|
|
726
|
+
if (field.type === "link") {
|
|
727
|
+
checkLinkValue(format, path, value, errors);
|
|
728
|
+
continue;
|
|
729
|
+
}
|
|
730
|
+
if (field.type === "composite") {
|
|
731
|
+
const nestedFields = Array.isArray(metadata.fields) ? metadata.fields : [];
|
|
732
|
+
if (value !== null && typeof value === "object" && !Array.isArray(value)) checkSeedData(format, nestedFields, value, errors, path);
|
|
733
|
+
continue;
|
|
734
|
+
}
|
|
735
|
+
if (field.type === "list") {
|
|
736
|
+
const item = isRecord(metadata.item) ? metadata.item : null;
|
|
737
|
+
if (item === null || !Array.isArray(value)) continue;
|
|
738
|
+
const itemFields = item.type === "composite" && Array.isArray(item.metadata?.fields) ? item.metadata.fields : null;
|
|
739
|
+
value.forEach((entry, index) => {
|
|
740
|
+
if (item.type === "media") checkMediaValue(format, `${path}[${index}]`, entry, errors);
|
|
741
|
+
else if (item.type === "link") checkLinkValue(format, `${path}[${index}]`, entry, errors);
|
|
742
|
+
else if (itemFields !== null && entry !== null && typeof entry === "object") checkSeedData(format, itemFields, entry, errors, `${path}[${index}]`);
|
|
743
|
+
});
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
function checkMediaValue(format, path, value, errors) {
|
|
748
|
+
if (!(value !== null && typeof value === "object" && !Array.isArray(value) && Object.keys(value).every((key) => MEDIA_VALUE_KEYS.has(key)) && typeof value.assetId === "string" && UUID_PATTERN.test(value.assetId))) errors.push(format(path, "media values must be {assetId: uuid} — use preview.json for demo imagery"));
|
|
749
|
+
}
|
|
750
|
+
/**
|
|
751
|
+
* One reference value. `_type` is required in both forms — Core reads it to
|
|
752
|
+
* know which catalog the identifier belongs to — and the id form is checked
|
|
753
|
+
* only for shape: whether that object exists is the deploy's to find out,
|
|
754
|
+
* exactly like the slug form.
|
|
755
|
+
*/
|
|
756
|
+
function checkReferenceValue(format, path, value, relation, errors) {
|
|
757
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
758
|
+
errors.push(format(path, REFERENCE_MESSAGE));
|
|
759
|
+
return;
|
|
760
|
+
}
|
|
761
|
+
const record = value;
|
|
762
|
+
const keys = Object.keys(record);
|
|
763
|
+
const type = typeof record._type === "string" ? record._type.trim() : "";
|
|
764
|
+
const byId = keys.every((key) => REFERENCE_ID_KEYS.has(key)) && type !== "" && typeof record.id === "string" && UUID_PATTERN.test(record.id);
|
|
765
|
+
const bySlug = keys.every((key) => REFERENCE_SLUG_KEYS.has(key)) && type === "collection" && typeof record.slug === "string" && record.slug.trim() !== "";
|
|
766
|
+
if (!byId && !bySlug) {
|
|
767
|
+
errors.push(format(path, REFERENCE_MESSAGE));
|
|
768
|
+
return;
|
|
769
|
+
}
|
|
770
|
+
if (bySlug && relation.allowCollections !== true) errors.push(format(path, REFERENCE_RELATION_MESSAGE));
|
|
771
|
+
}
|
|
772
|
+
/**
|
|
773
|
+
* One link value, and each of its children.
|
|
774
|
+
*
|
|
775
|
+
* A theme cannot know an organisation's catalog ids, so a seed may name a
|
|
776
|
+
* product or a collection by handle — `{_type: "product", slug}` — and Core
|
|
777
|
+
* resolves it against that organisation's own catalog on deploy, leaving the
|
|
778
|
+
* link out when nothing matches rather than failing the deploy. The other four
|
|
779
|
+
* kinds have no portable handle (a category, an entry and a page are all
|
|
780
|
+
* organisation-owned), so they can only be seeded by an id a theme does not
|
|
781
|
+
* have — which is why a seed for them names no target at all and an author
|
|
782
|
+
* fills it in.
|
|
783
|
+
*
|
|
784
|
+
* A target's `_type` must mirror its `kind` in both forms, which is what Core's
|
|
785
|
+
* own validator refuses a mismatch on at deploy; checking it here moves that
|
|
786
|
+
* failure to the scan instead.
|
|
787
|
+
*
|
|
788
|
+
* Unlike `reference`, a `link` carries no `relation`: which catalogs it may
|
|
789
|
+
* name is the type's own grammar rather than the field's, so there is no
|
|
790
|
+
* analogue of the relation check above.
|
|
791
|
+
*/
|
|
792
|
+
function checkLinkValue(format, path, value, errors, isChild = false) {
|
|
793
|
+
if (!isRecord(value)) {
|
|
794
|
+
errors.push(format(path, LINK_MESSAGE));
|
|
795
|
+
return;
|
|
796
|
+
}
|
|
797
|
+
const kind = typeof value.kind === "string" ? value.kind.trim() : "";
|
|
798
|
+
if (!LINK_KINDS.has(kind)) {
|
|
799
|
+
errors.push(format(path, LINK_MESSAGE));
|
|
800
|
+
return;
|
|
801
|
+
}
|
|
802
|
+
if (kind === "none") {
|
|
803
|
+
if (typeof value.label !== "string" || value.label.trim() === "" || value.target !== void 0 || value.url !== void 0 || !Array.isArray(value.children) || value.children.length === 0) errors.push(format(path, LINK_NONE_MESSAGE));
|
|
804
|
+
} else if (kind === "url") {
|
|
805
|
+
if (value.target !== void 0) errors.push(format(path, LINK_MESSAGE));
|
|
806
|
+
else if (safeLinkHref(value.url) === null) errors.push(format(path, LINK_URL_MESSAGE));
|
|
807
|
+
} else if (value.url !== void 0) errors.push(format(path, LINK_MESSAGE));
|
|
808
|
+
else if (value.target !== void 0 && !validLinkTarget(value.target, kind)) errors.push(format(path, LINK_MESSAGE));
|
|
809
|
+
if (value.children === void 0) return;
|
|
810
|
+
if (isChild || !Array.isArray(value.children)) {
|
|
811
|
+
errors.push(format(path, LINK_MESSAGE));
|
|
812
|
+
return;
|
|
813
|
+
}
|
|
814
|
+
value.children.forEach((child, index) => checkLinkValue(format, `${path}.children[${index}]`, child, errors, true));
|
|
815
|
+
}
|
|
816
|
+
/**
|
|
817
|
+
* A target's `_type` mirrors its `kind` exactly — `product`, `collection`,
|
|
818
|
+
* `category` and `page` by name, and `entry` for `kind: "entry"`, the value
|
|
819
|
+
* Core folds `entry_<apiId>` into. Checking it here is what moves a mismatch
|
|
820
|
+
* forward from the deploy, where Core's own validator refuses it, to the scan.
|
|
821
|
+
*/
|
|
822
|
+
function targetTypeMatchesKind(type, kind) {
|
|
823
|
+
return type === kind;
|
|
824
|
+
}
|
|
825
|
+
function validLinkTarget(target, kind) {
|
|
826
|
+
if (!isRecord(target)) return false;
|
|
827
|
+
const keys = Object.keys(target);
|
|
828
|
+
const type = typeof target._type === "string" ? target._type.trim() : "";
|
|
829
|
+
if (type === "" || !targetTypeMatchesKind(type, kind)) return false;
|
|
830
|
+
if (keys.every((key) => REFERENCE_ID_KEYS.has(key)) && typeof target.id === "string" && UUID_PATTERN.test(target.id)) return true;
|
|
831
|
+
return keys.every((key) => LINK_SLUG_KEYS.has(key)) && LINK_SLUG_TYPES.has(kind) && typeof target.slug === "string" && target.slug.trim() !== "";
|
|
832
|
+
}
|
|
833
|
+
//#endregion
|
|
834
|
+
//#region src/templates.ts
|
|
835
|
+
const MAX_TEMPLATES = 8;
|
|
836
|
+
const MAX_PAGE_SEEDS = 16;
|
|
837
|
+
const MAX_TEMPLATE_BLOCKS = 50;
|
|
838
|
+
const MAX_TITLE_LENGTH = 80;
|
|
839
|
+
const HOME_ROUTE_PATTERN = "/";
|
|
840
|
+
const CATALOG_SLUG_PARAM = "slug";
|
|
841
|
+
/**
|
|
842
|
+
* A category seed's own parameter, and it is a different shape: a category is
|
|
843
|
+
* addressed by its **canonical path** — the slugs of its ancestors, root first,
|
|
844
|
+
* then its own — so the pattern's trailing parameter is the catch-all `:path*`
|
|
845
|
+
* and the field it is resolved by is `path`. Core enforces the same pairing in
|
|
846
|
+
* route-template entry validation and in the manifest seed's target check, so a
|
|
847
|
+
* seed this accepted and the deploy refused would be a scan that lied.
|
|
848
|
+
*/
|
|
849
|
+
const CATEGORY_PATH_PARAM = "path";
|
|
850
|
+
const CATEGORY_SCHEMA_API_ID = "catalog:category";
|
|
851
|
+
/** The same id shape layout nodes take everywhere else in the kit. */
|
|
852
|
+
const NODE_ID_PATTERN = /^[a-z][a-z0-9-]{0,47}$/;
|
|
853
|
+
const SCHEMA_API_IDS = /* @__PURE__ */ new Set([
|
|
854
|
+
"catalog:product",
|
|
855
|
+
"catalog:collection",
|
|
856
|
+
CATEGORY_SCHEMA_API_ID,
|
|
857
|
+
"home"
|
|
858
|
+
]);
|
|
859
|
+
/** The target the home seed also emits a page seed for, and the slug it takes:
|
|
860
|
+
* the site root Page Core has always created from it. */
|
|
861
|
+
const HOME_SCHEMA_API_ID = "home";
|
|
862
|
+
const HOME_PAGE_SLUG = "home";
|
|
863
|
+
/** Core's own page-seed slug rule, mirrored exactly so the scan refuses what
|
|
864
|
+
* the ingest refuses: lowercase words joined by single hyphens, starting with a
|
|
865
|
+
* letter. The length cap is the kit's own — a slug is one path segment. */
|
|
866
|
+
const PAGE_SLUG_PATTERN = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/;
|
|
867
|
+
const MAX_PAGE_SLUG_LENGTH = 64;
|
|
868
|
+
const MAX_PAGE_SEED_BLOCKS = 50;
|
|
869
|
+
/** The two reserved entry types a page seed places a shared region with. They
|
|
870
|
+
* begin with `@`, which no block apiId may, so they can never collide. */
|
|
871
|
+
const REGION_TYPES = {
|
|
872
|
+
header: "@header",
|
|
873
|
+
footer: "@footer"
|
|
874
|
+
};
|
|
875
|
+
/**
|
|
876
|
+
* Every key each shape of a page seed may carry. Anything else is refused by
|
|
877
|
+
* name rather than dropped on the way to the manifest: Core decodes the
|
|
878
|
+
* manifest with unknown fields disallowed, so a key this swallowed would fail
|
|
879
|
+
* the **whole** ingest — every block, token and template in that deploy — with
|
|
880
|
+
* a message about a file the author never wrote. Refusing it here names the
|
|
881
|
+
* path instead, at the one moment the author can fix it.
|
|
882
|
+
*
|
|
883
|
+
* The three that are most tempting are the three a *template* seed does take:
|
|
884
|
+
* `layout`, `header` and `footer` steer a template's generated layout, and a
|
|
885
|
+
* page seed has none — its entry list is the page. Likewise `templates` and
|
|
886
|
+
* `bindings` belong to a template seed's block node, which resolves them
|
|
887
|
+
* against the object its route matched; a page matches no object.
|
|
888
|
+
*/
|
|
889
|
+
const PAGE_SEED_KEYS = /* @__PURE__ */ new Set([
|
|
890
|
+
"page",
|
|
891
|
+
"title",
|
|
892
|
+
"blocks"
|
|
893
|
+
]);
|
|
894
|
+
const PAGE_SEED_BLOCK_KEYS = /* @__PURE__ */ new Set([
|
|
895
|
+
"apiId",
|
|
896
|
+
"data",
|
|
897
|
+
"required"
|
|
898
|
+
]);
|
|
899
|
+
const PAGE_SEED_REGION_KEYS = /* @__PURE__ */ new Set(["role"]);
|
|
900
|
+
/** Why a key a page seed cannot take exists at all, where there is an answer —
|
|
901
|
+
* so the error says what to do instead of only what is wrong. `schemaApiId` and
|
|
902
|
+
* `routePattern` are deliberately absent: a seed carrying either of them beside
|
|
903
|
+
* `page` is the "names both targets" error and never reaches this list. */
|
|
904
|
+
const PAGE_SEED_KEY_HINTS = {
|
|
905
|
+
layout: "a page seed declares no layout: its `blocks` are the page, in order",
|
|
906
|
+
header: "place the region instead: an entry `{ role: \"header\" }` among the blocks",
|
|
907
|
+
footer: "place the region instead: an entry `{ role: \"footer\" }` among the blocks",
|
|
908
|
+
templates: "only a route template's block node takes templates: a page matches no object",
|
|
909
|
+
bindings: "only a route template's block node takes bindings: a page matches no object",
|
|
910
|
+
id: "a page seed emits no node ids — there is no layout to reference one from"
|
|
911
|
+
};
|
|
912
|
+
/**
|
|
913
|
+
* Reports every key `allowed` does not list, with the hint above when there is
|
|
914
|
+
* one. Called on the page seed, on each of its block entries and on each region
|
|
915
|
+
* placement, because Core refuses an unknown key at any depth.
|
|
916
|
+
*/
|
|
917
|
+
function checkPageSeedKeys(at, value, allowed, errors) {
|
|
918
|
+
for (const key of Object.keys(value)) {
|
|
919
|
+
if (allowed.has(key)) continue;
|
|
920
|
+
const hint = PAGE_SEED_KEY_HINTS[key];
|
|
921
|
+
errors.push(`${at}.${key} — unknown key on a page seed${hint === void 0 ? "" : ` (${hint})`}`);
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
const ROLES = /* @__PURE__ */ new Set(["header", "footer"]);
|
|
925
|
+
const ROOT_NODE_ID = "root";
|
|
926
|
+
const ROLE_NODE_IDS = {
|
|
927
|
+
header: "role-header",
|
|
928
|
+
footer: "role-footer"
|
|
929
|
+
};
|
|
930
|
+
/** A target path into a block's fields, the grammar `@eldrajs/theme-core`'s
|
|
931
|
+
* template layout already reads: identifier segments, list indices allowed
|
|
932
|
+
* (`heading`, `items.0.label`). The first segment must be a field the block
|
|
933
|
+
* declares. */
|
|
934
|
+
const TARGET_FIELD_PATH = /^[a-z][a-zA-Z0-9]{0,48}(?:\.(?:[a-z][a-zA-Z0-9]{0,48}|0|[1-9][0-9]*))*$/;
|
|
935
|
+
/** The two maps a seed block may carry for its layout node. */
|
|
936
|
+
const NODE_BINDING_KEYS = ["templates", "bindings"];
|
|
937
|
+
/**
|
|
938
|
+
* The layout a seed gets when it declares none: one column of its blocks in
|
|
939
|
+
* order, framed by the header and footer roles unless the seed opts out. The
|
|
940
|
+
* roles are resolved to the site's own reusable components on deploy, which is
|
|
941
|
+
* why they travel as a role rather than an id.
|
|
942
|
+
*/
|
|
943
|
+
function seedLayout(seed) {
|
|
944
|
+
const children = [];
|
|
945
|
+
if (seed.header !== false) children.push({
|
|
946
|
+
id: ROLE_NODE_IDS.header,
|
|
947
|
+
type: "reusable",
|
|
948
|
+
role: "header"
|
|
949
|
+
});
|
|
950
|
+
for (const block of Array.isArray(seed.blocks) ? seed.blocks : []) children.push({
|
|
951
|
+
id: block.id,
|
|
952
|
+
type: "block",
|
|
953
|
+
entryId: block.id,
|
|
954
|
+
...nodeBindingsOf(block)
|
|
955
|
+
});
|
|
956
|
+
if (seed.footer !== false) children.push({
|
|
957
|
+
id: ROLE_NODE_IDS.footer,
|
|
958
|
+
type: "reusable",
|
|
959
|
+
role: "footer"
|
|
960
|
+
});
|
|
961
|
+
return {
|
|
962
|
+
version: 1,
|
|
963
|
+
root: {
|
|
964
|
+
id: ROOT_NODE_ID,
|
|
965
|
+
type: "flex",
|
|
966
|
+
layout: { direction: { normal: "column" } },
|
|
967
|
+
children
|
|
968
|
+
}
|
|
969
|
+
};
|
|
970
|
+
}
|
|
971
|
+
/** The `templates`/`bindings` a seed block carries, as the keys its layout node
|
|
972
|
+
* takes — omitted entirely when it declared neither, so a seed without bindings
|
|
973
|
+
* emits exactly the node it always did. */
|
|
974
|
+
function nodeBindingsOf(block) {
|
|
975
|
+
const carried = {};
|
|
976
|
+
for (const key of NODE_BINDING_KEYS) {
|
|
977
|
+
const value = block?.[key];
|
|
978
|
+
if (value !== void 0) carried[key] = value;
|
|
979
|
+
}
|
|
980
|
+
return carried;
|
|
981
|
+
}
|
|
982
|
+
/** Which target a declared seed names: a route template (or home), a static
|
|
983
|
+
* page, both — which is the error the two forms exist to keep apart — or
|
|
984
|
+
* neither. A page target is the presence of `page`, a template target the
|
|
985
|
+
* presence of either of the two keys one needs, so a seed that half-declares
|
|
986
|
+
* one still reports against that form rather than silently becoming the other.
|
|
987
|
+
*/
|
|
988
|
+
function seedTargetOf(entry) {
|
|
989
|
+
const page = entry.page !== void 0;
|
|
990
|
+
const template = entry.schemaApiId !== void 0 || entry.routePattern !== void 0;
|
|
991
|
+
if (page && template) return "both";
|
|
992
|
+
if (page) return "page";
|
|
993
|
+
if (template) return "template";
|
|
994
|
+
return "none";
|
|
995
|
+
}
|
|
996
|
+
/** The declared entries that name a template target, with their index in the
|
|
997
|
+
* declared list — the index every error path is written against, so an author
|
|
998
|
+
* reads it against the array they wrote. Entries naming a page target belong to
|
|
999
|
+
* `validatePageSeeds`; one naming both is reported here, once, and belongs to
|
|
1000
|
+
* neither. */
|
|
1001
|
+
function templateEntriesOf(seeds, errors) {
|
|
1002
|
+
const entries = [];
|
|
1003
|
+
for (const [index, entry] of seeds.entries()) {
|
|
1004
|
+
if (!isRecord(entry)) {
|
|
1005
|
+
errors.push(`templates[${index}] — must be an object`);
|
|
1006
|
+
continue;
|
|
1007
|
+
}
|
|
1008
|
+
const target = seedTargetOf(entry);
|
|
1009
|
+
if (target === "page") continue;
|
|
1010
|
+
if (target === "both") {
|
|
1011
|
+
errors.push(`templates[${index}] — declares both a template target (schemaApiId) and a page target (page.slug): a seed names one or the other`);
|
|
1012
|
+
continue;
|
|
1013
|
+
}
|
|
1014
|
+
entries.push([index, entry]);
|
|
1015
|
+
}
|
|
1016
|
+
return entries;
|
|
1017
|
+
}
|
|
1018
|
+
/**
|
|
1019
|
+
* Validates the theme's declared template seeds against the blocks it ships and
|
|
1020
|
+
* returns them in manifest shape: every seed with a layout, and without the
|
|
1021
|
+
* `header`/`footer` switches, which steer the generated layout and are never
|
|
1022
|
+
* emitted.
|
|
1023
|
+
*
|
|
1024
|
+
* The declared list also carries static **page** seeds (`page: { slug }`);
|
|
1025
|
+
* those are skipped here and validated by `validatePageSeeds`, which reads the
|
|
1026
|
+
* same list. Only the template seeds count against the template cap.
|
|
1027
|
+
*/
|
|
1028
|
+
function validateTemplateSeeds(seeds, blocks, errors) {
|
|
1029
|
+
if (!Array.isArray(seeds)) {
|
|
1030
|
+
errors.push("templates — must be an array");
|
|
1031
|
+
return [];
|
|
1032
|
+
}
|
|
1033
|
+
const entries = templateEntriesOf(seeds, errors);
|
|
1034
|
+
if (entries.length > MAX_TEMPLATES) errors.push(`templates: contains ${entries.length} templates — exceeds ${MAX_TEMPLATES}`);
|
|
1035
|
+
const blockFields = /* @__PURE__ */ new Map();
|
|
1036
|
+
for (const block of blocks) {
|
|
1037
|
+
if (typeof block.apiId !== "string") continue;
|
|
1038
|
+
blockFields.set(block.apiId, Array.isArray(block.fields) ? block.fields : []);
|
|
1039
|
+
}
|
|
1040
|
+
const normalized = [];
|
|
1041
|
+
const patterns = /* @__PURE__ */ new Set();
|
|
1042
|
+
for (const [index, entry] of entries.slice(0, MAX_TEMPLATES)) {
|
|
1043
|
+
const at = `templates[${index}]`;
|
|
1044
|
+
const seed = entry;
|
|
1045
|
+
if (!SCHEMA_API_IDS.has(seed.schemaApiId)) errors.push(`${at}.schemaApiId — must be "catalog:product", "catalog:collection", "catalog:category" or "home" (a static page seed names \`page: { slug }\` instead)`);
|
|
1046
|
+
else checkRoutePattern(at, seed, errors);
|
|
1047
|
+
const title = stripPlainTextControls(seed.title).trim();
|
|
1048
|
+
if (codePointLength(title) < 1 || codePointLength(title) > MAX_TITLE_LENGTH) errors.push(`${at}.title — must contain 1..${MAX_TITLE_LENGTH} characters`);
|
|
1049
|
+
if (patterns.has(seed.routePattern)) errors.push(`${at}.routePattern — duplicate pattern ${JSON.stringify(seed.routePattern)}`);
|
|
1050
|
+
patterns.add(seed.routePattern);
|
|
1051
|
+
const seedBlocks = Array.isArray(seed.blocks) ? seed.blocks : [];
|
|
1052
|
+
if (seedBlocks.length < 1 || seedBlocks.length > MAX_TEMPLATE_BLOCKS) errors.push(`${at}.blocks — must declare 1..${MAX_TEMPLATE_BLOCKS} blocks`);
|
|
1053
|
+
const seedBlockIds = /* @__PURE__ */ new Set();
|
|
1054
|
+
const normalizedBlocks = [];
|
|
1055
|
+
const nodeBindings = /* @__PURE__ */ new Map();
|
|
1056
|
+
for (const [blockIndex, blockEntry] of seedBlocks.slice(0, MAX_TEMPLATE_BLOCKS).entries()) {
|
|
1057
|
+
const blockAt = `${at}.blocks[${blockIndex}]`;
|
|
1058
|
+
if (!isRecord(blockEntry)) {
|
|
1059
|
+
errors.push(`${blockAt} — must be an object`);
|
|
1060
|
+
continue;
|
|
1061
|
+
}
|
|
1062
|
+
const block = blockEntry;
|
|
1063
|
+
if (blockEntry.required !== void 0) errors.push(`${blockAt}.required — only a page seed's blocks may be required`);
|
|
1064
|
+
const id = typeof block.id === "string" ? block.id : "";
|
|
1065
|
+
if (!NODE_ID_PATTERN.test(id)) errors.push(`${blockAt}.id — invalid id "${String(block.id)}" (expected ${NODE_ID_PATTERN.source})`);
|
|
1066
|
+
else if (seedBlockIds.has(id)) errors.push(`${blockAt}.id — duplicate id "${id}"`);
|
|
1067
|
+
else seedBlockIds.add(id);
|
|
1068
|
+
const fields = blockFields.get(block.apiId);
|
|
1069
|
+
if (fields === void 0) errors.push(`${blockAt}.apiId — unknown block "${String(block.apiId)}"`);
|
|
1070
|
+
else if (!isRecord(block.data)) errors.push(`${blockAt}.data — must be an object`);
|
|
1071
|
+
else checkSeedData((path, message) => `${blockAt}.data — ${path}: ${message}`, fields, block.data, errors);
|
|
1072
|
+
const carried = checkedNodeBindings(blockAt, block, fields, errors);
|
|
1073
|
+
if (Object.keys(carried).length > 0 && typeof block.id === "string") nodeBindings.set(block.id, carried);
|
|
1074
|
+
normalizedBlocks.push({
|
|
1075
|
+
id: block.id,
|
|
1076
|
+
apiId: block.apiId,
|
|
1077
|
+
data: block.data
|
|
1078
|
+
});
|
|
1079
|
+
}
|
|
1080
|
+
const layout = seed.layout === void 0 ? seedLayout({
|
|
1081
|
+
...seed,
|
|
1082
|
+
blocks: normalizedBlocks.map((block) => ({
|
|
1083
|
+
...block,
|
|
1084
|
+
...nodeBindings.get(block.id)
|
|
1085
|
+
}))
|
|
1086
|
+
}) : checkedLayout(at, seed.layout, seedBlockIds, nodeBindings, errors);
|
|
1087
|
+
normalized.push({
|
|
1088
|
+
routePattern: seed.routePattern,
|
|
1089
|
+
schemaApiId: seed.schemaApiId,
|
|
1090
|
+
title,
|
|
1091
|
+
blocks: normalizedBlocks,
|
|
1092
|
+
layout
|
|
1093
|
+
});
|
|
1094
|
+
}
|
|
1095
|
+
return normalized;
|
|
1096
|
+
}
|
|
1097
|
+
/**
|
|
1098
|
+
* Validates the theme's declared **page** seeds — the entries of the same
|
|
1099
|
+
* `templates` list that name `page: { slug }` instead of a template target —
|
|
1100
|
+
* and returns them in manifest shape.
|
|
1101
|
+
*
|
|
1102
|
+
* A page seed is the simplest seed there is: a slug, a title and the page's
|
|
1103
|
+
* content in document order. There is no `layout` and there are no node ids:
|
|
1104
|
+
* the `blocks` array **is** the page, and Core synthesises the one-column
|
|
1105
|
+
* layout from it. An entry is either
|
|
1106
|
+
*
|
|
1107
|
+
* - a **block** — `{ apiId, data, required? }`, emitted as
|
|
1108
|
+
* `{ type: apiId, data, required? }`. `required: true` makes the node Core
|
|
1109
|
+
* creates locked: the author reorders it and edits its fields but cannot
|
|
1110
|
+
* delete it or move it out of the page root, so a cart page cannot lose its
|
|
1111
|
+
* cart.
|
|
1112
|
+
* - a **region placement** — `{ role: 'header' | 'footer' }`, emitted as the
|
|
1113
|
+
* reserved `{ type: '@header' }` / `{ type: '@footer' }`. It carries no data:
|
|
1114
|
+
* the block behind the region is `templateRoles`, and Core resolves the
|
|
1115
|
+
* placement to the site's own reusable component, so every seeded page shares
|
|
1116
|
+
* one header and one footer. A region may sit anywhere in the order — which
|
|
1117
|
+
* is what lets an announcement bar precede the header — at most once each, and
|
|
1118
|
+
* only when the theme declares that role.
|
|
1119
|
+
*
|
|
1120
|
+
* The **home** page is not a page seed. It keeps being the `templates` entry it
|
|
1121
|
+
* has always been, which Core maps to the site's root Page itself, so the home
|
|
1122
|
+
* seed on the wire is byte-identical to the one every theme has emitted; a page
|
|
1123
|
+
* seed may therefore not claim the slug `home` while that template seed exists.
|
|
1124
|
+
*
|
|
1125
|
+
* Every rule a seed's data obeys is the one `validateTemplateSeeds` applies —
|
|
1126
|
+
* the same `checkSeedData` walk, because a page seed is the same kind of write
|
|
1127
|
+
* Core makes on deploy.
|
|
1128
|
+
*/
|
|
1129
|
+
function validatePageSeeds(seeds, blocks, templates, roles, errors) {
|
|
1130
|
+
if (!Array.isArray(seeds)) return [];
|
|
1131
|
+
const blockFields = /* @__PURE__ */ new Map();
|
|
1132
|
+
for (const block of blocks) {
|
|
1133
|
+
if (typeof block.apiId !== "string") continue;
|
|
1134
|
+
blockFields.set(block.apiId, Array.isArray(block.fields) ? block.fields : []);
|
|
1135
|
+
}
|
|
1136
|
+
const normalized = [];
|
|
1137
|
+
const slugs = /* @__PURE__ */ new Set();
|
|
1138
|
+
const homeSeeded = templates.some((seed) => seed.schemaApiId === HOME_SCHEMA_API_ID);
|
|
1139
|
+
const entries = [];
|
|
1140
|
+
for (const [index, entry] of seeds.entries()) {
|
|
1141
|
+
if (!isRecord(entry)) continue;
|
|
1142
|
+
if (seedTargetOf(entry) !== "page") continue;
|
|
1143
|
+
entries.push([index, entry]);
|
|
1144
|
+
}
|
|
1145
|
+
if (entries.length > MAX_PAGE_SEEDS) errors.push(`templates: contains ${entries.length} page seeds — exceeds ${MAX_PAGE_SEEDS}`);
|
|
1146
|
+
for (const [index, entry] of entries.slice(0, MAX_PAGE_SEEDS)) {
|
|
1147
|
+
const at = `templates[${index}]`;
|
|
1148
|
+
const seed = entry;
|
|
1149
|
+
checkPageSeedKeys(at, entry, PAGE_SEED_KEYS, errors);
|
|
1150
|
+
const target = isRecord(seed.page) ? seed.page : {};
|
|
1151
|
+
const slug = typeof target.slug === "string" ? target.slug : "";
|
|
1152
|
+
const extra = Object.keys(target).filter((key) => key !== "slug");
|
|
1153
|
+
if (!PAGE_SLUG_PATTERN.test(slug) || slug.length > MAX_PAGE_SLUG_LENGTH) errors.push(`${at}.page.slug — invalid slug ${JSON.stringify(target.slug)} (expected ${PAGE_SLUG_PATTERN.source}, at most ${MAX_PAGE_SLUG_LENGTH} characters)`);
|
|
1154
|
+
else if (slug === HOME_PAGE_SLUG && homeSeeded) errors.push(`${at}.page.slug — "home" is the home template seed's own page: drop that seed or pick another slug`);
|
|
1155
|
+
else if (slugs.has(slug)) errors.push(`${at}.page.slug — duplicate slug ${JSON.stringify(slug)}`);
|
|
1156
|
+
else slugs.add(slug);
|
|
1157
|
+
for (const key of extra) errors.push(`${at}.page.${key} — unknown key (a page seed names only its slug)`);
|
|
1158
|
+
const title = stripPlainTextControls(seed.title).trim();
|
|
1159
|
+
if (codePointLength(title) < 1 || codePointLength(title) > MAX_TITLE_LENGTH) errors.push(`${at}.title — must contain 1..${MAX_TITLE_LENGTH} characters`);
|
|
1160
|
+
const seedBlocks = Array.isArray(seed.blocks) ? seed.blocks : [];
|
|
1161
|
+
if (seedBlocks.length < 1 || seedBlocks.length > MAX_PAGE_SEED_BLOCKS) errors.push(`${at}.blocks — must declare 1..${MAX_PAGE_SEED_BLOCKS} entries`);
|
|
1162
|
+
const placedRoles = /* @__PURE__ */ new Set();
|
|
1163
|
+
const normalizedBlocks = [];
|
|
1164
|
+
for (const [blockIndex, blockEntry] of seedBlocks.slice(0, MAX_PAGE_SEED_BLOCKS).entries()) {
|
|
1165
|
+
const blockAt = `${at}.blocks[${blockIndex}]`;
|
|
1166
|
+
if (!isRecord(blockEntry)) {
|
|
1167
|
+
errors.push(`${blockAt} — must be a block or a region placement`);
|
|
1168
|
+
continue;
|
|
1169
|
+
}
|
|
1170
|
+
if (blockEntry.role !== void 0) {
|
|
1171
|
+
const region = checkedRegion(blockAt, blockEntry, roles, placedRoles, errors);
|
|
1172
|
+
if (region !== null) normalizedBlocks.push(region);
|
|
1173
|
+
continue;
|
|
1174
|
+
}
|
|
1175
|
+
const block = blockEntry;
|
|
1176
|
+
checkPageSeedKeys(blockAt, blockEntry, PAGE_SEED_BLOCK_KEYS, errors);
|
|
1177
|
+
const fields = blockFields.get(block.apiId);
|
|
1178
|
+
if (fields === void 0) errors.push(`${blockAt}.apiId — unknown block "${String(block.apiId)}"`);
|
|
1179
|
+
else if (!isRecord(block.data)) errors.push(`${blockAt}.data — must be an object`);
|
|
1180
|
+
else checkSeedData((path, message) => `${blockAt}.data — ${path}: ${message}`, fields, block.data, errors);
|
|
1181
|
+
const required = blockEntry.required;
|
|
1182
|
+
if (required !== void 0 && required !== true) errors.push(`${blockAt}.required — must be true when present (omit it otherwise)`);
|
|
1183
|
+
normalizedBlocks.push({
|
|
1184
|
+
type: block.apiId,
|
|
1185
|
+
data: isRecord(block.data) ? block.data : {},
|
|
1186
|
+
...required === true ? { required: true } : {}
|
|
1187
|
+
});
|
|
1188
|
+
}
|
|
1189
|
+
normalized.push({
|
|
1190
|
+
slug,
|
|
1191
|
+
title,
|
|
1192
|
+
blocks: normalizedBlocks
|
|
1193
|
+
});
|
|
1194
|
+
}
|
|
1195
|
+
return normalized;
|
|
1196
|
+
}
|
|
1197
|
+
/**
|
|
1198
|
+
* One region placement, or null when the theme declared something that is not
|
|
1199
|
+
* one. A region is a placement and nothing else: it names a role the theme
|
|
1200
|
+
* actually declares the block data for (otherwise Core has nothing to resolve
|
|
1201
|
+
* it to), it appears at most once in a page — a page with two headers is one of
|
|
1202
|
+
* them being wrong — and it carries neither data nor `required`, because the
|
|
1203
|
+
* block behind it is `templateRoles` and a shared region is not a node an
|
|
1204
|
+
* author can delete in the first place.
|
|
1205
|
+
*/
|
|
1206
|
+
function checkedRegion(blockAt, entry, roles, placed, errors) {
|
|
1207
|
+
const role = entry.role;
|
|
1208
|
+
if (!ROLES.has(role)) {
|
|
1209
|
+
errors.push(`${blockAt}.role — must be "header" or "footer"`);
|
|
1210
|
+
return null;
|
|
1211
|
+
}
|
|
1212
|
+
const stray = [
|
|
1213
|
+
"apiId",
|
|
1214
|
+
"data",
|
|
1215
|
+
"required"
|
|
1216
|
+
].filter((key) => entry[key] !== void 0);
|
|
1217
|
+
for (const key of stray) errors.push(`${blockAt}.${key} — a region placement carries only its role (the block behind it is templateRoles.${role})`);
|
|
1218
|
+
checkPageSeedKeys(blockAt, entry, /* @__PURE__ */ new Set([...PAGE_SEED_REGION_KEYS, ...PAGE_SEED_BLOCK_KEYS]), errors);
|
|
1219
|
+
if (placed.has(role)) {
|
|
1220
|
+
errors.push(`${blockAt}.role — duplicate role "${role}"`);
|
|
1221
|
+
return null;
|
|
1222
|
+
}
|
|
1223
|
+
placed.add(role);
|
|
1224
|
+
if (roles?.[role] === void 0) errors.push(`${blockAt}.role — placing the ${role} region needs templateRoles.${role}`);
|
|
1225
|
+
return { type: REGION_TYPES[role] };
|
|
1226
|
+
}
|
|
1227
|
+
/**
|
|
1228
|
+
* Validates the `templates` / `bindings` a seed block declares for its layout
|
|
1229
|
+
* node and returns them in the shape the node carries.
|
|
1230
|
+
*
|
|
1231
|
+
* Both are maps from a **target path into the block's own fields** — the same
|
|
1232
|
+
* grammar the theme's template layout reads (identifier segments, list indices
|
|
1233
|
+
* allowed: `heading`, `items.0.label`), whose first segment must be a field the
|
|
1234
|
+
* block declares — to a non-empty string: a text template (`{{ title }}`) for
|
|
1235
|
+
* `templates`, a path on the routed entry for `bindings`. What the value
|
|
1236
|
+
* resolves to is the deploy's to check against the entry it renders; a theme
|
|
1237
|
+
* can only be held to the grammar and to fields it actually ships.
|
|
1238
|
+
*/
|
|
1239
|
+
function checkedNodeBindings(blockAt, block, fields, errors) {
|
|
1240
|
+
const carried = {};
|
|
1241
|
+
for (const key of NODE_BINDING_KEYS) {
|
|
1242
|
+
const declared = block[key];
|
|
1243
|
+
if (declared === void 0) continue;
|
|
1244
|
+
if (!isRecord(declared)) {
|
|
1245
|
+
errors.push(`${blockAt}.${key} — must be an object`);
|
|
1246
|
+
continue;
|
|
1247
|
+
}
|
|
1248
|
+
if (fields === void 0) continue;
|
|
1249
|
+
const declaredFields = new Set(fields.map((field) => field.fieldId).filter((fieldId) => typeof fieldId === "string"));
|
|
1250
|
+
const validated = {};
|
|
1251
|
+
for (const [path, value] of Object.entries(declared)) {
|
|
1252
|
+
const at = `${blockAt}.${key}.${path}`;
|
|
1253
|
+
if (!TARGET_FIELD_PATH.test(path) || !declaredFields.has(path.split(".")[0])) {
|
|
1254
|
+
errors.push(`${at} — must be a path into ${String(block.apiId)}'s fields (expected ${TARGET_FIELD_PATH.source})`);
|
|
1255
|
+
continue;
|
|
1256
|
+
}
|
|
1257
|
+
if (typeof value !== "string" || value === "") {
|
|
1258
|
+
errors.push(`${at} — must be a non-empty string`);
|
|
1259
|
+
continue;
|
|
1260
|
+
}
|
|
1261
|
+
validated[path] = value;
|
|
1262
|
+
}
|
|
1263
|
+
if (Object.keys(validated).length > 0) carried[key] = validated;
|
|
1264
|
+
}
|
|
1265
|
+
return carried;
|
|
1266
|
+
}
|
|
1267
|
+
/**
|
|
1268
|
+
* A declared layout is held to the one shape a seed may take — a single column
|
|
1269
|
+
* of role and block nodes — so the manifest always carries a document the
|
|
1270
|
+
* deploy can decode without a fallback.
|
|
1271
|
+
*/
|
|
1272
|
+
function checkedLayout(at, layout, seedBlockIds, nodeBindings, errors) {
|
|
1273
|
+
if (!isRecord(layout)) {
|
|
1274
|
+
errors.push(`${at}.layout — must be a layout document`);
|
|
1275
|
+
return layout;
|
|
1276
|
+
}
|
|
1277
|
+
if (layout.version !== 1) {
|
|
1278
|
+
errors.push(`${at}.layout.version — must be 1`);
|
|
1279
|
+
return layout;
|
|
1280
|
+
}
|
|
1281
|
+
const root = layout.root;
|
|
1282
|
+
if (!isRecord(root) || root.type !== "flex" || !isRecord(root.layout) || !isRecord(root.layout.direction) || root.layout.direction.normal !== "column") {
|
|
1283
|
+
errors.push(`${at}.layout.root — must be a flex container with direction.normal "column"`);
|
|
1284
|
+
return layout;
|
|
1285
|
+
}
|
|
1286
|
+
const nodeIds = /* @__PURE__ */ new Set();
|
|
1287
|
+
if (!NODE_ID_PATTERN.test(String(root.id))) errors.push(`${at}.layout.root.id — invalid node id "${String(root.id)}" (expected ${NODE_ID_PATTERN.source})`);
|
|
1288
|
+
else nodeIds.add(String(root.id));
|
|
1289
|
+
const children = Array.isArray(root.children) ? root.children : [];
|
|
1290
|
+
const placed = /* @__PURE__ */ new Set();
|
|
1291
|
+
const roles = /* @__PURE__ */ new Set();
|
|
1292
|
+
const normalizedChildren = [];
|
|
1293
|
+
for (const [index, child] of children.entries()) {
|
|
1294
|
+
const childAt = `${at}.layout.root.children[${index}]`;
|
|
1295
|
+
if (!isRecord(child)) {
|
|
1296
|
+
errors.push(`${childAt} — must be a reusable or block node`);
|
|
1297
|
+
continue;
|
|
1298
|
+
}
|
|
1299
|
+
const id = typeof child.id === "string" ? child.id : "";
|
|
1300
|
+
if (!NODE_ID_PATTERN.test(id)) errors.push(`${childAt}.id — invalid node id "${String(child.id)}" (expected ${NODE_ID_PATTERN.source})`);
|
|
1301
|
+
else if (nodeIds.has(id)) errors.push(`${childAt}.id — duplicate node id "${id}"`);
|
|
1302
|
+
else nodeIds.add(id);
|
|
1303
|
+
if (child.type === "block") {
|
|
1304
|
+
const entryId = typeof child.entryId === "string" ? child.entryId : "";
|
|
1305
|
+
if (!seedBlockIds.has(entryId)) errors.push(`${childAt}.entryId — no seed block with id "${String(child.entryId)}"`);
|
|
1306
|
+
else placed.add(entryId);
|
|
1307
|
+
normalizedChildren.push({
|
|
1308
|
+
id,
|
|
1309
|
+
type: "block",
|
|
1310
|
+
entryId,
|
|
1311
|
+
...nodeBindings.get(entryId)
|
|
1312
|
+
});
|
|
1313
|
+
continue;
|
|
1314
|
+
}
|
|
1315
|
+
if (child.type === "reusable") {
|
|
1316
|
+
const role = typeof child.role === "string" ? child.role : "";
|
|
1317
|
+
if (!ROLES.has(role)) errors.push(`${childAt}.role — must be "header" or "footer"`);
|
|
1318
|
+
else if (roles.has(role)) errors.push(`${childAt}.role — duplicate role "${role}"`);
|
|
1319
|
+
else roles.add(role);
|
|
1320
|
+
normalizedChildren.push({
|
|
1321
|
+
id,
|
|
1322
|
+
type: "reusable",
|
|
1323
|
+
role
|
|
1324
|
+
});
|
|
1325
|
+
continue;
|
|
1326
|
+
}
|
|
1327
|
+
errors.push(`${childAt}.type — must be "reusable" or "block" (got "${String(child.type)}")`);
|
|
1328
|
+
}
|
|
1329
|
+
for (const id of seedBlockIds) if (!placed.has(id)) errors.push(`${at}.layout — seed block "${id}" is not placed in the layout`);
|
|
1330
|
+
return {
|
|
1331
|
+
version: 1,
|
|
1332
|
+
root: {
|
|
1333
|
+
id: String(root.id),
|
|
1334
|
+
type: "flex",
|
|
1335
|
+
layout: { direction: { normal: "column" } },
|
|
1336
|
+
children: normalizedChildren
|
|
1337
|
+
}
|
|
1338
|
+
};
|
|
1339
|
+
}
|
|
1340
|
+
/**
|
|
1341
|
+
* A seed's pattern has to agree with what it is a template for: the home seed
|
|
1342
|
+
* owns the site root, and a catalog template is resolved by looking its slug up
|
|
1343
|
+
* in the catalog, so its pattern must end in the `:slug` parameter Core reads.
|
|
1344
|
+
* The parse is Core's own (`parseDynamicRoutePattern`): a static prefix plus one
|
|
1345
|
+
* trailing parameter, nothing else.
|
|
1346
|
+
*/
|
|
1347
|
+
function checkRoutePattern(at, seed, errors) {
|
|
1348
|
+
if (seed.schemaApiId === "home") {
|
|
1349
|
+
if (seed.routePattern !== HOME_ROUTE_PATTERN) errors.push(`${at}.routePattern — the home seed must be "${HOME_ROUTE_PATTERN}" (got ${JSON.stringify(seed.routePattern)})`);
|
|
1350
|
+
return;
|
|
1351
|
+
}
|
|
1352
|
+
const category = seed.schemaApiId === CATEGORY_SCHEMA_API_ID;
|
|
1353
|
+
const parameter = category ? `:${CATEGORY_PATH_PARAM}*` : `:${CATALOG_SLUG_PARAM}`;
|
|
1354
|
+
const parsed = parseDynamicRoutePattern(seed.routePattern);
|
|
1355
|
+
if (parsed === null) {
|
|
1356
|
+
errors.push(`${at}.routePattern — a catalog template needs a static prefix and one "${parameter}" parameter (got ${JSON.stringify(seed.routePattern)})`);
|
|
1357
|
+
return;
|
|
1358
|
+
}
|
|
1359
|
+
const expectedName = category ? CATEGORY_PATH_PARAM : CATALOG_SLUG_PARAM;
|
|
1360
|
+
if (parsed.paramName !== expectedName || parsed.catchAll !== category) errors.push(category ? `${at}.routePattern — a category template is resolved by its canonical path, so its parameter must be the catch-all "${parameter}" (got ":${parsed.paramName}${parsed.catchAll ? "*" : ""}")` : `${at}.routePattern — a catalog template is resolved by slug, so its parameter must be "${parameter}" (got ":${parsed.paramName}${parsed.catchAll ? "*" : ""}")`);
|
|
1361
|
+
}
|
|
1362
|
+
/**
|
|
1363
|
+
* Validates the block data a theme declares behind the `header`/`footer`
|
|
1364
|
+
* roles its template seed layouts may reference, and returns it in manifest
|
|
1365
|
+
* shape. A role's `apiId` must be a block the theme ships and its `data` is
|
|
1366
|
+
* validated exactly like a seed block's data (the same media walk `mock.json`
|
|
1367
|
+
* and `templates[].blocks[].data` go through) — the manifest write it becomes
|
|
1368
|
+
* is the same kind of write. Cross-checked against the already-validated
|
|
1369
|
+
* `templates`: a role a seed's layout places (whether generated or declared)
|
|
1370
|
+
* and that the theme did not declare is an error naming the seed that needs
|
|
1371
|
+
* it, so Core is never handed a template it cannot seed a site with.
|
|
1372
|
+
*/
|
|
1373
|
+
function validateTemplateRoles(roles, blocks, templates, errors) {
|
|
1374
|
+
const blockFields = /* @__PURE__ */ new Map();
|
|
1375
|
+
for (const block of blocks) {
|
|
1376
|
+
if (typeof block.apiId !== "string") continue;
|
|
1377
|
+
blockFields.set(block.apiId, Array.isArray(block.fields) ? block.fields : []);
|
|
1378
|
+
}
|
|
1379
|
+
const placedBy = /* @__PURE__ */ new Map();
|
|
1380
|
+
for (const [index, seed] of templates.entries()) {
|
|
1381
|
+
const layout = seed.layout;
|
|
1382
|
+
const root = isRecord(layout) ? layout.root : void 0;
|
|
1383
|
+
const children = isRecord(root) && Array.isArray(root.children) ? root.children : [];
|
|
1384
|
+
for (const child of children) if (isRecord(child) && child.type === "reusable" && ROLES.has(child.role) && !placedBy.has(child.role)) placedBy.set(child.role, index);
|
|
1385
|
+
}
|
|
1386
|
+
const input = isRecord(roles) ? roles : {};
|
|
1387
|
+
const normalized = {};
|
|
1388
|
+
for (const role of ROLES) {
|
|
1389
|
+
const at = `templateRoles.${role}`;
|
|
1390
|
+
const entry = input[role];
|
|
1391
|
+
if (entry === void 0) {
|
|
1392
|
+
const placedAt = placedBy.get(role);
|
|
1393
|
+
if (placedAt !== void 0) errors.push(`${at} — required: templates[${placedAt}] places the ${role} role`);
|
|
1394
|
+
continue;
|
|
1395
|
+
}
|
|
1396
|
+
if (!isRecord(entry)) {
|
|
1397
|
+
errors.push(`${at} — must be an object`);
|
|
1398
|
+
continue;
|
|
1399
|
+
}
|
|
1400
|
+
const apiId = typeof entry.apiId === "string" ? entry.apiId : "";
|
|
1401
|
+
const fields = blockFields.get(apiId);
|
|
1402
|
+
const data = isRecord(entry.data) ? entry.data : {};
|
|
1403
|
+
if (fields === void 0) errors.push(`${at}.apiId — unknown block "${String(entry.apiId)}"`);
|
|
1404
|
+
else if (!isRecord(entry.data)) errors.push(`${at}.data — must be an object`);
|
|
1405
|
+
else checkSeedData((path, message) => `${at}.data — ${path}: ${message}`, fields, data, errors);
|
|
1406
|
+
normalized[role] = {
|
|
1407
|
+
apiId,
|
|
1408
|
+
data
|
|
1409
|
+
};
|
|
1410
|
+
}
|
|
1411
|
+
return normalized.header === void 0 && normalized.footer === void 0 ? void 0 : normalized;
|
|
1412
|
+
}
|
|
1413
|
+
//#endregion
|
|
1414
|
+
//#region src/scan.ts
|
|
1415
|
+
const MAX_BLOCKS = 100;
|
|
1416
|
+
const MAX_MOCK_BYTES = 65536;
|
|
1417
|
+
const MAX_MANIFEST_BYTES = 2097152;
|
|
1418
|
+
const MAX_COMPOSITE_DEPTH = 5;
|
|
1419
|
+
const MAX_CUSTOM_PAGES = 100;
|
|
1420
|
+
const MAX_CUSTOM_PAGE_PATH_BYTES = 255;
|
|
1421
|
+
const MAX_CUSTOM_PAGE_TITLE_LENGTH = 80;
|
|
1422
|
+
const MAX_CUSTOM_PAGE_DESCRIPTION_LENGTH = 240;
|
|
1423
|
+
const DEFAULT_ROUTES = [{
|
|
1424
|
+
pattern: "/:path(.*)*",
|
|
1425
|
+
kind: "page"
|
|
1426
|
+
}];
|
|
1427
|
+
const SEMVER_PATTERN = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/;
|
|
1428
|
+
const SLOT_ID_PATTERN = /^[a-z][a-z0-9-]{0,47}$/;
|
|
1429
|
+
const SLOT_CONTROL_CHARS = /[\p{Cc}\p{Cf}]/gu;
|
|
1430
|
+
const RICH_TEXT_TOOLBAR_CONTROL_SET = new Set(RICH_TEXT_TOOLBAR_CONTROLS);
|
|
1431
|
+
const LINK_KIND_SET = /* @__PURE__ */ new Set([
|
|
1432
|
+
"product",
|
|
1433
|
+
"collection",
|
|
1434
|
+
"category",
|
|
1435
|
+
"entry",
|
|
1436
|
+
"page",
|
|
1437
|
+
"url",
|
|
1438
|
+
"none"
|
|
1439
|
+
]);
|
|
1440
|
+
/** The three metadata keys Core's `link` field type declares, and therefore
|
|
1441
|
+
* the whole surface a block.json may carry on one. */
|
|
1442
|
+
const LINK_METADATA_KEYS = [
|
|
1443
|
+
"kinds",
|
|
1444
|
+
"allowedEntrySchemaApiIds",
|
|
1445
|
+
"tree"
|
|
1446
|
+
];
|
|
1447
|
+
const SCHEMA_API_ID_PATTERN = /^[a-z][a-z0-9-]{1,48}$/;
|
|
1448
|
+
/** A message key: a dotted path of camelCase segments (`header.menu`,
|
|
1449
|
+
* `cart.empty.title`). Mirrors Core's own grammar for a theme message key. */
|
|
1450
|
+
const MESSAGE_KEY_PATTERN = /^[a-z0-9]+([A-Z][a-z0-9]*)*(\.[a-z0-9]+([A-Z][a-z0-9]*)*)*$/;
|
|
1451
|
+
const MAX_MESSAGE_KEY_LENGTH = 128;
|
|
1452
|
+
const MAX_MESSAGE_VALUE_LENGTH = 2e3;
|
|
1453
|
+
const MAX_MESSAGE_KEYS = 2e3;
|
|
1454
|
+
const MAX_MESSAGE_LOCALES = 20;
|
|
1455
|
+
/** The canonical `ll-RR` spelling the kit uses elsewhere (`en-US`, `is-IS`):
|
|
1456
|
+
* two lowercase language letters, a hyphen, two uppercase region letters. */
|
|
1457
|
+
const LOCALE_TAG_PATTERN = /^[a-z]{2}-[A-Z]{2}$/;
|
|
1458
|
+
const validateBlockJson = new Ajv({
|
|
1459
|
+
allErrors: true,
|
|
1460
|
+
allowUnionTypes: true
|
|
1461
|
+
}).compile(blockJsonSchema);
|
|
1462
|
+
function scanTheme(opts) {
|
|
1463
|
+
const themeDir = opts.themeDir;
|
|
1464
|
+
const blocksDir = join(themeDir, "blocks");
|
|
1465
|
+
const errors = [];
|
|
1466
|
+
const previous = previousBlocks(opts.previousManifest, errors);
|
|
1467
|
+
const blockDirs = {};
|
|
1468
|
+
const blocks = [];
|
|
1469
|
+
const seenApiIds = /* @__PURE__ */ new Set();
|
|
1470
|
+
const blockFiles = /* @__PURE__ */ new Map();
|
|
1471
|
+
const packageJson = readJsonObject(join(themeDir, "package.json"), themeDir, errors);
|
|
1472
|
+
const entries = directoryNames(blocksDir, errors);
|
|
1473
|
+
if (entries.length > MAX_BLOCKS) errors.push(`blocks: contains ${entries.length} blocks — exceeds ${MAX_BLOCKS}`);
|
|
1474
|
+
for (const dirName of entries.slice(0, MAX_BLOCKS)) {
|
|
1475
|
+
const blockDir = join(blocksDir, dirName);
|
|
1476
|
+
const blockFile = join(blockDir, "block.json");
|
|
1477
|
+
const relativeBlockFile = toRelative(themeDir, blockFile);
|
|
1478
|
+
const block = readJsonObject(blockFile, themeDir, errors);
|
|
1479
|
+
if (block === null) continue;
|
|
1480
|
+
const validBlock = validateBlockJson(block);
|
|
1481
|
+
if (!validBlock) for (const error of validateBlockJson.errors ?? []) errors.push(formatAjvError(relativeBlockFile, error));
|
|
1482
|
+
semanticChecks(relativeBlockFile, block, errors);
|
|
1483
|
+
if (validBlock) migrationChecks(relativeBlockFile, block, previous.get(String(block.apiId)), errors);
|
|
1484
|
+
const apiId = typeof block.apiId === "string" ? block.apiId : dirName;
|
|
1485
|
+
if (apiId !== dirName) errors.push(`${relativeBlockFile}: apiId — must match directory name "${dirName}" (got "${apiId}")`);
|
|
1486
|
+
if (seenApiIds.has(apiId)) errors.push(`${relativeBlockFile}: apiId — duplicate apiId "${apiId}"`);
|
|
1487
|
+
seenApiIds.add(apiId);
|
|
1488
|
+
blockDirs[apiId] = blockDir;
|
|
1489
|
+
const componentFile = join(blockDir, "Block.vue");
|
|
1490
|
+
if (!existsSync(componentFile)) errors.push(`${toRelative(themeDir, componentFile)}: file is required`);
|
|
1491
|
+
const mockFile = join(blockDir, "mock.json");
|
|
1492
|
+
let mock = null;
|
|
1493
|
+
if (existsSync(mockFile)) {
|
|
1494
|
+
const size = statSync(mockFile).size;
|
|
1495
|
+
if (size > MAX_MOCK_BYTES) errors.push(`${toRelative(themeDir, mockFile)}: exceeds 64 KB (${size} bytes)`);
|
|
1496
|
+
else mock = readJsonObject(mockFile, themeDir, errors);
|
|
1497
|
+
} else errors.push(`${toRelative(themeDir, mockFile)}: file is required`);
|
|
1498
|
+
if (mock !== null && validBlock) {
|
|
1499
|
+
const mockPath = toRelative(themeDir, mockFile);
|
|
1500
|
+
checkSeedData((path, message) => `${mockPath}: ${path}: ${message}`, Array.isArray(block.fields) ? block.fields : [], mock, errors);
|
|
1501
|
+
}
|
|
1502
|
+
let previewImage = null;
|
|
1503
|
+
const previewFile = join(blockDir, "preview.png");
|
|
1504
|
+
if (existsSync(previewFile)) {
|
|
1505
|
+
previewImage = `.eldra/previews/${apiId}.png`;
|
|
1506
|
+
const target = join(themeDir, previewImage);
|
|
1507
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
1508
|
+
copyFileSync(previewFile, target);
|
|
1509
|
+
}
|
|
1510
|
+
blocks.push({
|
|
1511
|
+
...block,
|
|
1512
|
+
category: typeof block.category === "string" ? block.category : "general",
|
|
1513
|
+
mock: mock ?? {},
|
|
1514
|
+
previewImage
|
|
1515
|
+
});
|
|
1516
|
+
blockFiles.set(apiId, relativeBlockFile);
|
|
1517
|
+
}
|
|
1518
|
+
const knownApiIds = new Set(blocks.map((block) => block.apiId));
|
|
1519
|
+
for (const block of blocks) {
|
|
1520
|
+
const slots = Array.isArray(block.slots) ? block.slots : [];
|
|
1521
|
+
for (const slot of slots) {
|
|
1522
|
+
if (!Array.isArray(slot.allowedBlockApiIds)) continue;
|
|
1523
|
+
for (const id of slot.allowedBlockApiIds) if (typeof id !== "string" || !knownApiIds.has(id)) {
|
|
1524
|
+
const file = typeof block.apiId === "string" ? blockFiles.get(block.apiId) ?? "" : "";
|
|
1525
|
+
errors.push(`${file}: slots — unknown allowlisted block "${String(id)}"`);
|
|
1526
|
+
}
|
|
1527
|
+
}
|
|
1528
|
+
}
|
|
1529
|
+
const templates = validateTemplateSeeds(opts.templates ?? [], blocks, errors);
|
|
1530
|
+
const templateRoles = validateTemplateRoles(opts.templateRoles, blocks, templates, errors);
|
|
1531
|
+
const pageSeeds = validatePageSeeds(opts.templates ?? [], blocks, templates, templateRoles, errors);
|
|
1532
|
+
const messages = readMessages(themeDir, packageJson, errors);
|
|
1533
|
+
const manifest = {
|
|
1534
|
+
manifestVersion: 2,
|
|
1535
|
+
theme: {
|
|
1536
|
+
id: readThemeId(packageJson, errors),
|
|
1537
|
+
name: sanitizeThemeName(typeof packageJson?.name === "string" ? packageJson.name : ""),
|
|
1538
|
+
version: typeof packageJson?.version === "string" ? packageJson.version : "0.0.0",
|
|
1539
|
+
framework: opts.framework ?? "vite",
|
|
1540
|
+
sdk: {
|
|
1541
|
+
core: resolvePackageVersion(themeDir, "@eldrajs/theme-core"),
|
|
1542
|
+
vitePlugin: resolvePackageVersion(themeDir, "@eldrajs/vite-plugin-theme")
|
|
1543
|
+
}
|
|
1544
|
+
},
|
|
1545
|
+
blocks,
|
|
1546
|
+
routes: opts.routes ?? DEFAULT_ROUTES,
|
|
1547
|
+
customPages: validateCustomPages(opts.customPages ?? [], errors),
|
|
1548
|
+
...templates.length === 0 ? {} : { templates },
|
|
1549
|
+
...templateRoles === void 0 ? {} : { templateRoles },
|
|
1550
|
+
...pageSeeds.length === 0 ? {} : { pageSeeds },
|
|
1551
|
+
tokens: readTokens(themeDir, errors),
|
|
1552
|
+
...messages === void 0 ? {} : { messages }
|
|
1553
|
+
};
|
|
1554
|
+
const breakpoints = resolveLayoutBreakpoints(opts.breakpoints);
|
|
1555
|
+
if (!/^[a-z][a-z0-9-]{1,48}$/.test(manifest.theme.name)) errors.push("package.json: name — cannot be sanitized to a valid theme name");
|
|
1556
|
+
if (!SEMVER_PATTERN.test(manifest.theme.version)) errors.push(`package.json: version — must be semver (got "${manifest.theme.version}")`);
|
|
1557
|
+
validateRoutes(manifest.routes, errors);
|
|
1558
|
+
if (new TextEncoder().encode(`${JSON.stringify(manifest, null, 2)}\n`).byteLength > MAX_MANIFEST_BYTES) errors.push(".eldra/manifest.json: exceeds 2 MB");
|
|
1559
|
+
return errors.length === 0 ? {
|
|
1560
|
+
manifest,
|
|
1561
|
+
blockDirs,
|
|
1562
|
+
errors,
|
|
1563
|
+
breakpoints
|
|
1564
|
+
} : {
|
|
1565
|
+
manifest: null,
|
|
1566
|
+
blockDirs,
|
|
1567
|
+
errors,
|
|
1568
|
+
breakpoints
|
|
1569
|
+
};
|
|
1570
|
+
}
|
|
1571
|
+
function validateCustomPages(pages, errors) {
|
|
1572
|
+
if (pages.length > MAX_CUSTOM_PAGES) errors.push(`customPages: contains ${pages.length} pages — exceeds ${MAX_CUSTOM_PAGES}`);
|
|
1573
|
+
const normalized = [];
|
|
1574
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1575
|
+
for (const [index, page] of pages.slice(0, MAX_CUSTOM_PAGES).entries()) {
|
|
1576
|
+
if (!validCustomPagePath(page.path)) errors.push(`customPages[${index}].path — invalid static path`);
|
|
1577
|
+
else if (seen.has(page.path)) errors.push(`customPages[${index}].path — duplicate path ${JSON.stringify(page.path)}`);
|
|
1578
|
+
seen.add(page.path);
|
|
1579
|
+
const title = stripPlainTextControls(page.title).trim();
|
|
1580
|
+
if (codePointLength(title) < 1 || codePointLength(title) > MAX_CUSTOM_PAGE_TITLE_LENGTH) errors.push(`customPages[${index}].title — must contain 1..${MAX_CUSTOM_PAGE_TITLE_LENGTH} characters`);
|
|
1581
|
+
const description = page.description === void 0 ? void 0 : stripPlainTextControls(page.description).trim();
|
|
1582
|
+
if (description !== void 0 && codePointLength(description) > MAX_CUSTOM_PAGE_DESCRIPTION_LENGTH) errors.push(`customPages[${index}].description — exceeds ${MAX_CUSTOM_PAGE_DESCRIPTION_LENGTH} characters`);
|
|
1583
|
+
normalized.push({
|
|
1584
|
+
path: page.path,
|
|
1585
|
+
title,
|
|
1586
|
+
...description === void 0 ? {} : { description }
|
|
1587
|
+
});
|
|
1588
|
+
}
|
|
1589
|
+
return normalized;
|
|
1590
|
+
}
|
|
1591
|
+
function validCustomPagePath(value) {
|
|
1592
|
+
return typeof value === "string" && value !== "" && new TextEncoder().encode(value).byteLength <= MAX_CUSTOM_PAGE_PATH_BYTES && value.startsWith("/") && !value.includes("//") && !/[:?#*\\]/.test(value) && (value === "/" || !value.endsWith("/"));
|
|
1593
|
+
}
|
|
1594
|
+
function directoryNames(blocksDir, errors) {
|
|
1595
|
+
if (!existsSync(blocksDir)) {
|
|
1596
|
+
errors.push("blocks: directory is required");
|
|
1597
|
+
return [];
|
|
1598
|
+
}
|
|
1599
|
+
return readdirSync(blocksDir).filter((name) => statSync(join(blocksDir, name)).isDirectory()).sort((a, b) => a.localeCompare(b));
|
|
1600
|
+
}
|
|
1601
|
+
function readJsonObject(path, themeDir, errors) {
|
|
1602
|
+
const file = toRelative(themeDir, path);
|
|
1603
|
+
if (!existsSync(path)) {
|
|
1604
|
+
errors.push(`${file}: file is required`);
|
|
1605
|
+
return null;
|
|
1606
|
+
}
|
|
1607
|
+
try {
|
|
1608
|
+
const source = readFileSync(path, "utf8");
|
|
1609
|
+
const duplicate = duplicateJsonKey(source);
|
|
1610
|
+
if (duplicate !== null) {
|
|
1611
|
+
errors.push(`${file}: invalid JSON — duplicate object key ${JSON.stringify(duplicate)}`);
|
|
1612
|
+
return null;
|
|
1613
|
+
}
|
|
1614
|
+
const parsed = JSON.parse(source);
|
|
1615
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
|
|
1616
|
+
errors.push(`${file}: root — must be an object`);
|
|
1617
|
+
return null;
|
|
1618
|
+
}
|
|
1619
|
+
return parsed;
|
|
1620
|
+
} catch (error) {
|
|
1621
|
+
errors.push(`${file}: invalid JSON — ${error instanceof Error ? error.message : String(error)}`);
|
|
1622
|
+
return null;
|
|
1623
|
+
}
|
|
1624
|
+
}
|
|
1625
|
+
function duplicateJsonKey(source) {
|
|
1626
|
+
let offset = 0;
|
|
1627
|
+
let duplicate = null;
|
|
1628
|
+
const whitespace = () => {
|
|
1629
|
+
while (/\s/.test(source[offset] ?? "")) offset += 1;
|
|
1630
|
+
};
|
|
1631
|
+
const string = () => {
|
|
1632
|
+
const start = offset;
|
|
1633
|
+
offset += 1;
|
|
1634
|
+
while (offset < source.length) {
|
|
1635
|
+
if (source[offset] === "\\") {
|
|
1636
|
+
offset += 2;
|
|
1637
|
+
continue;
|
|
1638
|
+
}
|
|
1639
|
+
if (source[offset] === "\"") {
|
|
1640
|
+
offset += 1;
|
|
1641
|
+
return JSON.parse(source.slice(start, offset));
|
|
1642
|
+
}
|
|
1643
|
+
offset += 1;
|
|
1644
|
+
}
|
|
1645
|
+
throw new Error("unterminated string");
|
|
1646
|
+
};
|
|
1647
|
+
const value = () => {
|
|
1648
|
+
whitespace();
|
|
1649
|
+
if (source[offset] === "{") {
|
|
1650
|
+
objectValue();
|
|
1651
|
+
return;
|
|
1652
|
+
}
|
|
1653
|
+
if (source[offset] === "[") {
|
|
1654
|
+
offset += 1;
|
|
1655
|
+
whitespace();
|
|
1656
|
+
if (source[offset] === "]") {
|
|
1657
|
+
offset += 1;
|
|
1658
|
+
return;
|
|
1659
|
+
}
|
|
1660
|
+
for (;;) {
|
|
1661
|
+
value();
|
|
1662
|
+
whitespace();
|
|
1663
|
+
if (source[offset] === "]") {
|
|
1664
|
+
offset += 1;
|
|
1665
|
+
return;
|
|
1666
|
+
}
|
|
1667
|
+
if (source[offset] !== ",") throw new Error("invalid array");
|
|
1668
|
+
offset += 1;
|
|
1669
|
+
}
|
|
1670
|
+
}
|
|
1671
|
+
if (source[offset] === "\"") {
|
|
1672
|
+
string();
|
|
1673
|
+
return;
|
|
1674
|
+
}
|
|
1675
|
+
const match = /^(?:-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?|true|false|null)/.exec(source.slice(offset));
|
|
1676
|
+
if (!match) throw new Error("invalid value");
|
|
1677
|
+
offset += match[0].length;
|
|
1678
|
+
};
|
|
1679
|
+
const objectValue = () => {
|
|
1680
|
+
offset += 1;
|
|
1681
|
+
whitespace();
|
|
1682
|
+
const keys = /* @__PURE__ */ new Set();
|
|
1683
|
+
if (source[offset] === "}") {
|
|
1684
|
+
offset += 1;
|
|
1685
|
+
return;
|
|
1686
|
+
}
|
|
1687
|
+
for (;;) {
|
|
1688
|
+
whitespace();
|
|
1689
|
+
if (source[offset] !== "\"") throw new Error("invalid object key");
|
|
1690
|
+
const key = string();
|
|
1691
|
+
if (keys.has(key) && duplicate === null) duplicate = key;
|
|
1692
|
+
keys.add(key);
|
|
1693
|
+
whitespace();
|
|
1694
|
+
if (source[offset] !== ":") throw new Error("missing colon");
|
|
1695
|
+
offset += 1;
|
|
1696
|
+
value();
|
|
1697
|
+
whitespace();
|
|
1698
|
+
if (source[offset] === "}") {
|
|
1699
|
+
offset += 1;
|
|
1700
|
+
return;
|
|
1701
|
+
}
|
|
1702
|
+
if (source[offset] !== ",") throw new Error("invalid object");
|
|
1703
|
+
offset += 1;
|
|
1704
|
+
}
|
|
1705
|
+
};
|
|
1706
|
+
try {
|
|
1707
|
+
value();
|
|
1708
|
+
} catch {
|
|
1709
|
+
return null;
|
|
1710
|
+
}
|
|
1711
|
+
return duplicate;
|
|
1712
|
+
}
|
|
1713
|
+
function formatAjvError(file, error) {
|
|
1714
|
+
const params = error.params;
|
|
1715
|
+
const where = error.instancePath === "" ? params.missingProperty ?? "(root)" : error.instancePath.slice(1).replaceAll("/", ".").replace(/\.(\d+)/g, "[$1]");
|
|
1716
|
+
if (error.keyword === "pattern") {
|
|
1717
|
+
const data = typeof error.data === "string" ? ` (got ${JSON.stringify(error.data)})` : "";
|
|
1718
|
+
return `${file}: ${where} — must match ${params.pattern ?? ""}${data}`;
|
|
1719
|
+
}
|
|
1720
|
+
if (error.keyword === "additionalProperties") {
|
|
1721
|
+
const property = params.additionalProperty ?? "";
|
|
1722
|
+
if (property === "allowedSchemaIds") return `${file}: ${where}.${property} — themes may use only relation.allowedTagIds, relation.allowProducts and relation.allowCollections`;
|
|
1723
|
+
return `${file}: ${where} — unknown property "${property}"`;
|
|
1724
|
+
}
|
|
1725
|
+
return `${file}: ${where} — ${error.message ?? "invalid"}`;
|
|
1726
|
+
}
|
|
1727
|
+
function semanticChecks(file, block, errors) {
|
|
1728
|
+
const fields = Array.isArray(block.fields) ? block.fields : [];
|
|
1729
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1730
|
+
let titleCount = 0;
|
|
1731
|
+
const groups = Array.isArray(block.groups) ? block.groups : [];
|
|
1732
|
+
const groupIds = /* @__PURE__ */ new Set();
|
|
1733
|
+
groups.forEach((group, index) => {
|
|
1734
|
+
const groupId = String(group.groupId ?? "");
|
|
1735
|
+
if (groupIds.has(groupId)) errors.push(`${file}: groups[${index}] — duplicate groupId "${groupId}"`);
|
|
1736
|
+
groupIds.add(groupId);
|
|
1737
|
+
});
|
|
1738
|
+
fields.forEach((field, index) => {
|
|
1739
|
+
const id = String(field.fieldId ?? "");
|
|
1740
|
+
if (seen.has(id)) errors.push(`${file}: fields[${index}] — duplicate fieldId "${id}"`);
|
|
1741
|
+
seen.add(id);
|
|
1742
|
+
if (field.isTitle === true) titleCount += 1;
|
|
1743
|
+
if (field.relation !== void 0 && field.type !== "reference") errors.push(`${file}: fields[${index}].relation — only allowed on type "reference" (got "${String(field.type)}")`);
|
|
1744
|
+
if (isRecord(field.relation) && !namesARelationTarget(field.relation)) errors.push(`${file}: fields[${index}] — relation requires one of allowedTagIds, allowProducts or allowCollections`);
|
|
1745
|
+
if (field.groupId !== void 0 && !groupIds.has(String(field.groupId))) errors.push(`${file}: fields[${index}].groupId — references undeclared group "${String(field.groupId)}"`);
|
|
1746
|
+
const depth = compositeDepth(field);
|
|
1747
|
+
if (depth > MAX_COMPOSITE_DEPTH) errors.push(`${file}: fields[${index}].metadata — composite nesting depth ${depth} exceeds ${MAX_COMPOSITE_DEPTH}`);
|
|
1748
|
+
const metadata = field.metadata !== null && typeof field.metadata === "object" ? field.metadata : {};
|
|
1749
|
+
if ("framing" in metadata || "framingAspectRatio" in metadata) {
|
|
1750
|
+
if (field.type !== "media") {
|
|
1751
|
+
const key = "framing" in metadata ? "framing" : "framingAspectRatio";
|
|
1752
|
+
errors.push(`${file}: fields[${index}].metadata.${key} — only allowed on type "media"`);
|
|
1753
|
+
} else {
|
|
1754
|
+
if ("framing" in metadata && typeof metadata.framing !== "boolean") errors.push(`${file}: fields[${index}].metadata.framing — must be a boolean`);
|
|
1755
|
+
if ("framingAspectRatio" in metadata) {
|
|
1756
|
+
const ratio = metadata.framingAspectRatio;
|
|
1757
|
+
if (typeof ratio !== "string" || !/^[1-9][0-9]*\/[1-9][0-9]*$/.test(ratio)) errors.push(`${file}: fields[${index}].metadata.framingAspectRatio — must be "W/H" with positive integers (got "${String(ratio)}")`);
|
|
1758
|
+
else if (metadata.framing !== true) errors.push(`${file}: fields[${index}].metadata.framingAspectRatio — requires metadata.framing: true`);
|
|
1759
|
+
}
|
|
1760
|
+
}
|
|
1761
|
+
}
|
|
1762
|
+
if ("toolbar" in metadata) {
|
|
1763
|
+
if (field.type !== "rich-text") errors.push(`${file}: fields[${index}].metadata.toolbar — only allowed on type "rich-text"`);
|
|
1764
|
+
else {
|
|
1765
|
+
const toolbar = metadata.toolbar;
|
|
1766
|
+
if (!Array.isArray(toolbar) || toolbar.some((control) => typeof control !== "string")) errors.push(`${file}: fields[${index}].metadata.toolbar — must be an array of control ids`);
|
|
1767
|
+
else {
|
|
1768
|
+
const controlSeen = /* @__PURE__ */ new Set();
|
|
1769
|
+
for (const control of toolbar) if (!RICH_TEXT_TOOLBAR_CONTROL_SET.has(control)) errors.push(`${file}: fields[${index}].metadata.toolbar — unknown control "${control}"`);
|
|
1770
|
+
else if (controlSeen.has(control)) errors.push(`${file}: fields[${index}].metadata.toolbar — duplicate control "${control}"`);
|
|
1771
|
+
else controlSeen.add(control);
|
|
1772
|
+
}
|
|
1773
|
+
}
|
|
1774
|
+
}
|
|
1775
|
+
});
|
|
1776
|
+
if (titleCount > 1) errors.push(`${file}: fields — at most one field may set isTitle (found ${titleCount})`);
|
|
1777
|
+
checkFieldSetVisibility(file, fields, "fields", errors);
|
|
1778
|
+
checkLinkFieldSet(file, fields, "fields", false, errors);
|
|
1779
|
+
const slots = Array.isArray(block.slots) ? block.slots : [];
|
|
1780
|
+
const slotIds = /* @__PURE__ */ new Set();
|
|
1781
|
+
for (const [index, slot] of slots.entries()) {
|
|
1782
|
+
const slotId = typeof slot.id === "string" ? slot.id : "";
|
|
1783
|
+
if (!SLOT_ID_PATTERN.test(slotId)) errors.push(`${file}: slots[${index}] — invalid slot id "${String(slot.id)}" (expected ^[a-z][a-z0-9-]{0,47}$)`);
|
|
1784
|
+
else if (slotIds.has(slotId)) errors.push(`${file}: slots[${index}] — duplicate slot id "${slotId}"`);
|
|
1785
|
+
else slotIds.add(slotId);
|
|
1786
|
+
if (typeof slot.label === "string") slot.label = slot.label.replace(SLOT_CONTROL_CHARS, "");
|
|
1787
|
+
if (typeof slot.description === "string") slot.description = slot.description.replace(SLOT_CONTROL_CHARS, "");
|
|
1788
|
+
if (Array.isArray(slot.allowedBlockApiIds) && slot.allowedBlockApiIds.includes(block.apiId)) errors.push(`${file}: slots[${index}] — allowedBlockApiIds may not contain the host block "${String(block.apiId)}"`);
|
|
1789
|
+
}
|
|
1790
|
+
}
|
|
1791
|
+
/**
|
|
1792
|
+
* The `link` half of the field grammar, over one field set and everything
|
|
1793
|
+
* nested under it.
|
|
1794
|
+
*
|
|
1795
|
+
* Three metadata keys belong to `link` and to nothing else, so a block that
|
|
1796
|
+
* puts `kinds` on a string field learns that here rather than from Core's
|
|
1797
|
+
* ingest. `tree` additionally demands that the link *is* a list's item: it
|
|
1798
|
+
* asks the editor to author a list of links as a tree, and a `list` cannot
|
|
1799
|
+
* carry the flag itself because Core's list field declares only
|
|
1800
|
+
* `allowedSchemas` and `item` and refuses every other metadata key.
|
|
1801
|
+
*/
|
|
1802
|
+
function checkLinkFieldSet(file, fields, path, isListItem, errors, depth = 1) {
|
|
1803
|
+
if (depth > 6) return;
|
|
1804
|
+
fields.forEach((field, index) => {
|
|
1805
|
+
if (!isRecord(field)) return;
|
|
1806
|
+
const at = fields.length === 1 && isListItem ? path : `${path}[${index}]`;
|
|
1807
|
+
checkLinkField(file, field, at, isListItem, errors);
|
|
1808
|
+
const metadata = isRecord(field.metadata) ? field.metadata : null;
|
|
1809
|
+
if (metadata === null) return;
|
|
1810
|
+
if (Array.isArray(metadata.fields)) checkLinkFieldSet(file, metadata.fields.filter(isRecord), `${at}.metadata.fields`, false, errors, depth + 1);
|
|
1811
|
+
if (isRecord(metadata.item)) checkLinkFieldSet(file, [metadata.item], `${at}.metadata.item`, true, errors, depth + 1);
|
|
1812
|
+
});
|
|
1813
|
+
}
|
|
1814
|
+
function checkLinkField(file, field, at, isListItem, errors) {
|
|
1815
|
+
const metadata = isRecord(field.metadata) ? field.metadata : {};
|
|
1816
|
+
const declared = LINK_METADATA_KEYS.filter((key) => key in metadata);
|
|
1817
|
+
if (declared.length === 0) return;
|
|
1818
|
+
if (field.type !== "link") {
|
|
1819
|
+
for (const key of declared) errors.push(`${file}: ${at}.metadata.${key} — only allowed on type "link"`);
|
|
1820
|
+
return;
|
|
1821
|
+
}
|
|
1822
|
+
const kinds = checkLinkKinds(file, metadata, at, errors);
|
|
1823
|
+
if ("allowedEntrySchemaApiIds" in metadata) {
|
|
1824
|
+
const ids = metadata.allowedEntrySchemaApiIds;
|
|
1825
|
+
if (!Array.isArray(ids)) errors.push(`${file}: ${at}.metadata.allowedEntrySchemaApiIds — must be an array of schema apiIds`);
|
|
1826
|
+
else if (ids.length === 0) errors.push(`${file}: ${at}.metadata.allowedEntrySchemaApiIds — must list at least one schema apiId`);
|
|
1827
|
+
else for (const id of ids) if (typeof id !== "string" || !SCHEMA_API_ID_PATTERN.test(id)) errors.push(`${file}: ${at}.metadata.allowedEntrySchemaApiIds — invalid schema apiId "${String(id)}"`);
|
|
1828
|
+
if (kinds !== null && !kinds.includes("entry")) errors.push(`${file}: ${at}.metadata.allowedEntrySchemaApiIds — requires metadata.kinds to include "entry"`);
|
|
1829
|
+
}
|
|
1830
|
+
if ("tree" in metadata) {
|
|
1831
|
+
if (typeof metadata.tree !== "boolean") errors.push(`${file}: ${at}.metadata.tree — must be a boolean`);
|
|
1832
|
+
else if (metadata.tree === true && !isListItem) errors.push(`${file}: ${at} — metadata.tree is only allowed on a link that is a list's item`);
|
|
1833
|
+
}
|
|
1834
|
+
}
|
|
1835
|
+
/** The declared kinds, or null when the field declares none (which means all
|
|
1836
|
+
* six) or declared something unusable. */
|
|
1837
|
+
function checkLinkKinds(file, metadata, at, errors) {
|
|
1838
|
+
if (!("kinds" in metadata)) return null;
|
|
1839
|
+
const kinds = metadata.kinds;
|
|
1840
|
+
if (!Array.isArray(kinds)) {
|
|
1841
|
+
errors.push(`${file}: ${at}.metadata.kinds — must be an array of link kinds`);
|
|
1842
|
+
return null;
|
|
1843
|
+
}
|
|
1844
|
+
if (kinds.length === 0) {
|
|
1845
|
+
errors.push(`${file}: ${at}.metadata.kinds — must list at least one kind`);
|
|
1846
|
+
return null;
|
|
1847
|
+
}
|
|
1848
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1849
|
+
let valid = true;
|
|
1850
|
+
for (const kind of kinds) if (typeof kind !== "string" || !LINK_KIND_SET.has(kind)) {
|
|
1851
|
+
errors.push(`${file}: ${at}.metadata.kinds — unknown kind "${String(kind)}"`);
|
|
1852
|
+
valid = false;
|
|
1853
|
+
} else if (seen.has(kind)) {
|
|
1854
|
+
errors.push(`${file}: ${at}.metadata.kinds — duplicate kind "${kind}"`);
|
|
1855
|
+
valid = false;
|
|
1856
|
+
} else seen.add(kind);
|
|
1857
|
+
return valid ? kinds : null;
|
|
1858
|
+
}
|
|
1859
|
+
const SHOW_WHEN_SIBLING_TYPES = /* @__PURE__ */ new Set([
|
|
1860
|
+
"select",
|
|
1861
|
+
"bool",
|
|
1862
|
+
"string"
|
|
1863
|
+
]);
|
|
1864
|
+
/** Every field in one field set, plus the field sets nested under it. */
|
|
1865
|
+
function checkFieldSetVisibility(file, fields, path, errors, depth = 1) {
|
|
1866
|
+
if (depth > 6) return;
|
|
1867
|
+
const siblings = /* @__PURE__ */ new Map();
|
|
1868
|
+
for (const field of fields) if (isRecord(field)) siblings.set(String(field.fieldId ?? ""), field);
|
|
1869
|
+
fields.forEach((field, index) => {
|
|
1870
|
+
if (!isRecord(field)) return;
|
|
1871
|
+
checkFieldVisibility(file, field, siblings, `${path}[${index}]`, errors);
|
|
1872
|
+
checkNestedVisibility(file, field, `${path}[${index}]`, errors, depth);
|
|
1873
|
+
});
|
|
1874
|
+
}
|
|
1875
|
+
function checkNestedVisibility(file, field, at, errors, depth) {
|
|
1876
|
+
const metadata = isRecord(field.metadata) ? field.metadata : null;
|
|
1877
|
+
if (metadata === null) return;
|
|
1878
|
+
if (Array.isArray(metadata.fields)) checkFieldSetVisibility(file, metadata.fields.filter(isRecord), `${at}.metadata.fields`, errors, depth + 1);
|
|
1879
|
+
if (isRecord(metadata.item)) {
|
|
1880
|
+
const item = metadata.item;
|
|
1881
|
+
const itemPath = `${at}.metadata.item`;
|
|
1882
|
+
checkFieldVisibility(file, item, /* @__PURE__ */ new Map([[String(item.fieldId ?? ""), item]]), itemPath, errors);
|
|
1883
|
+
checkNestedVisibility(file, item, itemPath, errors, depth + 1);
|
|
1884
|
+
}
|
|
1885
|
+
}
|
|
1886
|
+
/**
|
|
1887
|
+
* The half of the `showWhen` grammar JSON Schema cannot express, and the point
|
|
1888
|
+
* where `equals` sugar is normalized away: the manifest always carries `in`.
|
|
1889
|
+
* Core's manifest ingest re-validates the same rules and refuses a manifest
|
|
1890
|
+
* that breaks one, naming the field.
|
|
1891
|
+
*/
|
|
1892
|
+
function checkFieldVisibility(file, field, siblings, at, errors) {
|
|
1893
|
+
const showWhen = field.showWhen;
|
|
1894
|
+
if (showWhen === void 0 || !isRecord(showWhen)) return;
|
|
1895
|
+
const listed = Array.isArray(showWhen.in) ? showWhen.in : null;
|
|
1896
|
+
const equals = typeof showWhen.equals === "string" ? showWhen.equals : null;
|
|
1897
|
+
if (listed === null === (equals === null)) {
|
|
1898
|
+
errors.push(`${file}: ${at}.showWhen — set exactly one of "in" or "equals"`);
|
|
1899
|
+
return;
|
|
1900
|
+
}
|
|
1901
|
+
const values = listed ?? [equals];
|
|
1902
|
+
if (values.length === 0) {
|
|
1903
|
+
errors.push(`${file}: ${at}.showWhen.in — must list at least one value`);
|
|
1904
|
+
return;
|
|
1905
|
+
}
|
|
1906
|
+
const siblingId = String(showWhen.field ?? "");
|
|
1907
|
+
if (siblingId === String(field.fieldId ?? "")) {
|
|
1908
|
+
errors.push(`${file}: ${at}.showWhen.field — a field cannot depend on itself`);
|
|
1909
|
+
return;
|
|
1910
|
+
}
|
|
1911
|
+
const sibling = siblings.get(siblingId);
|
|
1912
|
+
if (sibling === void 0) {
|
|
1913
|
+
errors.push(`${file}: ${at}.showWhen.field — references unknown sibling "${siblingId}"`);
|
|
1914
|
+
return;
|
|
1915
|
+
}
|
|
1916
|
+
const siblingType = String(sibling.type ?? "");
|
|
1917
|
+
if (!SHOW_WHEN_SIBLING_TYPES.has(siblingType)) {
|
|
1918
|
+
errors.push(`${file}: ${at}.showWhen.field — sibling "${siblingId}" must be a select, bool or string field (got "${siblingType}")`);
|
|
1919
|
+
return;
|
|
1920
|
+
}
|
|
1921
|
+
if (sibling.showWhen !== void 0) {
|
|
1922
|
+
errors.push(`${file}: ${at}.showWhen.field — sibling "${siblingId}" carries showWhen itself; conditions do not chain`);
|
|
1923
|
+
return;
|
|
1924
|
+
}
|
|
1925
|
+
if (siblingType === "select") {
|
|
1926
|
+
const options = isRecord(sibling.metadata) && Array.isArray(sibling.metadata.options) ? sibling.metadata.options : null;
|
|
1927
|
+
if (options !== null) {
|
|
1928
|
+
const unknown = values.filter((value) => !options.includes(value));
|
|
1929
|
+
for (const value of unknown) errors.push(`${file}: ${at}.showWhen.in — "${value}" is not an option of sibling "${siblingId}"`);
|
|
1930
|
+
if (unknown.length > 0) return;
|
|
1931
|
+
}
|
|
1932
|
+
}
|
|
1933
|
+
field.showWhen = {
|
|
1934
|
+
field: siblingId,
|
|
1935
|
+
in: values
|
|
1936
|
+
};
|
|
1937
|
+
}
|
|
1938
|
+
function compositeDepth(field, depth = 1) {
|
|
1939
|
+
const metadata = field.metadata;
|
|
1940
|
+
if (typeof metadata !== "object" || metadata === null || Array.isArray(metadata)) return depth;
|
|
1941
|
+
const children = [];
|
|
1942
|
+
const meta = metadata;
|
|
1943
|
+
if (Array.isArray(meta.fields)) children.push(...meta.fields.filter(isRecord));
|
|
1944
|
+
if (isRecord(meta.item)) children.push(meta.item);
|
|
1945
|
+
return children.reduce((max, child) => Math.max(max, compositeDepth(child, depth + 1)), depth);
|
|
1946
|
+
}
|
|
1947
|
+
function readTokens(themeDir, errors) {
|
|
1948
|
+
const path = join(themeDir, "tokens.json");
|
|
1949
|
+
if (!existsSync(path)) return {
|
|
1950
|
+
colors: {},
|
|
1951
|
+
containers: {}
|
|
1952
|
+
};
|
|
1953
|
+
const parsed = readJsonObject(path, themeDir, errors);
|
|
1954
|
+
if (parsed === null) return {
|
|
1955
|
+
colors: {},
|
|
1956
|
+
containers: {}
|
|
1957
|
+
};
|
|
1958
|
+
try {
|
|
1959
|
+
const normalized = normalizeThemeDesignTokens(parsed);
|
|
1960
|
+
if (!Object.prototype.hasOwnProperty.call(parsed, "containers") && !Object.prototype.hasOwnProperty.call(parsed, "allowCustomColors")) return {
|
|
1961
|
+
colors: Object.fromEntries(Object.keys(normalized.colors).sort().map((id) => [id, normalized.colors[id].value])),
|
|
1962
|
+
fonts: sortedStringRecord(parsed.fonts),
|
|
1963
|
+
spacing: sortedStringRecord(parsed.spacing)
|
|
1964
|
+
};
|
|
1965
|
+
return normalized;
|
|
1966
|
+
} catch (error) {
|
|
1967
|
+
if (error instanceof DesignTokenValidationError) errors.push(`tokens.json: ${error.path || "(root)"} — ${error.code}`);
|
|
1968
|
+
else errors.push(`tokens.json: invalid design tokens — ${error instanceof Error ? error.message : String(error)}`);
|
|
1969
|
+
return {
|
|
1970
|
+
colors: {},
|
|
1971
|
+
containers: {}
|
|
1972
|
+
};
|
|
1973
|
+
}
|
|
1974
|
+
}
|
|
1975
|
+
function sortedStringRecord(value) {
|
|
1976
|
+
if (!isRecord(value)) return {};
|
|
1977
|
+
return Object.fromEntries(Object.keys(value).sort().map((key) => [key, value[key]]));
|
|
1978
|
+
}
|
|
1979
|
+
/**
|
|
1980
|
+
* Reads the theme's `i18n/<tag>.json` message catalogue (siblings of
|
|
1981
|
+
* `tokens.json`), flattens nested objects of strings into dotted keys, and
|
|
1982
|
+
* validates key grammar/length, value type/length, the locale and key-count
|
|
1983
|
+
* bounds, the locale file's name, and that every non-default locale's keys
|
|
1984
|
+
* are a subset of the default locale's. Returns `undefined` — not an empty
|
|
1985
|
+
* catalogue — when the theme has no `i18n/` directory, so a theme that ships
|
|
1986
|
+
* no texts keeps emitting a manifest an older Core already accepts.
|
|
1987
|
+
*
|
|
1988
|
+
* The default locale is the one the theme declares under package.json's own
|
|
1989
|
+
* `eldra.defaultLocale` key (beside `name`/`version`, read above); when the
|
|
1990
|
+
* theme declares none, it falls back to `en-US` when that locale ships a
|
|
1991
|
+
* file, else the alphabetically first locale file.
|
|
1992
|
+
*/
|
|
1993
|
+
function readMessages(themeDir, packageJson, errors) {
|
|
1994
|
+
const i18nDir = join(themeDir, "i18n");
|
|
1995
|
+
if (!existsSync(i18nDir) || !statSync(i18nDir).isDirectory()) return void 0;
|
|
1996
|
+
const fileNames = readdirSync(i18nDir).filter((name) => statSync(join(i18nDir, name)).isFile()).sort((a, b) => a.localeCompare(b));
|
|
1997
|
+
if (fileNames.length === 0) {
|
|
1998
|
+
errors.push("i18n: directory contains no locale files");
|
|
1999
|
+
return;
|
|
2000
|
+
}
|
|
2001
|
+
if (fileNames.length > MAX_MESSAGE_LOCALES) errors.push(`i18n: contains ${fileNames.length} locale files — exceeds ${MAX_MESSAGE_LOCALES}`);
|
|
2002
|
+
const locales = {};
|
|
2003
|
+
const validTags = [];
|
|
2004
|
+
for (const fileName of fileNames.slice(0, MAX_MESSAGE_LOCALES)) {
|
|
2005
|
+
const relFile = `i18n/${fileName}`;
|
|
2006
|
+
const tag = fileName.endsWith(".json") ? fileName.slice(0, -5) : fileName;
|
|
2007
|
+
if (!fileName.endsWith(".json") || !LOCALE_TAG_PATTERN.test(tag)) {
|
|
2008
|
+
errors.push(`${relFile}: file name — must be a BCP-47 locale tag in canonical "ll-RR" form with a .json extension (e.g. "en-US.json")`);
|
|
2009
|
+
continue;
|
|
2010
|
+
}
|
|
2011
|
+
const parsed = readJsonObject(join(i18nDir, fileName), themeDir, errors);
|
|
2012
|
+
if (parsed === null) continue;
|
|
2013
|
+
const flattened = {};
|
|
2014
|
+
flattenMessages(relFile, parsed, "", flattened, errors);
|
|
2015
|
+
const keyCount = Object.keys(flattened).length;
|
|
2016
|
+
if (keyCount > MAX_MESSAGE_KEYS) errors.push(`${relFile}: contains ${keyCount} keys — exceeds ${MAX_MESSAGE_KEYS}`);
|
|
2017
|
+
locales[tag] = flattened;
|
|
2018
|
+
validTags.push(tag);
|
|
2019
|
+
}
|
|
2020
|
+
if (validTags.length === 0) return void 0;
|
|
2021
|
+
const defaultLocale = resolveDefaultLocale(packageJson, validTags, errors);
|
|
2022
|
+
const defaultKeys = new Set(Object.keys(locales[defaultLocale] ?? {}));
|
|
2023
|
+
for (const tag of validTags) {
|
|
2024
|
+
if (tag === defaultLocale) continue;
|
|
2025
|
+
for (const key of Object.keys(locales[tag])) if (!defaultKeys.has(key)) {
|
|
2026
|
+
errors.push(`i18n/${tag}.json: "${key}" is not a key of the default locale "${defaultLocale}"`);
|
|
2027
|
+
break;
|
|
2028
|
+
}
|
|
2029
|
+
}
|
|
2030
|
+
return {
|
|
2031
|
+
defaultLocale,
|
|
2032
|
+
locales
|
|
2033
|
+
};
|
|
2034
|
+
}
|
|
2035
|
+
/** The theme's declared `eldra.defaultLocale`, validated against the locale
|
|
2036
|
+
* files actually present; falls back to `en-US` (if shipped) else the
|
|
2037
|
+
* alphabetically first locale tag when undeclared or invalid. */
|
|
2038
|
+
function resolveDefaultLocale(packageJson, validTags, errors) {
|
|
2039
|
+
const declared = isRecord(packageJson?.eldra) ? packageJson.eldra.defaultLocale : void 0;
|
|
2040
|
+
if (declared !== void 0) {
|
|
2041
|
+
if (typeof declared !== "string" || !LOCALE_TAG_PATTERN.test(declared)) errors.push(`package.json: eldra.defaultLocale — must be a BCP-47 locale tag in canonical "ll-RR" form (got ${JSON.stringify(declared)})`);
|
|
2042
|
+
else if (!validTags.includes(declared)) errors.push(`package.json: eldra.defaultLocale — no i18n/${declared}.json file`);
|
|
2043
|
+
else return declared;
|
|
2044
|
+
}
|
|
2045
|
+
if (validTags.includes("en-US")) return "en-US";
|
|
2046
|
+
return [...validTags].sort((a, b) => a.localeCompare(b))[0];
|
|
2047
|
+
}
|
|
2048
|
+
/** Flattens one locale file's nested JSON object of strings into dotted keys,
|
|
2049
|
+
* validating each key's grammar/length and each value's type/length along the
|
|
2050
|
+
* way. A key or value that fails its own rule is skipped (not written), so
|
|
2051
|
+
* one bad entry does not stop the rest of the file from being reported. */
|
|
2052
|
+
function flattenMessages(file, node, prefix, out, errors) {
|
|
2053
|
+
for (const key of Object.keys(node)) {
|
|
2054
|
+
const value = node[key];
|
|
2055
|
+
const path = prefix === "" ? key : `${prefix}.${key}`;
|
|
2056
|
+
if (isRecord(value)) {
|
|
2057
|
+
flattenMessages(file, value, path, out, errors);
|
|
2058
|
+
continue;
|
|
2059
|
+
}
|
|
2060
|
+
if (!MESSAGE_KEY_PATTERN.test(path) || path.length > MAX_MESSAGE_KEY_LENGTH) {
|
|
2061
|
+
errors.push(`${file}: "${path}" — key must match ${MESSAGE_KEY_PATTERN} and be at most ${MAX_MESSAGE_KEY_LENGTH} characters`);
|
|
2062
|
+
continue;
|
|
2063
|
+
}
|
|
2064
|
+
if (typeof value !== "string") {
|
|
2065
|
+
const got = value === null ? "null" : Array.isArray(value) ? "array" : typeof value;
|
|
2066
|
+
errors.push(`${file}: "${path}" — value must be a string (got ${got})`);
|
|
2067
|
+
continue;
|
|
2068
|
+
}
|
|
2069
|
+
if (codePointLength(value) > MAX_MESSAGE_VALUE_LENGTH) {
|
|
2070
|
+
errors.push(`${file}: "${path}" — value exceeds ${MAX_MESSAGE_VALUE_LENGTH} characters`);
|
|
2071
|
+
continue;
|
|
2072
|
+
}
|
|
2073
|
+
out[path] = value;
|
|
2074
|
+
}
|
|
2075
|
+
}
|
|
2076
|
+
function validateRoutes(routes, errors) {
|
|
2077
|
+
routes.forEach((route, index) => {
|
|
2078
|
+
const value = route;
|
|
2079
|
+
if (typeof value.pattern !== "string" || value.pattern.length === 0) errors.push(`routes[${index}].pattern — must be a non-empty string`);
|
|
2080
|
+
if (value.kind !== "page" && value.kind !== "entry") {
|
|
2081
|
+
errors.push(`routes[${index}].kind — must be "page" or "entry"`);
|
|
2082
|
+
return;
|
|
2083
|
+
}
|
|
2084
|
+
if (value.kind === "entry") {
|
|
2085
|
+
if (typeof value.schemaApiId !== "string" || value.schemaApiId.length === 0) errors.push(`routes[${index}].schemaApiId — required for entry routes`);
|
|
2086
|
+
if (typeof value.field !== "string" || value.field.length === 0) errors.push(`routes[${index}].field — required for entry routes`);
|
|
2087
|
+
} else if (value.schemaApiId !== void 0 || value.field !== void 0) errors.push(`routes[${index}] — page routes must not set schemaApiId or field`);
|
|
2088
|
+
});
|
|
2089
|
+
}
|
|
2090
|
+
function resolvePackageVersion(themeDir, packageName) {
|
|
2091
|
+
try {
|
|
2092
|
+
const require = createRequire(join(themeDir, "package.json"));
|
|
2093
|
+
let directory = dirname(require.resolve(packageName));
|
|
2094
|
+
for (;;) {
|
|
2095
|
+
const packagePath = join(directory, "package.json");
|
|
2096
|
+
if (existsSync(packagePath)) {
|
|
2097
|
+
const packageJson = JSON.parse(readFileSync(packagePath, "utf8"));
|
|
2098
|
+
return typeof packageJson.version === "string" ? packageJson.version : "0.0.0";
|
|
2099
|
+
}
|
|
2100
|
+
const parent = dirname(directory);
|
|
2101
|
+
if (parent === directory) return "0.0.0";
|
|
2102
|
+
directory = parent;
|
|
2103
|
+
}
|
|
2104
|
+
} catch {
|
|
2105
|
+
return "0.0.0";
|
|
2106
|
+
}
|
|
2107
|
+
}
|
|
2108
|
+
/** The theme's declared `eldra.themeId`: the stable identity Core stores the
|
|
2109
|
+
* theme's block schemas and entries under (`<themeId>:<apiId>`), independent of
|
|
2110
|
+
* the npm package name. Required; there is no derivation from `name`. Returns
|
|
2111
|
+
* an empty string after reporting an error, so the caller still builds the
|
|
2112
|
+
* rest of the manifest and every problem is reported in one pass. */
|
|
2113
|
+
function readThemeId(packageJson, errors) {
|
|
2114
|
+
const declared = isRecord(packageJson?.eldra) ? packageJson.eldra.themeId : void 0;
|
|
2115
|
+
if (declared === void 0) {
|
|
2116
|
+
errors.push("package.json: eldra.themeId — required; set \"eldra\": { \"themeId\": \"<id>\" } (lower-case letters, digits and hyphens, 2 to 49 characters, starting with a letter)");
|
|
2117
|
+
return "";
|
|
2118
|
+
}
|
|
2119
|
+
if (typeof declared !== "string" || !THEME_ID_PATTERN.test(declared)) {
|
|
2120
|
+
errors.push(`package.json: eldra.themeId — must match ${THEME_ID_PATTERN} (lower-case letters, digits and hyphens, 2 to 49 characters, starting with a letter; got ${JSON.stringify(declared)})`);
|
|
2121
|
+
return "";
|
|
2122
|
+
}
|
|
2123
|
+
if (RESERVED_THEME_IDS.includes(declared)) {
|
|
2124
|
+
errors.push(`package.json: eldra.themeId — "${declared}" is reserved`);
|
|
2125
|
+
return "";
|
|
2126
|
+
}
|
|
2127
|
+
return declared;
|
|
2128
|
+
}
|
|
2129
|
+
function sanitizeThemeName(name) {
|
|
2130
|
+
return (name.split("/").pop() ?? "").toLowerCase().replace(/[^a-z0-9-]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 49);
|
|
2131
|
+
}
|
|
2132
|
+
/** Whether a `relation` names something an editor can actually pick: at least
|
|
2133
|
+
* one semantic tag, catalog products, or catalog collections. Only values the
|
|
2134
|
+
* closed AJV schema already accepted reach here, so a non-empty
|
|
2135
|
+
* `allowedTagIds` array and boolean flags are the only shapes to weigh. */
|
|
2136
|
+
function namesARelationTarget(relation) {
|
|
2137
|
+
const tags = relation.allowedTagIds;
|
|
2138
|
+
return Array.isArray(tags) && tags.length > 0 || relation.allowProducts === true || relation.allowCollections === true;
|
|
2139
|
+
}
|
|
2140
|
+
function toRelative(themeDir, path) {
|
|
2141
|
+
return relative(themeDir, path).split("\\").join("/");
|
|
2142
|
+
}
|
|
2143
|
+
function previousBlocks(manifest, errors) {
|
|
2144
|
+
const result = /* @__PURE__ */ new Map();
|
|
2145
|
+
if (manifest === void 0) return result;
|
|
2146
|
+
if (!isRecord(manifest) || manifest.manifestVersion !== 2 || !Array.isArray(manifest.blocks)) {
|
|
2147
|
+
errors.push("previous manifest: expected manifestVersion 2 and a blocks array");
|
|
2148
|
+
return result;
|
|
2149
|
+
}
|
|
2150
|
+
for (const [index, value] of manifest.blocks.entries()) {
|
|
2151
|
+
const at = `previous manifest: blocks[${index}]`;
|
|
2152
|
+
if (!isRecord(value)) {
|
|
2153
|
+
errors.push(`${at} — invalid block`);
|
|
2154
|
+
continue;
|
|
2155
|
+
}
|
|
2156
|
+
const { mock: _mock, previewImage: _preview, ...block } = value;
|
|
2157
|
+
if (!validateBlockJson(block)) {
|
|
2158
|
+
errors.push(`${at} — invalid block definition`);
|
|
2159
|
+
continue;
|
|
2160
|
+
}
|
|
2161
|
+
const definition = block;
|
|
2162
|
+
if (!definition.fields.every((field) => validMigrationFieldShape(field))) {
|
|
2163
|
+
errors.push(`${at} — invalid recursive field storage shape`);
|
|
2164
|
+
continue;
|
|
2165
|
+
}
|
|
2166
|
+
if (result.has(definition.apiId)) errors.push(`${at} — duplicate block apiId`);
|
|
2167
|
+
semanticChecks(at, block, errors);
|
|
2168
|
+
migrationChecks(at, definition, void 0, errors);
|
|
2169
|
+
result.set(definition.apiId, definition);
|
|
2170
|
+
}
|
|
2171
|
+
return result;
|
|
2172
|
+
}
|
|
2173
|
+
function readPreviousManifest(themeDir, errors) {
|
|
2174
|
+
const path = join(themeDir, ".eldra", "manifest.json");
|
|
2175
|
+
if (!existsSync(path)) return void 0;
|
|
2176
|
+
if (statSync(path).size > MAX_MANIFEST_BYTES) {
|
|
2177
|
+
errors.push("previous manifest: .eldra/manifest.json exceeds 2 MB");
|
|
2178
|
+
return;
|
|
2179
|
+
}
|
|
2180
|
+
const parsed = readJsonObject(path, themeDir, errors);
|
|
2181
|
+
return parsed === null ? void 0 : parsed;
|
|
2182
|
+
}
|
|
2183
|
+
//#endregion
|
|
2184
|
+
export { validateTemplateRoles as a, validatePageSeeds as i, scanTheme as n, validateTemplateSeeds as o, seedLayout as r, readPreviousManifest as t };
|