@crombolo/schema 0.27.0 → 0.29.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/package.json +3 -3
- package/src/blocks.ts +198 -0
- package/src/config-references.ts +152 -0
- package/src/export.ts +35 -4
- package/src/generate-delivery.ts +16 -12
- package/src/generate.ts +45 -20
- package/src/index.ts +4 -0
- package/src/keys.ts +60 -0
- package/src/model.ts +43 -0
- package/src/parse.ts +107 -0
- package/src/presets.ts +177 -0
- package/src/scaffold.ts +97 -10
- package/src/sync.ts +134 -89
- package/src/validate.ts +55 -1
- package/src/write.ts +76 -9
package/src/sync.ts
CHANGED
|
@@ -4,7 +4,12 @@
|
|
|
4
4
|
* the multi-node rules are in docs/schema-as-code.md, "Synchronisation".
|
|
5
5
|
*/
|
|
6
6
|
import type { FormTypes } from '@crombolo/core'
|
|
7
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
type ContentTypeAggregate,
|
|
9
|
+
derivedKey,
|
|
10
|
+
ObjectTypes,
|
|
11
|
+
type PropertyTypeModel,
|
|
12
|
+
} from '@crombolo/core'
|
|
8
13
|
import {
|
|
9
14
|
appendCacheInstruction,
|
|
10
15
|
appendSchemaState,
|
|
@@ -17,6 +22,7 @@ import {
|
|
|
17
22
|
type Db,
|
|
18
23
|
DbDate,
|
|
19
24
|
DEFAULT_URL_OPTIONS,
|
|
25
|
+
ensureSystemMediaTypes,
|
|
20
26
|
FolderRepository,
|
|
21
27
|
LanguageRepository,
|
|
22
28
|
Locks,
|
|
@@ -34,14 +40,16 @@ import {
|
|
|
34
40
|
type WorkflowSyncReport,
|
|
35
41
|
writeUrlOptions,
|
|
36
42
|
} from '@crombolo/data'
|
|
37
|
-
import {
|
|
43
|
+
import { compileBlocks } from './blocks.ts'
|
|
44
|
+
import { BUILTIN_DATA_TYPES, storageTypeFor } from './builtin.ts'
|
|
45
|
+
import { type ReferenceKind, resolveConfig } from './config-references.ts'
|
|
46
|
+
import { dataTypeKeyFor, propertyKeyFor, shippedPropertyKeys, typeKeyFor } from './keys.ts'
|
|
38
47
|
import { hashSchemaSet, type LoadedSchema } from './load.ts'
|
|
39
48
|
import {
|
|
40
49
|
allProperties,
|
|
41
50
|
type SchemaContentType,
|
|
42
51
|
type SchemaProblem,
|
|
43
52
|
type SchemaProperty,
|
|
44
|
-
type SchemaSet,
|
|
45
53
|
typeFileKey,
|
|
46
54
|
} from './model.ts'
|
|
47
55
|
import { validateSchemaSet } from './validate.ts'
|
|
@@ -155,13 +163,6 @@ export async function syncSchema(
|
|
|
155
163
|
return emptySyncReport('refused', { reason: 'the schema has problems', problems })
|
|
156
164
|
|
|
157
165
|
const set = loaded.set
|
|
158
|
-
if (options.mode === 'production') {
|
|
159
|
-
const missing = elementsWithoutKeys(set)
|
|
160
|
-
if (missing.length > 0)
|
|
161
|
-
return emptySyncReport('refused', {
|
|
162
|
-
reason: `production requires every element to carry a key; run \`crombolo types sync\` in development to assign them: ${missing.join(', ')}`,
|
|
163
|
-
})
|
|
164
|
-
}
|
|
165
166
|
const hash = hashSchemaSet(set)
|
|
166
167
|
const fileState: NodeSchemaState = { version: set.version, revision: node.revision }
|
|
167
168
|
|
|
@@ -236,6 +237,72 @@ export async function syncSchema(
|
|
|
236
237
|
const templates = new ComponentRepository(tx)
|
|
237
238
|
const languages = new LanguageRepository(tx)
|
|
238
239
|
|
|
240
|
+
// Content types, then media types: one table, two kinds, the same rules.
|
|
241
|
+
const kinds = [
|
|
242
|
+
{
|
|
243
|
+
name: 'document' as const,
|
|
244
|
+
types: set.contentTypes,
|
|
245
|
+
repo: types,
|
|
246
|
+
folders: typeFolders,
|
|
247
|
+
noun: 'content type',
|
|
248
|
+
items: 'document(s)',
|
|
249
|
+
},
|
|
250
|
+
{
|
|
251
|
+
name: 'media' as const,
|
|
252
|
+
types: set.mediaTypes ?? [],
|
|
253
|
+
repo: new ContentTypeRepository(tx, { kind: 'media' }),
|
|
254
|
+
folders: new FolderRepository(
|
|
255
|
+
tx,
|
|
256
|
+
ObjectTypes.MediaTypeContainer,
|
|
257
|
+
ObjectTypes.MediaType,
|
|
258
|
+
),
|
|
259
|
+
noun: 'media type',
|
|
260
|
+
items: 'media item(s)',
|
|
261
|
+
},
|
|
262
|
+
{
|
|
263
|
+
name: 'member' as const,
|
|
264
|
+
types: set.memberTypes ?? [],
|
|
265
|
+
repo: new ContentTypeRepository(tx, { kind: 'member' }),
|
|
266
|
+
folders: new FolderRepository(
|
|
267
|
+
tx,
|
|
268
|
+
ObjectTypes.MemberTypeContainer,
|
|
269
|
+
ObjectTypes.MemberType,
|
|
270
|
+
),
|
|
271
|
+
noun: 'member type',
|
|
272
|
+
items: 'member(s)',
|
|
273
|
+
},
|
|
274
|
+
]
|
|
275
|
+
// ---- type keys, settled before anything is saved (story 161): a data
|
|
276
|
+
// type's config may name a type this same change adds.
|
|
277
|
+
const settledTypeKeys = new Map<string, Map<string, string>>()
|
|
278
|
+
for (const kind of kinds) {
|
|
279
|
+
const fileTypeKeys = new Set(
|
|
280
|
+
kind.types.flatMap((t) => (t.key ? [t.key.toLowerCase()] : [])),
|
|
281
|
+
)
|
|
282
|
+
const byAlias = new Map<string, string>()
|
|
283
|
+
for (const t of kind.types)
|
|
284
|
+
byAlias.set(t.alias, (await resolveTypeKey(kind.repo, kind.name, t, fileTypeKeys)).key)
|
|
285
|
+
settledTypeKeys.set(kind.name, byAlias)
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// A type named in a data type's config, by alias: one this change
|
|
289
|
+
// settles, or one Crombolo ships.
|
|
290
|
+
const typeByAlias = (alias: string, kind: ReferenceKind): string | undefined => {
|
|
291
|
+
const k = kind === 'element' ? 'document' : kind
|
|
292
|
+
const settled = settledTypeKeys.get(k)?.get(alias)
|
|
293
|
+
if (settled) return settled
|
|
294
|
+
const shipped =
|
|
295
|
+
k === 'document'
|
|
296
|
+
? SYSTEM_CONTENT_TYPES.find((t) => t.alias === alias)
|
|
297
|
+
: k === 'media'
|
|
298
|
+
? (SYSTEM_MEDIA_TYPES.find((t) => t.alias === alias) ??
|
|
299
|
+
BUILT_IN_MEDIA_TYPES.find((t) => t.alias === alias))
|
|
300
|
+
: SYSTEM_MEMBER_TYPE.alias === alias
|
|
301
|
+
? SYSTEM_MEMBER_TYPE
|
|
302
|
+
: undefined
|
|
303
|
+
return shipped?.key
|
|
304
|
+
}
|
|
305
|
+
|
|
239
306
|
// ---- data types: built-ins are seeded; files add or override by alias
|
|
240
307
|
for (const d of set.dataTypes) {
|
|
241
308
|
const existing = await dataTypes.byAlias(d.alias)
|
|
@@ -247,14 +314,24 @@ export async function syncSchema(
|
|
|
247
314
|
existing !== undefined &&
|
|
248
315
|
existing.key.toLowerCase() !== d.key.toLowerCase() &&
|
|
249
316
|
!(await dataTypes.byKey(d.key))
|
|
250
|
-
const
|
|
251
|
-
|
|
317
|
+
const expected = dataTypeKeyFor(d.alias)
|
|
318
|
+
const key = (rekeyed ? existing?.key : d.key) ?? existing?.key ?? expected
|
|
319
|
+
// Written back only when the file needs it: a key no file without one would get.
|
|
320
|
+
if ((!d.key && key.toLowerCase() !== expected.toLowerCase()) || rekeyed)
|
|
252
321
|
report.assignedKeys.push({
|
|
253
322
|
file: loaded.files.get(`data-type:${d.alias}`) ?? '',
|
|
254
323
|
typeAlias: d.alias,
|
|
255
324
|
key,
|
|
256
325
|
revived: false,
|
|
257
326
|
})
|
|
327
|
+
// Blocks as tables and types by alias, compiled to the keys the editor reads.
|
|
328
|
+
let config = d.config
|
|
329
|
+
if (d.blocks)
|
|
330
|
+
config = {
|
|
331
|
+
...config,
|
|
332
|
+
...compileBlocks(key, d.blocks, (alias) => typeByAlias(alias, 'element')).config,
|
|
333
|
+
}
|
|
334
|
+
config = resolveConfig(d.editor, config, typeByAlias).config
|
|
258
335
|
await dataTypes.save({
|
|
259
336
|
key,
|
|
260
337
|
alias: d.alias,
|
|
@@ -262,7 +339,7 @@ export async function syncSchema(
|
|
|
262
339
|
editorAlias: d.editor,
|
|
263
340
|
editorUiAlias: d.editorUi ?? builtin?.editorUi ?? null,
|
|
264
341
|
dbType: existing?.dbType ?? storageTypeFor(d.editor),
|
|
265
|
-
values: Object.entries(
|
|
342
|
+
values: Object.entries(config).map(([alias, value]) => ({ alias, value })),
|
|
266
343
|
parentKey: existing?.parentKey ?? (await folderKey(dataTypeFolders, d.folder)),
|
|
267
344
|
})
|
|
268
345
|
if (existing) report.updated.dataTypes += 1
|
|
@@ -319,41 +396,6 @@ export async function syncSchema(
|
|
|
319
396
|
componentKeyByAlias.set(alias, key)
|
|
320
397
|
}
|
|
321
398
|
|
|
322
|
-
// Content types, then media types: one table, two kinds, the same rules.
|
|
323
|
-
const kinds = [
|
|
324
|
-
{
|
|
325
|
-
name: 'document' as const,
|
|
326
|
-
types: set.contentTypes,
|
|
327
|
-
repo: types,
|
|
328
|
-
folders: typeFolders,
|
|
329
|
-
noun: 'content type',
|
|
330
|
-
items: 'document(s)',
|
|
331
|
-
},
|
|
332
|
-
{
|
|
333
|
-
name: 'media' as const,
|
|
334
|
-
types: set.mediaTypes ?? [],
|
|
335
|
-
repo: new ContentTypeRepository(tx, { kind: 'media' }),
|
|
336
|
-
folders: new FolderRepository(
|
|
337
|
-
tx,
|
|
338
|
-
ObjectTypes.MediaTypeContainer,
|
|
339
|
-
ObjectTypes.MediaType,
|
|
340
|
-
),
|
|
341
|
-
noun: 'media type',
|
|
342
|
-
items: 'media item(s)',
|
|
343
|
-
},
|
|
344
|
-
{
|
|
345
|
-
name: 'member' as const,
|
|
346
|
-
types: set.memberTypes ?? [],
|
|
347
|
-
repo: new ContentTypeRepository(tx, { kind: 'member' }),
|
|
348
|
-
folders: new FolderRepository(
|
|
349
|
-
tx,
|
|
350
|
-
ObjectTypes.MemberTypeContainer,
|
|
351
|
-
ObjectTypes.MemberType,
|
|
352
|
-
),
|
|
353
|
-
noun: 'member type',
|
|
354
|
-
items: 'member(s)',
|
|
355
|
-
},
|
|
356
|
-
]
|
|
357
399
|
// Umbraco's fixed keys for the built-in media types' groups, so a site's
|
|
358
400
|
// video.toml keeps the key Umbraco gave its "Video" group.
|
|
359
401
|
const fixedGroupKeys = new Map([
|
|
@@ -382,37 +424,18 @@ export async function syncSchema(
|
|
|
382
424
|
)
|
|
383
425
|
for (const t of inCompositionOrder(kind.types)) {
|
|
384
426
|
const file = loaded.files.get(typeFileKey(kind.name, t.alias)) ?? ''
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
let rekeyed = false
|
|
392
|
-
if (!existing && t.key) {
|
|
393
|
-
const sameAlias = await kind.repo.byAlias(t.alias, { includeRetired: true })
|
|
394
|
-
if (sameAlias && !fileTypeKeys.has(sameAlias.key.toLowerCase())) {
|
|
395
|
-
existing = sameAlias
|
|
396
|
-
rekeyed = true
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
const shippedKey =
|
|
400
|
-
kind.name === 'document'
|
|
401
|
-
? SYSTEM_CONTENT_TYPES.find((d) => d.alias === t.alias)?.key
|
|
402
|
-
: undefined
|
|
403
|
-
const key =
|
|
404
|
-
(rekeyed ? existing?.key : t.key) ??
|
|
405
|
-
existing?.key ??
|
|
406
|
-
shippedKey ??
|
|
407
|
-
crypto.randomUUID()
|
|
408
|
-
if ((!t.key && !existing) || rekeyed)
|
|
427
|
+
const resolved = await resolveTypeKey(kind.repo, kind.name, t, fileTypeKeys)
|
|
428
|
+
const existing = resolved.existing
|
|
429
|
+
const rekeyed = resolved.rekeyed
|
|
430
|
+
const expected = typeKeyFor(kind.name, t.alias)
|
|
431
|
+
const key = resolved.key
|
|
432
|
+
if ((!t.key && key.toLowerCase() !== expected.toLowerCase()) || rekeyed)
|
|
409
433
|
report.assignedKeys.push({ file, typeAlias: t.alias, key, revived: false })
|
|
410
434
|
typeKeyByAlias.set(t.alias, key)
|
|
435
|
+
const typeKey = key
|
|
411
436
|
const retired = existing ? await kind.repo.retiredProperties(existing.key) : []
|
|
412
437
|
const liveByAlias = new Map((existing?.properties ?? []).map((p) => [p.alias, p]))
|
|
413
|
-
const shippedPropertyKeys
|
|
414
|
-
? systemPropertyKeys(t.alias)
|
|
415
|
-
: new Map()
|
|
438
|
+
const shippedProperties = shippedPropertyKeys(kind.name, t.alias)
|
|
416
439
|
|
|
417
440
|
const containers: ContentTypeAggregate['containers'] = []
|
|
418
441
|
const properties: PropertyTypeModel[] = []
|
|
@@ -426,10 +449,10 @@ export async function syncSchema(
|
|
|
426
449
|
if (!key) {
|
|
427
450
|
const live = liveByAlias.get(p.alias)
|
|
428
451
|
const back = retired.find((r) => r.alias === p.alias)
|
|
429
|
-
|
|
430
|
-
|
|
452
|
+
const expectedKey = propertyKeyFor(typeKey, p.alias, shippedProperties)
|
|
453
|
+
key = live?.key ?? back?.key ?? expectedKey
|
|
431
454
|
revived = !live && back !== undefined
|
|
432
|
-
if (
|
|
455
|
+
if (key.toLowerCase() !== expectedKey.toLowerCase())
|
|
433
456
|
report.assignedKeys.push({
|
|
434
457
|
file,
|
|
435
458
|
typeAlias: t.alias,
|
|
@@ -493,7 +516,7 @@ export async function syncSchema(
|
|
|
493
516
|
(c) => c.name === name && (c.parentKey ?? null) === parentKey && c.type === type,
|
|
494
517
|
)?.key ??
|
|
495
518
|
fixedGroupKeys.get(`${t.alias}|${name}`) ??
|
|
496
|
-
|
|
519
|
+
derivedKey(typeKey, type, parentKey ?? '', name)
|
|
497
520
|
for (const p of t.properties) properties.push(resolveProperty(p, null))
|
|
498
521
|
let rootGroupOrder = 0
|
|
499
522
|
for (const g of t.groups ?? []) {
|
|
@@ -638,6 +661,11 @@ export async function syncSchema(
|
|
|
638
661
|
}
|
|
639
662
|
}
|
|
640
663
|
|
|
664
|
+
// Folder holds whichever built-in media types the site now has (story 180),
|
|
665
|
+
// in this transaction so no node sees the new state before Folder does.
|
|
666
|
+
// A file that defines Folder says itself what it allows.
|
|
667
|
+
if (!(set.mediaTypes ?? []).some((t) => t.alias.toLowerCase() === 'folder'))
|
|
668
|
+
await ensureSystemMediaTypes(tx, { linkFolderChildren: true })
|
|
641
669
|
if (cutOver) {
|
|
642
670
|
await makeStateCurrent(tx, state.id)
|
|
643
671
|
await updateSchemaStateHash(tx, state.id, hash)
|
|
@@ -661,18 +689,35 @@ export async function syncSchema(
|
|
|
661
689
|
|
|
662
690
|
export class SchemaSyncRefused extends Error {}
|
|
663
691
|
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
692
|
+
/**
|
|
693
|
+
* A type's key (story 161): the file's, else the row that has its alias, else
|
|
694
|
+
* the shipped or derived one. A key this site has never had, for an alias it
|
|
695
|
+
* has and no other file claims, is that type: the row keeps its key, and the
|
|
696
|
+
* file is given it (story 105).
|
|
697
|
+
*/
|
|
698
|
+
async function resolveTypeKey(
|
|
699
|
+
repo: ContentTypeRepository,
|
|
700
|
+
kind: 'document' | 'media' | 'member',
|
|
701
|
+
t: SchemaContentType,
|
|
702
|
+
fileTypeKeys: ReadonlySet<string>,
|
|
703
|
+
): Promise<{ existing: ContentTypeAggregate | undefined; key: string; rekeyed: boolean }> {
|
|
704
|
+
let existing = t.key
|
|
705
|
+
? await repo.byKey(t.key)
|
|
706
|
+
: await repo.byAlias(t.alias, { includeRetired: true })
|
|
707
|
+
let rekeyed = false
|
|
708
|
+
if (!existing && t.key) {
|
|
709
|
+
const sameAlias = await repo.byAlias(t.alias, { includeRetired: true })
|
|
710
|
+
if (sameAlias && !fileTypeKeys.has(sameAlias.key.toLowerCase())) {
|
|
711
|
+
existing = sameAlias
|
|
712
|
+
rekeyed = true
|
|
713
|
+
}
|
|
667
714
|
}
|
|
715
|
+
const key = (rekeyed ? existing?.key : t.key) ?? existing?.key ?? typeKeyFor(kind, t.alias)
|
|
716
|
+
return { existing, key, rekeyed }
|
|
668
717
|
}
|
|
669
718
|
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
if (!t.key) missing.push(t.alias)
|
|
674
|
-
for (const p of allProperties(t)) if (!p.key) missing.push(`${t.alias}.${p.alias}`)
|
|
719
|
+
class DryRun extends Error {
|
|
720
|
+
constructor(readonly report: SyncReport) {
|
|
721
|
+
super('dry run')
|
|
675
722
|
}
|
|
676
|
-
for (const d of set.dataTypes) if (!d.key) missing.push(`data-type ${d.alias}`)
|
|
677
|
-
return missing
|
|
678
723
|
}
|
package/src/validate.ts
CHANGED
|
@@ -3,7 +3,13 @@
|
|
|
3
3
|
* a person fixing a schema wants the full list.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
BUILT_IN_MEDIA_TYPES,
|
|
8
|
+
SYSTEM_CONTENT_TYPES,
|
|
9
|
+
SYSTEM_MEDIA_TYPES,
|
|
10
|
+
SYSTEM_MEMBER_TYPE,
|
|
11
|
+
} from '@crombolo/data'
|
|
12
|
+
import { compileBlocks } from './blocks.ts'
|
|
7
13
|
import {
|
|
8
14
|
BUILTIN_DATA_TYPE_ALIASES,
|
|
9
15
|
checkSystemContentTypeFile,
|
|
@@ -11,6 +17,7 @@ import {
|
|
|
11
17
|
SYSTEM_CONTENT_TYPE_ALIASES,
|
|
12
18
|
SYSTEM_MEDIA_TYPE_ALIASES,
|
|
13
19
|
} from './builtin.ts'
|
|
20
|
+
import { type ReferenceKind, resolveConfig } from './config-references.ts'
|
|
14
21
|
import {
|
|
15
22
|
allProperties,
|
|
16
23
|
type SchemaContentType,
|
|
@@ -190,6 +197,53 @@ export function validateSchemaSet(set: SchemaSet, options: ValidateOptions = {})
|
|
|
190
197
|
}
|
|
191
198
|
}
|
|
192
199
|
|
|
200
|
+
// ---- types a data type names, by alias, in its config or its block tables
|
|
201
|
+
// (story 161): each must exist, be of the kind the field takes, and be an
|
|
202
|
+
// element type where the editor places elements.
|
|
203
|
+
const contentType = (alias: string) =>
|
|
204
|
+
set.contentTypes.find((t) => t.alias === alias) ??
|
|
205
|
+
SYSTEM_CONTENT_TYPES.find((t) => t.alias === alias)
|
|
206
|
+
const exists = (alias: string, kind: ReferenceKind): boolean =>
|
|
207
|
+
kind === 'element'
|
|
208
|
+
? contentType(alias)?.isElement === true
|
|
209
|
+
: kind === 'document'
|
|
210
|
+
? contentType(alias) !== undefined
|
|
211
|
+
: kind === 'media'
|
|
212
|
+
? (set.mediaTypes ?? []).some((t) => t.alias === alias) ||
|
|
213
|
+
SYSTEM_MEDIA_TYPES.some((t) => t.alias === alias) ||
|
|
214
|
+
BUILT_IN_MEDIA_TYPES.some((t) => t.alias === alias)
|
|
215
|
+
: (set.memberTypes ?? []).some((t) => t.alias === alias) ||
|
|
216
|
+
SYSTEM_MEMBER_TYPE.alias === alias
|
|
217
|
+
const referenceMessage = (alias: string, kind: ReferenceKind) =>
|
|
218
|
+
kind === 'element' && contentType(alias)
|
|
219
|
+
? `"${alias}" is not an element type`
|
|
220
|
+
: `unknown ${kind === 'element' ? 'element' : kind} type "${alias}"`
|
|
221
|
+
for (const d of set.dataTypes) {
|
|
222
|
+
const file = `schema/data-types/${d.alias}`
|
|
223
|
+
const { problems: found } = resolveConfig(d.editor, d.config, (alias, kind) =>
|
|
224
|
+
exists(alias, kind) ? alias : undefined,
|
|
225
|
+
)
|
|
226
|
+
for (const p of found)
|
|
227
|
+
add(file, `data-type.config.${p.path}`, referenceMessage(p.alias, p.kind))
|
|
228
|
+
if (d.blocks) {
|
|
229
|
+
const compiled = compileBlocks('', d.blocks, (alias) =>
|
|
230
|
+
exists(alias, 'element') ? alias : undefined,
|
|
231
|
+
)
|
|
232
|
+
for (const p of compiled.problems)
|
|
233
|
+
add(file, `data-type.${p.path}`, referenceMessage(p.alias, p.kind))
|
|
234
|
+
const groups = new Set(d.blocks.flatMap((b) => (b.group ? [b.group] : [])))
|
|
235
|
+
for (const [index, b] of d.blocks.entries())
|
|
236
|
+
for (const [i, area] of (b.areas ?? []).entries())
|
|
237
|
+
for (const entry of area.allow ?? [])
|
|
238
|
+
if (entry.group && !groups.has(entry.group))
|
|
239
|
+
add(
|
|
240
|
+
file,
|
|
241
|
+
`data-type.block[${index}].area[${i}].allow`,
|
|
242
|
+
`no block is in the group "${entry.group}"`,
|
|
243
|
+
)
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
193
247
|
const defaults = set.languages.filter((l) => l.default)
|
|
194
248
|
if (set.languages.length > 0 && defaults.length !== 1)
|
|
195
249
|
add(
|
package/src/write.ts
CHANGED
|
@@ -6,6 +6,13 @@
|
|
|
6
6
|
* that survive), and none of the quote or blank-line churn of general-purpose
|
|
7
7
|
* TOML serialisers. `parse(write(x))` must equal `x` — the tests hold it to that.
|
|
8
8
|
*/
|
|
9
|
+
import {
|
|
10
|
+
dataTypeKeyFor,
|
|
11
|
+
isDerived,
|
|
12
|
+
propertyKeyFor,
|
|
13
|
+
shippedPropertyKeys,
|
|
14
|
+
typeKeyFor,
|
|
15
|
+
} from './keys.ts'
|
|
9
16
|
import {
|
|
10
17
|
type SchemaContentType,
|
|
11
18
|
type SchemaDataType,
|
|
@@ -20,12 +27,21 @@ function str(value: string): string {
|
|
|
20
27
|
return JSON.stringify(value)
|
|
21
28
|
}
|
|
22
29
|
|
|
30
|
+
/**
|
|
31
|
+
* A value as TOML: nested config as inline tables and arrays of them, not a
|
|
32
|
+
* JSON string (story 161), so block config or `validationLimit = { min, max }`
|
|
33
|
+
* survives a rewrite as the TOML it was written in. A null inside a table is
|
|
34
|
+
* left out, since TOML has none.
|
|
35
|
+
*/
|
|
23
36
|
function scalar(value: unknown): string {
|
|
24
37
|
if (typeof value === 'string') return str(value)
|
|
25
38
|
if (typeof value === 'number' || typeof value === 'boolean') return String(value)
|
|
26
39
|
if (Array.isArray(value)) return `[${value.map(scalar).join(', ')}]`
|
|
27
40
|
if (value === null || value === undefined) return '""'
|
|
28
|
-
|
|
41
|
+
const entries = Object.entries(value as Record<string, unknown>).filter(
|
|
42
|
+
([, v]) => v !== null && v !== undefined,
|
|
43
|
+
)
|
|
44
|
+
return `{ ${entries.map(([k, v]) => `${keyOf(k)} = ${scalar(v)}`).join(', ')} }`
|
|
29
45
|
}
|
|
30
46
|
|
|
31
47
|
/**
|
|
@@ -48,9 +64,21 @@ function line(out: string[], key: string, value: unknown, when = true): void {
|
|
|
48
64
|
out.push(`${keyOf(key)} = ${scalar(value)}`)
|
|
49
65
|
}
|
|
50
66
|
|
|
51
|
-
|
|
67
|
+
/** Where a property sits, for the key it would derive. */
|
|
68
|
+
interface PropertyOwner {
|
|
69
|
+
typeKey: string
|
|
70
|
+
shipped: ReadonlyMap<string, string>
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function writeProperty(
|
|
74
|
+
out: string[],
|
|
75
|
+
header: string,
|
|
76
|
+
p: SchemaProperty,
|
|
77
|
+
owner: PropertyOwner,
|
|
78
|
+
): void {
|
|
52
79
|
out.push('', `[[${header}]]`)
|
|
53
|
-
|
|
80
|
+
// A key is written only when it is not the one the property would derive (story 161).
|
|
81
|
+
line(out, 'key', p.key, !isDerived(p.key, propertyKeyFor(owner.typeKey, p.alias, owner.shipped)))
|
|
54
82
|
line(out, 'alias', p.alias)
|
|
55
83
|
line(out, 'name', p.name)
|
|
56
84
|
line(out, 'description', p.description)
|
|
@@ -74,7 +102,12 @@ function writeProperty(out: string[], header: string, p: SchemaProperty): void {
|
|
|
74
102
|
|
|
75
103
|
export function writeContentType(t: SchemaContentType, kind: SchemaTypeKind = 'document'): string {
|
|
76
104
|
const out: string[] = [`[${TYPE_KIND_FILES[kind].header}]`]
|
|
77
|
-
|
|
105
|
+
const expected = typeKeyFor(kind, t.alias)
|
|
106
|
+
line(out, 'key', t.key, !isDerived(t.key, expected))
|
|
107
|
+
const owner: PropertyOwner = {
|
|
108
|
+
typeKey: t.key ?? expected,
|
|
109
|
+
shipped: shippedPropertyKeys(kind, t.alias),
|
|
110
|
+
}
|
|
78
111
|
line(out, 'alias', t.alias)
|
|
79
112
|
line(out, 'name', t.name)
|
|
80
113
|
line(out, 'description', t.description)
|
|
@@ -103,23 +136,23 @@ export function writeContentType(t: SchemaContentType, kind: SchemaTypeKind = 'd
|
|
|
103
136
|
line(out, 'keep-all-newer-than-days', t.cleanup.keepAllNewerThanDays)
|
|
104
137
|
line(out, 'keep-latest-per-day-for-days', t.cleanup.keepLatestPerDayForDays)
|
|
105
138
|
}
|
|
106
|
-
for (const p of t.properties) writeProperty(out, 'property', p)
|
|
139
|
+
for (const p of t.properties) writeProperty(out, 'property', p, owner)
|
|
107
140
|
for (const g of t.groups ?? []) {
|
|
108
141
|
out.push('', '[[group]]')
|
|
109
142
|
line(out, 'name', g.name)
|
|
110
143
|
line(out, 'alias', g.alias)
|
|
111
|
-
for (const p of g.properties) writeProperty(out, 'group.property', p)
|
|
144
|
+
for (const p of g.properties) writeProperty(out, 'group.property', p, owner)
|
|
112
145
|
}
|
|
113
146
|
for (const tab of t.tabs) {
|
|
114
147
|
out.push('', '[[tab]]')
|
|
115
148
|
line(out, 'name', tab.name)
|
|
116
149
|
line(out, 'alias', tab.alias)
|
|
117
|
-
for (const p of tab.properties) writeProperty(out, 'tab.property', p)
|
|
150
|
+
for (const p of tab.properties) writeProperty(out, 'tab.property', p, owner)
|
|
118
151
|
for (const g of tab.groups) {
|
|
119
152
|
out.push('', '[[tab.group]]')
|
|
120
153
|
line(out, 'name', g.name)
|
|
121
154
|
line(out, 'alias', g.alias)
|
|
122
|
-
for (const p of g.properties) writeProperty(out, 'tab.group.property', p)
|
|
155
|
+
for (const p of g.properties) writeProperty(out, 'tab.group.property', p, owner)
|
|
123
156
|
}
|
|
124
157
|
}
|
|
125
158
|
return `${out.join('\n')}\n`
|
|
@@ -135,7 +168,7 @@ export function writeMemberType(t: SchemaContentType): string {
|
|
|
135
168
|
|
|
136
169
|
export function writeDataType(d: SchemaDataType): string {
|
|
137
170
|
const out: string[] = ['[data-type]']
|
|
138
|
-
line(out, 'key', d.key)
|
|
171
|
+
line(out, 'key', d.key, !isDerived(d.key, dataTypeKeyFor(d.alias)))
|
|
139
172
|
line(out, 'alias', d.alias)
|
|
140
173
|
line(out, 'name', d.name)
|
|
141
174
|
line(out, 'notes', d.notes)
|
|
@@ -148,6 +181,40 @@ export function writeDataType(d: SchemaDataType): string {
|
|
|
148
181
|
out.push('', '[data-type.config]')
|
|
149
182
|
for (const key of keys) line(out, key, d.config[key])
|
|
150
183
|
}
|
|
184
|
+
for (const b of d.blocks ?? []) {
|
|
185
|
+
out.push('', '[[data-type.block]]')
|
|
186
|
+
line(out, 'element', b.element)
|
|
187
|
+
line(out, 'settings', b.settings)
|
|
188
|
+
line(out, 'label', b.label)
|
|
189
|
+
line(out, 'size', b.size)
|
|
190
|
+
line(out, 'background', b.background)
|
|
191
|
+
line(out, 'icon-color', b.iconColor)
|
|
192
|
+
line(out, 'thumbnail', b.thumbnail)
|
|
193
|
+
line(out, 'group', b.group)
|
|
194
|
+
line(out, 'allow-at-root', b.allowAtRoot)
|
|
195
|
+
line(out, 'allow-in-areas', b.allowInAreas)
|
|
196
|
+
line(out, 'column-spans', b.columnSpans)
|
|
197
|
+
line(out, 'row-span', b.rowSpan)
|
|
198
|
+
line(out, 'area-columns', b.areaColumns)
|
|
199
|
+
for (const a of b.areas ?? []) {
|
|
200
|
+
out.push('', '[[data-type.block.area]]')
|
|
201
|
+
line(out, 'alias', a.alias)
|
|
202
|
+
line(out, 'column-span', a.columnSpan)
|
|
203
|
+
line(out, 'row-span', a.rowSpan)
|
|
204
|
+
line(out, 'create-label', a.createLabel)
|
|
205
|
+
line(out, 'min', a.min)
|
|
206
|
+
line(out, 'max', a.max)
|
|
207
|
+
line(
|
|
208
|
+
out,
|
|
209
|
+
'allow',
|
|
210
|
+
a.allow?.map((entry) =>
|
|
211
|
+
entry.element && entry.min === undefined && entry.max === undefined
|
|
212
|
+
? entry.element
|
|
213
|
+
: entry,
|
|
214
|
+
),
|
|
215
|
+
)
|
|
216
|
+
}
|
|
217
|
+
}
|
|
151
218
|
return `${out.join('\n')}\n`
|
|
152
219
|
}
|
|
153
220
|
|