@crombolo/server 0.26.0 → 0.28.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 +12 -12
- package/src/adapters/audit.ts +41 -15
- package/src/adapters/media.ts +5 -202
- package/src/config.ts +1 -1
- package/src/content-events.ts +45 -0
- package/src/content-operations.ts +40 -2
- package/src/index.ts +1 -0
- package/src/ports.ts +16 -3
- package/src/remote.ts +30 -4
- package/src/seed/compile.ts +732 -0
- package/src/seed/export.ts +149 -0
- package/src/seed/index.ts +5 -0
- package/src/seed/keys.ts +27 -0
- package/src/seed/lines.ts +58 -0
- package/src/seed/read.ts +175 -0
|
@@ -0,0 +1,732 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Seed files to a bundle (story 163). The items `readSeed` found are checked
|
|
3
|
+
* against the site's own schema, their values written in each property
|
|
4
|
+
* editor's stored shape from the shorthand a person writes, and the result is
|
|
5
|
+
* the bundle format every import already reads. Applying it is `content
|
|
6
|
+
* import`'s job, with its findings, its one transaction and its revert.
|
|
7
|
+
*/
|
|
8
|
+
import { createHash } from 'node:crypto'
|
|
9
|
+
import { extname } from 'node:path'
|
|
10
|
+
import {
|
|
11
|
+
type ContentTypeAggregate,
|
|
12
|
+
IMAGE_CROPPER_EDITOR_ALIAS,
|
|
13
|
+
normaliseUuid,
|
|
14
|
+
ObjectTypes,
|
|
15
|
+
SystemNodes,
|
|
16
|
+
} from '@crombolo/core'
|
|
17
|
+
import {
|
|
18
|
+
ComponentRepository,
|
|
19
|
+
ContentTypeRepository,
|
|
20
|
+
currentSchemaState,
|
|
21
|
+
DataTypeRepository,
|
|
22
|
+
type Db,
|
|
23
|
+
LanguageRepository,
|
|
24
|
+
looksLikeKey,
|
|
25
|
+
NodeRepository,
|
|
26
|
+
resolveNodeRef,
|
|
27
|
+
} from '@crombolo/data'
|
|
28
|
+
import {
|
|
29
|
+
type BundleBlob,
|
|
30
|
+
type BundleFile,
|
|
31
|
+
type BundleKind,
|
|
32
|
+
type BundleNode,
|
|
33
|
+
type BundleValue,
|
|
34
|
+
type ContentSet,
|
|
35
|
+
writeBundle,
|
|
36
|
+
writeBundlePackage,
|
|
37
|
+
} from '@crombolo/transfer'
|
|
38
|
+
import { createMediaPort } from '../adapters/media.ts'
|
|
39
|
+
import { type CromboloConfig, VERSION } from '../config.ts'
|
|
40
|
+
import { blockKey, seedKey, uuidV5 } from './keys.ts'
|
|
41
|
+
import { lineOf } from './lines.ts'
|
|
42
|
+
import { readSeed, type SeedFinding, type SeedItem } from './read.ts'
|
|
43
|
+
|
|
44
|
+
export interface SeedBuild {
|
|
45
|
+
findings: SeedFinding[]
|
|
46
|
+
/** The bundle, when nothing found stops it being built. */
|
|
47
|
+
set?: ContentSet
|
|
48
|
+
blobs: Map<string, Uint8Array>
|
|
49
|
+
/** The bundle as the npm package `content import` reads. */
|
|
50
|
+
files?: BundleFile[]
|
|
51
|
+
/** How many items of each kind it holds. */
|
|
52
|
+
counts: Record<'content' | 'library' | 'media', number>
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** A word for a file name: `about-us` → `About us`. */
|
|
56
|
+
const nameFromSegment = (segment: string) => {
|
|
57
|
+
const words = segment.replace(/[-_]+/g, ' ').trim()
|
|
58
|
+
return words.charAt(0).toUpperCase() + words.slice(1)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
62
|
+
typeof value === 'object' && value !== null && !Array.isArray(value) && !(value instanceof Date)
|
|
63
|
+
|
|
64
|
+
const BLOCK_LAYOUT = {
|
|
65
|
+
'Umbraco.BlockList': 'Umbraco.BlockList',
|
|
66
|
+
'Umbraco.BlockGrid': 'Umbraco.BlockGrid',
|
|
67
|
+
} as const
|
|
68
|
+
|
|
69
|
+
export async function compileSeed(db: Db, config: CromboloConfig): Promise<SeedBuild> {
|
|
70
|
+
const tree = readSeed(config.siteDir)
|
|
71
|
+
const findings: SeedFinding[] = [...tree.findings]
|
|
72
|
+
const counts = { content: 0, library: 0, media: 0 }
|
|
73
|
+
for (const item of tree.items) counts[item.kind] += 1
|
|
74
|
+
|
|
75
|
+
const state = await currentSchemaState(db)
|
|
76
|
+
const documentTypes = new ContentTypeRepository(db)
|
|
77
|
+
const mediaTypes = new ContentTypeRepository(db, { kind: 'media' })
|
|
78
|
+
const dataTypes = new DataTypeRepository(db)
|
|
79
|
+
const components = new ComponentRepository(db)
|
|
80
|
+
const nodes = new NodeRepository(db)
|
|
81
|
+
const port = createMediaPort(db, {
|
|
82
|
+
nodeState: { version: state.version, revision: state.revision },
|
|
83
|
+
})
|
|
84
|
+
const languages = new Map(
|
|
85
|
+
(await new LanguageRepository(db).all()).map((l) => [l.isoCode.toLowerCase(), l.isoCode]),
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
const find = (item: SeedItem, path: readonly string[], code: string, message: string) =>
|
|
89
|
+
findings.push({
|
|
90
|
+
file: item.file ?? `seed/${item.kind}/${item.path}`,
|
|
91
|
+
line: item.text ? lineOf(item.text, path) : 1,
|
|
92
|
+
code,
|
|
93
|
+
message,
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
// ---------------------------------------------------------------- keys
|
|
97
|
+
const keyOf = new Map<SeedItem, string>()
|
|
98
|
+
const byKey = new Map<string, SeedItem>()
|
|
99
|
+
for (const item of tree.items) {
|
|
100
|
+
const given = item.data.key
|
|
101
|
+
let key = seedKey(`${item.kind}/${item.path}`)
|
|
102
|
+
if (given !== undefined) {
|
|
103
|
+
if (typeof given === 'string' && looksLikeKey(given)) key = normaliseUuid(given)
|
|
104
|
+
else find(item, ['key'], 'bad-key', `key "${String(given)}" is not a uuid`)
|
|
105
|
+
}
|
|
106
|
+
const clash = byKey.get(key)
|
|
107
|
+
if (clash)
|
|
108
|
+
find(item, ['key'], 'duplicate-key', `the key ${key} is also ${clash.file ?? clash.path}'s`)
|
|
109
|
+
byKey.set(key, item)
|
|
110
|
+
keyOf.set(item, key)
|
|
111
|
+
}
|
|
112
|
+
const byPath = (kind: SeedItem['kind']) =>
|
|
113
|
+
new Map(tree.items.filter((i) => i.kind === kind).map((i) => [i.path.toLowerCase(), i]))
|
|
114
|
+
const contentByPath = byPath('content')
|
|
115
|
+
const libraryByPath = byPath('library')
|
|
116
|
+
const mediaByPath = byPath('media')
|
|
117
|
+
|
|
118
|
+
// ------------------------------------------------------ the site's schema
|
|
119
|
+
const typeCache = new Map<string, ContentTypeAggregate | undefined>()
|
|
120
|
+
const typeOf = async (repo: ContentTypeRepository, alias: string) => {
|
|
121
|
+
const cacheKey = `${repo.objectType}:${alias.toLowerCase()}`
|
|
122
|
+
if (!typeCache.has(cacheKey)) typeCache.set(cacheKey, await repo.byAlias(alias))
|
|
123
|
+
return typeCache.get(cacheKey)
|
|
124
|
+
}
|
|
125
|
+
const editorCache = new Map<string, Map<string, { editor: string; variesByCulture: boolean }>>()
|
|
126
|
+
const editorsOf = async (repo: ContentTypeRepository, type: ContentTypeAggregate) => {
|
|
127
|
+
const cached = editorCache.get(type.key)
|
|
128
|
+
if (cached) return cached
|
|
129
|
+
const properties = await repo.resolveProperties(type.key)
|
|
130
|
+
const editorByType = new Map(
|
|
131
|
+
(await dataTypes.byKeys(properties.map((p) => p.dataTypeKey))).map((d) => [
|
|
132
|
+
d.key,
|
|
133
|
+
d.editorAlias,
|
|
134
|
+
]),
|
|
135
|
+
)
|
|
136
|
+
const out = new Map<string, { editor: string; variesByCulture: boolean }>()
|
|
137
|
+
for (const property of properties) {
|
|
138
|
+
const editor = editorByType.get(property.dataTypeKey)
|
|
139
|
+
if (editor) out.set(property.alias, { editor, variesByCulture: property.variesByCulture })
|
|
140
|
+
}
|
|
141
|
+
editorCache.set(type.key, out)
|
|
142
|
+
return out
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Content on the site the seed does not declare, by path or key, for a reference to point at. */
|
|
146
|
+
const expected = new Map<string, { key: string; objectType: string; name: string; why: string }>()
|
|
147
|
+
const siteNode = async (ref: string, objectType: string, why: string) => {
|
|
148
|
+
const resolved = await resolveNodeRef(db, [objectType], ref)
|
|
149
|
+
if (!resolved.ok || resolved.node.objectType !== objectType) return undefined
|
|
150
|
+
expected.set(resolved.node.key, {
|
|
151
|
+
key: resolved.node.key,
|
|
152
|
+
objectType,
|
|
153
|
+
name: resolved.node.text ?? '',
|
|
154
|
+
why,
|
|
155
|
+
})
|
|
156
|
+
return resolved.node.key
|
|
157
|
+
}
|
|
158
|
+
const trimPath = (ref: string) => ref.replace(/^\/+|\/+$/g, '').toLowerCase()
|
|
159
|
+
const contentKey = async (ref: string, why: string) => {
|
|
160
|
+
const seeded = contentByPath.get(trimPath(ref))
|
|
161
|
+
if (seeded) return keyOf.get(seeded)
|
|
162
|
+
if (looksLikeKey(ref)) return normaliseUuid(ref)
|
|
163
|
+
return siteNode(ref, ObjectTypes.Document, why)
|
|
164
|
+
}
|
|
165
|
+
const mediaRef = async (ref: string, why: string) => {
|
|
166
|
+
const seeded = mediaByPath.get(trimPath(ref))
|
|
167
|
+
if (seeded)
|
|
168
|
+
return { key: keyOf.get(seeded) as string, typeAlias: mediaTypeAlias.get(seeded) ?? 'Image' }
|
|
169
|
+
const key = looksLikeKey(ref) ? normaliseUuid(ref) : await siteNode(ref, ObjectTypes.Media, why)
|
|
170
|
+
if (!key) return undefined
|
|
171
|
+
const rows = await db.query<{ alias: string }>(
|
|
172
|
+
`SELECT ct.alias FROM node n JOIN content c ON c.node_id = n.id
|
|
173
|
+
JOIN content_type ct ON ct.node_id = c.content_type_id WHERE n.unique_id = ?`,
|
|
174
|
+
[key],
|
|
175
|
+
)
|
|
176
|
+
return { key, typeAlias: rows[0] ? String(rows[0].alias) : 'Image' }
|
|
177
|
+
}
|
|
178
|
+
const libraryKey = async (ref: string, why: string) => {
|
|
179
|
+
const seeded = libraryByPath.get(trimPath(ref))
|
|
180
|
+
if (seeded) return keyOf.get(seeded)
|
|
181
|
+
if (looksLikeKey(ref)) return normaliseUuid(ref)
|
|
182
|
+
return siteNode(ref, ObjectTypes.Element, why)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// ---------------------------------------------------------------- values
|
|
186
|
+
type Converted = { ok: true; value: unknown } | { ok: false; message: string }
|
|
187
|
+
const fail = (message: string): Converted => ({ ok: false, message })
|
|
188
|
+
|
|
189
|
+
/** Rich text: Markdown by default, `{ html = … }` as written; links to content paths become local links. */
|
|
190
|
+
const richText = async (raw: unknown, why: string): Promise<Converted> => {
|
|
191
|
+
let html: string
|
|
192
|
+
if (typeof raw === 'string') html = Bun.markdown.html(raw)
|
|
193
|
+
else if (isRecord(raw) && typeof raw.html === 'string') html = raw.html
|
|
194
|
+
else if (isRecord(raw) && typeof raw.markdown === 'string')
|
|
195
|
+
html = Bun.markdown.html(raw.markdown)
|
|
196
|
+
else if (isRecord(raw) && 'markup' in raw) return { ok: true, value: raw }
|
|
197
|
+
else return fail('rich text is Markdown, or { html = "…" }')
|
|
198
|
+
const links = [...html.matchAll(/href="(\/[^"#?]*)"/g)]
|
|
199
|
+
for (const [whole, path] of links) {
|
|
200
|
+
const key = await contentKey(path as string, why)
|
|
201
|
+
if (key) html = html.replace(whole, `href="/{localLink:${key}}"`)
|
|
202
|
+
}
|
|
203
|
+
return { ok: true, value: { markup: html, blocks: null } }
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const blocks = async (
|
|
207
|
+
editor: keyof typeof BLOCK_LAYOUT,
|
|
208
|
+
raw: unknown,
|
|
209
|
+
owner: string,
|
|
210
|
+
where: string,
|
|
211
|
+
): Promise<Converted> => {
|
|
212
|
+
if (!Array.isArray(raw)) {
|
|
213
|
+
if (isRecord(raw) && 'layout' in raw) return { ok: true, value: raw }
|
|
214
|
+
return fail('blocks are a list of { type = "<element alias>", values = { … } }')
|
|
215
|
+
}
|
|
216
|
+
const layout: Record<string, unknown>[] = []
|
|
217
|
+
const contentData: unknown[] = []
|
|
218
|
+
const settingsData: unknown[] = []
|
|
219
|
+
for (const [index, entry] of raw.entries()) {
|
|
220
|
+
if (!isRecord(entry) || typeof entry.type !== 'string')
|
|
221
|
+
return fail(`block ${index + 1} has no type`)
|
|
222
|
+
const element = await typeOf(documentTypes, entry.type)
|
|
223
|
+
if (!element?.isElement)
|
|
224
|
+
return fail(`block ${index + 1}: there is no element type "${entry.type}"`)
|
|
225
|
+
const contentKey = blockKey(owner, `${where}/${index}`)
|
|
226
|
+
const values = await elementValues(element, entry.values, owner, `${where}/${index}`)
|
|
227
|
+
if (!values.ok) return fail(`block ${index + 1}: ${values.message}`)
|
|
228
|
+
contentData.push({ key: contentKey, contentTypeKey: element.key, values: values.value })
|
|
229
|
+
const item: Record<string, unknown> = { contentKey }
|
|
230
|
+
if (isRecord(entry.settings)) {
|
|
231
|
+
const settingsType =
|
|
232
|
+
typeof entry.settings.type === 'string'
|
|
233
|
+
? await typeOf(documentTypes, entry.settings.type)
|
|
234
|
+
: undefined
|
|
235
|
+
if (!settingsType?.isElement)
|
|
236
|
+
return fail(`block ${index + 1}: settings need a type, an element type`)
|
|
237
|
+
const settingsKey = blockKey(owner, `${where}/${index}/settings`)
|
|
238
|
+
const settings = await elementValues(
|
|
239
|
+
settingsType,
|
|
240
|
+
entry.settings.values,
|
|
241
|
+
owner,
|
|
242
|
+
`${where}/${index}/settings`,
|
|
243
|
+
)
|
|
244
|
+
if (!settings.ok) return fail(`block ${index + 1} settings: ${settings.message}`)
|
|
245
|
+
settingsData.push({
|
|
246
|
+
key: settingsKey,
|
|
247
|
+
contentTypeKey: settingsType.key,
|
|
248
|
+
values: settings.value,
|
|
249
|
+
})
|
|
250
|
+
item.settingsKey = settingsKey
|
|
251
|
+
}
|
|
252
|
+
if (editor === 'Umbraco.BlockGrid') {
|
|
253
|
+
item.columnSpan = typeof entry.columns === 'number' ? entry.columns : 12
|
|
254
|
+
item.rowSpan = typeof entry.rows === 'number' ? entry.rows : 1
|
|
255
|
+
item.areas = Array.isArray(entry.areas) ? entry.areas : []
|
|
256
|
+
}
|
|
257
|
+
layout.push(item)
|
|
258
|
+
}
|
|
259
|
+
return {
|
|
260
|
+
ok: true,
|
|
261
|
+
value: { layout: { [BLOCK_LAYOUT[editor]]: layout }, contentData, settingsData },
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const elementValues = async (
|
|
266
|
+
element: ContentTypeAggregate,
|
|
267
|
+
raw: unknown,
|
|
268
|
+
owner: string,
|
|
269
|
+
where: string,
|
|
270
|
+
): Promise<Converted> => {
|
|
271
|
+
if (raw === undefined) return { ok: true, value: [] }
|
|
272
|
+
if (!isRecord(raw)) return fail('values is a table')
|
|
273
|
+
const editors = await editorsOf(documentTypes, element)
|
|
274
|
+
const out: Array<{ alias: string; value: unknown }> = []
|
|
275
|
+
for (const [alias, value] of Object.entries(raw)) {
|
|
276
|
+
const property = editors.get(alias)
|
|
277
|
+
if (!property) return fail(`"${element.alias}" has no property "${alias}"`)
|
|
278
|
+
const converted = await convert(property.editor, value, owner, `${where}/${alias}`)
|
|
279
|
+
if (!converted.ok) return fail(`${alias}: ${converted.message}`)
|
|
280
|
+
out.push({ alias, value: converted.value })
|
|
281
|
+
}
|
|
282
|
+
return { ok: true, value: out }
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
const list = (raw: unknown) => (Array.isArray(raw) ? raw : [raw])
|
|
286
|
+
|
|
287
|
+
const convert = async (
|
|
288
|
+
editor: string,
|
|
289
|
+
raw: unknown,
|
|
290
|
+
owner: string,
|
|
291
|
+
where: string,
|
|
292
|
+
): Promise<Converted> => {
|
|
293
|
+
const why = `named by a value in ${where}`
|
|
294
|
+
switch (editor) {
|
|
295
|
+
case 'Umbraco.TextBox':
|
|
296
|
+
case 'Umbraco.TextArea':
|
|
297
|
+
return typeof raw === 'string' || typeof raw === 'number'
|
|
298
|
+
? { ok: true, value: String(raw) }
|
|
299
|
+
: fail('a text value is a string')
|
|
300
|
+
case 'Umbraco.RichText':
|
|
301
|
+
case 'Umbraco.TinyMCE':
|
|
302
|
+
return richText(raw, why)
|
|
303
|
+
case 'Umbraco.TrueFalse':
|
|
304
|
+
return typeof raw === 'boolean'
|
|
305
|
+
? { ok: true, value: raw }
|
|
306
|
+
: fail('a toggle is true or false')
|
|
307
|
+
case 'Umbraco.Integer':
|
|
308
|
+
case 'Umbraco.Decimal':
|
|
309
|
+
case 'Umbraco.Slider':
|
|
310
|
+
return typeof raw === 'number' ? { ok: true, value: raw } : fail('a number is a number')
|
|
311
|
+
case 'Umbraco.DateTime':
|
|
312
|
+
case 'Umbraco.DateTimeWithTimeZone':
|
|
313
|
+
if (raw instanceof Date) return { ok: true, value: raw.toISOString() }
|
|
314
|
+
return typeof raw === 'string' && !Number.isNaN(new Date(raw).getTime())
|
|
315
|
+
? { ok: true, value: raw }
|
|
316
|
+
: fail('a date is a TOML date or an ISO 8601 string')
|
|
317
|
+
case 'Umbraco.Tags':
|
|
318
|
+
return list(raw).every((tag) => typeof tag === 'string')
|
|
319
|
+
? { ok: true, value: list(raw) }
|
|
320
|
+
: fail('tags are a list of strings')
|
|
321
|
+
case 'Umbraco.ContentPicker': {
|
|
322
|
+
if (typeof raw !== 'string') return fail('a content picker takes a content path')
|
|
323
|
+
const key = await contentKey(raw, why)
|
|
324
|
+
return key
|
|
325
|
+
? { ok: true, value: `umb://document/${key.replaceAll('-', '')}` }
|
|
326
|
+
: fail(`"${raw}" is not a page in the seed or on the site`)
|
|
327
|
+
}
|
|
328
|
+
case 'Umbraco.MediaPicker3': {
|
|
329
|
+
if (isRecord(raw) || (Array.isArray(raw) && raw.some(isRecord)))
|
|
330
|
+
return { ok: true, value: raw }
|
|
331
|
+
const picks: unknown[] = []
|
|
332
|
+
for (const ref of list(raw)) {
|
|
333
|
+
if (typeof ref !== 'string') return fail('a media picker takes media paths')
|
|
334
|
+
const media = await mediaRef(ref, why)
|
|
335
|
+
if (!media) return fail(`"${ref}" is not media in the seed or on the site`)
|
|
336
|
+
picks.push({
|
|
337
|
+
key: uuidV5(`pick:${media.key}`),
|
|
338
|
+
mediaKey: media.key,
|
|
339
|
+
mediaTypeAlias: media.typeAlias,
|
|
340
|
+
crops: [],
|
|
341
|
+
focalPoint: null,
|
|
342
|
+
})
|
|
343
|
+
}
|
|
344
|
+
return { ok: true, value: picks }
|
|
345
|
+
}
|
|
346
|
+
case 'Umbraco.ElementPicker': {
|
|
347
|
+
const keys: string[] = []
|
|
348
|
+
for (const ref of list(raw)) {
|
|
349
|
+
if (typeof ref !== 'string') return fail('an element picker takes Library paths')
|
|
350
|
+
const key = await libraryKey(ref, why)
|
|
351
|
+
if (!key) return fail(`"${ref}" is not a Library element in the seed or on the site`)
|
|
352
|
+
keys.push(key)
|
|
353
|
+
}
|
|
354
|
+
return { ok: true, value: keys }
|
|
355
|
+
}
|
|
356
|
+
case 'Umbraco.MultiUrlPicker': {
|
|
357
|
+
const links: unknown[] = []
|
|
358
|
+
for (const entry of list(raw)) {
|
|
359
|
+
const link = typeof entry === 'string' ? { path: entry } : entry
|
|
360
|
+
if (!isRecord(link))
|
|
361
|
+
return fail('a link is a path, a URL, or { path | url, name, target }')
|
|
362
|
+
// A link as the editor stores it, as `seed export` writes one.
|
|
363
|
+
if ('unique' in link || 'type' in link) {
|
|
364
|
+
links.push(link)
|
|
365
|
+
continue
|
|
366
|
+
}
|
|
367
|
+
const target = typeof link.target === 'string' ? link.target : null
|
|
368
|
+
const url = typeof link.url === 'string' ? link.url : undefined
|
|
369
|
+
const path = typeof link.path === 'string' ? link.path : undefined
|
|
370
|
+
if (path && /^[a-z]+:\/\//i.test(path)) {
|
|
371
|
+
links.push({
|
|
372
|
+
unique: null,
|
|
373
|
+
type: 'external',
|
|
374
|
+
name: link.name ?? path,
|
|
375
|
+
target,
|
|
376
|
+
url: path,
|
|
377
|
+
})
|
|
378
|
+
continue
|
|
379
|
+
}
|
|
380
|
+
if (url) {
|
|
381
|
+
links.push({ unique: null, type: 'external', name: link.name ?? url, target, url })
|
|
382
|
+
continue
|
|
383
|
+
}
|
|
384
|
+
if (!path) return fail('a link needs a path or a url')
|
|
385
|
+
const media = mediaByPath.get(trimPath(path))
|
|
386
|
+
const key = media ? keyOf.get(media) : await contentKey(path, why)
|
|
387
|
+
if (!key) return fail(`"${path}" is not a page or media in the seed or on the site`)
|
|
388
|
+
links.push({
|
|
389
|
+
unique: key,
|
|
390
|
+
type: media ? 'media' : 'document',
|
|
391
|
+
name: typeof link.name === 'string' ? link.name : null,
|
|
392
|
+
target,
|
|
393
|
+
})
|
|
394
|
+
}
|
|
395
|
+
return { ok: true, value: links }
|
|
396
|
+
}
|
|
397
|
+
case 'Umbraco.BlockList':
|
|
398
|
+
case 'Umbraco.BlockGrid':
|
|
399
|
+
return blocks(editor, raw, owner, where)
|
|
400
|
+
default:
|
|
401
|
+
return { ok: true, value: raw }
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// ------------------------------------------------------------- the nodes
|
|
406
|
+
const mediaTypeAlias = new Map<SeedItem, string>()
|
|
407
|
+
const bundleNodes: BundleNode[] = []
|
|
408
|
+
const blobs = new Map<string, Uint8Array>()
|
|
409
|
+
const blobList: BundleBlob[] = []
|
|
410
|
+
const usedTypes = new Map<string, string>()
|
|
411
|
+
const usedComponents = new Set<string>()
|
|
412
|
+
const usedLanguages = new Set<string>()
|
|
413
|
+
const typeOfItem = new Map<SeedItem, ContentTypeAggregate>()
|
|
414
|
+
|
|
415
|
+
// Media types first: a content value may pick an item, and names its type.
|
|
416
|
+
const folderType = (await port.folderTypes()).find((t) => t.name === 'Folder')
|
|
417
|
+
for (const item of tree.items.filter((i) => i.kind === 'media')) {
|
|
418
|
+
const alias =
|
|
419
|
+
typeof item.data.type === 'string' ? item.data.type : item.folder ? 'Folder' : undefined
|
|
420
|
+
let type = alias ? await typeOf(mediaTypes, alias) : undefined
|
|
421
|
+
if (!type && !item.folder && !alias) {
|
|
422
|
+
const choices = await port.typesForExtension(extname(item.path))
|
|
423
|
+
const choice = choices.find((c) => c.matched) ?? choices[0]
|
|
424
|
+
type = choice ? await mediaTypes.byKey(choice.key) : undefined
|
|
425
|
+
}
|
|
426
|
+
if (!type && item.folder && folderType) type = await mediaTypes.byKey(folderType.key)
|
|
427
|
+
if (!type) {
|
|
428
|
+
find(
|
|
429
|
+
item,
|
|
430
|
+
['type'],
|
|
431
|
+
'unknown-type',
|
|
432
|
+
alias
|
|
433
|
+
? `there is no media type "${alias}"`
|
|
434
|
+
: `no media type takes a ${extname(item.path) || 'file without an extension'}`,
|
|
435
|
+
)
|
|
436
|
+
continue
|
|
437
|
+
}
|
|
438
|
+
typeOfItem.set(item, type)
|
|
439
|
+
mediaTypeAlias.set(item, type.alias)
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
const nameOf = (item: SeedItem): Array<{ culture: string | null; name: string }> => {
|
|
443
|
+
const segment = item.path.split('/').at(-1) as string
|
|
444
|
+
const given = item.data.name
|
|
445
|
+
if (typeof given === 'string') return [{ culture: null, name: given }]
|
|
446
|
+
if (isRecord(given))
|
|
447
|
+
return Object.entries(given).flatMap(([culture, name]) => {
|
|
448
|
+
const iso = languages.get(culture.toLowerCase())
|
|
449
|
+
if (!iso) {
|
|
450
|
+
find(
|
|
451
|
+
item,
|
|
452
|
+
['name', culture],
|
|
453
|
+
'language-missing',
|
|
454
|
+
`there is no language "${culture}" here`,
|
|
455
|
+
)
|
|
456
|
+
return []
|
|
457
|
+
}
|
|
458
|
+
usedLanguages.add(iso)
|
|
459
|
+
return [{ culture: iso, name: String(name) }]
|
|
460
|
+
})
|
|
461
|
+
return [{ culture: null, name: item.kind === 'media' ? segment : nameFromSegment(segment) }]
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
for (const item of tree.items) {
|
|
465
|
+
const key = keyOf.get(item) as string
|
|
466
|
+
let type = typeOfItem.get(item)
|
|
467
|
+
if (item.kind !== 'media') {
|
|
468
|
+
if (typeof item.data.type !== 'string') {
|
|
469
|
+
find(item, [], 'no-type', 'an item names its content type: type = "<alias>"')
|
|
470
|
+
continue
|
|
471
|
+
}
|
|
472
|
+
type = await typeOf(documentTypes, item.data.type)
|
|
473
|
+
if (!type) {
|
|
474
|
+
find(item, ['type'], 'unknown-type', `there is no content type "${item.data.type}"`)
|
|
475
|
+
continue
|
|
476
|
+
}
|
|
477
|
+
if (item.kind === 'library' && !type.isElement) {
|
|
478
|
+
find(
|
|
479
|
+
item,
|
|
480
|
+
['type'],
|
|
481
|
+
'not-an-element',
|
|
482
|
+
`"${type.alias}" is not an element type, so it cannot sit in the Library`,
|
|
483
|
+
)
|
|
484
|
+
continue
|
|
485
|
+
}
|
|
486
|
+
if (item.kind === 'content' && type.isElement) {
|
|
487
|
+
find(
|
|
488
|
+
item,
|
|
489
|
+
['type'],
|
|
490
|
+
'element-as-page',
|
|
491
|
+
`"${type.alias}" is an element type: put it in seed/library`,
|
|
492
|
+
)
|
|
493
|
+
continue
|
|
494
|
+
}
|
|
495
|
+
typeOfItem.set(item, type)
|
|
496
|
+
}
|
|
497
|
+
if (!type) continue
|
|
498
|
+
usedTypes.set(type.alias, type.key)
|
|
499
|
+
|
|
500
|
+
// Where it sits: its parent's type has to allow it, as an editor's would.
|
|
501
|
+
if (item.kind !== 'library') {
|
|
502
|
+
const parentItem = item.parent
|
|
503
|
+
? (item.kind === 'media' ? mediaByPath : contentByPath).get(item.parent.toLowerCase())
|
|
504
|
+
: undefined
|
|
505
|
+
const parentType = parentItem ? typeOfItem.get(parentItem) : undefined
|
|
506
|
+
const allowed = parentItem
|
|
507
|
+
? (parentType?.allowedContentTypes.some(
|
|
508
|
+
(a) => normaliseUuid(a.contentTypeKey) === normaliseUuid(type.key),
|
|
509
|
+
) ?? true)
|
|
510
|
+
: type.allowedAsRoot
|
|
511
|
+
if (!allowed)
|
|
512
|
+
find(
|
|
513
|
+
item,
|
|
514
|
+
['type'],
|
|
515
|
+
'not-allowed-here',
|
|
516
|
+
parentItem
|
|
517
|
+
? `"${type.name}" isn't allowed under "${parentType?.name ?? item.parent}"`
|
|
518
|
+
: `"${type.name}" isn't allowed at the top level`,
|
|
519
|
+
)
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
const editors = await editorsOf(item.kind === 'media' ? mediaTypes : documentTypes, type)
|
|
523
|
+
const values: BundleValue[] = []
|
|
524
|
+
const addValues = async (
|
|
525
|
+
table: Record<string, unknown>,
|
|
526
|
+
culture: string | null,
|
|
527
|
+
path: string[],
|
|
528
|
+
) => {
|
|
529
|
+
for (const [alias, raw] of Object.entries(table)) {
|
|
530
|
+
const property = editors.get(alias)
|
|
531
|
+
if (!property) {
|
|
532
|
+
const iso =
|
|
533
|
+
culture === null && isRecord(raw) ? languages.get(alias.toLowerCase()) : undefined
|
|
534
|
+
if (iso) {
|
|
535
|
+
usedLanguages.add(iso)
|
|
536
|
+
await addValues(raw as Record<string, unknown>, iso, [...path, alias])
|
|
537
|
+
continue
|
|
538
|
+
}
|
|
539
|
+
if (culture === null && isRecord(raw) && /^[a-z]{2}(-[a-z]{2,4})?$/i.test(alias)) {
|
|
540
|
+
find(item, [...path, alias], 'language-missing', `there is no language "${alias}" here`)
|
|
541
|
+
continue
|
|
542
|
+
}
|
|
543
|
+
find(
|
|
544
|
+
item,
|
|
545
|
+
[...path, alias],
|
|
546
|
+
'unknown-property',
|
|
547
|
+
`"${type.alias}" has no property "${alias}"`,
|
|
548
|
+
)
|
|
549
|
+
continue
|
|
550
|
+
}
|
|
551
|
+
const converted = await convert(property.editor, raw, key, alias)
|
|
552
|
+
if (!converted.ok) {
|
|
553
|
+
find(item, [...path, alias], 'bad-value', `${alias}: ${converted.message}`)
|
|
554
|
+
continue
|
|
555
|
+
}
|
|
556
|
+
values.push({
|
|
557
|
+
property: alias,
|
|
558
|
+
culture,
|
|
559
|
+
segment: null,
|
|
560
|
+
editor: property.editor,
|
|
561
|
+
value: converted.value,
|
|
562
|
+
})
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
if (item.data.values !== undefined) {
|
|
566
|
+
if (isRecord(item.data.values)) await addValues(item.data.values, null, ['values'])
|
|
567
|
+
else find(item, ['values'], 'bad-values', 'values is a table: [values]')
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
if (item.kind === 'media' && item.bytes) {
|
|
571
|
+
const file = item.path.split('/').at(-1) as string
|
|
572
|
+
const blobKey = `${key.replaceAll('-', '').slice(0, 8)}/${file}`
|
|
573
|
+
const src = `/media/${blobKey}`
|
|
574
|
+
const fileEditor = editors.get('umbracoFile')?.editor
|
|
575
|
+
const focalPoint = isRecord(item.data.focalPoint) ? item.data.focalPoint : null
|
|
576
|
+
const media: Record<string, unknown> = {
|
|
577
|
+
umbracoFile:
|
|
578
|
+
fileEditor === IMAGE_CROPPER_EDITOR_ALIAS ? { src, crops: [], focalPoint } : src,
|
|
579
|
+
umbracoBytes: item.bytes.length,
|
|
580
|
+
umbracoExtension: extname(file).slice(1).toLowerCase(),
|
|
581
|
+
}
|
|
582
|
+
if (fileEditor === IMAGE_CROPPER_EDITOR_ALIAS)
|
|
583
|
+
try {
|
|
584
|
+
const { width, height } = await new Bun.Image(item.bytes).metadata()
|
|
585
|
+
media.umbracoWidth = width
|
|
586
|
+
media.umbracoHeight = height
|
|
587
|
+
} catch {
|
|
588
|
+
find(item, [], 'bad-image', `${file} is not an image this site can read`)
|
|
589
|
+
}
|
|
590
|
+
for (const [alias, value] of Object.entries(media)) {
|
|
591
|
+
const editor = editors.get(alias)?.editor
|
|
592
|
+
if (editor && !values.some((v) => v.property === alias))
|
|
593
|
+
values.push({ property: alias, culture: null, segment: null, editor, value })
|
|
594
|
+
}
|
|
595
|
+
blobs.set(blobKey, item.bytes)
|
|
596
|
+
blobList.push({
|
|
597
|
+
key: blobKey,
|
|
598
|
+
node: key,
|
|
599
|
+
etag: createHash('sha256').update(item.bytes).digest('hex'),
|
|
600
|
+
size: item.bytes.length,
|
|
601
|
+
included: true,
|
|
602
|
+
})
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
const template =
|
|
606
|
+
item.kind !== 'content'
|
|
607
|
+
? null
|
|
608
|
+
: typeof item.data.template === 'string'
|
|
609
|
+
? item.data.template
|
|
610
|
+
: type.defaultComponentKey
|
|
611
|
+
? ((await components.byKey(type.defaultComponentKey))?.alias ?? null)
|
|
612
|
+
: null
|
|
613
|
+
if (template) usedComponents.add(template)
|
|
614
|
+
const published = item.data.published !== false
|
|
615
|
+
const parent =
|
|
616
|
+
item.kind === 'library' || !item.parent
|
|
617
|
+
? null
|
|
618
|
+
: (keyOf.get(
|
|
619
|
+
(item.kind === 'media' ? mediaByPath : contentByPath).get(
|
|
620
|
+
item.parent.toLowerCase(),
|
|
621
|
+
) as SeedItem,
|
|
622
|
+
) ?? null)
|
|
623
|
+
const names = nameOf(item)
|
|
624
|
+
// A renamed file is a new key: say so where a page of that name is already here.
|
|
625
|
+
if (item.kind === 'content') {
|
|
626
|
+
const parentId = parent ? (await nodes.byKey(parent))?.id : SystemNodes.Root
|
|
627
|
+
const name = names[0]?.name ?? ''
|
|
628
|
+
const here =
|
|
629
|
+
parentId === undefined
|
|
630
|
+
? []
|
|
631
|
+
: await nodes.childrenNamed(parentId, [ObjectTypes.Document], name)
|
|
632
|
+
const other = here.find((node) => node.key !== key)
|
|
633
|
+
if (other)
|
|
634
|
+
find(
|
|
635
|
+
item,
|
|
636
|
+
['name'],
|
|
637
|
+
'name-taken',
|
|
638
|
+
`"${name}" is already here as another page (${other.key}). A renamed file is a new page: give it key = "${other.key}" to keep the one that is here`,
|
|
639
|
+
)
|
|
640
|
+
}
|
|
641
|
+
const kind: BundleKind =
|
|
642
|
+
item.kind === 'content' ? 'document' : item.kind === 'library' ? 'element' : 'media'
|
|
643
|
+
bundleNodes.push({
|
|
644
|
+
key,
|
|
645
|
+
kind,
|
|
646
|
+
contentType: { key: type.key, alias: type.alias },
|
|
647
|
+
parent,
|
|
648
|
+
sortOrder: 0,
|
|
649
|
+
template,
|
|
650
|
+
variants: names.map((v) => ({ ...v, segment: null, published })),
|
|
651
|
+
values,
|
|
652
|
+
})
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
// Order among siblings: `order`, then the name.
|
|
656
|
+
const orderOf = (node: BundleNode) => {
|
|
657
|
+
const item = byKey.get(node.key)
|
|
658
|
+
return typeof item?.data.order === 'number' ? item.data.order : Number.MAX_SAFE_INTEGER
|
|
659
|
+
}
|
|
660
|
+
const groups = new Map<string, BundleNode[]>()
|
|
661
|
+
for (const node of bundleNodes) {
|
|
662
|
+
const group = `${node.kind}:${node.parent ?? ''}`
|
|
663
|
+
groups.set(group, [...(groups.get(group) ?? []), node])
|
|
664
|
+
}
|
|
665
|
+
for (const group of groups.values()) {
|
|
666
|
+
group.sort(
|
|
667
|
+
(a, b) =>
|
|
668
|
+
orderOf(a) - orderOf(b) ||
|
|
669
|
+
(a.variants[0]?.name ?? '').localeCompare(b.variants[0]?.name ?? ''),
|
|
670
|
+
)
|
|
671
|
+
for (const [index, node] of group.entries()) node.sortOrder = index
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
if (findings.length > 0) return { findings, blobs, counts }
|
|
675
|
+
|
|
676
|
+
// The seed's identity is its content: a changed seed is a new bundle, which
|
|
677
|
+
// `start --seed` applies and the three-way import takes as a newer one.
|
|
678
|
+
const identity = createHash('sha256')
|
|
679
|
+
for (const item of tree.items) {
|
|
680
|
+
identity.update(`${item.kind}\0${item.path}\0${item.text}\0`)
|
|
681
|
+
if (item.bytes) identity.update(item.bytes)
|
|
682
|
+
}
|
|
683
|
+
const id = uuidV5(`seed:${identity.digest('hex')}`)
|
|
684
|
+
const tally: Partial<Record<BundleKind, number>> = {}
|
|
685
|
+
for (const node of bundleNodes) tally[node.kind] = (tally[node.kind] ?? 0) + 1
|
|
686
|
+
const set: ContentSet = {
|
|
687
|
+
manifest: {
|
|
688
|
+
formatVersion: 1,
|
|
689
|
+
id,
|
|
690
|
+
label: 'Seed',
|
|
691
|
+
createdAt: new Date(0).toISOString(),
|
|
692
|
+
createdBy: 'crombolo seed',
|
|
693
|
+
integrity: '',
|
|
694
|
+
snapshot: 'drafts',
|
|
695
|
+
provenance: {
|
|
696
|
+
siteName: config.siteName,
|
|
697
|
+
schemaVersion: state.version,
|
|
698
|
+
schemaRevision: state.revision,
|
|
699
|
+
schemaHash: state.hash,
|
|
700
|
+
cmsVersion: VERSION,
|
|
701
|
+
},
|
|
702
|
+
selector: {
|
|
703
|
+
roots: bundleNodes.filter((n) => n.parent === null).map((n) => n.key),
|
|
704
|
+
descendants: true,
|
|
705
|
+
asGiven: ['seed/'],
|
|
706
|
+
},
|
|
707
|
+
counts: tally,
|
|
708
|
+
dependencies: {
|
|
709
|
+
carried: bundleNodes.map((n) => n.key),
|
|
710
|
+
expected: [...expected.values()].filter((e) => !byKey.has(e.key)),
|
|
711
|
+
schema: {
|
|
712
|
+
contentTypes: [...usedTypes].map(([alias, key]) => ({ key, alias })),
|
|
713
|
+
components: [...usedComponents],
|
|
714
|
+
languages: [...usedLanguages],
|
|
715
|
+
},
|
|
716
|
+
},
|
|
717
|
+
blobs: blobList,
|
|
718
|
+
},
|
|
719
|
+
nodes: bundleNodes,
|
|
720
|
+
}
|
|
721
|
+
const payload = writeBundle(set, blobs)
|
|
722
|
+
const files = writeBundlePackage(payload, {
|
|
723
|
+
name: 'crombolo-site-seed',
|
|
724
|
+
version: '0.0.0',
|
|
725
|
+
id: 'site-seed',
|
|
726
|
+
label: 'Seed',
|
|
727
|
+
description: `The seed of ${config.siteName}, built by crombolo seed build`,
|
|
728
|
+
private: true,
|
|
729
|
+
peerRange: `>=${VERSION}`,
|
|
730
|
+
})
|
|
731
|
+
return { findings, set, blobs, files, counts }
|
|
732
|
+
}
|