@crombolo/server 0.43.1 → 0.45.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.
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Code installed but not yet running (story 241). Installing a bundle, or the
3
+ * code a bundle carries, changes the site's files; the server only loads code
4
+ * at boot, so the change is live after `crombolo restart`. Each install adds a
5
+ * reason here, and a server that boots clears it, since it now runs that code.
6
+ */
7
+ import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
8
+ import { dirname, join } from 'node:path'
9
+
10
+ const FILE = join('.crombolo', 'restart-pending.json')
11
+
12
+ export interface RestartPending {
13
+ /** When the first change waiting for a restart was made. */
14
+ since: string
15
+ /** What is waiting, one line each, in the order it was installed. */
16
+ reasons: string[]
17
+ }
18
+
19
+ /** What is waiting for a restart on this site, or undefined when nothing is. */
20
+ export function restartPending(siteDir: string): RestartPending | undefined {
21
+ const file = join(siteDir, FILE)
22
+ if (!existsSync(file)) return undefined
23
+ try {
24
+ const read = JSON.parse(readFileSync(file, 'utf8')) as Partial<RestartPending>
25
+ return {
26
+ since: typeof read.since === 'string' ? read.since : new Date(0).toISOString(),
27
+ reasons: Array.isArray(read.reasons)
28
+ ? read.reasons.filter((r): r is string => typeof r === 'string')
29
+ : [],
30
+ }
31
+ } catch {
32
+ return undefined
33
+ }
34
+ }
35
+
36
+ /** Records that installed code waits for a restart. */
37
+ export function markRestartPending(siteDir: string, reason: string): RestartPending {
38
+ const now = restartPending(siteDir)
39
+ const next: RestartPending = {
40
+ since: now?.since ?? new Date().toISOString(),
41
+ reasons: [...(now?.reasons ?? []), reason],
42
+ }
43
+ const file = join(siteDir, FILE)
44
+ mkdirSync(dirname(file), { recursive: true })
45
+ writeFileSync(file, `${JSON.stringify(next, null, 2)}\n`)
46
+ return next
47
+ }
48
+
49
+ /** A server that booted runs the site's code as it is now. */
50
+ export function clearRestartPending(siteDir: string): void {
51
+ rmSync(join(siteDir, FILE), { force: true })
52
+ }
package/src/schema.ts CHANGED
@@ -13,6 +13,7 @@ import {
13
13
  contractsLedgeredAfter,
14
14
  currentSchemaState,
15
15
  type Db,
16
+ ensureFormLibraryTypes,
16
17
  ensureSystemContentTypes,
17
18
  ensureSystemMemberTypes,
18
19
  type Finding,
@@ -127,6 +128,11 @@ export async function syncSchemaDirectory(
127
128
  // sync never applies a snapshot that a later backoffice change has overtaken.
128
129
  return db.locks.withLock(Locks.Schema, async () => {
129
130
  const loaded = loadSchemaDirectory(config.schemaDir, { formTypes: formTypesFor(config) })
131
+ // The types made from forms follow the forms whether or not the files changed.
132
+ // Before the sync, not after: nothing may come between the sync recording a
133
+ // new schema state and its caller moving the node to it, or the poller can
134
+ // see the database ahead of the node and take it out of service.
135
+ await ensureFormLibraryTypes(db, loaded.set.forms ?? [])
130
136
  const report = await syncSchema(
131
137
  db,
132
138
  loaded,
@@ -153,6 +159,7 @@ export async function bootSchema(db: Db, config: CromboloConfig): Promise<Schema
153
159
  await touchServer(db, { nodeId, ...nodeState, role: config.role, cromboloVersion: VERSION })
154
160
  await ensureSystemMemberTypes(db)
155
161
  await ensureSystemContentTypes(db)
162
+ await ensureFormLibraryTypes(db, [])
156
163
  return { nodeState, nodeId, report: undefined, compatibilityMode: false }
157
164
  }
158
165
  const loaded = loadSchemaDirectory(config.schemaDir, { formTypes: formTypesFor(config) })
@@ -41,7 +41,7 @@ import { createMediaPort } from '../adapters/media.ts'
41
41
  import { type CromboloConfig, VERSION } from '../config.ts'
42
42
  import { blockKey, pickKey, SEED_NAMESPACE, seedKey, uuidV5 } from './keys.ts'
43
43
  import { lineOf } from './lines.ts'
44
- import { readSeed, type SeedFinding, type SeedItem } from './read.ts'
44
+ import { kindDir, readSeed, type SeedFinding, type SeedItem } from './read.ts'
45
45
 
46
46
  export interface SeedBuild {
47
47
  findings: SeedFinding[]
@@ -89,7 +89,7 @@ export async function compileSeed(db: Db, config: CromboloConfig): Promise<SeedB
89
89
 
90
90
  const find = (item: SeedItem, path: readonly string[], code: string, message: string) =>
91
91
  findings.push({
92
- file: item.file ?? `seed/${item.kind}/${item.path}`,
92
+ file: item.file ?? `${kindDir(item.kind)}/${item.path}`,
93
93
  line: item.text ? lineOf(item.text, path) : 1,
94
94
  code,
95
95
  message,
@@ -354,7 +354,7 @@ export async function compileSeed(db: Db, config: CromboloConfig): Promise<SeedB
354
354
  const key = await contentKey(raw, why)
355
355
  return key
356
356
  ? { ok: true, value: `umb://document/${key.replaceAll('-', '')}` }
357
- : fail(`"${raw}" is not a page in the seed or on the site`)
357
+ : fail(`"${raw}" is not a page in the content files or on the site`)
358
358
  }
359
359
  case 'Umbraco.MediaPicker3': {
360
360
  if (isRecord(raw) || (Array.isArray(raw) && raw.some(isRecord)))
@@ -363,7 +363,7 @@ export async function compileSeed(db: Db, config: CromboloConfig): Promise<SeedB
363
363
  for (const ref of list(raw)) {
364
364
  if (typeof ref !== 'string') return fail('a media picker takes media paths')
365
365
  const media = await mediaRef(ref, why)
366
- if (!media) return fail(`"${ref}" is not media in the seed or on the site`)
366
+ if (!media) return fail(`"${ref}" is not media in the content files or on the site`)
367
367
  picks.push({
368
368
  key: pickKey(media.key),
369
369
  mediaKey: media.key,
@@ -379,7 +379,8 @@ export async function compileSeed(db: Db, config: CromboloConfig): Promise<SeedB
379
379
  for (const ref of list(raw)) {
380
380
  if (typeof ref !== 'string') return fail('an element picker takes Library paths')
381
381
  const key = await libraryKey(ref, why)
382
- if (!key) return fail(`"${ref}" is not a Library element in the seed or on the site`)
382
+ if (!key)
383
+ return fail(`"${ref}" is not a Library element in the content files or on the site`)
383
384
  keys.push(key)
384
385
  }
385
386
  return { ok: true, value: keys }
@@ -390,7 +391,7 @@ export async function compileSeed(db: Db, config: CromboloConfig): Promise<SeedB
390
391
  const link = typeof entry === 'string' ? { path: entry } : entry
391
392
  if (!isRecord(link))
392
393
  return fail('a link is a path, a URL, or { path | url, name, target }')
393
- // A link as the editor stores it, as `seed export` writes one.
394
+ // A link as the editor stores it, as `bundle export --source` writes one.
394
395
  if ('unique' in link || 'type' in link) {
395
396
  links.push(link)
396
397
  continue
@@ -415,7 +416,8 @@ export async function compileSeed(db: Db, config: CromboloConfig): Promise<SeedB
415
416
  if (!path) return fail('a link needs a path or a url')
416
417
  const media = mediaByPath.get(trimPath(path))
417
418
  const key = media ? keyOf.get(media) : await contentKey(path, why)
418
- if (!key) return fail(`"${path}" is not a page or media in the seed or on the site`)
419
+ if (!key)
420
+ return fail(`"${path}" is not a page or media in the content files or on the site`)
419
421
  links.push({
420
422
  unique: key,
421
423
  type: media ? 'media' : 'document',
@@ -519,7 +521,7 @@ export async function compileSeed(db: Db, config: CromboloConfig): Promise<SeedB
519
521
  item,
520
522
  ['type'],
521
523
  'element-as-page',
522
- `"${type.alias}" is an element type: put it in seed/library`,
524
+ `"${type.alias}" is an element type: put it in content/library`,
523
525
  )
524
526
  continue
525
527
  }
@@ -708,8 +710,8 @@ export async function compileSeed(db: Db, config: CromboloConfig): Promise<SeedB
708
710
 
709
711
  if (findings.length > 0) return { findings, blobs, counts }
710
712
 
711
- // The seed's identity is its content: a changed seed is a new bundle, which
712
- // `start --seed` applies and the three-way import takes as a newer one.
713
+ // Its identity is its content: changed files are a new bundle, which
714
+ // `start --bundle .` applies and the three-way import takes as a newer one.
713
715
  const identity = createHash('sha256')
714
716
  for (const item of tree.items) {
715
717
  identity.update(`${item.kind}\0${item.path}\0${item.text}\0`)
@@ -722,9 +724,9 @@ export async function compileSeed(db: Db, config: CromboloConfig): Promise<SeedB
722
724
  manifest: {
723
725
  formatVersion: BUNDLE_FORMAT_VERSION,
724
726
  id,
725
- label: 'Seed',
727
+ label: 'Content files',
726
728
  createdAt: new Date(0).toISOString(),
727
- createdBy: 'crombolo seed',
729
+ createdBy: 'crombolo bundle apply',
728
730
  integrity: '',
729
731
  snapshot: 'drafts',
730
732
  provenance: {
@@ -737,7 +739,7 @@ export async function compileSeed(db: Db, config: CromboloConfig): Promise<SeedB
737
739
  selector: {
738
740
  roots: bundleNodes.filter((n) => n.parent === null).map((n) => n.key),
739
741
  descendants: true,
740
- asGiven: ['seed/'],
742
+ asGiven: ['content/'],
741
743
  },
742
744
  counts: tally,
743
745
  dependencies: {
@@ -758,8 +760,8 @@ export async function compileSeed(db: Db, config: CromboloConfig): Promise<SeedB
758
760
  name: 'crombolo-site-seed',
759
761
  version: '0.0.0',
760
762
  id: 'site-seed',
761
- label: 'Seed',
762
- description: `The seed of ${config.siteName}, built by crombolo seed build`,
763
+ label: 'Content files',
764
+ description: `The content files of ${config.siteName}, built by crombolo bundle build`,
763
765
  private: true,
764
766
  peerRange: `>=${VERSION}`,
765
767
  })
@@ -1,9 +1,10 @@
1
1
  /**
2
- * Content already in a site, written out as seed files (stories 163, 164), so
3
- * an existing site can move to seeds. Pages go to `seed/content/`, Library
4
- * elements to `seed/library/`, and media, files and all, to `seed/media/`.
2
+ * Content already in a site, written out as content files (stories 163, 164,
3
+ * 167), so an existing site can keep its content as files. Pages go to
4
+ * `content/pages/`, Library elements to `content/library/`, and media, files and
5
+ * all, to `content/media/`.
5
6
  *
6
- * Values are written in the shorthand `seed apply` reads back — rich text as
7
+ * Values are written in the shorthand `bundle apply` reads back — rich text as
7
8
  * `{ html }`, pickers and links as paths to what the export carries, blocks as
8
9
  * `{ type, key, values }` — and in their editor's stored shape where there is
9
10
  * none. A node keeps its key in its file wherever it is not the one its path
@@ -23,6 +24,7 @@ import {
23
24
  } from '@crombolo/data'
24
25
  import type { MediaFileStore } from '../media-files.ts'
25
26
  import { pickKey, seedKey } from './keys.ts'
27
+ import { CONTENT_FILES_DIR, KIND_DIRS, kindDir, type SeedKind } from './read.ts'
26
28
 
27
29
  /** A URL segment from a name: `About us` → `about-us`. */
28
30
  export const segmentOf = (name: string) =>
@@ -323,18 +325,23 @@ export async function exportSeed(
323
325
 
324
326
  // --------------------------------------------------------- the files
325
327
  const written: string[] = []
326
- const seedRoot = resolve(siteDir, 'seed')
328
+ const seedRoot = resolve(siteDir, CONTENT_FILES_DIR)
329
+ // `<kind>/<path>` to its file: pages under `content/pages`, and so on.
330
+ const placed = (relative: string) => {
331
+ const [kind, ...rest] = relative.split('/')
332
+ return `${KIND_DIRS[kind as SeedKind] ?? kind}/${rest.join('/')}`
333
+ }
327
334
  const under = (relative: string) => {
328
- const file = resolve(seedRoot, relative)
335
+ const file = resolve(seedRoot, placed(relative))
329
336
  if (!file.startsWith(seedRoot + sep))
330
- throw new Error(`Refusing to write ${relative} outside the seed directory.`)
337
+ throw new Error(`Refusing to write ${relative} outside the content files.`)
331
338
  return file
332
339
  }
333
340
  const write = (relative: string, text: string) => {
334
341
  const file = under(relative)
335
342
  mkdirSync(join(file, '..'), { recursive: true })
336
343
  writeFileSync(file, text)
337
- written.push(`seed/${relative}`)
344
+ written.push(`${CONTENT_FILES_DIR}/${placed(relative)}`)
338
345
  }
339
346
  const valueLines = async (
340
347
  values: ReadonlyArray<{
@@ -386,7 +393,7 @@ export async function exportSeed(
386
393
  const file = under(`media/${path}`)
387
394
  mkdirSync(join(file, '..'), { recursive: true })
388
395
  writeFileSync(file, await stored.bytes())
389
- written.push(`seed/media/${path}`)
396
+ written.push(`${kindDir('media')}/${path}`)
390
397
  }
391
398
  // A sidecar only for what the file cannot say.
392
399
  if (normaliseUuid(node.key) !== derived) lines.push(`key = ${JSON.stringify(node.key)}`)
package/src/seed/read.ts CHANGED
@@ -1,15 +1,31 @@
1
1
  /**
2
- * Reading a site's `seed/` directory (story 163) into items: the content tree
3
- * from `seed/content/`, Library elements from `seed/library/`, and media files
4
- * and their folders from `seed/media/`. A content item is a TOML file named for
5
- * its URL segment, with its children in a folder of the same name; a media file
6
- * may have a sidecar, `photo.jpg.toml`.
2
+ * Reading a site's content files (story 163) into items: pages from
3
+ * `content/pages/`, Library elements from `content/library/`, and media files
4
+ * and their folders from `content/media/` (story 167; once `seed/`). A page is a
5
+ * TOML file named for its URL segment, with its children in a folder of the same
6
+ * name; a media file may have a sidecar, `photo.jpg.toml`.
7
7
  */
8
8
  import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs'
9
9
  import { join, relative } from 'node:path'
10
10
 
11
11
  export type SeedKind = 'content' | 'library' | 'media'
12
12
 
13
+ /** The folder a site's content files are in. */
14
+ export const CONTENT_FILES_DIR = 'content'
15
+
16
+ /** Each kind's folder under it. A page's kind is `content`, as the keys derived from it say. */
17
+ export const KIND_DIRS: Readonly<Record<SeedKind, string>> = {
18
+ content: 'pages',
19
+ library: 'library',
20
+ media: 'media',
21
+ }
22
+
23
+ /** A kind's folder, relative to the site: `content/pages`. */
24
+ export const kindDir = (kind: SeedKind) => `${CONTENT_FILES_DIR}/${KIND_DIRS[kind]}`
25
+
26
+ /** What to run when a site still has its content files where they were before story 167. */
27
+ export const MOVE_FROM_SEED = 'git mv seed content && git mv content/content content/pages'
28
+
13
29
  export interface SeedItem {
14
30
  kind: SeedKind
15
31
  /** Its path in the tree, by segment: `about/team`, `logos/mark.png`. */
@@ -22,7 +38,7 @@ export interface SeedItem {
22
38
  data: Record<string, unknown>
23
39
  /** A media file's bytes. */
24
40
  bytes?: Uint8Array
25
- /** A media folder: a directory under `seed/media/`. */
41
+ /** A media folder: a directory under `content/media/`. */
26
42
  folder?: boolean
27
43
  }
28
44
 
@@ -57,10 +73,23 @@ function syntaxLine(text: string): number {
57
73
  }
58
74
 
59
75
  export function readSeed(siteDir: string): SeedTree {
60
- const root = join(siteDir, 'seed')
76
+ const root = join(siteDir, CONTENT_FILES_DIR)
61
77
  const items: SeedItem[] = []
62
78
  const findings: SeedFinding[] = []
63
79
  const shown = (path: string) => relative(siteDir, path)
80
+ // Content files where they were before story 167 are not read, and say where to go.
81
+ if (existsSync(join(siteDir, 'seed')) && !existsSync(root))
82
+ return {
83
+ items,
84
+ findings: [
85
+ {
86
+ file: 'seed',
87
+ line: 1,
88
+ code: 'moved',
89
+ message: `content files now live in content/pages, content/library and content/media: ${MOVE_FROM_SEED}`,
90
+ },
91
+ ],
92
+ }
64
93
 
65
94
  const parse = (path: string): { text: string; data: Record<string, unknown> } | undefined => {
66
95
  const text = readFileSync(path, 'utf8')
@@ -91,7 +120,7 @@ export function readSeed(siteDir: string): SeedTree {
91
120
  file: shown(path),
92
121
  line: 1,
93
122
  code: 'library-folder',
94
- message: 'Library elements sit at the top of seed/library: folders are not read yet',
123
+ message: 'Library elements sit at the top of content/library: folders are not read yet',
95
124
  })
96
125
  continue
97
126
  }
@@ -165,11 +194,12 @@ export function readSeed(siteDir: string): SeedTree {
165
194
  }
166
195
  }
167
196
 
168
- content('content', join(root, 'content'), null)
169
- content('library', join(root, 'library'), null)
170
- media(join(root, 'media'), null)
197
+ content('content', join(root, KIND_DIRS.content), null)
198
+ content('library', join(root, KIND_DIRS.library), null)
199
+ media(join(root, KIND_DIRS.media), null)
171
200
  return { items, findings }
172
201
  }
173
202
 
174
- /** Whether a site has a seed at all. */
175
- export const hasSeed = (siteDir: string) => existsSync(join(siteDir, 'seed'))
203
+ /** Whether a site has content files at all, where they are or where they were. */
204
+ export const hasSeed = (siteDir: string) =>
205
+ existsSync(join(siteDir, CONTENT_FILES_DIR)) || existsSync(join(siteDir, 'seed'))