@crombolo/schema 0.45.0 → 0.47.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/check.ts +64 -4
- package/src/compositions.ts +74 -0
- package/src/config-references.ts +1 -1
- package/src/describe.ts +333 -0
- package/src/edit.ts +369 -0
- package/src/form-edit.ts +612 -0
- package/src/generate-delivery.ts +2 -7
- package/src/generate.ts +6 -38
- package/src/index.ts +31 -0
- package/src/locate.ts +301 -0
- package/src/model.ts +2 -0
- package/src/presets.ts +4 -1
- package/src/reader.ts +32 -1
- package/src/rename.ts +303 -0
- package/src/scaffold.ts +1 -1
- package/src/site-edit.ts +153 -0
- package/src/sync.ts +1 -1
- package/src/user-groups.ts +4 -1
- package/src/validate.ts +36 -0
- package/src/workflow-edit.ts +200 -0
package/src/edit.ts
ADDED
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Changes to a content, element, media or member type (story 248), and to a
|
|
3
|
+
* data type (story 249): settings, tabs, properties, blocks, each a pure function to
|
|
4
|
+
* the changed type, or the reason it cannot change. Whether the change is safe
|
|
5
|
+
* for the site's content is the check's to say, not these.
|
|
6
|
+
*/
|
|
7
|
+
import {
|
|
8
|
+
allProperties,
|
|
9
|
+
type SchemaContentType,
|
|
10
|
+
type SchemaDataType,
|
|
11
|
+
type SchemaProperty,
|
|
12
|
+
type SchemaTab,
|
|
13
|
+
} from './model.ts'
|
|
14
|
+
import { DATA_TYPE_PRESETS, optionFlag, type PresetOptions } from './presets.ts'
|
|
15
|
+
import { addProperty } from './scaffold.ts'
|
|
16
|
+
|
|
17
|
+
export { addProperty }
|
|
18
|
+
|
|
19
|
+
export type EditResult = { ok: true; type: SchemaContentType } | { ok: false; reason: string }
|
|
20
|
+
|
|
21
|
+
const refuse = (reason: string): EditResult => ({ ok: false, reason })
|
|
22
|
+
|
|
23
|
+
export interface TypeChanges {
|
|
24
|
+
name?: string
|
|
25
|
+
description?: string
|
|
26
|
+
icon?: string
|
|
27
|
+
allowAtRoot?: boolean
|
|
28
|
+
isElement?: boolean
|
|
29
|
+
allowInLibrary?: boolean
|
|
30
|
+
variesByCulture?: boolean
|
|
31
|
+
variesBySegment?: boolean
|
|
32
|
+
/** The list view its children are shown in, or null for none. */
|
|
33
|
+
listView?: string | null
|
|
34
|
+
allowChild?: readonly string[]
|
|
35
|
+
disallowChild?: readonly string[]
|
|
36
|
+
compose?: readonly string[]
|
|
37
|
+
decompose?: readonly string[]
|
|
38
|
+
view?: readonly string[]
|
|
39
|
+
removeView?: readonly string[]
|
|
40
|
+
defaultView?: string
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const added = (list: readonly string[], more: readonly string[] = []) => [
|
|
44
|
+
...list,
|
|
45
|
+
...more.filter((item) => !list.includes(item)),
|
|
46
|
+
]
|
|
47
|
+
const without = (list: readonly string[], less: readonly string[] = []) =>
|
|
48
|
+
list.filter((item) => !less.includes(item))
|
|
49
|
+
|
|
50
|
+
/** A type's own settings, its allowed children, compositions and views. */
|
|
51
|
+
export function updateType(type: SchemaContentType, changes: TypeChanges): EditResult {
|
|
52
|
+
for (const alias of changes.compose ?? [])
|
|
53
|
+
if (alias === type.alias) return refuse(`"${type.alias}" cannot be composed of itself`)
|
|
54
|
+
for (const alias of changes.decompose ?? [])
|
|
55
|
+
if (!type.compositions.includes(alias))
|
|
56
|
+
return refuse(`"${type.alias}" is not composed of "${alias}"`)
|
|
57
|
+
for (const alias of changes.disallowChild ?? [])
|
|
58
|
+
if (!type.allowChildren.includes(alias))
|
|
59
|
+
return refuse(`"${type.alias}" does not allow "${alias}" beneath it`)
|
|
60
|
+
for (const view of changes.removeView ?? [])
|
|
61
|
+
if (!type.components.includes(view)) return refuse(`"${type.alias}" has no view "${view}"`)
|
|
62
|
+
const components = without(added(type.components, changes.view), changes.removeView)
|
|
63
|
+
const defaultView = changes.defaultView ?? type.defaultComponent
|
|
64
|
+
if (changes.defaultView && !components.includes(changes.defaultView))
|
|
65
|
+
return refuse(`"${changes.defaultView}" is not one of "${type.alias}"'s views`)
|
|
66
|
+
const next: SchemaContentType = {
|
|
67
|
+
...type,
|
|
68
|
+
...(changes.name !== undefined ? { name: changes.name } : {}),
|
|
69
|
+
...(changes.description !== undefined ? { description: changes.description || undefined } : {}),
|
|
70
|
+
...(changes.icon !== undefined ? { icon: changes.icon } : {}),
|
|
71
|
+
...(changes.allowAtRoot !== undefined ? { allowAtRoot: changes.allowAtRoot } : {}),
|
|
72
|
+
...(changes.isElement !== undefined ? { isElement: changes.isElement } : {}),
|
|
73
|
+
...(changes.allowInLibrary !== undefined ? { allowInLibrary: changes.allowInLibrary } : {}),
|
|
74
|
+
...(changes.variesByCulture !== undefined ? { variesByCulture: changes.variesByCulture } : {}),
|
|
75
|
+
...(changes.variesBySegment !== undefined ? { variesBySegment: changes.variesBySegment } : {}),
|
|
76
|
+
...(changes.listView !== undefined ? { collection: changes.listView ?? undefined } : {}),
|
|
77
|
+
allowChildren: without(added(type.allowChildren, changes.allowChild), changes.disallowChild),
|
|
78
|
+
compositions: without(added(type.compositions, changes.compose), changes.decompose),
|
|
79
|
+
components,
|
|
80
|
+
defaultComponent: defaultView && components.includes(defaultView) ? defaultView : components[0],
|
|
81
|
+
}
|
|
82
|
+
return { ok: true, type: next }
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Where a tab goes among the others: before or after one, or last. */
|
|
86
|
+
export interface Placement {
|
|
87
|
+
before?: string
|
|
88
|
+
after?: string
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function place<T>(
|
|
92
|
+
list: readonly T[],
|
|
93
|
+
item: T,
|
|
94
|
+
at: Placement,
|
|
95
|
+
nameOf: (x: T) => string,
|
|
96
|
+
): T[] | string {
|
|
97
|
+
const anchor = at.before ?? at.after
|
|
98
|
+
if (!anchor) return [...list, item]
|
|
99
|
+
const index = list.findIndex((x) => nameOf(x).toLowerCase() === anchor.toLowerCase())
|
|
100
|
+
if (index < 0) return `there is no "${anchor}" to put it ${at.before ? 'before' : 'after'}`
|
|
101
|
+
const out = [...list]
|
|
102
|
+
out.splice(at.before ? index : index + 1, 0, item)
|
|
103
|
+
return out
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const tabNamed = (type: SchemaContentType, name: string) =>
|
|
107
|
+
type.tabs.find((tab) => tab.name.toLowerCase() === name.toLowerCase())
|
|
108
|
+
|
|
109
|
+
export function addTab(type: SchemaContentType, name: string, at: Placement = {}): EditResult {
|
|
110
|
+
if (tabNamed(type, name)) return refuse(`"${type.alias}" already has a tab "${name}"`)
|
|
111
|
+
const tabs = place(type.tabs, { name, properties: [], groups: [] }, at, (t) => t.name)
|
|
112
|
+
return typeof tabs === 'string' ? refuse(tabs) : { ok: true, type: { ...type, tabs } }
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function renameTab(type: SchemaContentType, name: string, to: string): EditResult {
|
|
116
|
+
const tab = tabNamed(type, name)
|
|
117
|
+
if (!tab) return refuse(`"${type.alias}" has no tab "${name}"`)
|
|
118
|
+
if (to.toLowerCase() !== name.toLowerCase() && tabNamed(type, to))
|
|
119
|
+
return refuse(`"${type.alias}" already has a tab "${to}"`)
|
|
120
|
+
return {
|
|
121
|
+
ok: true,
|
|
122
|
+
type: { ...type, tabs: type.tabs.map((t) => (t === tab ? { ...t, name: to } : t)) },
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function moveTab(type: SchemaContentType, name: string, at: Placement): EditResult {
|
|
127
|
+
const tab = tabNamed(type, name)
|
|
128
|
+
if (!tab) return refuse(`"${type.alias}" has no tab "${name}"`)
|
|
129
|
+
if (!at.before && !at.after) return refuse('say where: before or after another tab')
|
|
130
|
+
const tabs = place(
|
|
131
|
+
type.tabs.filter((t) => t !== tab),
|
|
132
|
+
tab,
|
|
133
|
+
at,
|
|
134
|
+
(t) => t.name,
|
|
135
|
+
)
|
|
136
|
+
return typeof tabs === 'string' ? refuse(tabs) : { ok: true, type: { ...type, tabs } }
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const propertiesIn = (tab: SchemaTab) => [
|
|
140
|
+
...tab.properties,
|
|
141
|
+
...tab.groups.flatMap((group) => group.properties),
|
|
142
|
+
]
|
|
143
|
+
|
|
144
|
+
/** A tab taken out; its properties moved to another, or refused while it has any. */
|
|
145
|
+
export function removeTab(type: SchemaContentType, name: string, moveTo?: string): EditResult {
|
|
146
|
+
const tab = tabNamed(type, name)
|
|
147
|
+
if (!tab) return refuse(`"${type.alias}" has no tab "${name}"`)
|
|
148
|
+
const held = propertiesIn(tab)
|
|
149
|
+
if (held.length > 0 && !moveTo)
|
|
150
|
+
return refuse(
|
|
151
|
+
`the tab "${tab.name}" holds ${held.length} propert${held.length === 1 ? 'y' : 'ies'} (${held.map((p) => p.alias).join(', ')}); move them to another tab first, or name one to move them to`,
|
|
152
|
+
)
|
|
153
|
+
if (moveTo && !tabNamed(type, moveTo)) return refuse(`"${type.alias}" has no tab "${moveTo}"`)
|
|
154
|
+
if (moveTo && tabNamed(type, moveTo) === tab)
|
|
155
|
+
return refuse('a tab cannot take its own properties')
|
|
156
|
+
const target = moveTo ? tabNamed(type, moveTo) : undefined
|
|
157
|
+
const tabs = type.tabs
|
|
158
|
+
.filter((t) => t !== tab)
|
|
159
|
+
.map((t) => (t === target ? { ...t, properties: [...t.properties, ...held] } : t))
|
|
160
|
+
return { ok: true, type: { ...type, tabs } }
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export interface PropertyChanges {
|
|
164
|
+
name?: string
|
|
165
|
+
description?: string
|
|
166
|
+
mandatory?: boolean
|
|
167
|
+
regex?: string | null
|
|
168
|
+
regexMessage?: string | null
|
|
169
|
+
variesByCulture?: boolean
|
|
170
|
+
variesBySegment?: boolean
|
|
171
|
+
/** Its data type's alias. */
|
|
172
|
+
dataType?: string
|
|
173
|
+
memberCanView?: boolean
|
|
174
|
+
memberCanEdit?: boolean
|
|
175
|
+
sensitive?: boolean
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Every property of the type changed by `change` where `alias` matches; the rest as they are. */
|
|
179
|
+
function mapProperties(
|
|
180
|
+
type: SchemaContentType,
|
|
181
|
+
change: (p: SchemaProperty) => SchemaProperty | undefined,
|
|
182
|
+
): SchemaContentType {
|
|
183
|
+
const each = (list: readonly SchemaProperty[]) =>
|
|
184
|
+
list.flatMap((p) => {
|
|
185
|
+
const next = change(p)
|
|
186
|
+
return next ? [next] : []
|
|
187
|
+
})
|
|
188
|
+
return {
|
|
189
|
+
...type,
|
|
190
|
+
properties: each(type.properties),
|
|
191
|
+
groups: (type.groups ?? []).map((g) => ({ ...g, properties: each(g.properties) })),
|
|
192
|
+
tabs: type.tabs.map((t) => ({
|
|
193
|
+
...t,
|
|
194
|
+
properties: each(t.properties),
|
|
195
|
+
groups: t.groups.map((g) => ({ ...g, properties: each(g.properties) })),
|
|
196
|
+
})),
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const own = (type: SchemaContentType, alias: string) =>
|
|
201
|
+
allProperties(type).find((p) => p.alias.toLowerCase() === alias.toLowerCase())
|
|
202
|
+
|
|
203
|
+
/** A property's name, description, validation, variance or data type. */
|
|
204
|
+
export function updateProperty(
|
|
205
|
+
type: SchemaContentType,
|
|
206
|
+
alias: string,
|
|
207
|
+
changes: PropertyChanges,
|
|
208
|
+
): EditResult {
|
|
209
|
+
const property = own(type, alias)
|
|
210
|
+
if (!property) return refuse(`"${type.alias}" has no property "${alias}"`)
|
|
211
|
+
const next: SchemaProperty = {
|
|
212
|
+
...property,
|
|
213
|
+
...(changes.name !== undefined ? { name: changes.name } : {}),
|
|
214
|
+
...(changes.description !== undefined ? { description: changes.description || undefined } : {}),
|
|
215
|
+
...(changes.mandatory !== undefined ? { mandatory: changes.mandatory } : {}),
|
|
216
|
+
...(changes.regex !== undefined ? { regex: changes.regex ?? undefined } : {}),
|
|
217
|
+
...(changes.regexMessage !== undefined
|
|
218
|
+
? { regexMessage: changes.regexMessage ?? undefined }
|
|
219
|
+
: {}),
|
|
220
|
+
...(changes.variesByCulture !== undefined ? { variesByCulture: changes.variesByCulture } : {}),
|
|
221
|
+
...(changes.variesBySegment !== undefined ? { variesBySegment: changes.variesBySegment } : {}),
|
|
222
|
+
...(changes.dataType !== undefined ? { type: changes.dataType } : {}),
|
|
223
|
+
...(changes.memberCanView !== undefined ? { memberCanView: changes.memberCanView } : {}),
|
|
224
|
+
...(changes.memberCanEdit !== undefined ? { memberCanEdit: changes.memberCanEdit } : {}),
|
|
225
|
+
...(changes.sensitive !== undefined ? { sensitive: changes.sensitive } : {}),
|
|
226
|
+
}
|
|
227
|
+
return { ok: true, type: mapProperties(type, (p) => (p === property ? next : p)) }
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** A property taken out: retired by the sync, its values kept until `types purge`. */
|
|
231
|
+
export function removeProperty(type: SchemaContentType, alias: string): EditResult {
|
|
232
|
+
const property = own(type, alias)
|
|
233
|
+
if (!property) return refuse(`"${type.alias}" has no property "${alias}"`)
|
|
234
|
+
return { ok: true, type: mapProperties(type, (p) => (p === property ? undefined : p)) }
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export interface PropertyPlacement extends Placement {
|
|
238
|
+
/** The tab it goes into; the one it is in when absent. */
|
|
239
|
+
tab?: string
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** A property moved: to another tab, or before or after another property in its tab. */
|
|
243
|
+
export function moveProperty(
|
|
244
|
+
type: SchemaContentType,
|
|
245
|
+
alias: string,
|
|
246
|
+
at: PropertyPlacement,
|
|
247
|
+
): EditResult {
|
|
248
|
+
const property = own(type, alias)
|
|
249
|
+
if (!property) return refuse(`"${type.alias}" has no property "${alias}"`)
|
|
250
|
+
if (!at.tab && !at.before && !at.after)
|
|
251
|
+
return refuse('say where: a tab, or before or after a property')
|
|
252
|
+
const anchor = at.before ?? at.after
|
|
253
|
+
const anchorTab = anchor
|
|
254
|
+
? type.tabs.find((t) =>
|
|
255
|
+
t.properties.some((p) => p.alias.toLowerCase() === anchor.toLowerCase()),
|
|
256
|
+
)
|
|
257
|
+
: undefined
|
|
258
|
+
if (anchor && !anchorTab)
|
|
259
|
+
return refuse(`"${type.alias}" has no property "${anchor}" in a tab to put it beside`)
|
|
260
|
+
const target = at.tab ? tabNamed(type, at.tab) : anchorTab
|
|
261
|
+
if (!target) return refuse(`"${type.alias}" has no tab "${at.tab}"`)
|
|
262
|
+
if (anchorTab && anchorTab !== target)
|
|
263
|
+
return refuse(`"${anchor}" is in the tab "${anchorTab.name}", not "${target.name}"`)
|
|
264
|
+
const removed = mapProperties(type, (p) => (p === property ? undefined : p))
|
|
265
|
+
const tabs = removed.tabs.map((t) => {
|
|
266
|
+
if (t.name !== target.name) return t
|
|
267
|
+
const properties = place(t.properties, property, at, (p) => p.alias)
|
|
268
|
+
return {
|
|
269
|
+
...t,
|
|
270
|
+
properties: typeof properties === 'string' ? [...t.properties, property] : properties,
|
|
271
|
+
}
|
|
272
|
+
})
|
|
273
|
+
return { ok: true, type: { ...removed, tabs } }
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
export type DataTypeEditResult =
|
|
277
|
+
| { ok: true; dataType: SchemaDataType }
|
|
278
|
+
| { ok: false; reason: string }
|
|
279
|
+
|
|
280
|
+
export interface DataTypeChanges {
|
|
281
|
+
/** Settings stored as given, unchecked (story 249): they work when the editor uses the name. */
|
|
282
|
+
set?: Record<string, unknown>
|
|
283
|
+
unset?: readonly string[]
|
|
284
|
+
/** The presets' options (`--max-chars`, `--multiple`…), written as `types new data-type` writes them. */
|
|
285
|
+
options?: PresetOptions
|
|
286
|
+
addBlock?: { element: string; label?: string; settings?: string }
|
|
287
|
+
removeBlock?: string
|
|
288
|
+
/** A picker's start node: the page's key, or null for none. */
|
|
289
|
+
startNode?: string | null
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const BLOCK_EDITORS = new Set(['Umbraco.BlockList', 'Umbraco.BlockGrid', 'Umbraco.RichText'])
|
|
293
|
+
|
|
294
|
+
/** The preset whose editor a data type uses, when there is one. */
|
|
295
|
+
const presetFor = (dataType: SchemaDataType) =>
|
|
296
|
+
Object.entries(DATA_TYPE_PRESETS).find(([, preset]) => preset.editor === dataType.editor)
|
|
297
|
+
|
|
298
|
+
/** The settings a preset's options write, and only those: the rest of the data type is left as it is. */
|
|
299
|
+
function presetSettings(
|
|
300
|
+
dataType: SchemaDataType,
|
|
301
|
+
options: PresetOptions,
|
|
302
|
+
): Record<string, unknown> | string {
|
|
303
|
+
const given = Object.entries(options).filter(([, value]) => value !== undefined)
|
|
304
|
+
if (given.length === 0) return {}
|
|
305
|
+
const found = presetFor(dataType)
|
|
306
|
+
if (!found) return `"${dataType.editor}" takes no named settings: use --set <setting>=<value>`
|
|
307
|
+
const [name, preset] = found
|
|
308
|
+
const unknown = given.filter(([option]) => !preset.takes.includes(option as keyof PresetOptions))
|
|
309
|
+
if (unknown.length > 0)
|
|
310
|
+
return `${unknown.map(([option]) => optionFlag(option)).join(', ')} does not apply to ${name}: it takes ${preset.takes.map(optionFlag).join(', ') || 'no named settings'}`
|
|
311
|
+
const without = preset.build({}).config
|
|
312
|
+
const withThem = preset.build(options).config
|
|
313
|
+
return Object.fromEntries(
|
|
314
|
+
Object.entries(withThem).filter(
|
|
315
|
+
([key, value]) => JSON.stringify(value) !== JSON.stringify(without[key]),
|
|
316
|
+
),
|
|
317
|
+
)
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/** A data type's settings, blocks or start node. */
|
|
321
|
+
export function updateDataType(
|
|
322
|
+
dataType: SchemaDataType,
|
|
323
|
+
changes: DataTypeChanges,
|
|
324
|
+
): DataTypeEditResult {
|
|
325
|
+
const named = presetSettings(dataType, changes.options ?? {})
|
|
326
|
+
if (typeof named === 'string') return { ok: false, reason: named }
|
|
327
|
+
const config: Record<string, unknown> = { ...dataType.config, ...named, ...changes.set }
|
|
328
|
+
for (const setting of changes.unset ?? []) {
|
|
329
|
+
if (!(setting in config))
|
|
330
|
+
return { ok: false, reason: `"${dataType.alias}" has no setting "${setting}"` }
|
|
331
|
+
delete config[setting]
|
|
332
|
+
}
|
|
333
|
+
if (changes.startNode !== undefined) {
|
|
334
|
+
const current = (config.startNode ?? { type: 'content' }) as Record<string, unknown>
|
|
335
|
+
if (changes.startNode === null) {
|
|
336
|
+
const { id: _id, ...rest } = current
|
|
337
|
+
config.startNode = rest
|
|
338
|
+
} else config.startNode = { ...current, id: changes.startNode }
|
|
339
|
+
}
|
|
340
|
+
let blocks = dataType.blocks
|
|
341
|
+
if (changes.addBlock || changes.removeBlock) {
|
|
342
|
+
if (!BLOCK_EDITORS.has(dataType.editor))
|
|
343
|
+
return {
|
|
344
|
+
ok: false,
|
|
345
|
+
reason: `"${dataType.alias}" is a ${dataType.editor}, which has no blocks`,
|
|
346
|
+
}
|
|
347
|
+
const list = [...(dataType.blocks ?? [])]
|
|
348
|
+
if (changes.addBlock) {
|
|
349
|
+
if (list.some((b) => b.element === changes.addBlock?.element))
|
|
350
|
+
return {
|
|
351
|
+
ok: false,
|
|
352
|
+
reason: `"${dataType.alias}" already offers "${changes.addBlock.element}"`,
|
|
353
|
+
}
|
|
354
|
+
list.push({
|
|
355
|
+
element: changes.addBlock.element,
|
|
356
|
+
...(changes.addBlock.label ? { label: changes.addBlock.label } : {}),
|
|
357
|
+
...(changes.addBlock.settings ? { settings: changes.addBlock.settings } : {}),
|
|
358
|
+
})
|
|
359
|
+
}
|
|
360
|
+
if (changes.removeBlock) {
|
|
361
|
+
const index = list.findIndex((b) => b.element === changes.removeBlock)
|
|
362
|
+
if (index < 0)
|
|
363
|
+
return { ok: false, reason: `"${dataType.alias}" does not offer "${changes.removeBlock}"` }
|
|
364
|
+
list.splice(index, 1)
|
|
365
|
+
}
|
|
366
|
+
blocks = list
|
|
367
|
+
}
|
|
368
|
+
return { ok: true, dataType: { ...dataType, config, ...(blocks ? { blocks } : {}) } }
|
|
369
|
+
}
|