@crombolo/schema 0.45.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/src/index.ts CHANGED
@@ -1,19 +1,49 @@
1
1
  export * from './blocks.ts'
2
2
  export * from './builtin.ts'
3
3
  export * from './check.ts'
4
+ export * from './compositions.ts'
4
5
  export * from './config-references.ts'
6
+ export * from './describe.ts'
7
+ export {
8
+ addTab,
9
+ type DataTypeChanges,
10
+ type DataTypeEditResult,
11
+ type EditResult,
12
+ moveProperty,
13
+ moveTab,
14
+ type Placement,
15
+ type PropertyChanges,
16
+ type PropertyPlacement,
17
+ removeProperty,
18
+ removeTab,
19
+ renameTab,
20
+ type TypeChanges,
21
+ updateDataType,
22
+ updateProperty,
23
+ updateType,
24
+ } from './edit.ts'
5
25
  export * from './export.ts'
26
+ export * from './form-edit.ts'
6
27
  export * from './forms.ts'
7
28
  export * from './generate.ts'
8
29
  export * from './generate-delivery.ts'
9
30
  export * from './keys.ts'
10
31
  export * from './load.ts'
32
+ export * from './locate.ts'
11
33
  export * from './model.ts'
12
34
  export * from './parse.ts'
13
35
  export * from './parse-forms.ts'
14
36
  export * from './presets.ts'
15
37
  export * from './reader.ts'
38
+ export {
39
+ type RenameReference,
40
+ type RenameResult,
41
+ renameDataType,
42
+ renameProperty,
43
+ renameType,
44
+ } from './rename.ts'
16
45
  export * from './scaffold.ts'
46
+ export * from './site-edit.ts'
17
47
  export * from './sync.ts'
18
48
  export * from './upgrade.ts'
19
49
  export * from './user-groups.ts'
@@ -21,6 +51,7 @@ export * from './validate.ts'
21
51
  export * from './validate-forms.ts'
22
52
  export * from './value-migrations.ts'
23
53
  export * from './version.ts'
54
+ export * from './workflow-edit.ts'
24
55
  export * from './workflows.ts'
25
56
  export * from './write.ts'
26
57
  export * from './write-forms.ts'
package/src/locate.ts ADDED
@@ -0,0 +1,301 @@
1
+ /**
2
+ * Where a schema problem is, in the site's own terms (story 252): the
3
+ * definition it belongs to and the part of it, such as a property, with the
4
+ * file and line kept for tools and for a file that cannot be read at all.
5
+ */
6
+ import { existsSync, readdirSync, readFileSync } from 'node:fs'
7
+ import { join } from 'node:path'
8
+ import type { SchemaProblem } from './model.ts'
9
+
10
+ export type DefinitionKind =
11
+ | 'content type'
12
+ | 'element type'
13
+ | 'media type'
14
+ | 'member type'
15
+ | 'data type'
16
+ | 'form'
17
+ | 'workflow'
18
+ | 'user group'
19
+ | 'languages'
20
+ | 'site settings'
21
+
22
+ export interface ProblemDefinition {
23
+ kind: DefinitionKind
24
+ alias: string
25
+ name?: string
26
+ }
27
+
28
+ export interface ProblemPart {
29
+ kind: string
30
+ name: string
31
+ }
32
+
33
+ export interface LocatedProblem extends SchemaProblem {
34
+ /** Absent when the file does not read as a definition. */
35
+ definition?: ProblemDefinition
36
+ part?: ProblemPart
37
+ /** The key within the definition or its part the problem is about: `subject`, `width`. */
38
+ key?: string
39
+ line?: number
40
+ }
41
+
42
+ const DIRS: ReadonlyArray<{ dir: string; header: string; kind: DefinitionKind }> = [
43
+ { dir: 'content-types', header: 'content-type', kind: 'content type' },
44
+ { dir: 'media-types', header: 'media-type', kind: 'media type' },
45
+ { dir: 'member-types', header: 'member-type', kind: 'member type' },
46
+ { dir: 'data-types', header: 'data-type', kind: 'data type' },
47
+ { dir: 'forms', header: 'form', kind: 'form' },
48
+ { dir: 'workflows', header: 'workflow', kind: 'workflow' },
49
+ { dir: 'user-groups', header: 'user-group', kind: 'user group' },
50
+ ]
51
+
52
+ const SINGLE_FILES: Record<string, ProblemDefinition> = {
53
+ 'schema.toml': { kind: 'site settings', alias: 'site' },
54
+ 'languages.toml': { kind: 'languages', alias: 'languages' },
55
+ }
56
+
57
+ const PART_WORDS: Record<string, string> = { allow: 'allowed block' }
58
+
59
+ type Data = Record<string, unknown>
60
+ const isRecord = (value: unknown): value is Data =>
61
+ typeof value === 'object' && value !== null && !Array.isArray(value)
62
+
63
+ interface DefinitionFile {
64
+ /** Relative to the site, as problems name it: `schema/content-types/page.toml`. */
65
+ file: string
66
+ dir: string
67
+ text: string
68
+ data?: Data
69
+ definition?: ProblemDefinition
70
+ /** The alias a file that does not read appears to define, for its follow-on problems. */
71
+ claimedAlias?: string
72
+ }
73
+
74
+ function readDefinitionFile(
75
+ schemaDir: string,
76
+ dir: string,
77
+ name: string,
78
+ header: string | undefined,
79
+ kind: DefinitionKind | undefined,
80
+ ): DefinitionFile {
81
+ const path = dir ? join(schemaDir, dir, name) : join(schemaDir, name)
82
+ const text = readFileSync(path, 'utf8')
83
+ const file = `schema/${dir ? `${dir}/` : ''}${name}`
84
+ let data: Data | undefined
85
+ try {
86
+ data = Bun.TOML.parse(text) as Data
87
+ } catch {
88
+ const claimed = /^\s*alias\s*=\s*"([^"]+)"/m.exec(text)?.[1]
89
+ return { file, dir, text, ...(claimed ? { claimedAlias: claimed } : {}) }
90
+ }
91
+ if (!header || !kind) {
92
+ const single = SINGLE_FILES[name]
93
+ return { file, dir, text, data, ...(single ? { definition: single } : {}) }
94
+ }
95
+ const table = data[header]
96
+ if (!isRecord(table) || typeof table.alias !== 'string') return { file, dir, text, data }
97
+ return {
98
+ file,
99
+ dir,
100
+ text,
101
+ data,
102
+ definition: {
103
+ kind: kind === 'content type' && table['is-element'] === true ? 'element type' : kind,
104
+ alias: table.alias,
105
+ ...(typeof table.name === 'string' ? { name: table.name } : {}),
106
+ },
107
+ }
108
+ }
109
+
110
+ function definitionFiles(schemaDir: string): DefinitionFile[] {
111
+ const out: DefinitionFile[] = []
112
+ for (const name of Object.keys(SINGLE_FILES))
113
+ if (existsSync(join(schemaDir, name)))
114
+ out.push(readDefinitionFile(schemaDir, '', name, undefined, undefined))
115
+ for (const { dir, header, kind } of DIRS) {
116
+ const at = join(schemaDir, dir)
117
+ if (!existsSync(at)) continue
118
+ for (const name of readdirSync(at)
119
+ .filter((n) => n.endsWith('.toml'))
120
+ .sort())
121
+ out.push(readDefinitionFile(schemaDir, dir, name, header, kind))
122
+ }
123
+ return out
124
+ }
125
+
126
+ const SEGMENT = /([a-z-]+)\[(\d+)\]/g
127
+ const escaped = (text: string) => text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
128
+ /** Whole-set checks name a part by its alias: `property "body".type`. */
129
+ const NAMED = /^([a-z-]+) "([^"]+)"/
130
+
131
+ /** The deepest indexed part of a path, such as `tab[0].property[1]`, named from the file. */
132
+ function partOf(data: Data | undefined, header: string | undefined, path: string) {
133
+ if (!data) return undefined
134
+ const named = NAMED.exec(path)
135
+ if (named) {
136
+ const [, segment, name] = named as unknown as [string, string, string]
137
+ return { kind: PART_WORDS[segment] ?? segment, name }
138
+ }
139
+ let at: unknown = data
140
+ let part: ProblemPart | undefined
141
+ for (const match of path.matchAll(SEGMENT)) {
142
+ const [, segment, index] = match as unknown as [string, string, string]
143
+ const from: unknown =
144
+ isRecord(at) && segment in at ? at : header && isRecord(data[header]) ? data[header] : at
145
+ const list = isRecord(from) ? from[segment] : undefined
146
+ const item = Array.isArray(list) ? list[Number(index)] : undefined
147
+ if (!isRecord(item)) break
148
+ at = item
149
+ const name = [item.alias, item.name, item.caption, item.iso, item.element].find(
150
+ (v): v is string => typeof v === 'string' && v !== '',
151
+ )
152
+ part = { kind: PART_WORDS[segment] ?? segment, name: name ?? `${Number(index) + 1}` }
153
+ }
154
+ return part
155
+ }
156
+
157
+ /** The line each table and key is on, by its dotted path: `tab[0].property[1].width`. */
158
+ function linesOf(text: string): Map<string, number> {
159
+ const lines = new Map<string, number>()
160
+ const counts = new Map<string, number>()
161
+ let current: string[] = []
162
+ text.split('\n').forEach((raw, i) => {
163
+ const line = raw.trim()
164
+ const many = /^\[\[\s*([^\]]+?)\s*\]\]/.exec(line)
165
+ const one = many ? null : /^\[\s*([^\]]+?)\s*\]/.exec(line)
166
+ if (many || one) {
167
+ const names = ((many ?? one)?.[1] ?? '').split('.').map((n) => n.trim())
168
+ const path: string[] = []
169
+ for (const [j, name] of names.entries()) {
170
+ const last = j === names.length - 1
171
+ if (!last) {
172
+ // A parent array of tables is its latest entry
173
+ const prefix = [...path, name].join('.')
174
+ const n = counts.get(prefix)
175
+ path.push(n === undefined ? name : `${name}[${n - 1}]`)
176
+ continue
177
+ }
178
+ if (many) {
179
+ const prefix = [...path, name].join('.')
180
+ const n = counts.get(prefix) ?? 0
181
+ counts.set(prefix, n + 1)
182
+ path.push(`${name}[${n}]`)
183
+ } else path.push(name)
184
+ }
185
+ current = path
186
+ lines.set(current.join('.'), i + 1)
187
+ return
188
+ }
189
+ const key = /^("[^"]+"|[A-Za-z0-9_-]+)\s*=/.exec(line)?.[1]?.replace(/"/g, '')
190
+ if (key) {
191
+ const at = [...current, key].join('.')
192
+ if (!lines.has(at)) lines.set(at, i + 1)
193
+ }
194
+ })
195
+ return lines
196
+ }
197
+
198
+ function lineOf(text: string, header: string | undefined, path: string): number | undefined {
199
+ if (!path) return undefined
200
+ const named = NAMED.exec(path) as [string, string, string] | null
201
+ if (named) {
202
+ const key = named[1] === 'language' ? 'iso' : 'alias'
203
+ const lines = text.split('\n')
204
+ const at = lines.findIndex((line) =>
205
+ new RegExp(`^\\s*${key}\\s*=\\s*"${escaped(named[2])}"`).test(line),
206
+ )
207
+ if (at < 0) return undefined
208
+ // The key after the part, such as `.type`, within the part's own table
209
+ const rest = path.slice(named[0].length).replace(/^\./, '').split('.')[0]
210
+ if (rest)
211
+ for (let i = at + 1; i < lines.length && !/^\s*\[/.test(lines[i] ?? ''); i++)
212
+ if (new RegExp(`^\\s*${escaped(rest)}\\s*=`).test(lines[i] ?? '')) return i + 1
213
+ return at + 1
214
+ }
215
+ const lines = linesOf(text)
216
+ const parts = path.split('.')
217
+ for (let n = parts.length; n > 0; n--) {
218
+ const at = parts.slice(0, n).join('.')
219
+ const found = lines.get(at) ?? (header ? lines.get(`${header}.${at}`) : undefined)
220
+ if (found !== undefined) return found
221
+ }
222
+ return undefined
223
+ }
224
+
225
+ /** What follows the part in a path, or the header: `workflow[0].settings.subject` is `settings.subject`. */
226
+ function keyOf(path: string, header: string | undefined): string | undefined {
227
+ const named = NAMED.exec(path)
228
+ const rest = named
229
+ ? path.slice(named[0].length)
230
+ : path.includes(']')
231
+ ? path.slice(path.lastIndexOf(']') + 1)
232
+ : header && path.startsWith(`${header}.`)
233
+ ? path.slice(header.length)
234
+ : ''
235
+ const key = rest.replace(/^\./, '')
236
+ return key && !key.includes('[') ? key : undefined
237
+ }
238
+
239
+ /** A definition checked before its file is written, named as whole-set checks name it. */
240
+ function unwritten(file: string): ProblemDefinition | undefined {
241
+ const single = SINGLE_FILES[`${file.replace(/^schema\//, '').replace(/\.toml$/, '')}.toml`]
242
+ if (single) return single
243
+ const named = /^schema\/([a-z-]+)\/([^/]+)$/.exec(file) as [string, string, string] | null
244
+ if (!named || named[2].endsWith('.toml')) return undefined
245
+ const kind = DIRS.find((d) => d.dir === named[1])?.kind
246
+ return kind ? { kind, alias: named[2] } : undefined
247
+ }
248
+
249
+ /**
250
+ * Each problem with its definition, part and line. A problem a file that
251
+ * cannot be read causes in another file, such as an unknown type that file
252
+ * defines, is dropped: the unreadable file is the problem to fix.
253
+ */
254
+ export function locateProblems(
255
+ problems: readonly SchemaProblem[],
256
+ schemaDir: string,
257
+ ): LocatedProblem[] {
258
+ const files = existsSync(schemaDir) ? definitionFiles(schemaDir) : []
259
+ const byFile = new Map(files.map((f) => [f.file, f]))
260
+ const unreadable = files.filter((f) => f.data === undefined)
261
+ const claimed = unreadable.flatMap((f) => (f.claimedAlias ? [f.claimedAlias] : []))
262
+ const headerOf = (dir: string) => DIRS.find((d) => d.dir === dir)?.header
263
+
264
+ const fileOf = (problem: SchemaProblem): DefinitionFile | undefined => {
265
+ const direct = byFile.get(problem.file) ?? byFile.get(`${problem.file}.toml`)
266
+ if (direct) return direct
267
+ // Whole-set checks name a definition as `schema/<dir>/<alias>`
268
+ const named = /^schema\/([a-z-]+)\/(.+?)(?:\.toml)?$/.exec(problem.file)
269
+ if (!named) return undefined
270
+ const [, dir, alias] = named as unknown as [string, string, string]
271
+ return files.find((f) => f.dir === dir && f.definition?.alias === alias)
272
+ }
273
+
274
+ const out: LocatedProblem[] = []
275
+ for (const problem of problems) {
276
+ const found = fileOf(problem)
277
+ if (
278
+ found?.data !== undefined &&
279
+ claimed.some((alias) => problem.message.includes(`"${alias}"`))
280
+ )
281
+ continue
282
+ if (!found) {
283
+ const definition = unwritten(problem.file)
284
+ out.push({ ...problem, ...(definition ? { definition } : {}) })
285
+ continue
286
+ }
287
+ const header = headerOf(found.dir)
288
+ const part = found.definition ? partOf(found.data, header, problem.path) : undefined
289
+ const key = keyOf(problem.path, header)
290
+ const line = problem.line ?? lineOf(found.text, header, problem.path)
291
+ out.push({
292
+ ...problem,
293
+ file: found.file,
294
+ ...(found.definition ? { definition: found.definition } : {}),
295
+ ...(part ? { part } : {}),
296
+ ...(key ? { key } : {}),
297
+ ...(line !== undefined ? { line } : {}),
298
+ })
299
+ }
300
+ return out
301
+ }
package/src/model.ts CHANGED
@@ -182,6 +182,8 @@ export interface SchemaProblem {
182
182
  /** A dotted path into the file, e.g. `tab[1].property[0].type`. */
183
183
  path: string
184
184
  message: string
185
+ /** The line, when the file could not be read as TOML at all. */
186
+ line?: number
185
187
  }
186
188
 
187
189
  /** Which kind of content type a file defines. */
package/src/presets.ts CHANGED
@@ -148,7 +148,10 @@ export function presetDataType(
148
148
  options: PresetOptions = {},
149
149
  name?: string,
150
150
  ): PresetResult {
151
- const chosen = DATA_TYPE_PRESETS[preset]
151
+ const letters = (name: string) => name.toLowerCase().replace(/[^a-z0-9]/g, '')
152
+ const chosen =
153
+ DATA_TYPE_PRESETS[preset] ??
154
+ Object.entries(DATA_TYPE_PRESETS).find(([key]) => letters(key) === letters(preset))?.[1]
152
155
  if (!chosen)
153
156
  return {
154
157
  ok: false,
package/src/reader.ts CHANGED
@@ -93,7 +93,38 @@ export function toml(source: string, r: Reader): Toml | undefined {
93
93
  try {
94
94
  return Bun.TOML.parse(source) as Toml
95
95
  } catch (error) {
96
- r.problem('', `invalid TOML: ${(error as Error).message}`)
96
+ const line = syntaxErrorLine(source)
97
+ r.problems.push({
98
+ file: r.file,
99
+ path: '',
100
+ message: `invalid TOML: ${(error as Error).message}`,
101
+ ...(line !== undefined ? { line } : {}),
102
+ })
97
103
  return undefined
98
104
  }
99
105
  }
106
+
107
+ const parses = (source: string) => {
108
+ try {
109
+ Bun.TOML.parse(source)
110
+ return true
111
+ } catch {
112
+ return false
113
+ }
114
+ }
115
+
116
+ /**
117
+ * The line a TOML syntax error is on: the first from which every longer start
118
+ * of the file fails to read. Bun's error does not say, and a start that cuts a
119
+ * multi-line array or string in half fails too, so the first failing start is
120
+ * not enough.
121
+ */
122
+ export function syntaxErrorLine(source: string): number | undefined {
123
+ const lines = source.split('\n')
124
+ let line: number | undefined
125
+ for (let n = lines.length; n > 0; n--) {
126
+ if (parses(lines.slice(0, n).join('\n'))) break
127
+ line = n
128
+ }
129
+ return line
130
+ }