@crombolo/schema 0.39.1 → 0.41.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.39.1",
3
+ "version": "0.41.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.39.1",
32
- "@crombolo/data": "0.39.1"
31
+ "@crombolo/core": "0.41.1",
32
+ "@crombolo/data": "0.41.1"
33
33
  }
34
34
  }
package/src/export.ts CHANGED
@@ -113,6 +113,7 @@ export function toSchemaContentType(
113
113
  }
114
114
  if (aggregate.description) type.description = aggregate.description
115
115
  if (aggregate.siteSearch === false) type.siteSearch = false
116
+ if (aggregate.siteSettings) type.siteSettings = true
116
117
  if (aggregate.defaultComponentKey) {
117
118
  const alias = ctx.componentAlias(aggregate.defaultComponentKey)
118
119
  if (alias) type.defaultComponent = alias
@@ -41,6 +41,7 @@ export function generateDeliveryTypes(set: SchemaSet, options: DeliveryTypesOpti
41
41
  const serves = options.serves ?? (() => true)
42
42
  const pages = set.contentTypes.filter((t) => !t.isElement && serves(t.alias)).sort(byAlias)
43
43
  const elements = set.contentTypes.filter((t) => t.isElement).sort(byAlias)
44
+ const settingsTypes = pages.filter((t) => t.siteSettings)
44
45
  const media = mediaTypes(set)
45
46
 
46
47
  const name = (type: SchemaContentType) => `${pascal(type.alias)}Properties`
@@ -160,6 +161,9 @@ export function generateDeliveryTypes(set: SchemaSet, options: DeliveryTypesOpti
160
161
  ' media: {',
161
162
  ...media.map((t) => ` ${key(t.alias)}: ${mediaName(t.alias)}`),
162
163
  ' }',
164
+ ...(settingsTypes.length > 0
165
+ ? [` settings: ${settingsTypes.map((t) => `'${t.alias}'`).join(' | ')}`]
166
+ : []),
163
167
  '}',
164
168
  '',
165
169
  '/** Any page, as the item, query and search endpoints answer it. */',
package/src/index.ts CHANGED
@@ -16,6 +16,7 @@ export * from './reader.ts'
16
16
  export * from './scaffold.ts'
17
17
  export * from './sync.ts'
18
18
  export * from './upgrade.ts'
19
+ export * from './user-groups.ts'
19
20
  export * from './validate.ts'
20
21
  export * from './validate-forms.ts'
21
22
  export * from './value-migrations.ts'
package/src/load.ts CHANGED
@@ -28,6 +28,7 @@ import {
28
28
  parseSchemaSettings,
29
29
  } from './parse.ts'
30
30
  import { parseForm } from './parse-forms.ts'
31
+ import { parseUserGroup, type SchemaUserGroup, writeUserGroup } from './user-groups.ts'
31
32
  import { parseWorkflow, writeWorkflow } from './workflows.ts'
32
33
  import {
33
34
  writeContentType,
@@ -134,6 +135,15 @@ export function loadSchemaDirectory(
134
135
  files.set(`workflow:${parsed.value.alias}`, file)
135
136
  }
136
137
  }
138
+ const userGroups: SchemaUserGroup[] = []
139
+ for (const file of tomlFiles(join(schemaDir, 'user-groups'))) {
140
+ const parsed = parseUserGroup(rel(file), readFileSync(file, 'utf8'))
141
+ problems.push(...parsed.problems)
142
+ if (parsed.value) {
143
+ userGroups.push(parsed.value)
144
+ files.set(`user-group:${parsed.value.alias}`, file)
145
+ }
146
+ }
137
147
  const languagesFile = join(schemaDir, 'languages.toml')
138
148
  if (existsSync(languagesFile)) {
139
149
  const parsed = parseLanguages(rel(languagesFile), readFileSync(languagesFile, 'utf8'))
@@ -150,6 +160,7 @@ export function loadSchemaDirectory(
150
160
  languages,
151
161
  forms,
152
162
  ...(workflows.length > 0 ? { workflows } : {}),
163
+ ...(userGroups.length > 0 ? { userGroups } : {}),
153
164
  ...(urls ? { urls } : {}),
154
165
  },
155
166
  problems,
@@ -171,6 +182,8 @@ export function hashSchemaSet(set: SchemaSet): string {
171
182
  ...(set.forms ?? []).map((f) => writeForm(f)).sort(),
172
183
  // Absent from a set with none, so every hash taken before workflows stands.
173
184
  ...(set.workflows ?? []).map((w) => writeWorkflow(w)).sort(),
185
+ // Likewise for user groups (story 228).
186
+ ...(set.userGroups ?? []).map((g) => writeUserGroup(g)).sort(),
174
187
  ]
175
188
  return createHash('sha256').update(parts.join('\n---\n')).digest('hex')
176
189
  }
package/src/model.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import type { SchemaForm, SchemaWorkflow, SiteSearchLevel } from '@crombolo/core'
2
+ import type { SchemaUserGroup } from './user-groups.ts'
2
3
 
3
4
  /**
4
5
  * The schema-as-code model: what a site's `schema/*.toml` files describe.
@@ -77,6 +78,8 @@ export interface SchemaContentType {
77
78
  since?: string
78
79
  /** False leaves every page of the type out of site search. */
79
80
  siteSearch?: false
81
+ /** A site's settings (story 058): its page, a root's direct child, has no address. */
82
+ siteSettings?: true
80
83
  /** Properties outside any tab. */
81
84
  properties: SchemaProperty[]
82
85
  /** Groups outside any tab — `[[group]]`; Umbraco's built-in media types use one. */
@@ -163,6 +166,8 @@ export interface SchemaSet {
163
166
  urls?: SchemaUrls
164
167
  /** `schema/workflows/*.toml` — approval workflows; see `docs/workflows.md`. */
165
168
  workflows?: SchemaWorkflow[]
169
+ /** `schema/user-groups/*.toml` — the user groups the site declares (story 228). */
170
+ userGroups?: SchemaUserGroup[]
166
171
  }
167
172
 
168
173
  /** How URLs are built, as Umbraco's `Umbraco:CMS:Global` settings set it. */
package/src/parse.ts CHANGED
@@ -66,6 +66,7 @@ const TYPE_KEYS = [
66
66
  'cleanup',
67
67
  'since',
68
68
  'site-search',
69
+ 'site-settings',
69
70
  ] as const
70
71
  const CLEANUP_KEYS = [
71
72
  'prevent',
@@ -305,10 +306,16 @@ export function parseContentType(
305
306
  ...(t['site-search'] !== undefined && typeSiteSearch(r, t, path) === false
306
307
  ? { siteSearch: false as const }
307
308
  : {}),
309
+ ...(r.bool(t, 'site-settings', path, false) ? { siteSettings: true as const } : {}),
308
310
  properties: r.tables(root.property, 'property').map((p, i) => property(r, p, `property[${i}]`)),
309
311
  tabs: r.tables(root.tab, 'tab').map((x, i) => tab(r, x, `tab[${i}]`)),
310
312
  groups: r.tables(root.group, 'group').map((g, i) => group(r, g, `group[${i}]`)),
311
313
  }
314
+ if (value.siteSettings && value.isElement)
315
+ r.problem(
316
+ `${path}.site-settings`,
317
+ "an element type is never a page, so it cannot be a site's settings",
318
+ )
312
319
  return { value: r.problems.length === 0 ? value : undefined, problems: r.problems }
313
320
  }
314
321
 
package/src/sync.ts CHANGED
@@ -36,6 +36,7 @@ import {
36
36
  sameUrlOptions,
37
37
  syncWorkflows,
38
38
  type UrlOptions,
39
+ UserGroupRepository,
39
40
  updateSchemaStateHash,
40
41
  type WorkflowSyncReport,
41
42
  writeUrlOptions,
@@ -110,6 +111,8 @@ export interface SyncReport {
110
111
  urls?: { before: UrlOptions; after: UrlOptions }
111
112
  /** Set when a workflow changed, with what that did to requests in flight. */
112
113
  workflows?: WorkflowSyncReport
114
+ /** The declared user groups this sync created or changed (story 228). */
115
+ userGroups?: { created: string[]; updated: string[] }
113
116
  }
114
117
 
115
118
  export const emptySyncReport = (
@@ -385,6 +388,10 @@ export async function syncSchema(
385
388
  })),
386
389
  )
387
390
  if (workflows.changed.length > 0) report.workflows = workflows
391
+ // The user groups the files declare (story 228): what each is; where
392
+ // it starts and who is in it stay this environment's.
393
+ const groups = await new UserGroupRepository(tx).syncDeclared(set.userGroups ?? [])
394
+ if (groups.created.length > 0 || groups.updated.length > 0) report.userGroups = groups
388
395
  }
389
396
 
390
397
  // ---- templates referenced by types must have a row
@@ -574,6 +581,7 @@ export async function syncSchema(
574
581
  collectionKey: t.collection ? (dataTypeKeyByAlias.get(t.collection) ?? null) : null,
575
582
  sinceVersion: t.since ?? null,
576
583
  siteSearch: t.siteSearch !== false,
584
+ siteSettings: t.siteSettings === true,
577
585
  cleanup: {
578
586
  preventCleanup: t.cleanup.prevent,
579
587
  keepAllVersionsNewerThanDays: t.cleanup.keepAllNewerThanDays ?? null,
@@ -0,0 +1,89 @@
1
+ /**
2
+ * `schema/user-groups/*.toml` — user groups a site declares (story 228): what a
3
+ * group is (its name, sections, permission verbs and languages), never where it
4
+ * starts or who is in it, which each environment's Users section keeps. Strict
5
+ * as the type parser is, and canonical on the way out, so
6
+ * `parseUserGroup(writeUserGroup(x))` equals `x`. `docs/schema-as-code.md`.
7
+ */
8
+ import {
9
+ ADMIN_GROUP_ALIAS,
10
+ isExtensionSectionAlias,
11
+ KNOWN_PERMISSIONS,
12
+ SECTION_ALIASES,
13
+ } from '@crombolo/core'
14
+ import { type ParseResult, Reader, toml } from './reader.ts'
15
+
16
+ export interface SchemaUserGroup {
17
+ alias: string
18
+ name: string
19
+ description?: string
20
+ icon?: string
21
+ /** Application aliases (`content`), or an extension section's manifest alias. */
22
+ sections: string[]
23
+ permissions: string[]
24
+ /** ISO codes, or every language. */
25
+ languages: string[] | 'all'
26
+ }
27
+
28
+ const KEYS = ['alias', 'name', 'description', 'icon', 'sections', 'permissions', 'languages']
29
+
30
+ export function parseUserGroup(file: string, source: string): ParseResult<SchemaUserGroup> {
31
+ const r = new Reader(file)
32
+ const root = toml(source, r)
33
+ if (!root) return { value: undefined, problems: r.problems }
34
+ r.unknown(root, ['user-group'], '')
35
+ const t = r.table(root['user-group'], 'user-group')
36
+ if (!t) {
37
+ r.problem('user-group', 'a [user-group] table is required')
38
+ return { value: undefined, problems: r.problems }
39
+ }
40
+ const path = 'user-group'
41
+ r.unknown(t, KEYS, path)
42
+ const alias = r.str(t, 'alias', path, true) ?? ''
43
+ if (alias.toLowerCase() === ADMIN_GROUP_ALIAS)
44
+ r.problem(
45
+ `${path}.alias`,
46
+ 'the administrators group is built in, and a file cannot redefine it',
47
+ )
48
+ const sections = r.strs(t, 'sections', path)
49
+ for (const section of sections)
50
+ if (!(section in SECTION_ALIASES) && !isExtensionSectionAlias(section))
51
+ r.problem(
52
+ `${path}.sections`,
53
+ `"${section}" is not a section: one of ${Object.keys(SECTION_ALIASES).join(', ')}, or an extension's section alias`,
54
+ )
55
+ const permissions = r.strs(t, 'permissions', path)
56
+ for (const permission of permissions)
57
+ if (!KNOWN_PERMISSIONS.has(permission))
58
+ r.problem(`${path}.permissions`, `"${permission}" is not a permission verb`)
59
+ let languages: string[] | 'all' = []
60
+ if (t.languages === 'all') languages = 'all'
61
+ else if (t.languages !== undefined) languages = r.strs(t, 'languages', path)
62
+ const description = r.str(t, 'description', path)
63
+ const icon = r.str(t, 'icon', path)
64
+ const value: SchemaUserGroup = {
65
+ alias,
66
+ name: r.str(t, 'name', path, true) ?? '',
67
+ ...(description !== undefined ? { description } : {}),
68
+ ...(icon !== undefined ? { icon } : {}),
69
+ sections,
70
+ permissions,
71
+ languages,
72
+ }
73
+ return { value: r.problems.length === 0 ? value : undefined, problems: r.problems }
74
+ }
75
+
76
+ const str = (value: string) => JSON.stringify(value)
77
+ const list = (values: readonly string[]) => `[${values.map(str).join(', ')}]`
78
+
79
+ export function writeUserGroup(group: SchemaUserGroup): string {
80
+ const out = ['[user-group]', `alias = ${str(group.alias)}`, `name = ${str(group.name)}`]
81
+ if (group.description !== undefined) out.push(`description = ${str(group.description)}`)
82
+ if (group.icon !== undefined) out.push(`icon = ${str(group.icon)}`)
83
+ out.push(
84
+ `sections = ${list(group.sections)}`,
85
+ `permissions = ${list(group.permissions)}`,
86
+ `languages = ${group.languages === 'all' ? '"all"' : list(group.languages)}`,
87
+ )
88
+ return `${out.join('\n')}\n`
89
+ }
package/src/write.ts CHANGED
@@ -126,6 +126,7 @@ export function writeContentType(t: SchemaContentType, kind: SchemaTypeKind = 'd
126
126
  line(out, 'folder', t.folder)
127
127
  line(out, 'since', t.since)
128
128
  line(out, 'site-search', false, t.siteSearch === false)
129
+ line(out, 'site-settings', true, t.siteSettings === true)
129
130
  if (
130
131
  t.cleanup.prevent ||
131
132
  t.cleanup.keepAllNewerThanDays !== undefined ||