@crombolo/schema 0.44.0 → 0.46.1

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crombolo/schema",
3
- "version": "0.44.0",
3
+ "version": "0.46.1",
4
4
  "description": "Internal to crombolo, with no compatibility promise: use @crombolo/crombolo. Schema-as-code for crombolo: TOML document, media and member type definitions.",
5
5
  "license": "MIT",
6
6
  "author": "Crombolo",
@@ -28,7 +28,7 @@
28
28
  "access": "public"
29
29
  },
30
30
  "dependencies": {
31
- "@crombolo/core": "0.44.0",
32
- "@crombolo/data": "0.44.0"
31
+ "@crombolo/core": "0.46.1",
32
+ "@crombolo/data": "0.46.1"
33
33
  }
34
34
  }
package/src/check.ts CHANGED
@@ -22,6 +22,7 @@ import {
22
22
  SYSTEM_MEMBER_TYPE,
23
23
  } from '@crombolo/data'
24
24
  import { BUILTIN_DATA_TYPES, storageTypeFor } from './builtin.ts'
25
+ import { propertyKeyFor, shippedPropertyKeys, typeKeyFor } from './keys.ts'
25
26
  import type { LoadedSchema } from './load.ts'
26
27
  import { allProperties, type SchemaContentType, type SchemaProperty } from './model.ts'
27
28
  import {
@@ -33,6 +34,14 @@ import {
33
34
  } from './sync.ts'
34
35
  import { findConverter, outputsOf, type ValueMigration } from './value-migrations.ts'
35
36
 
37
+ /** Findings that only say what will happen, with nothing to settle first. */
38
+ const SAID_ONLY = new Set([
39
+ 'framework-migration',
40
+ 'mandatory-unfilled',
41
+ 'type-renamed',
42
+ 'property-renamed',
43
+ ])
44
+
36
45
  export type ChangeClass = 'none' | 'additive' | 'data-requiring' | 'breaking'
37
46
 
38
47
  export interface CheckOptions extends Omit<SyncOptions, 'dryRun' | 'status'> {
@@ -252,6 +261,8 @@ export async function runCheck(
252
261
  for (const { kind, files, noun, items } of kinds) {
253
262
  const repo = kind === 'document' ? types : new ContentTypeRepository(db, { kind })
254
263
  const inFiles = new Set(files.map((t) => t.alias.toLowerCase()))
264
+ // A type renamed keeps its key (story 250): it is the same type under another alias.
265
+ const keysInFiles = new Set(files.flatMap((t) => (t.key ? [t.key.toLowerCase()] : [])))
255
266
  const dbTypes = await db.q
256
267
  .selectFrom('content_type as ct')
257
268
  .innerJoin('node as n', 'n.id', 'ct.node_id')
@@ -263,7 +274,23 @@ export async function runCheck(
263
274
  .execute()
264
275
  for (const row of dbTypes) {
265
276
  const key = String(row.unique_id).toLowerCase()
266
- if (inFiles.has(String(row.alias).toLowerCase()) || shipped.has(key)) continue
277
+ const renamedTo = !inFiles.has(String(row.alias).toLowerCase())
278
+ ? files.find((t) => t.key?.toLowerCase() === key)
279
+ : undefined
280
+ if (renamedTo)
281
+ findings.push({
282
+ kind: 'auto',
283
+ code: 'type-renamed',
284
+ subjectType: `${kind}-type` as const,
285
+ subjectKey: key,
286
+ subjectName: renamedTo.alias,
287
+ propertyAlias: null,
288
+ culture: null,
289
+ message: `${noun} "${row.alias}" is renamed "${renamedTo.alias}", keeping what is made of it`,
290
+ link: null,
291
+ })
292
+ if (inFiles.has(String(row.alias).toLowerCase()) || keysInFiles.has(key) || shipped.has(key))
293
+ continue
267
294
  const count = await repo.contentCount(key)
268
295
  if (count === 0) continue
269
296
  findings.push({
@@ -280,6 +307,41 @@ export async function runCheck(
280
307
  }
281
308
  }
282
309
 
310
+ // ---- properties renamed under their key (story 250), so a release says so
311
+ for (const { kind, files, noun } of kinds)
312
+ for (const t of files) {
313
+ const typeKey = (t.key ?? typeKeyFor(kind, t.alias)).toLowerCase()
314
+ const shippedKeys = shippedPropertyKeys(kind, t.alias)
315
+ const inFile = new Map(
316
+ allProperties(t).map((p) => [
317
+ (p.key ?? propertyKeyFor(typeKey, p.alias, shippedKeys)).toLowerCase(),
318
+ p.alias,
319
+ ]),
320
+ )
321
+ const rows = await db.q
322
+ .selectFrom('property_type as pt')
323
+ .innerJoin('node as n', 'n.id', 'pt.content_type_id')
324
+ .select(['pt.unique_id', 'pt.alias'])
325
+ .where('n.unique_id', '=', typeKey)
326
+ .where('pt.retired_at', 'is', null)
327
+ .execute()
328
+ for (const row of rows) {
329
+ const now = inFile.get(String(row.unique_id).toLowerCase())
330
+ if (!now || now === row.alias) continue
331
+ findings.push({
332
+ kind: 'auto',
333
+ code: 'property-renamed',
334
+ subjectType: 'property',
335
+ subjectKey: String(row.unique_id).toLowerCase(),
336
+ subjectName: t.alias,
337
+ propertyAlias: now,
338
+ culture: null,
339
+ message: `property "${row.alias}" of ${noun} "${t.alias}" is renamed "${now}", keeping its values`,
340
+ link: null,
341
+ })
342
+ }
343
+ }
344
+
283
345
  // ---- properties that need data: newly mandatory, or changing editor
284
346
  let breaking = false
285
347
  let dataRequiring = false
@@ -536,9 +598,7 @@ export async function runCheck(
536
598
  : 'none'
537
599
  return {
538
600
  findings,
539
- outstanding: findings.filter(
540
- (f) => f.code !== 'framework-migration' && f.code !== 'mandatory-unfilled',
541
- ),
601
+ outstanding: findings.filter((f) => !SAID_ONLY.has(f.code)),
542
602
  classification,
543
603
  sync,
544
604
  frameworkPending,
@@ -0,0 +1,74 @@
1
+ /**
2
+ * What a type takes from its compositions (story 255): each property with the
3
+ * type that defines it, so `types show` can say where it comes from and the
4
+ * check can refuse a property whose alias a composition already has.
5
+ */
6
+ import { BUILT_IN_MEDIA_TYPES, SYSTEM_CONTENT_TYPES } from '@crombolo/data'
7
+ import { builtInContentTypeSchema, builtInMediaTypeSchema } from './builtin.ts'
8
+ import type { SchemaContentType, SchemaProperty, SchemaSet } from './model.ts'
9
+
10
+ export interface PlacedProperty {
11
+ property: SchemaProperty
12
+ tab: string | null
13
+ group: string | null
14
+ }
15
+
16
+ export interface ComposedProperty extends PlacedProperty {
17
+ /** The type that defines it. */
18
+ from: string
19
+ /** The composition it comes through, when that is not the type defining it. */
20
+ via?: string
21
+ }
22
+
23
+ /** A type's own properties, each with its tab and group, in editor order. */
24
+ export function placedProperties(type: SchemaContentType): PlacedProperty[] {
25
+ return [
26
+ ...type.properties.map((property) => ({ property, tab: null, group: null })),
27
+ ...(type.groups ?? []).flatMap((group) =>
28
+ group.properties.map((property) => ({ property, tab: null, group: group.name })),
29
+ ),
30
+ ...type.tabs.flatMap((tab) => [
31
+ ...tab.properties.map((property) => ({ property, tab: tab.name, group: null })),
32
+ ...tab.groups.flatMap((group) =>
33
+ group.properties.map((property) => ({ property, tab: tab.name, group: group.name })),
34
+ ),
35
+ ]),
36
+ ]
37
+ }
38
+
39
+ /** The type a composition names: the site's own, or a built-in one such as `seoFields`. */
40
+ export function compositionType(set: SchemaSet, alias: string): SchemaContentType | undefined {
41
+ const shipped = SYSTEM_CONTENT_TYPES.find((t) => t.alias === alias)
42
+ const shippedMedia = BUILT_IN_MEDIA_TYPES.find((t) => t.alias === alias)
43
+ return (
44
+ set.contentTypes.find((t) => t.alias === alias) ??
45
+ set.mediaTypes?.find((t) => t.alias === alias) ??
46
+ (shipped ? builtInContentTypeSchema(shipped) : undefined) ??
47
+ (shippedMedia ? builtInMediaTypeSchema(shippedMedia) : undefined)
48
+ )
49
+ }
50
+
51
+ /** Every property a type takes from its compositions, each once, with where it comes from. */
52
+ export function composedProperties(set: SchemaSet, type: SchemaContentType): ComposedProperty[] {
53
+ const out: ComposedProperty[] = []
54
+ const seen = new Set<string>([type.alias])
55
+ const walk = (alias: string, via: string | undefined) => {
56
+ if (seen.has(alias)) return
57
+ seen.add(alias)
58
+ const composed = compositionType(set, alias)
59
+ if (!composed) return
60
+ for (const placed of placedProperties(composed))
61
+ out.push({ ...placed, from: composed.alias, ...(via ? { via } : {}) })
62
+ for (const next of composed.compositions) walk(next, via ?? alias)
63
+ }
64
+ for (const alias of type.compositions) walk(alias, undefined)
65
+ return out
66
+ }
67
+
68
+ /** Every property a type carries, including those from its compositions. */
69
+ export function effectiveProperties(set: SchemaSet, type: SchemaContentType): SchemaProperty[] {
70
+ return [
71
+ ...placedProperties(type).map((p) => p.property),
72
+ ...composedProperties(set, type).map((p) => p.property),
73
+ ]
74
+ }
@@ -75,7 +75,7 @@ const itemsOf = (value: unknown): string[] =>
75
75
  * `map` makes of it, returning a copy. `map` answers undefined to leave a
76
76
  * value as it is.
77
77
  */
78
- function mapReferences(
78
+ export function mapReferences(
79
79
  editor: string,
80
80
  config: Record<string, unknown>,
81
81
  map: (value: string, kind: ReferenceKind, path: string) => string | undefined,
@@ -0,0 +1,333 @@
1
+ /**
2
+ * A site's definitions described for a person (story 247): the rows
3
+ * `crombolo types list` prints, the detail `types show` prints, and what uses
4
+ * each one, read from a loaded `SchemaSet` alone. Pure, so stories 248 to 251
5
+ * reuse it to say what a change touches.
6
+ */
7
+ import { BUILTIN_DATA_TYPES } from './builtin.ts'
8
+ import { composedProperties, type PlacedProperty, placedProperties } from './compositions.ts'
9
+ import {
10
+ allProperties,
11
+ type SchemaContentType,
12
+ type SchemaDataType,
13
+ type SchemaSet,
14
+ } from './model.ts'
15
+
16
+ /** What a definition is, as a person names it. */
17
+ export type DescribedKind = 'content' | 'element' | 'media' | 'member' | 'data'
18
+
19
+ export const DESCRIBED_KINDS: readonly DescribedKind[] = [
20
+ 'content',
21
+ 'element',
22
+ 'media',
23
+ 'member',
24
+ 'data',
25
+ ]
26
+
27
+ export interface TypeRow {
28
+ alias: string
29
+ name: string
30
+ kind: DescribedKind
31
+ icon: string | null
32
+ /** Properties, for a type; the editor, for a data type. */
33
+ properties: number | null
34
+ editor: string | null
35
+ builtIn: boolean
36
+ }
37
+
38
+ /** One way a definition is used by another. */
39
+ export interface Use {
40
+ /** What uses it: a type's alias, or a data type's. */
41
+ by: string
42
+ byKind: DescribedKind
43
+ /** The property, when it is a property that uses it. */
44
+ property?: string
45
+ how:
46
+ | 'allowed child'
47
+ | 'composition'
48
+ | 'block'
49
+ | 'block settings'
50
+ | 'picker allows'
51
+ | 'property of this data type'
52
+ }
53
+
54
+ export interface PropertyDetail {
55
+ alias: string
56
+ name: string
57
+ tab: string | null
58
+ group: string | null
59
+ dataType: string
60
+ mandatory: boolean
61
+ variesByCulture: boolean
62
+ variesBySegment: boolean
63
+ description: string | null
64
+ /** The composition that defines it; null for the type's own (story 255). */
65
+ from: string | null
66
+ /** The composition it comes through, when that is not the one defining it. */
67
+ via: string | null
68
+ }
69
+
70
+ export interface TypeDetail {
71
+ kind: Exclude<DescribedKind, 'data'>
72
+ alias: string
73
+ name: string
74
+ description: string | null
75
+ icon: string | null
76
+ allowAtRoot: boolean
77
+ allowChildren: string[]
78
+ compositions: string[]
79
+ views: string[]
80
+ defaultView: string | null
81
+ variesByCulture: boolean
82
+ variesBySegment: boolean
83
+ library: boolean
84
+ listView: string | null
85
+ tabs: string[]
86
+ properties: PropertyDetail[]
87
+ usedBy: Use[]
88
+ }
89
+
90
+ export interface DataTypeDetail {
91
+ kind: 'data'
92
+ alias: string
93
+ name: string
94
+ editor: string
95
+ builtIn: boolean
96
+ /** Its settings by their own names, with the plain name where one is known. */
97
+ settings: Array<{ setting: string; label: string | null; value: unknown }>
98
+ blocks: Array<{ element: string; settings: string | null; label: string | null }>
99
+ usedBy: Use[]
100
+ }
101
+
102
+ /** Plain names for the settings `types new data-type` already names (story 161). */
103
+ const SETTING_LABELS: Record<string, string> = {
104
+ maxChars: 'Most characters',
105
+ rows: 'Rows',
106
+ min: 'Minimum',
107
+ max: 'Maximum',
108
+ step: 'Step',
109
+ default: 'Starts on',
110
+ labelOn: 'Label',
111
+ format: 'Format',
112
+ multiple: 'More than one may be chosen',
113
+ items: 'Options',
114
+ filter: 'Allows',
115
+ allowedContentTypes: 'Allows',
116
+ startNode: 'Starts at',
117
+ validationLimit: 'How many',
118
+ gridColumns: 'Columns',
119
+ }
120
+
121
+ const typesOf = (set: SchemaSet) => [
122
+ ...set.contentTypes.map((type) => ({
123
+ type,
124
+ kind: (type.isElement ? 'element' : 'content') as Exclude<DescribedKind, 'data'>,
125
+ })),
126
+ ...(set.mediaTypes ?? []).map((type) => ({ type, kind: 'media' as const })),
127
+ ...(set.memberTypes ?? []).map((type) => ({ type, kind: 'member' as const })),
128
+ ]
129
+
130
+ const builtInAliases = new Set(BUILTIN_DATA_TYPES.map((dataType) => dataType.alias))
131
+
132
+ /** The site's data types and the built-in ones it has not replaced. */
133
+ function dataTypesOf(set: SchemaSet): Array<{ dataType: SchemaDataType; builtIn: boolean }> {
134
+ const own = new Set(set.dataTypes.map((dataType) => dataType.alias))
135
+ return [
136
+ ...set.dataTypes.map((dataType) => ({ dataType, builtIn: false })),
137
+ ...BUILTIN_DATA_TYPES.filter((dataType) => !own.has(dataType.alias)).map((dataType) => ({
138
+ dataType,
139
+ builtIn: true,
140
+ })),
141
+ ]
142
+ }
143
+
144
+ /** Every row `types list` prints, types then data types, each by alias. */
145
+ export function listTypes(
146
+ set: SchemaSet,
147
+ options: { kind?: DescribedKind; own?: boolean } = {},
148
+ ): TypeRow[] {
149
+ const rows: TypeRow[] = [
150
+ ...typesOf(set).map(({ type, kind }) => ({
151
+ alias: type.alias,
152
+ name: type.name,
153
+ kind,
154
+ icon: type.icon || null,
155
+ properties: allProperties(type).length,
156
+ editor: null,
157
+ builtIn: false,
158
+ })),
159
+ ...dataTypesOf(set).map(({ dataType, builtIn }) => ({
160
+ alias: dataType.alias,
161
+ name: dataType.name,
162
+ kind: 'data' as const,
163
+ icon: null,
164
+ properties: null,
165
+ editor: dataType.editor,
166
+ builtIn,
167
+ })),
168
+ ]
169
+ return rows
170
+ .filter((row) => !options.kind || row.kind === options.kind)
171
+ .filter((row) => !options.own || !row.builtIn)
172
+ .sort((a, b) =>
173
+ a.kind === b.kind
174
+ ? a.alias.localeCompare(b.alias)
175
+ : DESCRIBED_KINDS.indexOf(a.kind) - DESCRIBED_KINDS.indexOf(b.kind),
176
+ )
177
+ }
178
+
179
+ /** A picker's allowed types, whichever way its setting is written. */
180
+ function allowedBy(config: Record<string, unknown>): string[] {
181
+ const out: string[] = []
182
+ for (const setting of ['filter', 'allowedContentTypes']) {
183
+ const value = config[setting]
184
+ if (typeof value === 'string')
185
+ out.push(
186
+ ...value
187
+ .split(',')
188
+ .map((part) => part.trim())
189
+ .filter(Boolean),
190
+ )
191
+ else if (Array.isArray(value))
192
+ for (const part of value) if (typeof part === 'string') out.push(part)
193
+ }
194
+ return out
195
+ }
196
+
197
+ /** Everything in the set that uses `alias`, as a type or as a data type. */
198
+ export function usesOf(set: SchemaSet, alias: string): Use[] {
199
+ const uses: Use[] = []
200
+ for (const { type, kind } of typesOf(set)) {
201
+ if (type.allowChildren.includes(alias))
202
+ uses.push({ by: type.alias, byKind: kind, how: 'allowed child' })
203
+ if (type.compositions.includes(alias))
204
+ uses.push({ by: type.alias, byKind: kind, how: 'composition' })
205
+ for (const property of allProperties(type))
206
+ if (property.type === alias)
207
+ uses.push({
208
+ by: type.alias,
209
+ byKind: kind,
210
+ property: property.alias,
211
+ how: 'property of this data type',
212
+ })
213
+ }
214
+ for (const { dataType } of dataTypesOf(set)) {
215
+ for (const block of dataType.blocks ?? []) {
216
+ if (block.element === alias) uses.push({ by: dataType.alias, byKind: 'data', how: 'block' })
217
+ if (block.settings === alias)
218
+ uses.push({ by: dataType.alias, byKind: 'data', how: 'block settings' })
219
+ }
220
+ if (allowedBy(dataType.config).includes(alias))
221
+ uses.push({ by: dataType.alias, byKind: 'data', how: 'picker allows' })
222
+ }
223
+ return uses
224
+ }
225
+
226
+ /**
227
+ * A type's properties, its own and its compositions', in its tabs: a
228
+ * composition's tab merges into the type's tab of the same name, as the back
229
+ * office shows it.
230
+ */
231
+ function propertyDetails(set: SchemaSet, type: SchemaContentType): PropertyDetail[] {
232
+ const detail = (
233
+ { property, tab, group }: PlacedProperty,
234
+ from: string | null,
235
+ via: string | null,
236
+ ): PropertyDetail => ({
237
+ alias: property.alias,
238
+ name: property.name,
239
+ tab,
240
+ group,
241
+ dataType: property.type,
242
+ mandatory: property.mandatory,
243
+ variesByCulture: property.variesByCulture,
244
+ variesBySegment: property.variesBySegment,
245
+ description: property.description ?? null,
246
+ from,
247
+ via,
248
+ })
249
+ const all = [
250
+ ...placedProperties(type).map((p) => detail(p, null, null)),
251
+ ...composedProperties(set, type).map((p) => detail(p, p.from, p.via ?? null)),
252
+ ]
253
+ const tabs: Array<string | null> = []
254
+ for (const p of all) if (!tabs.includes(p.tab)) tabs.push(p.tab)
255
+ const place = (p: PropertyDetail) => tabs.indexOf(p.tab)
256
+ return all
257
+ .map((p, i) => ({ p, i }))
258
+ .sort((a, b) => place(a.p) - place(b.p) || a.i - b.i)
259
+ .map(({ p }) => p)
260
+ }
261
+
262
+ export type Described = TypeDetail | DataTypeDetail
263
+
264
+ export type FindResult =
265
+ | { ok: true; found: Described }
266
+ | { ok: false; reason: 'not-found' }
267
+ | { ok: false; reason: 'ambiguous'; kinds: DescribedKind[] }
268
+
269
+ /** The definition an alias names, or why none is picked: none, or more than one kind. */
270
+ export function describeType(set: SchemaSet, alias: string, kind?: DescribedKind): FindResult {
271
+ const wanted = alias.toLowerCase()
272
+ const types = typesOf(set).filter(
273
+ ({ type, kind: k }) => type.alias.toLowerCase() === wanted && (!kind || k === kind),
274
+ )
275
+ const dataTypes =
276
+ kind && kind !== 'data'
277
+ ? []
278
+ : dataTypesOf(set).filter(({ dataType }) => dataType.alias.toLowerCase() === wanted)
279
+ const kinds = [...types.map((t) => t.kind), ...dataTypes.map(() => 'data' as const)]
280
+ if (kinds.length === 0) return { ok: false, reason: 'not-found' }
281
+ if (kinds.length > 1) return { ok: false, reason: 'ambiguous', kinds }
282
+ const [match] = types
283
+ if (match) {
284
+ const { type, kind: k } = match
285
+ return {
286
+ ok: true,
287
+ found: {
288
+ kind: k,
289
+ alias: type.alias,
290
+ name: type.name,
291
+ description: type.description ?? null,
292
+ icon: type.icon || null,
293
+ allowAtRoot: type.allowAtRoot,
294
+ allowChildren: [...type.allowChildren],
295
+ compositions: [...type.compositions],
296
+ views: [...type.components],
297
+ defaultView: type.defaultComponent ?? null,
298
+ variesByCulture: type.variesByCulture,
299
+ variesBySegment: type.variesBySegment,
300
+ library: type.allowInLibrary,
301
+ listView: type.collection ?? null,
302
+ tabs: type.tabs.map((tab) => tab.name),
303
+ properties: propertyDetails(set, type),
304
+ usedBy: usesOf(set, type.alias),
305
+ },
306
+ }
307
+ }
308
+ const { dataType, builtIn } = dataTypes[0] as { dataType: SchemaDataType; builtIn: boolean }
309
+ return {
310
+ ok: true,
311
+ found: {
312
+ kind: 'data',
313
+ alias: dataType.alias,
314
+ name: dataType.name,
315
+ editor: dataType.editor,
316
+ builtIn,
317
+ settings: Object.entries(dataType.config).map(([setting, value]) => ({
318
+ setting,
319
+ label: SETTING_LABELS[setting] ?? null,
320
+ value,
321
+ })),
322
+ blocks: (dataType.blocks ?? []).map((block) => ({
323
+ element: block.element,
324
+ settings: block.settings ?? null,
325
+ label: block.label ?? null,
326
+ })),
327
+ usedBy: usesOf(set, dataType.alias),
328
+ },
329
+ }
330
+ }
331
+
332
+ /** Whether a built-in data type: one a site uses without defining it. */
333
+ export const isBuiltInDataType = (alias: string): boolean => builtInAliases.has(alias)