@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 +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/rename.ts
ADDED
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Renames that keep their content (story 250): a type's, a property's or a data
|
|
3
|
+
* type's alias changed across a whole `SchemaSet`. The renamed definition keeps
|
|
4
|
+
* its key, written into its file since the key would otherwise follow the new
|
|
5
|
+
* alias, so the sync moves the alias and the values stay. Every other
|
|
6
|
+
* definition naming the old alias is listed, and rewritten when asked.
|
|
7
|
+
*/
|
|
8
|
+
import {
|
|
9
|
+
BUILTIN_DATA_TYPE_ALIASES,
|
|
10
|
+
SYSTEM_CONTENT_TYPE_ALIASES,
|
|
11
|
+
SYSTEM_MEDIA_TYPE_ALIASES,
|
|
12
|
+
} from './builtin.ts'
|
|
13
|
+
import { mapReferences, type ReferenceKind } from './config-references.ts'
|
|
14
|
+
import { dataTypeKeyFor, propertyKeyFor, shippedPropertyKeys, typeKeyFor } from './keys.ts'
|
|
15
|
+
import {
|
|
16
|
+
allProperties,
|
|
17
|
+
type SchemaContentType,
|
|
18
|
+
type SchemaDataType,
|
|
19
|
+
type SchemaSet,
|
|
20
|
+
type SchemaTypeKind,
|
|
21
|
+
} from './model.ts'
|
|
22
|
+
|
|
23
|
+
/** One definition that names what is renamed, as a person reads it. */
|
|
24
|
+
export interface RenameReference {
|
|
25
|
+
/** `content type "page"`, `data type "pageBlocks"`, `form "contact"`. */
|
|
26
|
+
in: string
|
|
27
|
+
/** What there names it: `allowed child`, `block`, `save as content`. */
|
|
28
|
+
how: string
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export type RenameResult =
|
|
32
|
+
| { ok: true; set: SchemaSet; references: RenameReference[] }
|
|
33
|
+
| { ok: false; reason: string }
|
|
34
|
+
|
|
35
|
+
const ALIAS = /^[A-Za-z][A-Za-z0-9_]*$/
|
|
36
|
+
|
|
37
|
+
const nouns: Record<SchemaTypeKind, string> = {
|
|
38
|
+
document: 'content type',
|
|
39
|
+
media: 'media type',
|
|
40
|
+
member: 'member type',
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const typesOfKind = (set: SchemaSet, kind: SchemaTypeKind) =>
|
|
44
|
+
kind === 'media'
|
|
45
|
+
? (set.mediaTypes ?? [])
|
|
46
|
+
: kind === 'member'
|
|
47
|
+
? (set.memberTypes ?? [])
|
|
48
|
+
: set.contentTypes
|
|
49
|
+
|
|
50
|
+
const withTypes = (set: SchemaSet, kind: SchemaTypeKind, types: SchemaContentType[]): SchemaSet =>
|
|
51
|
+
kind === 'media'
|
|
52
|
+
? { ...set, mediaTypes: types }
|
|
53
|
+
: kind === 'member'
|
|
54
|
+
? { ...set, memberTypes: types }
|
|
55
|
+
: { ...set, contentTypes: types }
|
|
56
|
+
|
|
57
|
+
const refuse = (reason: string): RenameResult => ({ ok: false, reason })
|
|
58
|
+
|
|
59
|
+
function newAliasProblem(to: string, taken: boolean, reserved: boolean): string | undefined {
|
|
60
|
+
if (!ALIAS.test(to))
|
|
61
|
+
return `"${to}" is not an alias: letters, digits and _, starting with a letter`
|
|
62
|
+
if (reserved) return `"${to}" belongs to a type every site has`
|
|
63
|
+
if (taken) return `"${to}" is taken`
|
|
64
|
+
return undefined
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
type Property = SchemaContentType['properties'][number]
|
|
68
|
+
|
|
69
|
+
/** A type with each of its properties, wherever they sit, put through `map`. */
|
|
70
|
+
const mapProperties = (
|
|
71
|
+
type: SchemaContentType,
|
|
72
|
+
map: (p: Property) => Property,
|
|
73
|
+
): SchemaContentType => ({
|
|
74
|
+
...type,
|
|
75
|
+
properties: type.properties.map(map),
|
|
76
|
+
groups: type.groups?.map((g) => ({ ...g, properties: g.properties.map(map) })),
|
|
77
|
+
tabs: type.tabs.map((t) => ({
|
|
78
|
+
...t,
|
|
79
|
+
properties: t.properties.map(map),
|
|
80
|
+
groups: t.groups.map((g) => ({ ...g, properties: g.properties.map(map) })),
|
|
81
|
+
})),
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
/** Every type of every kind, each with its kind's noun. */
|
|
85
|
+
const allTypes = (set: SchemaSet) =>
|
|
86
|
+
(['document', 'media', 'member'] as const).flatMap((kind) =>
|
|
87
|
+
typesOfKind(set, kind).map((type) => ({ kind, type })),
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
/** A type's alias renamed: the type keeps its key, and every definition naming it follows. */
|
|
91
|
+
export function renameType(
|
|
92
|
+
set: SchemaSet,
|
|
93
|
+
kind: SchemaTypeKind,
|
|
94
|
+
from: string,
|
|
95
|
+
to: string,
|
|
96
|
+
): RenameResult {
|
|
97
|
+
const types = typesOfKind(set, kind)
|
|
98
|
+
const type = types.find((t) => t.alias === from)
|
|
99
|
+
if (!type) return refuse(`there is no ${nouns[kind]} "${from}"`)
|
|
100
|
+
if (SYSTEM_CONTENT_TYPE_ALIASES.has(from) || SYSTEM_MEDIA_TYPE_ALIASES.has(from))
|
|
101
|
+
return refuse(`"${from}" is a type every site has, and is not renamed`)
|
|
102
|
+
const problem = newAliasProblem(
|
|
103
|
+
to,
|
|
104
|
+
types.some((t) => t.alias.toLowerCase() === to.toLowerCase() && t !== type),
|
|
105
|
+
SYSTEM_CONTENT_TYPE_ALIASES.has(to) || SYSTEM_MEDIA_TYPE_ALIASES.has(to),
|
|
106
|
+
)
|
|
107
|
+
if (problem) return refuse(problem)
|
|
108
|
+
const references: RenameReference[] = []
|
|
109
|
+
const swap = (list: readonly string[]) => list.map((alias) => (alias === from ? to : alias))
|
|
110
|
+
const elementOrKind: ReferenceKind[] = kind === 'document' ? ['element', 'document'] : [kind]
|
|
111
|
+
|
|
112
|
+
// A built-in type's properties take shipped keys by its alias: the renamed one says them.
|
|
113
|
+
const shipped = shippedPropertyKeys(kind, from)
|
|
114
|
+
const keep = <P extends { alias: string; key?: string }>(p: P): P =>
|
|
115
|
+
shipped.has(p.alias) ? { ...p, key: p.key ?? shipped.get(p.alias) } : p
|
|
116
|
+
const renamedType: SchemaContentType = {
|
|
117
|
+
...mapProperties(type, keep),
|
|
118
|
+
alias: to,
|
|
119
|
+
key: type.key ?? typeKeyFor(kind, from),
|
|
120
|
+
}
|
|
121
|
+
let next = withTypes(
|
|
122
|
+
set,
|
|
123
|
+
kind,
|
|
124
|
+
types.map((t) => (t === type ? renamedType : t)),
|
|
125
|
+
)
|
|
126
|
+
// Other types' allowed children and compositions.
|
|
127
|
+
for (const k of ['document', 'media', 'member'] as const)
|
|
128
|
+
next = withTypes(
|
|
129
|
+
next,
|
|
130
|
+
k,
|
|
131
|
+
typesOfKind(next, k).map((t) => {
|
|
132
|
+
if (k !== kind) return t
|
|
133
|
+
const child = t.allowChildren.includes(from)
|
|
134
|
+
const composed = t.compositions.includes(from)
|
|
135
|
+
if (child) references.push({ in: `${nouns[k]} "${t.alias}"`, how: 'allowed child' })
|
|
136
|
+
if (composed) references.push({ in: `${nouns[k]} "${t.alias}"`, how: 'composition' })
|
|
137
|
+
return child || composed
|
|
138
|
+
? { ...t, allowChildren: swap(t.allowChildren), compositions: swap(t.compositions) }
|
|
139
|
+
: t
|
|
140
|
+
}),
|
|
141
|
+
)
|
|
142
|
+
// Data types: blocks, and the settings that name types (story 161).
|
|
143
|
+
next = {
|
|
144
|
+
...next,
|
|
145
|
+
dataTypes: next.dataTypes.map((d) => {
|
|
146
|
+
let named = false
|
|
147
|
+
const blocks = d.blocks?.map((b) => {
|
|
148
|
+
if (b.element !== from && b.settings !== from) return b
|
|
149
|
+
named = true
|
|
150
|
+
references.push({ in: `data type "${d.alias}"`, how: 'block' })
|
|
151
|
+
return {
|
|
152
|
+
...b,
|
|
153
|
+
element: b.element === from ? to : b.element,
|
|
154
|
+
settings: b.settings === from ? to : b.settings,
|
|
155
|
+
}
|
|
156
|
+
})
|
|
157
|
+
const config = mapReferences(
|
|
158
|
+
d.editor,
|
|
159
|
+
d.config,
|
|
160
|
+
(value, k) => {
|
|
161
|
+
if (value !== from || !elementOrKind.includes(k)) return undefined
|
|
162
|
+
named = true
|
|
163
|
+
references.push({ in: `data type "${d.alias}"`, how: 'picker allows' })
|
|
164
|
+
return to
|
|
165
|
+
},
|
|
166
|
+
(items) => items,
|
|
167
|
+
)
|
|
168
|
+
return named ? { ...d, config, ...(blocks ? { blocks } : {}) } : d
|
|
169
|
+
}),
|
|
170
|
+
}
|
|
171
|
+
// Approval workflows and forms' save-as-content workflows.
|
|
172
|
+
next = {
|
|
173
|
+
...next,
|
|
174
|
+
workflows: next.workflows?.map((w) => {
|
|
175
|
+
if (!w.appliesTo.contentTypes.includes(from)) return w
|
|
176
|
+
references.push({ in: `approval workflow "${w.alias}"`, how: 'applies to' })
|
|
177
|
+
return { ...w, appliesTo: { ...w.appliesTo, contentTypes: swap(w.appliesTo.contentTypes) } }
|
|
178
|
+
}),
|
|
179
|
+
forms: next.forms?.map((f) => {
|
|
180
|
+
let named = false
|
|
181
|
+
const workflows = f.workflows.map((w) => {
|
|
182
|
+
if (w.type !== 'saveAsContent' || w.settings['content-type'] !== from) return w
|
|
183
|
+
named = true
|
|
184
|
+
references.push({ in: `form "${f.alias}"`, how: 'save as content' })
|
|
185
|
+
return { ...w, settings: { ...w.settings, 'content-type': to } }
|
|
186
|
+
})
|
|
187
|
+
return named ? { ...f, workflows } : f
|
|
188
|
+
}),
|
|
189
|
+
}
|
|
190
|
+
return { ok: true, set: next, references }
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** A property's alias renamed: it keeps its key, and every definition naming it follows. */
|
|
194
|
+
export function renameProperty(
|
|
195
|
+
set: SchemaSet,
|
|
196
|
+
kind: SchemaTypeKind,
|
|
197
|
+
typeAlias: string,
|
|
198
|
+
from: string,
|
|
199
|
+
to: string,
|
|
200
|
+
): RenameResult {
|
|
201
|
+
const types = typesOfKind(set, kind)
|
|
202
|
+
const type = types.find((t) => t.alias === typeAlias)
|
|
203
|
+
if (!type) return refuse(`there is no ${nouns[kind]} "${typeAlias}"`)
|
|
204
|
+
if (SYSTEM_CONTENT_TYPE_ALIASES.has(typeAlias) || SYSTEM_MEDIA_TYPE_ALIASES.has(typeAlias))
|
|
205
|
+
return refuse(`"${typeAlias}" is a type every site has, and its properties are not renamed`)
|
|
206
|
+
const properties = allProperties(type)
|
|
207
|
+
if (!properties.some((p) => p.alias === from))
|
|
208
|
+
return refuse(`"${typeAlias}" has no property "${from}"`)
|
|
209
|
+
const problem = newAliasProblem(
|
|
210
|
+
to,
|
|
211
|
+
properties.some((p) => p.alias.toLowerCase() === to.toLowerCase() && p.alias !== from),
|
|
212
|
+
false,
|
|
213
|
+
)
|
|
214
|
+
if (problem) return refuse(problem)
|
|
215
|
+
const typeKey = type.key ?? typeKeyFor(kind, typeAlias)
|
|
216
|
+
const shipped = shippedPropertyKeys(kind, typeAlias)
|
|
217
|
+
const renamed = mapProperties(type, (p) =>
|
|
218
|
+
p.alias === from
|
|
219
|
+
? { ...p, alias: to, key: p.key ?? propertyKeyFor(typeKey, from, shipped) }
|
|
220
|
+
: p,
|
|
221
|
+
)
|
|
222
|
+
const references: RenameReference[] = []
|
|
223
|
+
let next = withTypes(
|
|
224
|
+
set,
|
|
225
|
+
kind,
|
|
226
|
+
types.map((t) => (t === type ? renamed : t)),
|
|
227
|
+
)
|
|
228
|
+
// A block's label names an element type's properties: `{{heading}}`.
|
|
229
|
+
const label = new RegExp(`\\{\\{\\s*${from}\\s*\\}\\}`, 'g')
|
|
230
|
+
next = {
|
|
231
|
+
...next,
|
|
232
|
+
dataTypes: next.dataTypes.map((d) => {
|
|
233
|
+
let named = false
|
|
234
|
+
const blocks = d.blocks?.map((b) => {
|
|
235
|
+
if (b.element !== typeAlias || !b.label || !label.test(b.label)) return b
|
|
236
|
+
label.lastIndex = 0
|
|
237
|
+
named = true
|
|
238
|
+
references.push({ in: `data type "${d.alias}"`, how: 'block label' })
|
|
239
|
+
return { ...b, label: b.label.replace(label, `{{${to}}}`) }
|
|
240
|
+
})
|
|
241
|
+
return named ? { ...d, ...(blocks ? { blocks } : {}) } : d
|
|
242
|
+
}),
|
|
243
|
+
// A save-as-content workflow maps form fields to the type's properties.
|
|
244
|
+
forms: next.forms?.map((f) => {
|
|
245
|
+
let named = false
|
|
246
|
+
const workflows = f.workflows.map((w) => {
|
|
247
|
+
if (w.type !== 'saveAsContent' || w.settings['content-type'] !== typeAlias) return w
|
|
248
|
+
const map = w.settings.map as Record<string, string> | undefined
|
|
249
|
+
if (!map || !Object.values(map).includes(from)) return w
|
|
250
|
+
named = true
|
|
251
|
+
references.push({ in: `form "${f.alias}"`, how: 'save as content' })
|
|
252
|
+
return {
|
|
253
|
+
...w,
|
|
254
|
+
settings: {
|
|
255
|
+
...w.settings,
|
|
256
|
+
map: Object.fromEntries(
|
|
257
|
+
Object.entries(map).map(([field, p]) => [field, p === from ? to : p]),
|
|
258
|
+
),
|
|
259
|
+
},
|
|
260
|
+
}
|
|
261
|
+
})
|
|
262
|
+
return named ? { ...f, workflows } : f
|
|
263
|
+
}),
|
|
264
|
+
}
|
|
265
|
+
return { ok: true, set: next, references }
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** A data type's alias renamed: it keeps its key, and every property using it follows. */
|
|
269
|
+
export function renameDataType(set: SchemaSet, from: string, to: string): RenameResult {
|
|
270
|
+
const dataType = set.dataTypes.find((d) => d.alias === from)
|
|
271
|
+
if (!dataType) {
|
|
272
|
+
if (BUILTIN_DATA_TYPE_ALIASES.has(from))
|
|
273
|
+
return refuse(`"${from}" is built in, and is not renamed`)
|
|
274
|
+
return refuse(`there is no data type "${from}"`)
|
|
275
|
+
}
|
|
276
|
+
const problem = newAliasProblem(
|
|
277
|
+
to,
|
|
278
|
+
set.dataTypes.some((d) => d.alias.toLowerCase() === to.toLowerCase() && d !== dataType),
|
|
279
|
+
BUILTIN_DATA_TYPE_ALIASES.has(to),
|
|
280
|
+
)
|
|
281
|
+
if (problem) return refuse(problem)
|
|
282
|
+
const references: RenameReference[] = []
|
|
283
|
+
let next: SchemaSet = {
|
|
284
|
+
...set,
|
|
285
|
+
dataTypes: set.dataTypes.map(
|
|
286
|
+
(d): SchemaDataType =>
|
|
287
|
+
d === dataType ? { ...d, alias: to, key: d.key ?? dataTypeKeyFor(from) } : d,
|
|
288
|
+
),
|
|
289
|
+
}
|
|
290
|
+
for (const { kind, type } of allTypes(next)) {
|
|
291
|
+
const using = allProperties(type).filter((p) => p.type === from)
|
|
292
|
+
if (using.length === 0) continue
|
|
293
|
+
for (const p of using)
|
|
294
|
+
references.push({ in: `${nouns[kind]} "${type.alias}"`, how: `property "${p.alias}"` })
|
|
295
|
+
const changed = mapProperties(type, (p) => (p.type === from ? { ...p, type: to } : p))
|
|
296
|
+
next = withTypes(
|
|
297
|
+
next,
|
|
298
|
+
kind,
|
|
299
|
+
typesOfKind(next, kind).map((t) => (t === type ? changed : t)),
|
|
300
|
+
)
|
|
301
|
+
}
|
|
302
|
+
return { ok: true, set: next, references }
|
|
303
|
+
}
|
package/src/scaffold.ts
CHANGED
|
@@ -94,7 +94,7 @@ export function newType(
|
|
|
94
94
|
icon: options.icon ?? DEFAULT_ICONS[kind],
|
|
95
95
|
allowAtRoot: kind === 'document' ? options.allowAtRoot === true : false,
|
|
96
96
|
isElement: element,
|
|
97
|
-
allowInLibrary:
|
|
97
|
+
allowInLibrary: options.library === true,
|
|
98
98
|
variesByCulture: options.variesByCulture === true,
|
|
99
99
|
variesBySegment: options.variesBySegment === true,
|
|
100
100
|
compositions: options.compose ?? [],
|
package/src/site-edit.ts
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Changes to a site's languages, user groups and URL setting (story 251), each a
|
|
3
|
+
* pure function to the changed definition, or the reason it cannot change.
|
|
4
|
+
*/
|
|
5
|
+
import type { SchemaLanguage, SchemaUrls } from './model.ts'
|
|
6
|
+
import type { SchemaUserGroup } from './user-groups.ts'
|
|
7
|
+
|
|
8
|
+
export type LanguagesResult =
|
|
9
|
+
| { ok: true; languages: SchemaLanguage[] }
|
|
10
|
+
| { ok: false; reason: string }
|
|
11
|
+
|
|
12
|
+
export interface LanguageChanges {
|
|
13
|
+
name?: string
|
|
14
|
+
/** The language shown when this one is empty; null for none. */
|
|
15
|
+
fallback?: string | null
|
|
16
|
+
mandatory?: boolean
|
|
17
|
+
/** True makes it the default and every other not; false is refused, since one has to be. */
|
|
18
|
+
default?: boolean
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const same = (a: string, b: string) => a.toLowerCase() === b.toLowerCase()
|
|
22
|
+
|
|
23
|
+
export function updateLanguage(
|
|
24
|
+
languages: readonly SchemaLanguage[],
|
|
25
|
+
iso: string,
|
|
26
|
+
changes: LanguageChanges,
|
|
27
|
+
): LanguagesResult {
|
|
28
|
+
const language = languages.find((l) => same(l.iso, iso))
|
|
29
|
+
if (!language) return { ok: false, reason: `the site has no language ${iso}` }
|
|
30
|
+
if (changes.default === false && language.default)
|
|
31
|
+
return {
|
|
32
|
+
ok: false,
|
|
33
|
+
reason: `${language.iso} is the default language; make another the default instead`,
|
|
34
|
+
}
|
|
35
|
+
if (changes.fallback && same(changes.fallback, language.iso))
|
|
36
|
+
return { ok: false, reason: 'a language cannot fall back to itself' }
|
|
37
|
+
if (changes.fallback && !languages.some((l) => same(l.iso, changes.fallback as string)))
|
|
38
|
+
return { ok: false, reason: `the site has no language ${changes.fallback} to fall back to` }
|
|
39
|
+
const next = languages.map((l) => {
|
|
40
|
+
if (l !== language) return changes.default ? { ...l, default: false } : l
|
|
41
|
+
const changed: SchemaLanguage = {
|
|
42
|
+
...l,
|
|
43
|
+
...(changes.name !== undefined ? { name: changes.name } : {}),
|
|
44
|
+
...(changes.mandatory !== undefined ? { mandatory: changes.mandatory } : {}),
|
|
45
|
+
...(changes.default ? { default: true } : {}),
|
|
46
|
+
}
|
|
47
|
+
if (changes.fallback === null) delete changed.fallback
|
|
48
|
+
else if (changes.fallback !== undefined) changed.fallback = changes.fallback
|
|
49
|
+
return changed
|
|
50
|
+
})
|
|
51
|
+
return { ok: true, languages: next }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** A language taken out: never the default, nor one another falls back to. */
|
|
55
|
+
export function removeLanguage(languages: readonly SchemaLanguage[], iso: string): LanguagesResult {
|
|
56
|
+
const language = languages.find((l) => same(l.iso, iso))
|
|
57
|
+
if (!language) return { ok: false, reason: `the site has no language ${iso}` }
|
|
58
|
+
if (language.default)
|
|
59
|
+
return {
|
|
60
|
+
ok: false,
|
|
61
|
+
reason: `${language.iso} is the default language; make another the default first`,
|
|
62
|
+
}
|
|
63
|
+
const falling = languages.filter((l) => l.fallback && same(l.fallback, language.iso))
|
|
64
|
+
if (falling.length > 0)
|
|
65
|
+
return {
|
|
66
|
+
ok: false,
|
|
67
|
+
reason: `${falling.map((l) => l.iso).join(', ')} fall${falling.length === 1 ? 's' : ''} back to ${language.iso}; change that first`,
|
|
68
|
+
}
|
|
69
|
+
return { ok: true, languages: languages.filter((l) => l !== language) }
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export type UserGroupResult = { ok: true; group: SchemaUserGroup } | { ok: false; reason: string }
|
|
73
|
+
|
|
74
|
+
export interface UserGroupChanges {
|
|
75
|
+
name?: string
|
|
76
|
+
description?: string
|
|
77
|
+
icon?: string
|
|
78
|
+
addSections?: readonly string[]
|
|
79
|
+
removeSections?: readonly string[]
|
|
80
|
+
addPermissions?: readonly string[]
|
|
81
|
+
removePermissions?: readonly string[]
|
|
82
|
+
/** Every language, or the languages added and removed. */
|
|
83
|
+
allLanguages?: boolean
|
|
84
|
+
addLanguages?: readonly string[]
|
|
85
|
+
removeLanguages?: readonly string[]
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const plus = (list: readonly string[], more: readonly string[] = []) => [
|
|
89
|
+
...list,
|
|
90
|
+
...more.filter((item) => !list.includes(item)),
|
|
91
|
+
]
|
|
92
|
+
|
|
93
|
+
function minus(
|
|
94
|
+
list: readonly string[],
|
|
95
|
+
less: readonly string[] = [],
|
|
96
|
+
what: string,
|
|
97
|
+
): string[] | string {
|
|
98
|
+
const missing = less.filter((item) => !list.includes(item))
|
|
99
|
+
if (missing.length > 0) return `the group has no ${what} ${missing.join(', ')}`
|
|
100
|
+
return list.filter((item) => !less.includes(item))
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** A group's name, description, icon, sections, permissions or languages. */
|
|
104
|
+
export function updateUserGroup(
|
|
105
|
+
group: SchemaUserGroup,
|
|
106
|
+
changes: UserGroupChanges,
|
|
107
|
+
): UserGroupResult {
|
|
108
|
+
const sections = minus(
|
|
109
|
+
plus(group.sections, changes.addSections),
|
|
110
|
+
changes.removeSections,
|
|
111
|
+
'section',
|
|
112
|
+
)
|
|
113
|
+
if (typeof sections === 'string') return { ok: false, reason: sections }
|
|
114
|
+
const permissions = minus(
|
|
115
|
+
plus(group.permissions, changes.addPermissions),
|
|
116
|
+
changes.removePermissions,
|
|
117
|
+
'permission',
|
|
118
|
+
)
|
|
119
|
+
if (typeof permissions === 'string') return { ok: false, reason: permissions }
|
|
120
|
+
let languages: string[] | 'all' = group.languages
|
|
121
|
+
if (changes.allLanguages === true) languages = 'all'
|
|
122
|
+
else if (changes.allLanguages === false && group.languages === 'all') languages = []
|
|
123
|
+
if (changes.addLanguages?.length || changes.removeLanguages?.length) {
|
|
124
|
+
if (languages === 'all')
|
|
125
|
+
return {
|
|
126
|
+
ok: false,
|
|
127
|
+
reason: 'the group has every language; give --no-all-languages to name them instead',
|
|
128
|
+
}
|
|
129
|
+
const named = minus(plus(languages, changes.addLanguages), changes.removeLanguages, 'language')
|
|
130
|
+
if (typeof named === 'string') return { ok: false, reason: named }
|
|
131
|
+
languages = named
|
|
132
|
+
}
|
|
133
|
+
return {
|
|
134
|
+
ok: true,
|
|
135
|
+
group: {
|
|
136
|
+
...group,
|
|
137
|
+
...(changes.name !== undefined ? { name: changes.name } : {}),
|
|
138
|
+
...(changes.description !== undefined
|
|
139
|
+
? { description: changes.description || undefined }
|
|
140
|
+
: {}),
|
|
141
|
+
...(changes.icon !== undefined ? { icon: changes.icon || undefined } : {}),
|
|
142
|
+
sections,
|
|
143
|
+
permissions,
|
|
144
|
+
languages,
|
|
145
|
+
},
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** The URL setting: whether the top-level page is left out of every path. */
|
|
150
|
+
export const updateUrls = (urls: SchemaUrls | undefined, hideTopLevel: boolean): SchemaUrls => ({
|
|
151
|
+
...(urls ?? { hideTopLevel: true }),
|
|
152
|
+
hideTopLevel,
|
|
153
|
+
})
|
package/src/sync.ts
CHANGED
|
@@ -200,7 +200,7 @@ export async function syncSchema(
|
|
|
200
200
|
if (hash === current.hash) return emptySyncReport('skipped-same')
|
|
201
201
|
if (options.mode === 'production')
|
|
202
202
|
return emptySyncReport('refused', {
|
|
203
|
-
reason: `
|
|
203
|
+
reason: `the definitions changed without a new version (still ${set.version}); \`crombolo bundle build\` or \`crombolo push\` from the site gives them one, or deploy with a newer revision`,
|
|
204
204
|
})
|
|
205
205
|
}
|
|
206
206
|
|
package/src/user-groups.ts
CHANGED
|
@@ -59,7 +59,10 @@ export function parseUserGroup(file: string, source: string): ParseResult<Schema
|
|
|
59
59
|
const permissions = r.strs(t, 'permissions', path)
|
|
60
60
|
for (const permission of permissions)
|
|
61
61
|
if (!KNOWN_PERMISSIONS.has(permission))
|
|
62
|
-
r.problem(
|
|
62
|
+
r.problem(
|
|
63
|
+
`${path}.permissions`,
|
|
64
|
+
`"${permission}" is not a permission verb; \`crombolo users groups permissions --verbs\` lists them`,
|
|
65
|
+
)
|
|
63
66
|
let languages: string[] | 'all' = []
|
|
64
67
|
if (t.languages === 'all') languages = 'all'
|
|
65
68
|
else if (t.languages !== undefined) languages = r.strs(t, 'languages', path)
|
package/src/validate.ts
CHANGED
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
SYSTEM_CONTENT_TYPE_ALIASES,
|
|
18
18
|
SYSTEM_MEDIA_TYPE_ALIASES,
|
|
19
19
|
} from './builtin.ts'
|
|
20
|
+
import { composedProperties } from './compositions.ts'
|
|
20
21
|
import { type ReferenceKind, resolveConfig } from './config-references.ts'
|
|
21
22
|
import {
|
|
22
23
|
allProperties,
|
|
@@ -181,6 +182,8 @@ export function validateSchemaSet(set: SchemaSet, options: ValidateOptions = {})
|
|
|
181
182
|
`"${c}" has fields that vary by culture, so this type must vary by culture too`,
|
|
182
183
|
)
|
|
183
184
|
}
|
|
185
|
+
for (const message of compositionClashes(set, t, header))
|
|
186
|
+
add(file, message.path, message.text)
|
|
184
187
|
for (const c of t.allowChildren)
|
|
185
188
|
if (!known(c)) add(file, `${header}.allow-children`, `unknown type "${c}"`)
|
|
186
189
|
if (t.collection && !dataTypeAliases.has(t.collection))
|
|
@@ -300,3 +303,36 @@ function findCompositionCycle(
|
|
|
300
303
|
const found = walk(start, [])
|
|
301
304
|
return found && found[0] === start ? found : undefined
|
|
302
305
|
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* A property whose alias one of the type's compositions already has, and two
|
|
309
|
+
* compositions bringing the same alias (story 255): a page would carry two
|
|
310
|
+
* values under one alias, and a view could not tell which it reads.
|
|
311
|
+
*/
|
|
312
|
+
function compositionClashes(
|
|
313
|
+
set: SchemaSet,
|
|
314
|
+
type: SchemaContentType,
|
|
315
|
+
header: string,
|
|
316
|
+
): Array<{ path: string; text: string }> {
|
|
317
|
+
const out: Array<{ path: string; text: string }> = []
|
|
318
|
+
const composed = new Map<string, string>()
|
|
319
|
+
for (const p of composedProperties(set, type)) {
|
|
320
|
+
const alias = p.property.alias.toLowerCase()
|
|
321
|
+
const from = composed.get(alias)
|
|
322
|
+
if (from === undefined) composed.set(alias, p.from)
|
|
323
|
+
else if (from !== p.from)
|
|
324
|
+
out.push({
|
|
325
|
+
path: `${header}.compositions`,
|
|
326
|
+
text: `"${from}" and "${p.from}" both have a property "${p.property.alias}"`,
|
|
327
|
+
})
|
|
328
|
+
}
|
|
329
|
+
for (const own of allProperties(type)) {
|
|
330
|
+
const from = composed.get(own.alias.toLowerCase())
|
|
331
|
+
if (from)
|
|
332
|
+
out.push({
|
|
333
|
+
path: `property "${own.alias}"`,
|
|
334
|
+
text: `"${from}" has a property "${own.alias}" already; \`crombolo types property remove ${type.alias} ${own.alias}\` takes this one away`,
|
|
335
|
+
})
|
|
336
|
+
}
|
|
337
|
+
return out
|
|
338
|
+
}
|