@crombolo/cli 0.14.4 → 0.16.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.
Files changed (104) hide show
  1. package/bin/crombolo.ts +4 -3166
  2. package/oclif.manifest.json +8077 -0
  3. package/package.json +79 -8
  4. package/src/base.ts +134 -0
  5. package/src/classify.ts +119 -0
  6. package/src/clispec.ts +163 -0
  7. package/src/commands/admin/reset-password.ts +53 -0
  8. package/src/commands/api-client/rotate.ts +57 -0
  9. package/src/commands/assets/index.ts +6 -0
  10. package/src/commands/assets/list.ts +27 -0
  11. package/src/commands/assets/new.ts +42 -0
  12. package/src/commands/bundle/purge.ts +71 -0
  13. package/src/commands/cache/rebuild.ts +56 -0
  14. package/src/commands/components/check.ts +58 -0
  15. package/src/commands/components/index.ts +6 -0
  16. package/src/commands/components/list.ts +40 -0
  17. package/src/commands/components/new.ts +49 -0
  18. package/src/commands/content/check.ts +102 -0
  19. package/src/commands/content/export.ts +116 -0
  20. package/src/commands/content/import.ts +109 -0
  21. package/src/commands/content/publish.ts +21 -0
  22. package/src/commands/content/revert.ts +137 -0
  23. package/src/commands/content/runs.ts +62 -0
  24. package/src/commands/content/unpublish.ts +21 -0
  25. package/src/commands/dictionary/export.ts +49 -0
  26. package/src/commands/dictionary/import.ts +46 -0
  27. package/src/commands/domains/apply.ts +23 -0
  28. package/src/commands/domains/clear.ts +27 -0
  29. package/src/commands/domains/index.ts +6 -0
  30. package/src/commands/domains/list.ts +86 -0
  31. package/src/commands/domains/set.ts +55 -0
  32. package/src/commands/domains/undo.ts +29 -0
  33. package/src/commands/export.ts +100 -0
  34. package/src/commands/generate.ts +33 -0
  35. package/src/commands/health.ts +90 -0
  36. package/src/commands/import/index.ts +175 -0
  37. package/src/commands/import/umbraco/apply.ts +128 -0
  38. package/src/commands/import/umbraco/report.ts +56 -0
  39. package/src/commands/index/list.ts +45 -0
  40. package/src/commands/index/rebuild.ts +66 -0
  41. package/src/commands/init.ts +76 -0
  42. package/src/commands/login.ts +38 -0
  43. package/src/commands/logout.ts +36 -0
  44. package/src/commands/maintenance/pause.ts +29 -0
  45. package/src/commands/maintenance/resume.ts +20 -0
  46. package/src/commands/maintenance/status.ts +34 -0
  47. package/src/commands/media/export.ts +91 -0
  48. package/src/commands/media/fetch.ts +62 -0
  49. package/src/commands/media/import.ts +175 -0
  50. package/src/commands/pull.ts +210 -0
  51. package/src/commands/push.ts +108 -0
  52. package/src/commands/recycle-bin/empty.ts +157 -0
  53. package/src/commands/remote/add.ts +78 -0
  54. package/src/commands/remote/job.ts +53 -0
  55. package/src/commands/remote/list.ts +41 -0
  56. package/src/commands/remote/remove.ts +26 -0
  57. package/src/commands/schema.ts +32 -0
  58. package/src/commands/start.ts +64 -0
  59. package/src/commands/status.ts +103 -0
  60. package/src/commands/types/add-property.ts +116 -0
  61. package/src/commands/types/check.ts +88 -0
  62. package/src/commands/types/export.ts +32 -0
  63. package/src/commands/types/new.ts +116 -0
  64. package/src/commands/types/purge.ts +65 -0
  65. package/src/commands/types/rewrite.ts +30 -0
  66. package/src/commands/types/sync.ts +66 -0
  67. package/src/commands/upgrade/check.ts +69 -0
  68. package/src/commands/upgrade/index.ts +120 -0
  69. package/src/commands/upgrade/ledger.ts +55 -0
  70. package/src/commands/users/disable.ts +54 -0
  71. package/src/commands/users/enable.ts +54 -0
  72. package/src/commands/users/invite.ts +82 -0
  73. package/src/commands/users/list.ts +62 -0
  74. package/src/commands/users/reset-2fa.ts +48 -0
  75. package/src/commands/users/reset-password.ts +58 -0
  76. package/src/commands/users/unlock.ts +54 -0
  77. package/src/commands/whoami.ts +41 -0
  78. package/src/errors.ts +90 -0
  79. package/src/load.ts +59 -0
  80. package/src/main.ts +41 -0
  81. package/src/manifest.ts +30 -0
  82. package/src/plugin.ts +38 -0
  83. package/src/reference.ts +118 -0
  84. package/src/remote/client.ts +25 -11
  85. package/src/remote/remotes-file.ts +3 -2
  86. package/src/remote/transfer.ts +27 -11
  87. package/src/support/components.ts +7 -0
  88. package/src/support/describe.ts +179 -0
  89. package/src/support/domains.ts +89 -0
  90. package/src/support/flags.ts +157 -0
  91. package/src/support/media-tree.ts +72 -0
  92. package/src/support/people.ts +134 -0
  93. package/src/support/prompt.ts +126 -0
  94. package/src/support/publish.ts +142 -0
  95. package/src/support/remote.ts +57 -0
  96. package/src/support/serve.ts +460 -0
  97. package/src/support/site.ts +26 -0
  98. package/src/support/types.ts +112 -0
  99. package/src/support/umbraco.ts +86 -0
  100. package/templates/basic/bundle/content/bundle.json +1 -1
  101. package/templates/basic/bundle/content/nodes/36955f6e-d110-469e-b280-72096eccd148.json +1 -1
  102. package/templates/basic/bundle/package.json +1 -1
  103. package/templates/demo/harbourstone/bundle/package.json +1 -1
  104. package/templates/starter/bundle/package.json +1 -1
package/bin/crombolo.ts CHANGED
@@ -1,3171 +1,9 @@
1
1
  #!/usr/bin/env bun
2
2
  /**
3
3
  * The crombolo CLI. Runs from a site directory and reads `crombolo.config.ts`
4
- * there when present.
5
- *
6
- * crombolo start boot the site; resets and prints the admin password
7
- * crombolo start --keep-admin boot without touching the admin password
8
- * crombolo start --bundle <pkg> apply a bundle's content, once, then serve
9
- * crombolo start --from <file> in an empty folder: a working site from a whole-site file
10
- * crombolo init scaffold a site in the current directory
11
- * crombolo init --template <id> …from a starter template, content included
12
- * crombolo status what this environment is, without starting it
13
- * crombolo admin reset-password reset the administrator's password and print it
14
- * crombolo schema check validate schema/ and report what a sync would do
15
- * crombolo schema check --static validate the files only, no database
16
- * crombolo schema sync apply schema/ to the database
17
- * crombolo schema export write the database's definitions as schema/ files
18
- * crombolo schema new scaffold a type file, and the view it points at
19
- * crombolo schema add-property append a property to a type, and apply it
20
- * crombolo generate TypeScript for the content types in schema/
21
- * crombolo components check compile every component before a visitor does
22
- * crombolo assets list the stylesheets and scripts beside them
23
- *
24
- * crombolo export --site write the whole site to one file, to move it to another machine or host
25
- * crombolo content export write a subtree as a private bundle package, to move to another environment
26
- * crombolo content check read-only: what installing a bundle's content here would do
27
- * crombolo content import apply a bundle's content, in one transaction, and record the run
28
- * crombolo content runs the imports applied here
29
- * crombolo content revert put an import back, restoring what the site served
30
- *
31
- * crombolo domains the hostnames this site answers on
32
- * crombolo domains set write domains.toml and apply it
33
- *
34
- * crombolo import <file> replace this site with a whole-site file; says what first
35
- * crombolo remote add|list|remove the remotes this site pulls from and pushes to (remotes.toml)
36
- * crombolo login|logout|whoami sign in to a remote with a code you approve in its back office
37
- * crombolo pull [<remote>] a copy of a remote environment: started here, or replacing this site
38
- * crombolo push [<remote>] replace a remote environment with this site
39
- * crombolo import umbraco report read-only: what importing an Umbraco backup would and would not bring
40
- * crombolo import umbraco apply write a crombolo site from an Umbraco backup
41
- *
42
- * crombolo upgrade --plan print the DDL each pending framework migration would run
43
- * crombolo upgrade back up, apply pending framework migrations, record them
44
- * crombolo upgrade ledger print migration_history
45
- * crombolo bundle purge <id> back up, drop a bundle's tables and clear its state
46
- *
47
- * crombolo maintenance pause refuse editors' saves cluster-wide; readers unaffected
48
- * crombolo maintenance resume let editors save again
4
+ * there when present. Each command is a class under `src/commands/`; `crombolo
5
+ * --help` lists them, and `crombolo schema` describes them for a machine.
49
6
  */
50
- import { existsSync } from 'node:fs'
51
- import { mkdir, writeFile } from 'node:fs/promises'
52
- import { hostname } from 'node:os'
53
- import { basename, join, relative, resolve } from 'node:path'
54
- import { AuthStore, resetAdminPassword } from '@crombolo/auth'
55
- import { ObjectTypes } from '@crombolo/core'
56
- import {
57
- ContentTypeRepository,
58
- connect,
59
- currentSchemaState,
60
- DictionaryRepository,
61
- DocumentRepository,
62
- DomainRepository,
63
- describeRefFailure,
64
- type Finding,
65
- MigrationStepError,
66
- NodeRepository,
67
- readLedger,
68
- readSetupState,
69
- recordInLedger,
70
- resolveNodeRef,
71
- TransferRunRepository,
72
- UpgradeStateError,
73
- writeReport,
74
- } from '@crombolo/data'
75
- import {
76
- addProperty,
77
- allProperties,
78
- BUILTIN_DATA_TYPES,
79
- exportSchemaSet,
80
- fileNameFor,
81
- generateTypes,
82
- loadSchemaDirectory,
83
- loadValueMigrations,
84
- newType,
85
- type SchemaContentType,
86
- type SchemaTypeKind,
87
- type SyncReport,
88
- TYPE_KIND_FILES,
89
- type ValueMigration,
90
- validateSchemaSet,
91
- viewScaffold,
92
- writeContentType,
93
- writeSchemaFiles,
94
- } from '@crombolo/schema'
95
- import {
96
- assertPortAvailable,
97
- assetDirs,
98
- assetScaffold,
99
- type BootFailure,
100
- backupBefore,
101
- bootFailurePage,
102
- bootRetryDelays,
103
- bootstrapDatabase,
104
- type CromboloConfig,
105
- checkComponents,
106
- componentAliasesIn,
107
- componentFileFor,
108
- componentScaffold,
109
- createServer,
110
- DOMAINS_FILE,
111
- type DomainDeclaration,
112
- describeBootFailure,
113
- describeDatabase,
114
- dictionaryToUdt,
115
- domainsFilePath,
116
- forgetSignIn,
117
- formTypesFor,
118
- importDictionaryUdt,
119
- isSafeAlias,
120
- listAssets,
121
- listComponents,
122
- listeningBodyLimit,
123
- loadConfig,
124
- mediaStoreFor,
125
- osKeychain,
126
- placeBlobs,
127
- readDomainsFile,
128
- readRemoteMedia,
129
- readSignIn,
130
- resolveDomainNode,
131
- resolvePlaceholders,
132
- type ServerHandle,
133
- siteStatus,
134
- syncDomainsFile,
135
- syncSchemaDirectory,
136
- undoDomainsFile,
137
- VERSION,
138
- withoutNode,
139
- writeDictionaryUdt,
140
- writeDomains,
141
- writeDomainsFile,
142
- writeRemoteMedia,
143
- writeUdtAll,
144
- } from '@crombolo/server'
145
- import {
146
- importBundle,
147
- isResolution,
148
- isRevertResolution,
149
- type NodeResolutions,
150
- packageNameFor,
151
- parseResolveFlags,
152
- propertyHandle,
153
- RESOLUTIONS,
154
- RESOLUTIONS_FILE,
155
- REVERT_RESOLUTIONS,
156
- type Resolution,
157
- type RevertResolution,
158
- resolutionsFor,
159
- revertResolutionsFor,
160
- revertRun,
161
- type TransferCheck,
162
- writeResolutions,
163
- } from '@crombolo/transfer'
164
- import * as ops from '../src/operations/commands.ts'
165
- import {
166
- discover,
167
- type Job,
168
- problemOf,
169
- RemoteClient,
170
- RemoteError,
171
- signIn,
172
- TimedOut,
173
- } from '../src/remote/client.ts'
174
- import { addRemote, pickRemote, readRemotes, removeRemote } from '../src/remote/remotes-file.ts'
175
- import {
176
- fetchReferencedMedia,
177
- pullFile,
178
- pushSite,
179
- RefusedBeforeSending,
180
- referencedBlobs,
181
- } from '../src/remote/transfer.ts'
182
- import { findTemplate, listTemplates, scaffoldFiles } from '../src/templates.ts'
7
+ import { main } from '../src/main.ts'
183
8
 
184
- const cwd = process.cwd()
185
- const [command = 'help', ...rest] = Bun.argv.slice(2)
186
- const flags = new Set(rest.filter((arg) => arg.startsWith('--')))
187
- const positional = rest.filter((arg) => !arg.startsWith('--'))
188
-
189
- async function siteConfig(): Promise<CromboloConfig> {
190
- const file = join(cwd, 'crombolo.config.ts')
191
- const overrides = existsSync(file)
192
- ? ((await import(file)).default as Partial<CromboloConfig>)
193
- : {}
194
- return loadConfig(overrides, cwd)
195
- }
196
-
197
- function banner(lines: string[]): void {
198
- const width = Math.max(...lines.map((line) => line.length))
199
- console.log(`┌─${'─'.repeat(width)}─┐`)
200
- for (const line of lines) console.log(`│ ${line.padEnd(width)} │`)
201
- console.log(`└─${'─'.repeat(width)}─┘`)
202
- }
203
-
204
- /** How long a shutdown gets before it is abandoned and the process leaves anyway. */
205
- const SHUTDOWN_MS = 10_000
206
-
207
- /**
208
- * Stops the site on SIGINT and SIGTERM.
209
- *
210
- * Without a handler neither signal does anything at all in a container: `bun` is
211
- * PID 1 there, and the kernel drops a signal PID 1 has registered no handler
212
- * for. That is why Ctrl-C did nothing under `docker compose run` — the keystroke
213
- * reaches the container's own terminal, which raises a SIGINT nothing was
214
- * listening for — and why `docker stop` sat out its ten-second grace and then
215
- * resorted to SIGKILL. Outside a container the default would at least have
216
- * killed the process, so this was invisible there.
217
- *
218
- * `server.close()` is the point of it either way: the job runner, the event
219
- * hubs, the component watcher, the poller and the database all get to stop,
220
- * rather than being taken away mid-write. Connections are closed with the
221
- * listener because the live-update sockets are long-lived, and waiting for a
222
- * socket that is meant to stay open would be waiting for ever.
223
- *
224
- * A second signal, or a close that will not finish, leaves immediately.
225
- */
226
- function stopOnSignal(
227
- listening: { stop: (closeActiveConnections?: boolean) => void },
228
- current: () => ServerHandle | undefined,
229
- ): void {
230
- let stopping = false
231
- const stop = (signal: string) => {
232
- // 130 is what a shell reports for a SIGINT death, and what this was.
233
- if (stopping) process.exit(130)
234
- stopping = true
235
- console.log(`\nStopping (${signal})…`)
236
- setTimeout(() => process.exit(130), SHUTDOWN_MS).unref()
237
- void (async () => {
238
- listening.stop(true)
239
- const server = current()
240
- if (server) await server.close()
241
- process.exit(0)
242
- })()
243
- }
244
- for (const signal of ['SIGINT', 'SIGTERM'] as const) process.on(signal, () => stop(signal))
245
- }
246
-
247
- /**
248
- * `start --from <file>` (story 105): an empty folder in, a working site out —
249
- * scaffolded as `init` scaffolds one, its packages installed, and the file
250
- * imported as a replace so nothing of the scaffold's own starting point stays.
251
- * The new administrator's password is printed here, once.
252
- */
253
- async function startFromFile(from: string, known?: string): Promise<void> {
254
- const file = resolve(cwd, from)
255
- if (!existsSync(file)) {
256
- console.error(`${from} does not exist.`)
257
- process.exit(1)
258
- }
259
- if (existsSync(join(cwd, 'crombolo.config.ts'))) {
260
- console.error(
261
- 'There is a site here already. `crombolo import <file> --replace` replaces it; start --from starts a new one in an empty folder.',
262
- )
263
- process.exit(1)
264
- }
265
- const dir = await ops.unpackBundle(file, cwd)
266
- const manifest = ops.readBundle(dir, cwd).set.manifest
267
- if (manifest.scope !== 'site') {
268
- console.error(`${from} is not a whole-site file: write one with \`crombolo export --site\`.`)
269
- process.exit(1)
270
- }
271
- // A pull already holds the passphrase it sealed the file with.
272
- const sealed = manifest.people !== undefined || manifest.content === 'sealed'
273
- const passphrase = known
274
- ? known
275
- : sealed
276
- ? (process.env[PASSPHRASE_ENV] ??
277
- (process.stdin.isTTY ? await readSecret(' Passphrase for its people: ') : undefined))
278
- : undefined
279
- if (sealed && !passphrase) {
280
- console.error(`${from} is sealed: set ${PASSPHRASE_ENV} to its passphrase.`)
281
- process.exit(1)
282
- }
283
-
284
- for (const scaffolded of scaffoldFiles({
285
- siteName: flagValue('--name') ?? manifest.provenance.siteName,
286
- postgres: flags.has('--postgres'),
287
- })) {
288
- const target = join(cwd, scaffolded.path)
289
- if (existsSync(target)) continue
290
- await mkdir(join(target, '..'), { recursive: true })
291
- if (scaffolded.copyFrom === undefined) await writeFile(target, scaffolded.text ?? '')
292
- else await Bun.write(target, Bun.file(scaffolded.copyFrom))
293
- }
294
- console.log(` scaffolded a site for ${manifest.provenance.siteName || 'the file'}`)
295
- if (!flags.has('--no-install')) {
296
- const install = Bun.spawnSync(['bun', 'install'], { cwd, stdout: 'inherit', stderr: 'inherit' })
297
- if (install.exitCode !== 0) {
298
- console.error('`bun install` failed; run it, then `crombolo import <file> --replace`.')
299
- process.exit(1)
300
- }
301
- }
302
-
303
- const config = await siteConfig()
304
- const { db, seededAdminPassword } = await bootstrapDatabase(config)
305
- await db.close()
306
- const result = await ops.contentImport(config, {
307
- bundle: dir,
308
- passphrase,
309
- replace: 'replace',
310
- allowMissingBlobs: flags.has('--allow-missing-media'),
311
- backupTaken: true,
312
- label: `start --from ${basename(file)}`,
313
- })
314
- for (const problem of result.problems) console.error(` ${problem}`)
315
- if (!result.runId) {
316
- printTransferFindings(result.check)
317
- console.error('\nThe file could not be imported; the scaffolded site is left as it is.')
318
- process.exit(1)
319
- }
320
- console.log(` imported ${result.check.counts.create} item(s), run ${result.runId}`)
321
- const placed = describePlaced(result.blobs)
322
- if (placed) console.log(` media ${placed}`)
323
- if (seededAdminPassword)
324
- banner([
325
- 'Sign in with (shown once)',
326
- ` username ${config.adminLogin}`,
327
- ` password ${seededAdminPassword}`,
328
- ])
329
- // The password above is the one to use: this first start leaves it alone.
330
- flags.add('--keep-admin')
331
- }
332
-
333
- async function start(): Promise<void> {
334
- const from = flagValue('--from')
335
- if (from) await startFromFile(from)
336
- const config = await siteConfig()
337
- // Before the migration and the seed, so a clash with the container stack is
338
- // reported in a second rather than after the database work.
339
- await assertPortAvailable(config.port)
340
- const { listening, server, bundles } = await bootWithRetries(config)
341
- const seededAdminPassword = server.seededAdminPassword
342
-
343
- const lines = ['Crombolo is running', '']
344
- // What this node actually answers, so a split deployment's banner does not
345
- // advertise an address that 404s here.
346
- if (config.role !== 'web')
347
- lines.push(` backoffice ${listening.url}${config.backOfficePath.slice(1)}`)
348
- if (config.role !== 'api') lines.push(` site ${listening.url}`)
349
- if (config.role !== 'all') lines.push(` role ${config.role}`)
350
- lines.push('')
351
-
352
- // A `web` node owns nothing and writes nothing at boot: resetting the password
353
- // here would be a write from the node whose whole point is not to make them,
354
- // and would end the sessions of editors signed in to the api node.
355
- if (config.role === 'web') {
356
- // Nothing to print: this node has no sign-in.
357
- } else if (!config.development) {
358
- // Production never touches an existing administrator at start: a restart an
359
- // attacker forced must not reset, re-enable or sign anyone out.
360
- if (await setupPending(server))
361
- lines.push(
362
- ' Set up your new site at',
363
- ` ${listening.url}${config.backOfficePath.slice(1)}/setup`,
364
- config.setupCode
365
- ? ' with the code in CROMBOLO_SETUP_CODE.'
366
- : ' First set CROMBOLO_SETUP_CODE (16+ characters) and restart.',
367
- '',
368
- )
369
- } else if (flags.has('--keep-admin')) {
370
- if (seededAdminPassword)
371
- lines.push(
372
- ' Sign in with',
373
- ` username ${config.adminLogin}`,
374
- ` password ${seededAdminPassword}`,
375
- '',
376
- )
377
- } else {
378
- const admin = await resetAdminPassword(
379
- new AuthStore(server.db),
380
- config.adminLogin,
381
- config.adminPassword,
382
- )
383
- if (admin) {
384
- lines.push(
385
- ' Sign in with',
386
- ` username ${admin.login}`,
387
- ` password ${admin.password}`,
388
- '',
389
- )
390
- lines.push(
391
- config.adminPassword
392
- ? ' (from CROMBOLO_ADMIN_PASSWORD)'
393
- : ' A fresh password is generated on every start; set',
394
- )
395
- if (!config.adminPassword)
396
- lines.push(' CROMBOLO_ADMIN_PASSWORD to keep one, or pass --keep-admin.')
397
- if (admin.sessionsEnded > 0)
398
- lines.push(` Ended ${admin.sessionsEnded} session(s): the password changed.`)
399
- }
400
- }
401
- const components = server.health().components
402
- lines.push(
403
- ` database ${describeDatabase(config)}`,
404
- ` components ${config.componentsDir}${components.hash ? ` generation ${components.hash.slice(0, 8)}` : ''}`,
405
- )
406
- for (const line of bundles) lines.push(line)
407
- const report = server.schema.report
408
- if (report) lines.push(` schema ${describeSync(report)}`)
409
- if (server.schema.compatibilityMode)
410
- lines.push(' ⚠ database is ahead of schema/: reads only, writes refused with 409')
411
- banner(lines)
412
- if (seededAdminPassword) console.log('Database created and seeded.')
413
- }
414
-
415
- /**
416
- * Listens at once, serving a page that says why the site is not up yet, and
417
- * retries the boot with backoff: a database that comes up after the server is
418
- * picked up without a restart. After the last attempt it exits, so the
419
- * platform's restart policy and alerts take over. docs/operations.md.
420
- */
421
- async function bootWithRetries(config: CromboloConfig) {
422
- const attempts = Math.max(1, Number(Bun.env.CROMBOLO_BOOT_ATTEMPTS ?? 10) || 10)
423
- const delays = bootRetryDelays(attempts)
424
- let failure: BootFailure | undefined
425
- let progress = { attempt: 1, attempts, retryInSeconds: undefined as number | undefined }
426
- const holding = (request: Request) => {
427
- if (new URL(request.url).pathname === '/health')
428
- return Response.json(
429
- { ok: false, starting: true, problem: failure?.title ?? null },
430
- { status: 503 },
431
- )
432
- return new Response(bootFailurePage(failure, progress), {
433
- status: 503,
434
- headers: { 'content-type': 'text/html; charset=utf-8', 'retry-after': '10' },
435
- })
436
- }
437
- const listening = Bun.serve({
438
- port: config.port,
439
- maxRequestBodySize: listeningBodyLimit(config),
440
- fetch: holding,
441
- websocket: { message() {} },
442
- })
443
- let current: ServerHandle | undefined
444
- // Registered the moment it listens, so there is no window, retries included,
445
- // in which it answers requests and cannot be asked to stop.
446
- stopOnSignal(listening, () => current)
447
- for (let attempt = 1; ; attempt++) {
448
- let server: ServerHandle | undefined
449
- try {
450
- server = await createServer(config)
451
- current = server
452
- // After the schema sync inside `createServer`, which is what gives the
453
- // bundle content types to land on, and before anything is served.
454
- const bundles = await importStartupBundles(server, config)
455
- listening.reload(server.serveOptions as never)
456
- return { listening, server, bundles }
457
- } catch (error) {
458
- current = undefined
459
- await server?.close().catch(() => {})
460
- failure = describeBootFailure(error, describeDatabase(config))
461
- const delay = delays[attempt - 1]
462
- console.error(
463
- `crombolo: ${failure.title}. ${failure.detail}\n To fix it: ${failure.remedy}\n (${error instanceof Error ? error.message : String(error)})`,
464
- )
465
- if (delay === undefined || error instanceof ops.CommandError) {
466
- progress = { attempt, attempts, retryInSeconds: undefined }
467
- listening.stop(true)
468
- if (error instanceof ops.CommandError) throw error
469
- throw new ops.CommandError(`Gave up after ${attempt} attempt(s): ${failure.title}.`)
470
- }
471
- progress = { attempt, attempts, retryInSeconds: Math.round(delay / 1000) }
472
- if (attempt === 1) console.error(` ${listening.url} explains this while it retries.`)
473
- console.error(
474
- ` Trying again in ${Math.round(delay / 1000)}s (attempt ${attempt} of ${attempts}).`,
475
- )
476
- await Bun.sleep(delay)
477
- }
478
- }
479
- }
480
-
481
- async function setupPending(server: ServerHandle): Promise<boolean> {
482
- return (await readSetupState(server.db)) === 'pending'
483
- }
484
-
485
- /**
486
- * `start --bundle <dir>`, repeatable: apply a bundle before the site serves
487
- * anything, taking the flags `content import` takes.
488
- *
489
- * Imported once per bundle — a run still standing here means this site already
490
- * has that content — so a restart costs a query and a line in the banner. It
491
- * runs after the schema sync, which is what gives the bundle its content types,
492
- * and before anything is served. A refusal stops the boot: a site missing the
493
- * content the command line asked for is worse than a site that did not come up.
494
- */
495
- async function importStartupBundles(
496
- server: ServerHandle,
497
- config: CromboloConfig,
498
- ): Promise<string[]> {
499
- const targets = flagValues('--bundle')
500
- if (targets.length === 0) return []
501
-
502
- const lines: string[] = []
503
- const db = server.db
504
- let imported = false
505
- for (const target of targets) {
506
- let resolved: ops.ResolvedBundle
507
- try {
508
- resolved = ops.readBundle(target, config.siteDir, await passphraseToOpen(target, config))
509
- } catch (error) {
510
- if (!(error instanceof ops.CommandError)) throw error
511
- console.error(` ${error.message}`)
512
- for (const problem of error.problems) console.error(` ${problem}`)
513
- console.error(`\n${target} cannot be read. The site has not started.`)
514
- process.exit(1)
515
- }
516
- const set = resolved.set
517
- const standing = await new TransferRunRepository(db).appliedFor(set.manifest.id)
518
- if (standing) {
519
- lines.push(` bundle ${set.manifest.id} already imported (run ${standing.id})`)
520
- continue
521
- }
522
-
523
- // Nothing to roll back to on a database this boot created, so the backup
524
- // rule — which on Postgres means refusing without one — applies only to a
525
- // site that already had content.
526
- if (!server.seededAdminPassword) {
527
- const backup = await backupBefore(config, {
528
- reason: 'content-import',
529
- backupTaken: flags.has('--backup-taken'),
530
- })
531
- if (backup.path) console.log(` backup ${backup.path}`)
532
- }
533
-
534
- const prepared = await ops.transferOptions(db, config, resolved, transferInput(target))
535
- for (const problem of prepared.problems) console.error(` ${problem}`)
536
- const started = performance.now()
537
- const result = await importBundle(db, set, {
538
- ...prepared.options,
539
- // The administrator this boot created stands in until the site's own arrive.
540
- adoptUsers: Boolean(server.seededAdminPassword),
541
- publish: flags.has('--publish'),
542
- nodeId: config.nodeId,
543
- label: flagValue('--label'),
544
- package: { name: resolved.package.packageName, version: resolved.package.version },
545
- })
546
- await writeReport(db, result.check.findings, { source: 'transfer', scope: set.manifest.id })
547
- if (!result.runId) {
548
- printTransferFindings(result.check)
549
- console.error(
550
- `\n${target} was refused: ${result.check.outstanding.length} finding(s) outstanding. ` +
551
- 'Nothing was written, and the site has not started.',
552
- )
553
- process.exit(1)
554
- }
555
- const c = result.check.counts
556
- const placed = describePlaced(
557
- resolved.blobs.size > 0
558
- ? await placeBlobs(await mediaStoreFor(config), resolved.blobs, resolved.openBlob)
559
- : null,
560
- )
561
- lines.push(
562
- ` bundle ${set.manifest.id}: ${c.create} created, ${c.update} updated` +
563
- `${result.published.length > 0 ? `, ${result.published.length} published` : ''} (run ${result.runId})`,
564
- )
565
- if (placed) lines.push(` blobs ${placed}`)
566
- const people = result.people.filter((p) => p.kind === 'member' || p.kind === 'user')
567
- if (people.length > 0)
568
- lines.push(
569
- ` people ${people.filter((p) => p.kind === 'member').length} member(s), ${people.filter((p) => p.kind === 'user').length} user(s); their passwords are the ones they had`,
570
- )
571
- for (const finding of result.check.findings.filter((f) => f.code === 'user-adopted'))
572
- lines.push(` admin ${finding.message}`)
573
- for (const failure of result.publishFailures)
574
- console.error(` not published ${failure.key}: ${failure.reason}`)
575
- await recordInLedger(db, {
576
- name: `content import ${set.manifest.id}`,
577
- kind: 'transfer',
578
- durationMs: Math.round(performance.now() - started),
579
- appliedBy: config.nodeId,
580
- note: `run ${result.runId}, ${resolved.package.packageName}@${resolved.package.version} (start --bundle)`,
581
- })
582
- imported = true
583
- }
584
- // The content landed under a cache this process had already constructed.
585
- if (imported) server.cache.invalidate()
586
- return lines
587
- }
588
-
589
- async function init(): Promise<void> {
590
- const requested = flagValue('--template')
591
- if (requested === 'list') {
592
- for (const t of listTemplates()) console.log(` ${t.id.padEnd(20)} ${t.description}`)
593
- console.log('\n (no --template scaffolds the files and no content)')
594
- return
595
- }
596
- const template = requested ? findTemplate(requested) : undefined
597
- if (requested && !template) {
598
- console.error(`No template "${requested}". \`crombolo init --template list\` prints them.`)
599
- process.exit(1)
600
- }
601
-
602
- const files = scaffoldFiles({
603
- siteName: flagValue('--name'),
604
- postgres: flags.has('--postgres'),
605
- template,
606
- })
607
-
608
- for (const file of files) {
609
- const target = join(cwd, file.path)
610
- if (existsSync(target) && !flags.has('--force')) {
611
- console.log(` exists ${file.path}`)
612
- continue
613
- }
614
- await mkdir(join(target, '..'), { recursive: true })
615
- if (file.copyFrom === undefined) await writeFile(target, file.text ?? '')
616
- else await Bun.write(target, Bun.file(file.copyFrom))
617
- console.log(` created ${file.path}`)
618
- }
619
-
620
- console.log(`\nNext:\n bun install\n bun start`)
621
- if (template?.bundle)
622
- console.log(
623
- `\n\`bun start\` imports ${template.bundle} on the first boot of an empty database,\n` +
624
- 'and says so. Nothing happens on later boots; it is one line in package.json.',
625
- )
626
- }
627
-
628
- /**
629
- * `crombolo status`: what this environment resolved to, and what is wrong with
630
- * it, without starting it.
631
- *
632
- * Read-only by construction — it opens the database rather than bootstrapping
633
- * it, so asking a production node how it is never migrates it.
634
- */
635
- async function status(): Promise<void> {
636
- const config = await siteConfig()
637
- const report = await siteStatus(config)
638
-
639
- if (flags.has('--json')) {
640
- console.log(JSON.stringify(report, null, 2))
641
- if (report.problems.length > 0) process.exit(1)
642
- return
643
- }
644
-
645
- const line = (label: string, value: string) => console.log(` ${label.padEnd(10)} ${value}`)
646
- line('site', `${report.site.name}${report.site.dir ? ` ${report.site.dir}` : ''}`)
647
- line('components', `${report.site.components} snapshots in ${report.site.componentsCache}`)
648
- line(
649
- 'database',
650
- `${report.database.description} ${
651
- report.database.reachable
652
- ? report.database.installed
653
- ? 'reachable'
654
- : 'reachable, not installed'
655
- : 'unreachable'
656
- }`,
657
- )
658
- const files = report.schema.files ?? 'none'
659
- const database = report.schema.database
660
- ? `${report.schema.database.version}+${report.schema.database.revision}`
661
- : 'not installed'
662
- line(
663
- 'schema',
664
- `files ${files} database ${database} ${
665
- !report.database.installed
666
- ? 'nothing applied yet'
667
- : report.schema.compatibilityMode
668
- ? 'compatibility mode'
669
- : report.schema.pending
670
- ? 'a deploy is pending'
671
- : 'up to date'
672
- }`,
673
- )
674
- const pending = report.framework.pending
675
- line(
676
- 'framework',
677
- pending.length === 0
678
- ? 'no migration pending'
679
- : // The whole list is a wall of names on a fresh database; `upgrade --plan`
680
- // is where somebody goes when they want them.
681
- pending.length > 3
682
- ? `${pending.length} pending (\`crombolo upgrade --plan\` lists them)`
683
- : `${pending.length} pending: ${pending.join(', ')}`,
684
- )
685
- line(
686
- 'content',
687
- `${report.content.documents} document(s), ${report.content.media} media, ${report.content.elements} element(s)` +
688
- `${report.content.lastImport ? `; last import ${report.content.lastImport}` : ''}`,
689
- )
690
- line(
691
- 'email',
692
- report.email.available
693
- ? (report.email.description ?? 'configured')
694
- : report.email.provider === 'log'
695
- ? 'console only, nothing is sent'
696
- : `none — ${report.email.affects.join(', ')} unavailable`,
697
- )
698
- line(
699
- 'findings',
700
- `${report.findings.blocking} blocking, ${report.findings.person} need a person, ` +
701
- `${report.findings.auto} automatic, ${report.findings.resolved} resolved`,
702
- )
703
- line(
704
- 'versions',
705
- `crombolo ${report.versions.crombolo}${report.versions.backoffice ? `, backoffice ${report.versions.backoffice}` : ''}, bun ${report.versions.bun}`,
706
- )
707
-
708
- for (const problem of report.problems) console.error(`\n ⚠ ${problem}`)
709
- if (report.problems.length > 0) process.exit(1)
710
- }
711
-
712
- async function admin(): Promise<void> {
713
- if (positional[0] !== 'reset-password') return help()
714
- const config = await siteConfig()
715
- // The database, not the server: resetting a password has nothing to do with
716
- // the schema sync, the caches or the port, and should not need them to be
717
- // healthy to work.
718
- const { db } = await bootstrapDatabase(config)
719
- const result = await resetAdminPassword(new AuthStore(db), config.adminLogin, positional[1])
720
- await db.close()
721
- if (!result) {
722
- console.error(`No account '${config.adminLogin}'.`)
723
- process.exit(1)
724
- }
725
- console.log(`${result.login}\n${result.password}`)
726
- }
727
-
728
- function describeSync(report: SyncReport): string {
729
- switch (report.action) {
730
- case 'applied':
731
- case 'would-apply': {
732
- const verb = report.action === 'applied' ? 'applied' : 'would apply'
733
- const c = report.created
734
- const parts = [
735
- `${c.types} type(s)`,
736
- `${c.properties} propert${c.properties === 1 ? 'y' : 'ies'}`,
737
- `${c.dataTypes} data type(s)`,
738
- `${c.languages} language(s)`,
739
- ]
740
- const extra = [
741
- report.retired.length > 0 ? `retired ${report.retired.join(', ')}` : '',
742
- report.retiredTypes.length > 0 ? `retired types ${report.retiredTypes.join(', ')}` : '',
743
- report.revived.length > 0 ? `revived ${report.revived.join(', ')}` : '',
744
- report.assignedKeys.length > 0 ? `${report.assignedKeys.length} key(s) assigned` : '',
745
- ].filter(Boolean)
746
- return `${verb} ${report.state?.version ?? ''}: created ${parts.join(', ')}${extra.length > 0 ? `; ${extra.join('; ')}` : ''}`
747
- }
748
- case 'skipped-same':
749
- return 'up to date'
750
- case 'skipped-older':
751
- return `compatibility mode: ${report.reason ?? ''}`
752
- case 'refused':
753
- return `refused: ${report.reason ?? `${report.problems.length} problem(s)`}`
754
- }
755
- }
756
-
757
- function printProblems(problems: SyncReport['problems']): void {
758
- for (const p of problems)
759
- console.error(` ${p.file}${p.path ? ` (${p.path})` : ''}: ${p.message}`)
760
- }
761
-
762
- async function schema(): Promise<void> {
763
- const config = await siteConfig()
764
- const sub = positional[0]
765
- if (!existsSync(config.schemaDir) && sub !== 'export') {
766
- console.error(`No schema directory at ${relative(cwd, config.schemaDir) || '.'}.`)
767
- process.exit(1)
768
- }
769
- switch (sub) {
770
- case 'check': {
771
- const loaded = loadSchemaDirectory(config.schemaDir, { formTypes: formTypesFor(config) })
772
- const problems = [
773
- ...loaded.problems,
774
- ...validateSchemaSet(loaded.set, {
775
- componentAliases: componentAliasesIn(config.componentsDir),
776
- types: formTypesFor(config),
777
- }),
778
- ]
779
- if (problems.length > 0) {
780
- console.error(`${problems.length} problem(s):`)
781
- printProblems(problems)
782
- process.exit(1)
783
- }
784
- console.log(
785
- `schema ${loaded.set.version}: ${loaded.set.contentTypes.length} content type(s), ${(loaded.set.mediaTypes ?? []).length} media type(s), ${loaded.set.dataTypes.length} data type(s), ${loaded.set.languages.length} language(s) — valid`,
786
- )
787
- if (flags.has('--static')) {
788
- const issues = staticProblems(loaded, await loadValueMigrations(config.schemaDir))
789
- for (const issue of issues) console.error(` ${issue}`)
790
- if (issues.length > 0) process.exit(1)
791
- return
792
- }
793
- // The same check an upgrade runs, over the site's own diff.
794
- positional[0] = 'check'
795
- await upgrade()
796
- return
797
- }
798
- case 'sync': {
799
- const { db } = await bootstrapDatabase(config)
800
- try {
801
- const report = await syncSchemaDirectory(
802
- db,
803
- config,
804
- { nodeId: config.nodeId, revision: config.schemaRevision },
805
- { forceRetireTypes: flags.has('--force-retire-types') },
806
- )
807
- if (report.problems.length > 0) printProblems(report.problems)
808
- console.log(describeSync(report))
809
- if (report.action === 'refused') process.exit(1)
810
- } finally {
811
- await db.close()
812
- }
813
- return
814
- }
815
- case 'purge':
816
- return schemaPurge(config)
817
- case 'new':
818
- return schemaNew(config)
819
- case 'add-property':
820
- return schemaAddProperty(config)
821
- case 'rewrite': {
822
- // Every file in the canonical layout: a reviewable commit when the vocabulary changes.
823
- const loaded = loadSchemaDirectory(config.schemaDir, { formTypes: formTypesFor(config) })
824
- if (loaded.problems.length > 0) {
825
- printProblems(loaded.problems)
826
- process.exit(1)
827
- }
828
- for (const file of writeSchemaFiles(config.schemaDir, loaded.set))
829
- console.log(` wrote ${relative(cwd, file)}`)
830
- return
831
- }
832
- case 'export': {
833
- const { db } = await bootstrapDatabase(config)
834
- try {
835
- const version = existsSync(config.schemaDir)
836
- ? loadSchemaDirectory(config.schemaDir, { formTypes: formTypesFor(config) }).set.version
837
- : '1.0.0'
838
- const set = await exportSchemaSet(db, version)
839
- for (const file of writeSchemaFiles(config.schemaDir, set))
840
- console.log(` wrote ${relative(cwd, file)}`)
841
- } finally {
842
- await db.close()
843
- }
844
- return
845
- }
846
- default:
847
- return help()
848
- }
849
- }
850
-
851
- const TYPE_KINDS: Record<string, SchemaTypeKind> = {
852
- 'content-type': 'document',
853
- 'media-type': 'media',
854
- 'member-type': 'member',
855
- }
856
-
857
- /**
858
- * `schema new <kind> <alias>`: the file a site would otherwise write from
859
- * memory, keys and all, and the view it points at.
860
- */
861
- async function schemaNew(config: CromboloConfig): Promise<void> {
862
- const kindArg = positional[1] ?? ''
863
- const alias = positional[2]
864
- const kind = TYPE_KINDS[kindArg]
865
- if (!kind || !alias) {
866
- console.error(
867
- `schema new needs a kind and an alias: ${Object.keys(TYPE_KINDS).join(', ')}.\n` +
868
- ' crombolo schema new content-type article --at-root',
869
- )
870
- process.exit(1)
871
- }
872
-
873
- const loaded = loadSchemaDirectory(config.schemaDir, { formTypes: formTypesFor(config) })
874
- const existing = [
875
- ...loaded.set.contentTypes,
876
- ...(loaded.set.mediaTypes ?? []),
877
- ...(loaded.set.memberTypes ?? []),
878
- ]
879
- if (existing.some((type) => type.alias === alias)) {
880
- console.error(`There is already a type with the alias "${alias}".`)
881
- process.exit(1)
882
- }
883
-
884
- const element = flags.has('--element')
885
- const type = newType({
886
- alias,
887
- kind,
888
- name: flagValue('--name'),
889
- description: flagValue('--description'),
890
- icon: flagValue('--icon'),
891
- allowAtRoot: flags.has('--at-root'),
892
- element,
893
- template: !flags.has('--no-view'),
894
- // A content type with nothing in it cannot be published, so it starts with
895
- // the one property every page has; the other kinds get what they are given.
896
- tab: kind === 'document' ? (flagValue('--tab') ?? 'Content') : undefined,
897
- })
898
-
899
- const file = join(config.schemaDir, TYPE_KIND_FILES[kind].dir, fileNameFor(alias))
900
- await mkdir(join(file, '..'), { recursive: true })
901
- await writeFile(file, writeContentType(type, kind))
902
- console.log(` created ${relative(cwd, file)}`)
903
-
904
- // A type nothing allows cannot be created anywhere, which is a confusing
905
- // first five minutes: the editor simply never offers it.
906
- if (kind === 'document' && !element && !type.allowAtRoot) {
907
- const parents = loaded.set.contentTypes.filter((t) => t.allowChildren.includes(alias))
908
- if (parents.length === 0)
909
- console.log(
910
- ` note nothing allows a "${alias}" yet — add it to a parent's allow-children, or pass --at-root`,
911
- )
912
- }
913
-
914
- if (type.components.length > 0) {
915
- const view = join(config.componentsDir, `${alias}.tsx`)
916
- if (existsSync(view)) console.log(` exists ${relative(cwd, view)}`)
917
- else {
918
- await mkdir(join(view, '..'), { recursive: true })
919
- await writeFile(view, viewScaffold(type))
920
- console.log(` created ${relative(cwd, view)}`)
921
- }
922
- }
923
- return applySchema(config)
924
- }
925
-
926
- /** `schema add-property <type> <alias> --type <data type>`: the repetitive edit. */
927
- async function schemaAddProperty(config: CromboloConfig): Promise<void> {
928
- const typeAlias = positional[1]
929
- const alias = positional[2]
930
- const dataType = flagValue('--type')
931
- if (!typeAlias || !alias || !dataType) {
932
- console.error(
933
- 'schema add-property needs the type, the property alias and --type <data type>:\n' +
934
- ' crombolo schema add-property article summary --type textarea --mandatory',
935
- )
936
- process.exit(1)
937
- }
938
-
939
- const loaded = loadSchemaDirectory(config.schemaDir, { formTypes: formTypesFor(config) })
940
- if (loaded.problems.length > 0) {
941
- printProblems(loaded.problems)
942
- process.exit(1)
943
- }
944
- const kinds: Array<[SchemaTypeKind, SchemaContentType[]]> = [
945
- ['document', loaded.set.contentTypes],
946
- ['media', loaded.set.mediaTypes ?? []],
947
- ['member', loaded.set.memberTypes ?? []],
948
- ]
949
- const found = kinds
950
- .map(([kind, types]) => ({ kind, type: types.find((t) => t.alias === typeAlias) }))
951
- .find((candidate) => candidate.type)
952
- if (!found?.type) {
953
- console.error(`No type "${typeAlias}" in ${relative(cwd, config.schemaDir)}.`)
954
- process.exit(1)
955
- }
956
-
957
- const known = new Set([
958
- ...BUILTIN_DATA_TYPES.map((d) => d.alias),
959
- ...loaded.set.dataTypes.map((d) => d.alias),
960
- ])
961
- if (!known.has(dataType)) {
962
- console.error(`No data type "${dataType}". Built in: ${[...known].sort().join(', ')}`)
963
- process.exit(1)
964
- }
965
-
966
- const result = addProperty(found.type, {
967
- alias,
968
- type: dataType,
969
- name: flagValue('--name'),
970
- description: flagValue('--description'),
971
- mandatory: flags.has('--mandatory'),
972
- tab: flagValue('--tab'),
973
- })
974
- if (!result.ok) {
975
- console.error(` ${result.reason}`)
976
- process.exit(1)
977
- }
978
-
979
- const file = join(config.schemaDir, TYPE_KIND_FILES[found.kind].dir, fileNameFor(typeAlias))
980
- // The whole file is re-emitted from the model, as `schema rewrite` does, so a
981
- // comment in it does not survive. Said out loud rather than discovered in a
982
- // diff; `--dry-run` prints the file instead of writing it.
983
- const text = writeContentType(result.type, found.kind)
984
- if (flags.has('--dry-run')) {
985
- console.log(text)
986
- return
987
- }
988
- const before = existsSync(file) ? await Bun.file(file).text() : ''
989
- await writeFile(file, text)
990
- console.log(` wrote ${relative(cwd, file)}`)
991
- console.log(
992
- ` ${result.property.alias} ${result.property.name} (${dataType}${result.property.mandatory ? ', mandatory' : ''}) in tab "${result.tab}"`,
993
- )
994
- if (/^\s*#/m.test(before))
995
- console.log(' note the file is rewritten canonically, so its comments are not kept')
996
- return applySchema(config)
997
- }
998
-
999
- /** Applies `schema/` as a boot or a deploy step would, and reports what it did. */
1000
- async function applySchema(config: CromboloConfig): Promise<void> {
1001
- const { db } = await bootstrapDatabase(config)
1002
- try {
1003
- const report = await syncSchemaDirectory(db, config, {
1004
- nodeId: config.nodeId,
1005
- revision: config.schemaRevision,
1006
- })
1007
- if (report.problems.length > 0) printProblems(report.problems)
1008
- console.log(` schema ${describeSync(report)}`)
1009
- if (report.action === 'refused') process.exit(1)
1010
- } finally {
1011
- await db.close()
1012
- }
1013
- }
1014
-
1015
- /** The pull-request rules: keys present, migrations well-formed, nothing the database needs. */
1016
- function staticProblems(
1017
- loaded: ReturnType<typeof loadSchemaDirectory>,
1018
- migrations: ValueMigration[],
1019
- ): string[] {
1020
- const problems: string[] = []
1021
- for (const t of loaded.set.contentTypes) {
1022
- if (!t.key)
1023
- problems.push(
1024
- `${t.alias}: no key — run \`crombolo schema sync\` in development to assign one`,
1025
- )
1026
- for (const p of allProperties(t)) if (!p.key) problems.push(`${t.alias}.${p.alias}: no key`)
1027
- }
1028
- for (const d of loaded.set.dataTypes) if (!d.key) problems.push(`data type ${d.alias}: no key`)
1029
- for (const m of migrations) {
1030
- const type = loaded.set.contentTypes.find((t) => t.alias === m.from.type)
1031
- if (!type) {
1032
- problems.push(`migration ${m.id}: no content type "${m.from.type}"`)
1033
- continue
1034
- }
1035
- for (const target of m.to)
1036
- if (!allProperties(type).some((p) => p.alias === target.property))
1037
- problems.push(
1038
- `migration ${m.id}: "${m.from.type}" has no property "${target.property}" to write to`,
1039
- )
1040
- }
1041
- return problems
1042
- }
1043
-
1044
- async function schemaPurge(config: CromboloConfig): Promise<void> {
1045
- const index = rest.indexOf('--older-than')
1046
- const days = index >= 0 ? Number(rest[index + 1]) : Number.NaN
1047
- if (!Number.isFinite(days) || days < 0) {
1048
- console.error(
1049
- 'schema purge needs --older-than <days>: how long a property must have been retired.',
1050
- )
1051
- process.exit(1)
1052
- }
1053
- const backup = await backupBefore(config, {
1054
- reason: 'purge',
1055
- backupTaken: flags.has('--backup-taken'),
1056
- })
1057
- if (backup.path) console.log(` backup ${backup.path}`)
1058
- const { db } = await bootstrapDatabase(config)
1059
- try {
1060
- const before = new Date(Date.now() - days * 86_400_000)
1061
- const started = performance.now()
1062
- const purged = await new ContentTypeRepository(db).purgeRetired(before)
1063
- for (const p of purged) console.log(` purged ${p.type}.${p.alias}`)
1064
- if (purged.length === 0) console.log(`Nothing retired more than ${days} day(s) ago.`)
1065
- else
1066
- await recordInLedger(db, {
1067
- name: `purge ${purged.map((p) => `${p.type}.${p.alias}`).join(', ')}`,
1068
- kind: 'contract',
1069
- durationMs: Math.round(performance.now() - started),
1070
- appliedBy: config.nodeId,
1071
- note: `--older-than ${days}`,
1072
- })
1073
- } finally {
1074
- await db.close()
1075
- }
1076
- }
1077
-
1078
- async function generate(): Promise<void> {
1079
- const config = await siteConfig()
1080
- const loaded = loadSchemaDirectory(config.schemaDir, { formTypes: formTypesFor(config) })
1081
- const problems = [
1082
- ...loaded.problems,
1083
- ...validateSchemaSet(loaded.set, { types: formTypesFor(config) }),
1084
- ]
1085
- if (problems.length > 0) {
1086
- printProblems(problems)
1087
- process.exit(1)
1088
- }
1089
- const target = join(config.schemaDir, 'content-types.d.ts')
1090
- await writeFile(target, generateTypes(loaded.set))
1091
- console.log(` wrote ${relative(cwd, target)}`)
1092
- }
1093
-
1094
- /** Every value given for a repeatable flag, in the order they appeared. */
1095
- function flagValues(name: string): string[] {
1096
- const out: string[] = []
1097
- for (let i = 0; i < rest.length; i++) {
1098
- if (rest[i] !== name) continue
1099
- const value = rest[i + 1]
1100
- if (value !== undefined && !value.startsWith('--')) out.push(value)
1101
- }
1102
- return out
1103
- }
1104
-
1105
- function flagValue(name: string): string | undefined {
1106
- return flagValues(name)[0]
1107
- }
1108
-
1109
- /** Where a script gives a bundle's passphrase, rather than typing it. */
1110
- const PASSPHRASE_ENV = 'CROMBOLO_BUNDLE_PASSPHRASE'
1111
- const MIN_PASSPHRASE = 12
1112
-
1113
- /** A line typed without echo. */
1114
- async function readSecret(question: string): Promise<string> {
1115
- process.stdout.write(question)
1116
- const stdin = process.stdin
1117
- stdin.setRawMode(true)
1118
- stdin.resume()
1119
- return new Promise((done) => {
1120
- let typed = ''
1121
- const onData = (chunk: Buffer) => {
1122
- for (const char of chunk.toString('utf8')) {
1123
- if (char === '\u0003') {
1124
- process.stdout.write('\n')
1125
- process.exit(130)
1126
- }
1127
- if (char === '\r' || char === '\n') {
1128
- stdin.setRawMode(false)
1129
- stdin.pause()
1130
- stdin.off('data', onData)
1131
- process.stdout.write('\n')
1132
- done(typed)
1133
- return
1134
- }
1135
- if (char === '\u007f' || char === '\b') typed = typed.slice(0, -1)
1136
- else typed += char
1137
- }
1138
- }
1139
- stdin.on('data', onData)
1140
- })
1141
- }
1142
-
1143
- /** The passphrase to open a bundle with, when it is sealed: the environment's, or asked for. */
1144
- async function passphraseToOpen(
1145
- target: string,
1146
- config: CromboloConfig,
1147
- ): Promise<string | undefined> {
1148
- const given = process.env[PASSPHRASE_ENV]
1149
- if (given) return given
1150
- let sealed = false
1151
- try {
1152
- sealed = ops.readBundle(target, config.siteDir).set.sealed === true
1153
- } catch {
1154
- // The command that follows reads it again and says what is wrong with it.
1155
- return undefined
1156
- }
1157
- if (!sealed) return undefined
1158
- if (!process.stdin.isTTY) {
1159
- console.error(
1160
- `${target} is encrypted. Set ${PASSPHRASE_ENV} to the passphrase it was exported with.`,
1161
- )
1162
- process.exit(1)
1163
- }
1164
- return readSecret(' Passphrase for this bundle: ')
1165
- }
1166
-
1167
- /** A new passphrase to seal an export with: the environment's, or asked for twice. */
1168
- async function passphraseToSeal(): Promise<string> {
1169
- const given = process.env[PASSPHRASE_ENV]
1170
- if (given) {
1171
- if (given.length < MIN_PASSPHRASE) {
1172
- console.error(`${PASSPHRASE_ENV} is shorter than ${MIN_PASSPHRASE} characters.`)
1173
- process.exit(1)
1174
- }
1175
- return given
1176
- }
1177
- if (!process.stdin.isTTY) {
1178
- console.error(`An encrypted export needs a passphrase: set ${PASSPHRASE_ENV}.`)
1179
- process.exit(1)
1180
- }
1181
- for (;;) {
1182
- const first = await readSecret(' Passphrase to encrypt this bundle with: ')
1183
- if (first.length < MIN_PASSPHRASE) {
1184
- console.error(` At least ${MIN_PASSPHRASE} characters, please.`)
1185
- continue
1186
- }
1187
- if ((await readSecret(' Again, to confirm: ')) === first) return first
1188
- console.error(' They did not match.')
1189
- }
1190
- }
1191
-
1192
- async function content(): Promise<void> {
1193
- const config = await siteConfig()
1194
- switch (positional[0]) {
1195
- case 'export':
1196
- return contentExport(config)
1197
- case 'check':
1198
- return contentCheck(config)
1199
- case 'import':
1200
- return contentImport(config)
1201
- case 'runs':
1202
- return contentRuns(config)
1203
- case 'publish':
1204
- return contentPublish(config, true)
1205
- case 'unpublish':
1206
- return contentPublish(config, false)
1207
- case 'revert':
1208
- return contentRevert(config)
1209
- default:
1210
- return help()
1211
- }
1212
- }
1213
-
1214
- async function contentRevert(config: CromboloConfig): Promise<void> {
1215
- const runId = positional[1]
1216
- if (!runId) {
1217
- console.error('content revert needs the run id. `crombolo content runs` lists them.')
1218
- process.exit(1)
1219
- }
1220
- const resolutions: Record<string, RevertResolution> = {}
1221
- for (const pair of flagValues('--resolve')) {
1222
- const eq = pair.indexOf('=')
1223
- const choice = eq >= 0 ? pair.slice(eq + 1) : ''
1224
- if (eq < 0 || !isRevertResolution(choice)) {
1225
- console.error(` --resolve wants <key>=${REVERT_RESOLUTIONS.join('|')}, not "${pair}"`)
1226
- process.exit(1)
1227
- }
1228
- resolutions[pair.slice(0, eq)] = choice
1229
- }
1230
- const allFlag = flagValue('--resolve-all')
1231
- if (allFlag !== undefined && !isRevertResolution(allFlag)) {
1232
- console.error(` "${allFlag}" is not one of ${REVERT_RESOLUTIONS.join(', ')}`)
1233
- process.exit(1)
1234
- }
1235
-
1236
- const backup = await backupBefore(config, {
1237
- reason: 'content-revert',
1238
- backupTaken: flags.has('--backup-taken'),
1239
- })
1240
- if (backup.path) console.log(` backup ${backup.path}`)
1241
-
1242
- const { db } = await bootstrapDatabase(config)
1243
- try {
1244
- const started = performance.now()
1245
- const forceIndex = rest.indexOf('--force')
1246
- const result = await revertRun(db, runId, {
1247
- resolutions,
1248
- resolveAll: allFlag as RevertResolution | undefined,
1249
- force: forceIndex >= 0,
1250
- nodeId: config.nodeId,
1251
- backOfficePath: config.backOfficePath,
1252
- })
1253
-
1254
- if (!result.runId) {
1255
- for (const f of result.check.findings) {
1256
- console.error(` [${f.kind.padEnd(8)}] ${f.message}`)
1257
- for (const suggestion of revertResolutionsFor(f.code))
1258
- console.error(` → ${suggestion}`)
1259
- }
1260
- console.error('\nrevert refused. Nothing was changed.')
1261
- process.exit(1)
1262
- }
1263
-
1264
- // The files an install replaced come back from the run's own copies, not
1265
- // from git: these files are editable in the backoffice, so a site need not
1266
- // be a checkout at all.
1267
- const files = await ops.revertSections(db, config, runId)
1268
- for (const file of files.restored) console.log(` restored ${relative(cwd, file)}`)
1269
- for (const file of files.removed)
1270
- console.log(` removed ${relative(cwd, file)} (created by that run)`)
1271
- if (files.schema) console.log(` schema ${files.schema}`)
1272
- if (files.dictionary) console.log(' restored the dictionary')
1273
-
1274
- for (const key of result.restored) console.log(` restored ${key}`)
1275
- for (const key of result.recycled) console.log(` recycled ${key} (created by that run)`)
1276
- for (const entry of result.check.plan.filter((p) => p.action === 'skip'))
1277
- console.log(` left ${entry.name}`)
1278
- await recordInLedger(db, {
1279
- name: `content revert ${runId}`,
1280
- kind: 'revert',
1281
- durationMs: Math.round(performance.now() - started),
1282
- appliedBy: config.nodeId,
1283
- note: `run ${result.runId}`,
1284
- })
1285
- console.log(
1286
- `\nReverted ${runId} as run ${result.runId}. ` +
1287
- `\`crombolo content revert ${result.runId}\` would put it back again.`,
1288
- )
1289
- } finally {
1290
- await db.close()
1291
- }
1292
- }
1293
-
1294
- /**
1295
- * Puts the media files a bundle carries into this environment's store.
1296
- *
1297
- * After the content, because the run is what decides whether any of this
1298
- * happened: a refused import leaves the store alone. The keys are the ones the
1299
- * values already name, so a picked image finds its file wherever media lives
1300
- * here — a disk, a bucket, a container.
1301
- */
1302
- function describePlaced(placed: { placed: number; bytes: number } | null): string | undefined {
1303
- if (!placed) return undefined
1304
- return `${placed.placed} media file(s) placed, ${describeBytes(placed.bytes)}`
1305
- }
1306
-
1307
- /** The flags `check` and `import` share: resolutions, placement, blobs. */
1308
- function transferInput(bundle: string): ops.TransferInput {
1309
- const allFlag = flagValue('--resolve-all')
1310
- if (allFlag !== undefined && !isResolution(allFlag)) {
1311
- console.error(` "${allFlag}" is not one of ${RESOLUTIONS.join(', ')}`)
1312
- process.exit(1)
1313
- }
1314
- return {
1315
- bundle,
1316
- under: flagValue('--under'),
1317
- resolutions: parseResolutions(),
1318
- resolveAll: allFlag as Resolution | undefined,
1319
- allowMissingBlobs: flags.has('--allow-missing-blobs'),
1320
- publish: flags.has('--publish'),
1321
- // What a bundle carries is applied by default — that is the point of it
1322
- // carrying anything — so these decline a half, for someone who wants the
1323
- // content without the structure that came with it.
1324
- withoutSchema: flags.has('--no-schema'),
1325
- withoutFiles: flags.has('--no-files'),
1326
- replaceFiles: flags.has('--replace-files'),
1327
- }
1328
- }
1329
-
1330
- /** What the carried sections would do, or did. */
1331
- function printSections(plan: ops.SectionPlan | undefined): void {
1332
- if (!plan) return
1333
- console.log(` carries ${plan.sections.join(', ')}`)
1334
- const counts = new Map<string, number>()
1335
- for (const file of plan.files) counts.set(file.action, (counts.get(file.action) ?? 0) + 1)
1336
- const summary = [...counts].map(([action, n]) => `${n} ${action}`).join(', ')
1337
- if (summary) console.log(` files ${summary}`)
1338
- for (const file of plan.files)
1339
- if (file.action === 'overwrite') console.log(` overwrites ${relative(cwd, file.target)}`)
1340
- if (plan.dictionaryItems > 0) console.log(` dict ${plan.dictionaryItems} file(s)`)
1341
- if (plan.schema) {
1342
- console.log(` schema ${plan.schema.classification}`)
1343
- for (const finding of plan.schema.findings) console.log(` ${finding}`)
1344
- }
1345
- }
1346
-
1347
- /** Bytes as somebody reads them, since a bundle of media is measured in them. */
1348
- function describeBytes(bytes: number): string {
1349
- if (bytes < 1024) return `${bytes} B`
1350
- const units = ['KB', 'MB', 'GB']
1351
- let value = bytes / 1024
1352
- let unit = 0
1353
- while (value >= 1024 && unit < units.length - 1) {
1354
- value /= 1024
1355
- unit += 1
1356
- }
1357
- return `${value < 10 ? value.toFixed(1) : Math.round(value)} ${units[unit]}`
1358
- }
1359
-
1360
- function describeBundle(bundle: ops.BundleSummary): void {
1361
- console.log(
1362
- ` bundle ${bundle.id} from ${bundle.from || 'elsewhere'} ` +
1363
- `(schema ${bundle.schemaVersion}+${bundle.schemaRevision}, ` +
1364
- `${bundle.snapshot} snapshot, ${bundle.nodes} node(s))`,
1365
- )
1366
- if (bundle.blobsCarried > 0)
1367
- console.log(
1368
- ` blobs ${bundle.blobsCarried} media file(s) carried, ${describeBytes(bundle.blobBytes)}`,
1369
- )
1370
- }
1371
-
1372
- async function contentImport(config: CromboloConfig): Promise<void> {
1373
- const dir = positional[1]
1374
- if (!dir) {
1375
- console.error(
1376
- 'content import needs a bundle: a package directory, or the name of a bundle this site depends on.',
1377
- )
1378
- process.exit(1)
1379
- }
1380
- const result = await ops.contentImport(config, {
1381
- ...transferInput(dir),
1382
- passphrase: await passphraseToOpen(dir, config),
1383
- publish: flags.has('--publish'),
1384
- label: flagValue('--label'),
1385
- backupTaken: flags.has('--backup-taken'),
1386
- })
1387
- for (const problem of result.problems) console.error(` ${problem}`)
1388
- if (result.backup.path) console.log(` backup ${result.backup.path}`)
1389
- describeBundle(result.bundle)
1390
-
1391
- if (!result.runId) {
1392
- printTransferFindings(result.check)
1393
- // Staged, then applied in one transaction: a refusal leaves nothing behind.
1394
- console.error(
1395
- `\nimport refused: ${result.check.outstanding.length || result.problems.length} finding(s) outstanding. Nothing was written.`,
1396
- )
1397
- process.exit(1)
1398
- }
1399
-
1400
- const c = result.check.counts
1401
- console.log(` run ${result.runId}`)
1402
- console.log(
1403
- ` applied ${c.create} created, ${c.update} updated, ${c.unchanged} unchanged, ${c.skip} skipped`,
1404
- )
1405
- const placed = describePlaced(result.blobs)
1406
- if (placed) console.log(` blobs ${placed}`)
1407
- if (result.sections) {
1408
- console.log(
1409
- ` files ${result.sections.written.length} written` +
1410
- (result.sections.replaced.length > 0
1411
- ? `, ${result.sections.replaced.length} replaced`
1412
- : ''),
1413
- )
1414
- if (result.sections.schema) console.log(` schema ${result.sections.schema.action}`)
1415
- if (result.sections.dictionary)
1416
- console.log(` dict ${result.sections.dictionary.imported} imported`)
1417
- // Named, because this is what makes reverting the install possible.
1418
- if (result.replacedFilesKept)
1419
- console.log(` kept ${relative(cwd, result.replacedFilesKept)} (the files it replaced)`)
1420
- }
1421
- if (result.published > 0) console.log(` published ${result.published} node(s)`)
1422
- for (const failure of result.publishFailures)
1423
- console.error(` not published ${failure.key}: ${failure.reason}`)
1424
- for (const finding of result.check.findings.filter((f) => f.kind === 'auto'))
1425
- console.log(` [auto ] ${finding.message}`)
1426
- console.log(`\nDone. \`crombolo content revert ${result.runId}\` puts it back.`)
1427
- }
1428
-
1429
- /**
1430
- * `content publish` / `content unpublish`: taking a page or a branch live, and
1431
- * back off again, without the backoffice.
1432
- *
1433
- * A publish can be refused per node — an unpublished ancestor, an unpublished
1434
- * mandatory language, an empty mandatory property — so the whole selection is
1435
- * checked before any of it is written. Half a published branch is a state
1436
- * nobody asked for and nobody can reason about.
1437
- */
1438
- async function contentPublish(config: CromboloConfig, publishing: boolean): Promise<void> {
1439
- const ref = positional[1]
1440
- const verb = publishing ? 'publish' : 'unpublish'
1441
- if (!ref) {
1442
- console.error(`content ${verb} needs the page: a path or a uuid.`)
1443
- process.exit(1)
1444
- }
1445
- const cultures = flagValues('--culture')
1446
- const at = flagValue('--at')
1447
- const until = flagValue('--until')
1448
- const when = (value: string | undefined, what: string): Date | null => {
1449
- if (value === undefined) return null
1450
- const date = new Date(value)
1451
- if (Number.isNaN(date.getTime())) {
1452
- console.error(` ${what} "${value}" is not a date I can read; try 2026-10-01T09:00`)
1453
- process.exit(1)
1454
- }
1455
- return date
1456
- }
1457
- const publishTime = when(at, '--at')
1458
- const unpublishTime = when(until, '--until')
1459
-
1460
- const { db } = await bootstrapDatabase(config)
1461
- try {
1462
- const resolved = await resolveNodeRef(db, [ObjectTypes.Document], ref)
1463
- if (!resolved.ok) {
1464
- console.error(` ${describeRefFailure(ref, resolved)}`)
1465
- process.exit(1)
1466
- }
1467
- const state = await currentSchemaState(db)
1468
- const docs = new DocumentRepository(db, {
1469
- nodeState: { version: state.version, revision: state.revision },
1470
- nodeId: config.nodeId,
1471
- })
1472
- // Parents before children, which is the order publishing has to happen in.
1473
- const targets = [
1474
- resolved.node,
1475
- ...(flags.has('--descendants')
1476
- ? await docs.nodes.descendants(resolved.node, [ObjectTypes.Document])
1477
- : []),
1478
- ]
1479
-
1480
- if (publishTime || unpublishTime) {
1481
- // A schedule is a statement about the future, so today's blockers are not
1482
- // the question: the background job checks them when the time comes.
1483
- for (const node of targets) {
1484
- await docs.setSchedule(node.key, [
1485
- { culture: cultures[0] ?? null, publishTime, unpublishTime },
1486
- ])
1487
- console.log(
1488
- ` scheduled ${node.text ?? node.key}${publishTime ? ` from ${publishTime.toISOString()}` : ''}${unpublishTime ? ` until ${unpublishTime.toISOString()}` : ''}`,
1489
- )
1490
- }
1491
- return
1492
- }
1493
-
1494
- if (publishing) {
1495
- const blocked: Array<{ name: string; reasons: string[] }> = []
1496
- // Everything in the selection counts as published for the check: the run
1497
- // publishes parents first, so a child is not blocked by an ancestor this
1498
- // very command is about to take live.
1499
- const selection = new Set(targets.map((node) => node.id))
1500
- for (const node of targets) {
1501
- const reasons = await docs.publishBlockers(node.key, cultures, selection)
1502
- if (reasons.length > 0) blocked.push({ name: node.text ?? node.key, reasons })
1503
- }
1504
- if (blocked.length > 0) {
1505
- console.error(
1506
- ` refused ${blocked.length} of ${targets.length} page(s) cannot be published:`,
1507
- )
1508
- for (const entry of blocked) console.error(` ${entry.name} ${entry.reasons.join('; ')}`)
1509
- console.error(' nothing was published')
1510
- process.exit(1)
1511
- }
1512
- }
1513
-
1514
- for (const node of targets) {
1515
- const result = publishing
1516
- ? await docs.publish(node.key, cultures.length > 0 ? cultures : null)
1517
- : await docs.unpublish(node.key, cultures.length > 0 ? cultures : null)
1518
- if (!result) continue
1519
- console.log(` ${publishing ? 'published' : 'unpublished'} ${node.text ?? node.key}`)
1520
- }
1521
- console.log(
1522
- `\n${targets.length} page(s) ${publishing ? 'published' : 'unpublished'}${cultures.length > 0 ? ` in ${cultures.join(', ')}` : ''}.`,
1523
- )
1524
- } finally {
1525
- await db.close()
1526
- }
1527
- }
1528
-
1529
- async function contentRuns(config: CromboloConfig): Promise<void> {
1530
- const limitIndex = rest.indexOf('--limit')
1531
- const limit = limitIndex >= 0 ? Number(rest[limitIndex + 1]) : 20
1532
- const { db } = await bootstrapDatabase(config)
1533
- try {
1534
- const runs = await new TransferRunRepository(db).recent(
1535
- Number.isFinite(limit) && limit > 0 ? limit : 20,
1536
- )
1537
- if (runs.length === 0) return console.log('No content has been imported here.')
1538
- for (const run of runs)
1539
- console.log(
1540
- ` ${run.startedAt.toISOString()} ${run.direction.padEnd(6)} ${run.status.padEnd(8)} ` +
1541
- `${run.id} ${run.nodeCount} node(s) bundle ${run.bundleId}` +
1542
- `${run.appliedBy ? ` by ${run.appliedBy}` : ''}${run.bundleLabel ? ` (${run.bundleLabel})` : ''}`,
1543
- )
1544
- } finally {
1545
- await db.close()
1546
- }
1547
- }
1548
-
1549
- /** `--resolve <key>=<choice>` or `--resolve <key>/<property>[@<culture>]=<choice>`, repeatable. */
1550
- function parseResolutions(): Record<string, NodeResolutions> {
1551
- const parsed = parseResolveFlags(flagValues('--resolve'))
1552
- if (parsed.problems.length > 0) {
1553
- for (const problem of parsed.problems) console.error(` ${problem}`)
1554
- process.exit(1)
1555
- }
1556
- return parsed.byNode
1557
- }
1558
-
1559
- function printTransferFindings(check: TransferCheck): void {
1560
- const byKind = { blocking: 0, person: 0, auto: 0 }
1561
- for (const f of check.findings) byKind[f.kind] += 1
1562
- const c = check.counts
1563
- console.log(
1564
- ` plan ${c.create} new, ${c.update} updated, ${c.unchanged} unchanged, ${c.skip} skipped`,
1565
- )
1566
- console.log(
1567
- ` findings ${byKind.blocking} blocking, ${byKind.person} need a person, ${byKind.auto} automatic`,
1568
- )
1569
- for (const f of check.findings) {
1570
- console.log(` [${f.kind.padEnd(8)}] ${f.message}`)
1571
- // The handle is the CLI's to name; the dashboard offers the same answers as choices.
1572
- if (f.code === 'local-edit' && f.propertyAlias)
1573
- console.log(
1574
- ` answer it as ${f.subjectKey}/${propertyHandle(f.propertyAlias, f.culture)}`,
1575
- )
1576
- for (const suggestion of resolutionsFor(f.code)) console.log(` → ${suggestion}`)
1577
- if (f.link) console.log(` ${f.link}`)
1578
- }
1579
- }
1580
-
1581
- async function contentCheck(config: CromboloConfig): Promise<void> {
1582
- const dir = positional[1]
1583
- if (!dir) {
1584
- console.error(
1585
- 'content check needs a bundle: a package directory, or the name of a bundle this site depends on.',
1586
- )
1587
- process.exit(1)
1588
- }
1589
- const result = await ops.contentCheck(config, {
1590
- ...transferInput(dir),
1591
- passphrase: await passphraseToOpen(dir, config),
1592
- })
1593
- for (const problem of result.problems) console.error(` ${problem}`)
1594
- describeBundle(result.bundle)
1595
- if (result.under) console.log(` under ${result.under}`)
1596
- printSections(result.sections)
1597
- printTransferFindings(result.check)
1598
- // The reasons are publish's own: the check rehearsed the import to find them.
1599
- for (const refusal of result.publishRefusals)
1600
- console.error(` would not publish ${refusal.key}: ${refusal.reason}`)
1601
-
1602
- if (flags.has('--save')) {
1603
- const target = join(result.payloadDir, RESOLUTIONS_FILE)
1604
- await writeFile(
1605
- target,
1606
- writeResolutions({
1607
- bundleId: result.bundle.id,
1608
- under: flagValue('--under') ?? null,
1609
- all: result.decisions.all,
1610
- byNode: result.decisions.byNode,
1611
- }),
1612
- )
1613
- console.log(` wrote ${relative(cwd, target)}`)
1614
- }
1615
-
1616
- if (!result.clean) {
1617
- console.log(
1618
- `\n${result.check.outstanding.length} finding(s) would stop an import. Resolve them, or pass --resolve.`,
1619
- )
1620
- process.exit(1)
1621
- }
1622
- console.log('\nNothing outstanding: this bundle would import cleanly.')
1623
- }
1624
-
1625
- /**
1626
- * What an export or an import does with property values and media files:
1627
- * left out, sealed, or — when asked twice — in the open.
1628
- */
1629
- function contentFlags(): { content: 'omitted' | 'sealed' | 'open' } {
1630
- const include = flags.has('--include-content')
1631
- const open = flags.has('--allow-content-unencrypted')
1632
- if (open && !include) {
1633
- console.error('--allow-content-unencrypted goes with --include-content.')
1634
- process.exit(1)
1635
- }
1636
- if (flags.has('--with-blobs') && !include) {
1637
- console.error('Media files are content: --with-blobs goes with --include-content.')
1638
- process.exit(1)
1639
- }
1640
- if (open)
1641
- console.error(
1642
- ' warning property values and media files are written unencrypted: anyone with the files can read any personal information in them',
1643
- )
1644
- return { content: !include ? 'omitted' : open ? 'open' : 'sealed' }
1645
- }
1646
-
1647
- function describeContentMode(
1648
- content: 'omitted' | 'sealed' | 'open',
1649
- people?: { members: number; users: number },
1650
- ): void {
1651
- if (content === 'omitted')
1652
- console.log(
1653
- ' content not included: pages are placed and named, and no value is written. Pass --include-content to bring the values and media files',
1654
- )
1655
- else if (content === 'sealed') console.log(' content encrypted: importing needs the passphrase')
1656
- else console.log(' content unencrypted')
1657
- if (people && people.members + people.users > 0)
1658
- console.log(` people ${people.members} member(s), ${people.users} user(s), encrypted`)
1659
- }
1660
-
1661
- async function contentExport(config: CromboloConfig): Promise<void> {
1662
- const out = flagValue('--out')
1663
- const refs = flagValues('--root')
1664
- if (!out || refs.length === 0) {
1665
- console.error('content export needs --root <path|uuid> (repeatable) and --out <dir>.')
1666
- process.exit(1)
1667
- }
1668
- const content = contentFlags()
1669
- const result = await ops.contentExport(config, {
1670
- ...content,
1671
- roots: refs,
1672
- only: flags.has('--only'),
1673
- drafts: flags.has('--drafts'),
1674
- withBlobs: flags.has('--with-blobs'),
1675
- withMembers: flags.has('--with-members'),
1676
- withUsers: flags.has('--with-users'),
1677
- passphrase:
1678
- content.content === 'sealed' || flags.has('--with-members') || flags.has('--with-users')
1679
- ? await passphraseToSeal()
1680
- : undefined,
1681
- withBlueprints: flags.has('--with-blueprints'),
1682
- // An export is a private package; without --name it is named for its directory.
1683
- package: {
1684
- name: flagValue('--name') ?? packageNameFor(basename(resolve(out))),
1685
- version: flagValue('--package-version'),
1686
- },
1687
- })
1688
- for (const key of result.blobs.missing)
1689
- console.error(` missing ${key} is not in this environment's media store`)
1690
- for (const file of result.files) {
1691
- const target = join(out, file.path)
1692
- await mkdir(join(target, '..'), { recursive: true })
1693
- await writeFile(target, file.bytes ?? (file.text as string))
1694
- }
1695
- const counts = Object.entries(result.counts)
1696
- console.log(` wrote ${relative(cwd, out) || '.'}`)
1697
- console.log(` package ${result.package.name}@${result.package.version} (private)`)
1698
- console.log(
1699
- ` bundle ${result.bundleId} (${result.snapshot}): ${
1700
- counts.length > 0 ? counts.map(([kind, n]) => `${n} ${kind}`).join(', ') : 'nothing selected'
1701
- }`,
1702
- )
1703
- if (result.expected.length > 0) {
1704
- console.log(` expects ${result.expected.length} node(s) to exist at the destination:`)
1705
- for (const e of result.expected) console.log(` ${e.key} ${e.name} (${e.why})`)
1706
- }
1707
- describeContentMode(result.content, result.people)
1708
- if (result.blobs.carried > 0)
1709
- console.log(
1710
- ` blobs ${result.blobs.carried} media file(s), ${describeBytes(result.blobs.bytes)}`,
1711
- )
1712
- else if (result.blobs.notCarried > 0)
1713
- console.log(
1714
- ` blobs ${result.blobs.notCarried} media file(s) are not carried — pass --with-blobs to include them`,
1715
- )
1716
- if (counts.length === 0) process.exit(1)
1717
- }
1718
-
1719
- /** `crombolo export --site`: the whole site as one file (story 104). */
1720
- async function exportCommand(): Promise<void> {
1721
- if (!flags.has('--site')) {
1722
- console.error(
1723
- 'export needs --site: the whole site, as one file. For a part of it, use content export.',
1724
- )
1725
- process.exit(1)
1726
- }
1727
- const config = await siteConfig()
1728
- const withPeople = flags.has('--with-members') || flags.has('--with-users')
1729
- const anonymise = flags.has('--anonymise')
1730
- const out = resolve(
1731
- flagValue('--out') ??
1732
- `${packageNameFor(`${config.siteName}-site`)}-${new Date().toISOString().slice(0, 10)}.tgz`,
1733
- )
1734
- const result = await ops.siteExport(config, {
1735
- out,
1736
- media: !flags.has('--no-media'),
1737
- drafts: flags.has('--drafts'),
1738
- withMembers: flags.has('--with-members'),
1739
- withUsers: flags.has('--with-users'),
1740
- anonymise,
1741
- passphrase:
1742
- withPeople && !(anonymise && !flags.has('--with-members'))
1743
- ? await passphraseToSeal()
1744
- : undefined,
1745
- package: { name: flagValue('--name'), version: flagValue('--package-version') },
1746
- })
1747
- const counts = Object.entries(result.counts)
1748
- console.log(` wrote ${relative(cwd, result.out) || result.out}`)
1749
- console.log(` package ${result.package.name}@${result.package.version} (private)`)
1750
- console.log(
1751
- ` content ${counts.length > 0 ? counts.map(([kind, n]) => `${n} ${kind}`).join(', ') : 'none'}, unencrypted`,
1752
- )
1753
- const sections = Object.entries(result.sections)
1754
- if (sections.length > 0)
1755
- console.log(` files ${sections.map(([section, n]) => `${n} ${section}`).join(', ')}`)
1756
- if (result.media.omitted)
1757
- console.log(
1758
- ' media left out (--no-media): importing it brings the media items without their files',
1759
- )
1760
- else
1761
- console.log(` media ${result.media.carried} file(s), ${describeBytes(result.media.bytes)}`)
1762
- for (const key of result.media.missing)
1763
- console.error(` missing ${key} is not in this environment's media store`)
1764
- if (result.people.members + result.people.users > 0)
1765
- console.log(
1766
- ` people ${result.people.members} member(s), ${result.people.users} user(s), encrypted`,
1767
- )
1768
- else console.log(' people left out: pass --with-members or --with-users to bring them')
1769
- if (result.anonymised !== undefined)
1770
- console.log(
1771
- ` anonymised ${result.anonymised} email address(es) replaced; no back-office users`,
1772
- )
1773
- }
1774
-
1775
- /**
1776
- * `schema-as-code.md` leaves the dictionary in the database, because
1777
- * translators work in production where `schema/` is read-only — so this is how
1778
- * a lower environment gets the translations.
1779
- */
1780
- async function dictionary(): Promise<void> {
1781
- const config = await siteConfig()
1782
- const sub = positional[0]
1783
- if (sub !== 'export' && sub !== 'import') return help()
1784
- const { db } = await bootstrapDatabase(config)
1785
- try {
1786
- if (sub === 'export') {
1787
- const out = flagValue('--out')
1788
- if (!out) {
1789
- console.error('dictionary export needs --out <file.udt>.')
1790
- process.exit(1)
1791
- }
1792
- const items = await dictionaryToUdt(new DictionaryRepository(db), flagValue('--key'))
1793
- if (items.length === 0) {
1794
- console.error('Nothing to export: this site has no dictionary items.')
1795
- process.exit(1)
1796
- }
1797
- await mkdir(join(out, '..'), { recursive: true })
1798
- await writeFile(out, writeDictionaryUdt(items))
1799
- const count = (list: typeof items): number =>
1800
- list.reduce((total, item) => total + 1 + count(item.children), 0)
1801
- console.log(` wrote ${relative(cwd, out)}`)
1802
- console.log(` items ${count(items)}`)
1803
- return
1804
- }
1805
-
1806
- const file = positional[1]
1807
- if (!file) {
1808
- console.error('dictionary import needs the .udt file.')
1809
- process.exit(1)
1810
- }
1811
- if (!existsSync(file)) {
1812
- console.error(`No file at ${file}.`)
1813
- process.exit(1)
1814
- }
1815
- const outcome = await importDictionaryUdt(db, await Bun.file(file).text(), null)
1816
- if (!outcome.ok) {
1817
- console.error(` refused: ${outcome.reason}`)
1818
- process.exit(1)
1819
- }
1820
- console.log(` imported ${outcome.imported} item(s)`)
1821
- // Umbraco's own behaviour, and worth saying out loud rather than dropping
1822
- // translations silently: a language this site has not got is skipped.
1823
- if (outcome.skipped.length > 0)
1824
- console.log(
1825
- ` skipped translations for ${outcome.skipped.join(', ')} — no such language here`,
1826
- )
1827
- } finally {
1828
- await db.close()
1829
- }
1830
- }
1831
-
1832
- /**
1833
- * `crombolo domains`: which hostnames reach which page.
1834
- *
1835
- * `domains.toml` is the truth and the database follows it, so `set` and `clear`
1836
- * write the file and then apply it — one command, rather than an edit here and
1837
- * a deploy to make it real. The file is committed; `${VAR}` in a host is read
1838
- * from the environment when it is applied, so one file serves every
1839
- * environment.
1840
- */
1841
- async function domains(): Promise<void> {
1842
- const config = await siteConfig()
1843
- const sub = positional[0] ?? 'list'
1844
- if (sub !== 'list' && sub !== 'apply' && sub !== 'set' && sub !== 'clear' && sub !== 'undo')
1845
- return help()
1846
-
1847
- const file = domainsFilePath(config.siteDir)
1848
- const read = readDomainsFile(config.siteDir)
1849
- const declarations = read?.declarations ?? []
1850
- for (const problem of read?.problems ?? []) console.error(` ${file}: ${problem}`)
1851
-
1852
- if (sub === 'list') {
1853
- const { db } = await bootstrapDatabase(config)
1854
- try {
1855
- if (!read) console.log(`No ${DOMAINS_FILE}; nothing is deployed from a file here.`)
1856
- for (const declaration of declarations) {
1857
- const resolved = resolvePlaceholders(declaration.host)
1858
- const culture = declaration.culture ? ` ${declaration.culture}` : ''
1859
- // What the file says, and what it comes to here, which is the whole
1860
- // point of a placeholder and the first thing to check when one is wrong.
1861
- const here =
1862
- resolved.missing.length > 0
1863
- ? ` (${resolved.missing.join(', ')} not set here)`
1864
- : resolved.value !== declaration.host
1865
- ? ` → ${resolved.value}`
1866
- : ''
1867
- console.log(
1868
- ` file ${declaration.node.padEnd(24)} ${declaration.host}${here}${culture}`,
1869
- )
1870
- }
1871
- // What the database actually answers on, which is the thing a person is
1872
- // usually asking about when a hostname is not working.
1873
- const nodes = new NodeRepository(db)
1874
- for (const row of await new DomainRepository(db).all()) {
1875
- const node = await nodes.byId(row.nodeId)
1876
- console.log(
1877
- ` bound ${(node?.text ?? String(row.nodeId)).padEnd(24)} ${row.domainName}${row.isoCode ? ` ${row.isoCode}` : ''}`,
1878
- )
1879
- }
1880
- } finally {
1881
- await db.close()
1882
- }
1883
- return
1884
- }
1885
-
1886
- if (sub === 'apply') return applyDomains(config)
1887
-
1888
- if (sub === 'undo') {
1889
- const undone = await undoDomainsFile(config.siteDir)
1890
- if (!undone.ok) {
1891
- console.error(` ${undone.message}`)
1892
- process.exit(1)
1893
- }
1894
- console.log(` restored ${relative(cwd, undone.file)}`)
1895
- return applyDomains(config)
1896
- }
1897
-
1898
- const ref = positional[1]
1899
- if (!ref) {
1900
- console.error(`domains ${sub} needs the page: a path, a uuid, or / for the root page.`)
1901
- process.exit(1)
1902
- }
1903
-
1904
- // Stating a node's hostnames says what they are, as the backoffice's dialog
1905
- // does: whatever that page had is replaced, so running this twice is the same
1906
- // as running it once. Which entries belong to that page is decided by
1907
- // resolving them, not by matching the text — `/` and `/Home` can be the same
1908
- // page, and clearing one while the other stayed would do nothing at all.
1909
- const { db: resolving } = await bootstrapDatabase(config)
1910
- let kept: DomainDeclaration[] = []
1911
- try {
1912
- const target = await resolveDomainNode(resolving, ref)
1913
- if (!target.ok) {
1914
- console.error(` ${target.message}`)
1915
- process.exit(1)
1916
- }
1917
- kept = await withoutNode(resolving, declarations, target.node)
1918
- } finally {
1919
- await resolving.close()
1920
- }
1921
- const defaultCulture = flagValue('--default-culture')
1922
- const next: DomainDeclaration[] = [...kept]
1923
- if (sub === 'set') {
1924
- const hosts = flagValues('--host')
1925
- if (hosts.length === 0 && !defaultCulture) {
1926
- console.error('domains set needs --host <host>[=<culture>], or --default-culture <iso>.')
1927
- process.exit(1)
1928
- }
1929
- if (hosts.length === 0 && defaultCulture) next.push({ node: ref, host: '', defaultCulture })
1930
- for (const [index, entry] of hosts.entries()) {
1931
- const [host = '', culture] = entry.split('=')
1932
- next.push({
1933
- node: ref,
1934
- host,
1935
- culture,
1936
- // The node's own fallback culture belongs to the node, so it is written
1937
- // once rather than repeated on every hostname.
1938
- defaultCulture: index === 0 ? defaultCulture : undefined,
1939
- })
1940
- }
1941
- }
1942
-
1943
- const written = await writeDomains(config.siteDir, next)
1944
- console.log(
1945
- ` wrote ${relative(cwd, written.file)}${written.backup ? ' (previous kept; `crombolo domains undo` puts it back)' : ''}`,
1946
- )
1947
- return applyDomains(config)
1948
- }
1949
-
1950
- /** Applies the file as a boot would, and reports what it did. */
1951
- async function applyDomains(config: CromboloConfig): Promise<void> {
1952
- const { db } = await bootstrapDatabase(config)
1953
- try {
1954
- const report = await syncDomainsFile(db, config.siteDir)
1955
- for (const problem of report.problems) console.error(` ${problem}`)
1956
- for (const applied of report.applied)
1957
- console.log(
1958
- ` applied ${applied.node.padEnd(24)} ${applied.host}${applied.culture ? ` ${applied.culture}` : ''}`,
1959
- )
1960
- if (report.removed > 0) console.log(` removed ${report.removed} binding(s)`)
1961
- if (report.problems.length > 0) process.exit(1)
1962
- } finally {
1963
- await db.close()
1964
- }
1965
- }
1966
-
1967
- /**
1968
- * `crombolo import <file>`: replace this site with a whole-site file (story
1969
- * 105). It always says first what would be replaced, and changes nothing
1970
- * without `--replace` or `--replace-all`.
1971
- */
1972
- async function importFile(file: string): Promise<void> {
1973
- const config = await siteConfig()
1974
- const replace: ops.ReplaceMode | undefined = flags.has('--replace-all')
1975
- ? 'replace-all'
1976
- : flags.has('--replace')
1977
- ? 'replace'
1978
- : undefined
1979
- const dir = await ops.unpackBundle(file, config.siteDir)
1980
- let carriesPeople = false
1981
- try {
1982
- carriesPeople = ops.readBundle(dir, config.siteDir).set.manifest.people !== undefined
1983
- } catch (error) {
1984
- console.error(` ${error instanceof Error ? error.message : String(error)}`)
1985
- process.exit(1)
1986
- }
1987
- const passphrase = carriesPeople
1988
- ? (process.env[PASSPHRASE_ENV] ??
1989
- (process.stdin.isTTY ? await readSecret(' Passphrase for its people: ') : undefined))
1990
- : await passphraseToOpen(dir, config)
1991
- const allowMissingBlobs = flags.has('--allow-missing-media')
1992
-
1993
- const { check, summary } = await ops.siteReplaceCheck(config, {
1994
- bundle: dir,
1995
- passphrase,
1996
- replace: replace ?? 'replace',
1997
- allowMissingBlobs,
1998
- })
1999
- describeBundle(check.bundle)
2000
- const c = check.check.counts
2001
- console.log(` brings ${c.create} new, ${c.update} changed, ${c.unchanged} unchanged`)
2002
- const leaving = Object.entries(summary.leaving)
2003
- const fate = replace === 'replace-all' ? 'deleted' : 'recycled'
2004
- if (leaving.length > 0)
2005
- console.log(
2006
- ` ${fate.padEnd(8)} ${leaving.map(([kind, n]) => `${n} ${kind}`).join(', ')} not in the file`,
2007
- )
2008
- if (summary.filesRemoved.length > 0) {
2009
- console.log(
2010
- ` removes ${summary.filesRemoved.length} file(s) not in the file, kept for a revert:`,
2011
- )
2012
- for (const path of summary.filesRemoved.slice(0, 20)) console.log(` ${relative(cwd, path)}`)
2013
- if (summary.filesRemoved.length > 20)
2014
- console.log(` … and ${summary.filesRemoved.length - 20} more`)
2015
- }
2016
- if (replace === 'replace-all' && summary.people.members + summary.people.users > 0)
2017
- console.log(
2018
- ` deletes ${summary.people.members} member(s) and ${summary.people.users} user(s) not in the file`,
2019
- )
2020
- console.log(" keeps domains, the secret key and this environment's settings")
2021
- for (const problem of check.problems) console.error(` ${problem}`)
2022
-
2023
- if (!replace) {
2024
- console.error(
2025
- '\nNothing was changed. Pass --replace to replace this site (what it loses is recycled, people kept), or --replace-all to make it exactly the file (deleted, people too).',
2026
- )
2027
- process.exit(1)
2028
- }
2029
- if (replace === 'replace-all' && !flags.has('--yes')) {
2030
- if (!process.stdin.isTTY) {
2031
- console.error(
2032
- '\n--replace-all deletes what the file does not have: pass --yes to confirm it.',
2033
- )
2034
- process.exit(1)
2035
- }
2036
- const answer = prompt(
2037
- ` This deletes for good. Type the site's name (${config.siteName}) to go on:`,
2038
- )
2039
- if (answer?.trim() !== config.siteName) {
2040
- console.error(' Nothing was changed.')
2041
- process.exit(1)
2042
- }
2043
- }
2044
-
2045
- const result = await ops.contentImport(config, {
2046
- bundle: dir,
2047
- passphrase,
2048
- replace,
2049
- allowMissingBlobs,
2050
- label: `${replace} from ${basename(file)}`,
2051
- backupTaken: flags.has('--backup-taken'),
2052
- })
2053
- for (const problem of result.problems) console.error(` ${problem}`)
2054
- if (result.backup.path) console.log(` backup ${result.backup.path}`)
2055
- if (!result.runId) {
2056
- printTransferFindings(result.check)
2057
- console.error('\nreplace refused. Nothing was changed.')
2058
- process.exit(1)
2059
- }
2060
- const counts = result.check.counts
2061
- console.log(` run ${result.runId}`)
2062
- console.log(
2063
- ` applied ${counts.create} created, ${counts.update} updated, ${counts.unchanged} unchanged`,
2064
- )
2065
- const placed = describePlaced(result.blobs)
2066
- if (placed) console.log(` media ${placed}`)
2067
- if (result.sections?.schema) console.log(` schema ${result.sections.schema.action}`)
2068
- if (result.published > 0) console.log(` published ${result.published} page(s)`)
2069
- for (const failure of result.publishFailures)
2070
- console.error(` not published ${failure.key}: ${failure.reason}`)
2071
- console.log(
2072
- replace === 'replace-all'
2073
- ? `\nDone. \`crombolo content revert ${result.runId}\` puts the files and the pages it changed back; what was deleted is in the backup.`
2074
- : `\nDone. \`crombolo content revert ${result.runId}\` puts it back.`,
2075
- )
2076
- }
2077
-
2078
- /**
2079
- * `crombolo import umbraco`: a compatibility report for an Umbraco backup, and
2080
- * then a crombolo site written from it.
2081
- *
2082
- * Neither subcommand touches a database. `report` reads the backup and prints
2083
- * what will and will not come across; `apply` writes a site directory — schema
2084
- * files, view stubs, a content bundle — whose `start` script does the import
2085
- * through `start --bundle`, the path every other bundle takes.
2086
- *
2087
- * The importer is a package of its own, loaded here on demand, so a site that
2088
- * never imports anything does not carry it or the `.bacpac` reader it needs.
2089
- */
2090
- async function importSite(): Promise<void> {
2091
- const VALUE_FLAGS = new Set(['--out', '--site', '--media', '--name', '--staging'])
2092
- const args: string[] = []
2093
- for (let i = 0; i < rest.length; i++) {
2094
- const arg = rest[i] as string
2095
- if (VALUE_FLAGS.has(arg)) i++
2096
- else if (!arg.startsWith('--')) args.push(arg)
2097
- }
2098
- const [kind, sub, backup] = args
2099
- if (kind && kind !== 'umbraco') return importFile(kind)
2100
- if (kind !== 'umbraco' || (sub !== 'report' && sub !== 'apply')) return help()
2101
- // Not into --json output, which a machine reads.
2102
- if (!flags.has('--json'))
2103
- console.error(
2104
- ' deprecated `crombolo import umbraco` is going away: an Umbraco extension will write a crombolo file that `crombolo import <file>` reads (story 074)',
2105
- )
2106
- if (!backup) {
2107
- console.error(`import umbraco ${sub} needs the backup: a .bacpac, or Umbraco's SQLite file.`)
2108
- process.exit(1)
2109
- }
2110
- const out = flagValue('--out')
2111
- if (sub === 'apply' && !out) {
2112
- console.error('import umbraco apply needs --out <dir>: the site directory to write.')
2113
- process.exit(1)
2114
- }
2115
-
2116
- let importer: typeof import('@crombolo/import-umbraco')
2117
- try {
2118
- importer = await import('@crombolo/import-umbraco')
2119
- } catch {
2120
- console.error(
2121
- 'The importer is a separate package. Add it, then run this again:\n\n bun add -d @crombolo/import-umbraco',
2122
- )
2123
- process.exit(1)
2124
- }
2125
-
2126
- const { content } = sub === 'apply' ? contentFlags() : { content: 'omitted' as const }
2127
- const withMembers = flags.has('--with-members')
2128
- const withUsers = flags.has('--with-users')
2129
- const sealing = content === 'sealed' || withMembers || withUsers
2130
- // A report writes no bundle, so what it would seal with is never kept.
2131
- const passphrase = !sealing
2132
- ? undefined
2133
- : sub === 'apply'
2134
- ? await passphraseToSeal()
2135
- : crypto.randomUUID()
2136
- let plan: Awaited<ReturnType<typeof importer.planImport>>
2137
- try {
2138
- plan = await importer.planImport({
2139
- source: backup,
2140
- site: flagValue('--site'),
2141
- media: flagValue('--media'),
2142
- siteName: flagValue('--name'),
2143
- drafts: flags.has('--drafts'),
2144
- staging: flagValue('--staging'),
2145
- coreVersion: VERSION,
2146
- content: sub === 'apply' ? content : flags.has('--include-content') ? 'open' : 'omitted',
2147
- withMembers,
2148
- withUsers,
2149
- passphrase,
2150
- })
2151
- } catch (error) {
2152
- console.error(` ${error instanceof Error ? error.message : error}`)
2153
- process.exit(1)
2154
- }
2155
- const { report } = plan
2156
-
2157
- if (sub === 'report') {
2158
- if (flags.has('--json')) console.log(importer.reportJson(report).trimEnd())
2159
- else for (const line of importer.reportSummary(report)) console.log(` ${line}`)
2160
- if (out) {
2161
- await mkdir(out, { recursive: true })
2162
- await writeFile(join(out, 'report.md'), importer.reportMarkdown(report))
2163
- await writeFile(join(out, 'report.json'), importer.reportJson(report))
2164
- if (!flags.has('--json')) console.log(`\n wrote ${relative(cwd, join(out, 'report.md'))}`)
2165
- }
2166
- if (!report.ready) process.exit(1)
2167
- return
2168
- }
2169
-
2170
- const target = out as string
2171
- if (!report.ready) {
2172
- for (const finding of report.findings.filter((f) => f.class === 'blocking'))
2173
- console.error(
2174
- ` blocked ${finding.title}${finding.detail ? `\n ${finding.detail}` : ''}`,
2175
- )
2176
- console.error('\nNothing was written.')
2177
- process.exit(1)
2178
- }
2179
- if (
2180
- existsSync(target) &&
2181
- (await Array.fromAsync(new Bun.Glob('*').scan({ cwd: target, onlyFiles: false, dot: true })))
2182
- .length > 0 &&
2183
- !flags.has('--force')
2184
- ) {
2185
- console.error(
2186
- `${target} is not empty. Choose a new directory, or pass --force to write into it.`,
2187
- )
2188
- process.exit(1)
2189
- }
2190
-
2191
- const files = new Map<string, { text?: string; bytes?: Uint8Array; copyFrom?: string }>()
2192
- const scaffold = scaffoldFiles({
2193
- siteName: plan.siteName,
2194
- postgres: flags.has('--postgres'),
2195
- bundle: {
2196
- path: importer.IMPORT_BUNDLE,
2197
- // A media item whose file was not in the backup is reported, not refused.
2198
- flags: plan.missingMedia > 0 ? ['--allow-missing-blobs'] : [],
2199
- },
2200
- })
2201
- for (const file of scaffold) files.set(file.path, file)
2202
- // The importer's files win: the source site's media types replace the defaults.
2203
- for (const file of plan.files) files.set(file.path, file)
2204
- if (plan.dictionary.length > 0)
2205
- files.set(`${importer.IMPORT_DIR}/dictionary.udt`, { text: writeUdtAll(plan.dictionary) })
2206
- if (plan.domains.length > 0)
2207
- files.set(`${importer.IMPORT_DIR}/domains.toml`, { text: writeDomainsFile(plan.domains) })
2208
-
2209
- for (const [path, file] of files) {
2210
- const destination = join(target, path)
2211
- await mkdir(join(destination, '..'), { recursive: true })
2212
- if (file.copyFrom !== undefined) await Bun.write(destination, Bun.file(file.copyFrom))
2213
- else await Bun.write(destination, file.bytes ?? file.text ?? '')
2214
- }
2215
-
2216
- for (const line of importer.reportSummary(report)) console.log(` ${line}`)
2217
- console.log(`\n wrote ${files.size} files to ${relative(cwd, target) || '.'}`)
2218
- console.log(` report ${relative(cwd, join(target, importer.IMPORT_DIR, 'report.md'))}`)
2219
- console.log(`\nNext:\n cd ${relative(cwd, target) || '.'}\n bun install\n bun start`)
2220
- console.log(
2221
- `\n\`bun start\` imports ${importer.IMPORT_BUNDLE} on the first boot of an empty database.`,
2222
- )
2223
- if (sealing)
2224
- console.log(
2225
- `It is encrypted: \`bun start\` asks for the passphrase, or reads ${PASSPHRASE_ENV}.`,
2226
- )
2227
- if (content === 'omitted')
2228
- console.log(
2229
- 'Pages and media were brought without what they hold. Run again with --include-content to bring it.',
2230
- )
2231
- if (plan.dictionary.length > 0)
2232
- console.log(`Then: crombolo dictionary import ${importer.IMPORT_DIR}/dictionary.udt`)
2233
- }
2234
-
2235
- /**
2236
- * `crombolo views`: what is on disk, whether it compiles, and a new one.
2237
- *
2238
- * A view is loaded when a request renders it, so one that does not compile is a
2239
- * 500 waiting for a visitor. `components check` is that failure brought forward.
2240
- */
2241
- async function components(): Promise<void> {
2242
- const config = await siteConfig()
2243
- const sub = positional[0] ?? 'check'
2244
- if (sub !== 'check' && sub !== 'list' && sub !== 'new') return help()
2245
-
2246
- const loaded = existsSync(config.schemaDir)
2247
- ? loadSchemaDirectory(config.schemaDir, { formTypes: formTypesFor(config) })
2248
- : undefined
2249
- const types = loaded?.set.contentTypes ?? []
2250
-
2251
- if (sub === 'list') {
2252
- const found = listComponents(config.componentsDir)
2253
- if (found.length === 0) console.log(`Nothing in ${relative(cwd, config.componentsDir) || '.'}.`)
2254
- for (const component of found) {
2255
- // A component is a template because a content type names it, not
2256
- // because of where it sits — so the label is read off the schema.
2257
- const declaredBy = types
2258
- .filter((type) => type.components.includes(component.alias))
2259
- .map((type) => type.alias)
2260
- const kind = declaredBy.length > 0 ? 'template' : 'component'
2261
- const note = declaredBy.length > 0 ? ` ← ${declaredBy.join(', ')}` : ''
2262
- console.log(` ${kind.padEnd(9)} ${relative(cwd, component.path)}${note}`)
2263
- }
2264
- return
2265
- }
2266
-
2267
- if (sub === 'new') {
2268
- const alias = positional[1]
2269
- if (!alias) {
2270
- console.error('components new needs a name: `crombolo components new pages/article`.')
2271
- process.exit(1)
2272
- }
2273
- if (!isSafeAlias(alias)) {
2274
- console.error(`"${alias}" is not a path inside ${relative(cwd, config.componentsDir)}.`)
2275
- process.exit(1)
2276
- }
2277
- const file = componentFileFor(config.componentsDir, alias)
2278
- if (existsSync(file)) {
2279
- console.error(`${relative(cwd, file)} already exists.`)
2280
- process.exit(1)
2281
- }
2282
- // A content type that names this alias gets the page scaffold, which takes
2283
- // PageProps; anything else gets the plain one. Nothing turns on the folder.
2284
- const type = types.find((candidate) => candidate.components.includes(alias))
2285
- if (!type)
2286
- console.log(
2287
- ` note no content type declares the template "${alias}" — add it to one's templates, or \`crombolo schema new\``,
2288
- )
2289
- await mkdir(join(file, '..'), { recursive: true })
2290
- await writeFile(file, type ? viewScaffold(type) : componentScaffold(alias))
2291
- console.log(` created ${relative(cwd, file)}`)
2292
- return
2293
- }
2294
-
2295
- const report = await checkComponents({
2296
- componentsDir: config.componentsDir,
2297
- siteDir: config.siteDir,
2298
- declaredComponents: types.flatMap((type) => type.components),
2299
- })
2300
- const templates = report.declared.length
2301
- console.log(
2302
- ` checked ${report.components.length} file(s): ${templates} template(s), ` +
2303
- `${report.components.length - templates} component(s)`,
2304
- )
2305
- for (const problem of report.problems)
2306
- console.error(` [error] ${problem.file}: ${problem.message}`)
2307
- if (report.types.note) console.log(` types ${report.types.note}`)
2308
- else {
2309
- console.log(` types tsc --noEmit over ${relative(cwd, config.siteDir) || '.'}`)
2310
- for (const problem of report.types.problems) console.error(` [types] ${problem}`)
2311
- }
2312
- if (report.problems.length > 0 || report.types.problems.length > 0) process.exit(1)
2313
- }
2314
-
2315
- /** `crombolo assets`: the stylesheets and scripts the backoffice also edits. */
2316
- async function assets(): Promise<void> {
2317
- const config = await siteConfig()
2318
- const sub = positional[0] ?? 'list'
2319
- if (sub !== 'list' && sub !== 'new') return help()
2320
-
2321
- if (sub === 'list') {
2322
- const found = listAssets(config)
2323
- if (found.length === 0) console.log('No stylesheets or scripts here.')
2324
- for (const asset of found)
2325
- console.log(` ${asset.kind.padEnd(10)} ${relative(cwd, asset.path)}`)
2326
- return
2327
- }
2328
-
2329
- const kind = positional[1]
2330
- const name = positional[2]
2331
- if ((kind !== 'stylesheet' && kind !== 'script') || !name) {
2332
- console.error('assets new needs a kind and a name: `crombolo assets new stylesheet site.css`.')
2333
- process.exit(1)
2334
- }
2335
- const where = assetDirs(config)[kind]
2336
- const file = join(where.dir, name.endsWith(where.extension) ? name : `${name}${where.extension}`)
2337
- if (existsSync(file)) {
2338
- console.error(`${relative(cwd, file)} already exists.`)
2339
- process.exit(1)
2340
- }
2341
- await mkdir(join(file, '..'), { recursive: true })
2342
- await writeFile(file, assetScaffold(kind, relative(where.dir, file)))
2343
- console.log(` created ${relative(cwd, file)}`)
2344
- }
2345
-
2346
- function parseSet(): Record<string, unknown> {
2347
- const out: Record<string, unknown> = {}
2348
- for (let i = 0; i < rest.length; i++) {
2349
- if (rest[i] !== '--set') continue
2350
- const pair = rest[i + 1] ?? ''
2351
- const eq = pair.indexOf('=')
2352
- if (eq < 0) continue
2353
- const raw = pair.slice(eq + 1)
2354
- let value: unknown = raw
2355
- try {
2356
- value = JSON.parse(raw)
2357
- } catch {}
2358
- out[pair.slice(0, eq)] = value
2359
- }
2360
- return out
2361
- }
2362
-
2363
- function printFindings(report: { classification: string; findings: Finding[] }): void {
2364
- const byKind = { blocking: 0, person: 0, auto: 0 }
2365
- for (const f of report.findings) byKind[f.kind] += 1
2366
- console.log(
2367
- `change: ${report.classification}; findings: ${byKind.blocking} blocking, ${byKind.person} need a person, ${byKind.auto} automatic`,
2368
- )
2369
- for (const f of report.findings) {
2370
- const where = f.link ? ` ${f.link}` : ''
2371
- console.log(` [${f.kind.padEnd(8)}] ${f.message}${where}`)
2372
- }
2373
- }
2374
-
2375
- async function upgrade(): Promise<void> {
2376
- const config = await siteConfig()
2377
- switch (positional[0]) {
2378
- case 'ledger': {
2379
- const db = await connect({ file: config.sqliteFile })
2380
- try {
2381
- for (const row of await readLedger(db))
2382
- console.log(
2383
- ` ${row.appliedAt} ${row.kind.padEnd(8)} ${row.bundle ? `[${row.bundle}] ` : ''}${row.name} ${row.durationMs}ms${row.appliedBy ? ` by ${row.appliedBy}` : ''}${row.note ? ` (${row.note})` : ''}`,
2384
- )
2385
- } finally {
2386
- await db.close()
2387
- }
2388
- return
2389
- }
2390
- case 'check': {
2391
- if (!flags.has('--fix')) {
2392
- const report = await ops.upgradeCheck(config, { set: parseSet() })
2393
- for (const bundle of report.bundles)
2394
- if (bundle.pending.length > 0)
2395
- console.log(
2396
- ` bundle ${bundle.id}: ${bundle.pending.map((step) => `${step.name} (${step.kind})`).join(', ')}`,
2397
- )
2398
- printFindings(report)
2399
- if (!report.siteChecked)
2400
- console.log(
2401
- 'The site schema is checked once the framework migrations have run (`crombolo upgrade`).',
2402
- )
2403
- if (report.blocking) process.exit(1)
2404
- return
2405
- }
2406
- const fix = await ops.upgradeFix(config, {
2407
- set: parseSet(),
2408
- backupTaken: flags.has('--backup-taken'),
2409
- })
2410
- if (fix.backup.path) console.log(` backup ${fix.backup.path}`)
2411
- console.log(
2412
- ` sync ${fix.sync.action}${fix.sync.deferred.length > 0 ? ` (deferred to the cut-over: ${fix.sync.deferred.join(', ')})` : ''}`,
2413
- )
2414
- for (const name of fix.applied) console.log(` applied ${name}`)
2415
- if (fix.skipped > 0)
2416
- console.log(` skipped ${fix.skipped} value(s) a converter refused; see the findings`)
2417
- printFindings(fix)
2418
- return
2419
- }
2420
- case 'schema': {
2421
- positional[0] = 'rewrite'
2422
- return schema()
2423
- }
2424
- case undefined: {
2425
- if (flags.has('--plan')) {
2426
- const plan = await ops.upgradePlan(config)
2427
- if (plan.steps.length === 0) console.log('No framework migration is pending.')
2428
- const printStep = (step: ops.PlannedStepResult, indent = '') => {
2429
- console.log(
2430
- `\n${indent}== ${step.name} (${step.kind}${step.release ? `, release ${step.release}` : ''})`,
2431
- )
2432
- if (step.unplannable) console.log(`${indent} unplannable: ${step.unplannable}`)
2433
- for (const sql of step.statements) console.log(`${indent} ${sql};`)
2434
- }
2435
- for (const step of plan.steps) printStep(step)
2436
- // After core's, in the order `bundles` lists them, because that is the order they run.
2437
- for (const bundle of plan.bundles) {
2438
- console.log(
2439
- `\nbundle ${bundle.id} (${bundle.name}): ${bundle.steps.length === 0 ? 'nothing pending' : `${bundle.steps.length} step(s)`}`,
2440
- )
2441
- for (const step of bundle.steps) printStep(step, ' ')
2442
- }
2443
- console.log(
2444
- plan.site.checked
2445
- ? `\nsite schema: ${plan.site.action} (${plan.site.classification})`
2446
- : '\nsite schema: checked once the framework migrations have run',
2447
- )
2448
- return
2449
- }
2450
- const forceIndex = rest.indexOf('--force')
2451
- const result = await ops.upgradeRun(config, {
2452
- force: forceIndex >= 0 ? rest[forceIndex + 1] : undefined,
2453
- set: parseSet(),
2454
- backupTaken: flags.has('--backup-taken'),
2455
- })
2456
- if (result.backup.path) console.log(` backup ${result.backup.path}`)
2457
- if (result.action === 'refused') {
2458
- console.error(`upgrade refused: ${result.reasons.length} outstanding finding(s)`)
2459
- for (const f of result.reasons)
2460
- console.error(` [${f.kind.padEnd(8)}] ${f.message}${f.link ? ` ${f.link}` : ''}`)
2461
- console.error(
2462
- 'Run `crombolo upgrade check --fix`, resolve what needs a person, and try again.',
2463
- )
2464
- process.exit(1)
2465
- }
2466
- if (result.action === 'nothing')
2467
- return console.log('Nothing to upgrade: the database is at this version.')
2468
- for (const name of result.frameworkApplied) console.log(` applied ${name}`)
2469
- for (const bundle of result.bundlesApplied)
2470
- for (const name of bundle.applied) console.log(` applied [${bundle.id}] ${name}`)
2471
- console.log(
2472
- ` schema ${result.schema?.action} ${result.schema?.version ?? ''}+${result.schema?.revision ?? ''}`,
2473
- )
2474
- return
2475
- }
2476
- default:
2477
- return help()
2478
- }
2479
- }
2480
-
2481
- /**
2482
- * `bundle purge <id>`: drops a bundle's tables and forgets its state. Taking a
2483
- * bundle out of `bundles` leaves them in place, so re-adding it resumes; this is
2484
- * the deliberate act that does not.
2485
- */
2486
- async function bundlePurge(): Promise<void> {
2487
- const config = await siteConfig()
2488
- const id = positional[1]
2489
- if (!id) {
2490
- console.error('bundle purge needs the bundle id.')
2491
- process.exit(1)
2492
- }
2493
- const plan = await ops.bundlePurgePlan(config, id)
2494
- if (plan.tables.length === 0) console.log(` The bundle "${id}" has no tables in this database.`)
2495
- else {
2496
- console.log(` This drops ${plan.tables.length} table(s), and every row in them:`)
2497
- for (const table of plan.tables) console.log(` ${table}`)
2498
- }
2499
- if (!flags.has('--yes')) {
2500
- const answer = prompt(` Purge the bundle "${id}"? Type its id to confirm:`)
2501
- if (answer?.trim() !== id) {
2502
- console.log(' Nothing was dropped.')
2503
- process.exit(1)
2504
- }
2505
- }
2506
- const result = await ops.bundlePurge(config, id, { backupTaken: flags.has('--backup-taken') })
2507
- if (result.backup.path) console.log(` backup ${result.backup.path}`)
2508
- for (const table of result.dropped) console.log(` dropped ${table}`)
2509
- console.log(` The bundle "${id}"'s state is cleared; adding it again starts its tables afresh.`)
2510
- }
2511
-
2512
- /**
2513
- * `maintenance`: the cluster-wide pause on editing that a restore or a swap
2514
- * needs. It is a database flag, so it reaches every node on its next write.
2515
- */
2516
- async function maintenance(): Promise<void> {
2517
- const config = await siteConfig()
2518
- switch (positional[0]) {
2519
- case 'pause': {
2520
- const paused = await ops.pauseEditing(config, { reason: flagValue('--reason') })
2521
- console.log(`Editing paused${paused.reason ? `: ${paused.reason}` : ''}.`)
2522
- return
2523
- }
2524
- case 'resume':
2525
- await ops.resumeEditing(config)
2526
- console.log('Editing resumed.')
2527
- return
2528
- case 'status': {
2529
- const paused = await ops.editingPaused(config)
2530
- if (flags.has('--json')) {
2531
- console.log(JSON.stringify({ paused: paused ?? null }, null, 2))
2532
- return
2533
- }
2534
- console.log(
2535
- paused
2536
- ? `Editing paused since ${paused.at}${paused.by ? ` by ${paused.by}` : ''}${paused.reason ? `: ${paused.reason}` : ''}.`
2537
- : 'Editing is not paused.',
2538
- )
2539
- return
2540
- }
2541
- default:
2542
- return help()
2543
- }
2544
- }
2545
-
2546
- function help(): void {
2547
- console.log(`crombolo <command>
2548
-
2549
- start [--keep-admin] boot the site in the current directory
2550
- [--bundle <pkg>] apply a bundle's content first, once per bundle, then serve;
2551
- takes the \`content import\` flags (--publish, --under, …)
2552
- [--from <file>] in an empty folder: scaffold a site, install it, import a
2553
- whole-site file (crombolo export --site) and serve it; the
2554
- new administrator's password is printed once
2555
- [--postgres] [--name <site name>] [--no-install]
2556
- init [--force] [--name <site name>] [--postgres] [--template <id>]
2557
- scaffold a site here; --template list prints the
2558
- starter templates, which bring schema, views and content
2559
- status [--json] what this environment resolved to, and what is wrong with
2560
- it; non-zero when something would stop the site working
2561
- admin reset-password [pw] reset the administrator's password
2562
- schema check [--static] validate schema/ and report what a sync would do
2563
- schema sync [--force-retire-types]
2564
- apply schema/ to the database
2565
- schema export write the database's definitions as schema/ files
2566
- schema rewrite rewrite every schema/ file canonically
2567
- schema new <content-type|media-type|member-type> <alias>
2568
- write the type file, with keys, and the view it points at
2569
- [--name <name>] [--description <text>] [--icon <icon>]
2570
- [--at-root] [--element] [--tab <tab>] [--no-view]
2571
- schema add-property <type> <alias> --type <data type>
2572
- append a property to that type and apply it
2573
- [--name <name>] [--mandatory] [--tab <tab>] [--dry-run]
2574
- schema purge --older-than N delete properties retired more than N days ago, with their values
2575
- generate schema/content-types.d.ts from schema/
2576
- components check compile every component, and report one a type declares with no file
2577
- components list what is in components/, and which type declares each
2578
- components new <alias> components/<alias>.tsx; the alias may carry folders
2579
- assets list the stylesheets and scripts this site has
2580
- assets new <stylesheet|script> <name>
2581
- write an empty one in the place the backoffice edits
2582
- export --site [--out <file.tgz>]
2583
- the whole site in one file: schema, components, styles,
2584
- scripts, dictionary, every page and media item and their
2585
- files. Domains and people stay behind
2586
- [--no-media] [--drafts] [--with-members] [--with-users]
2587
- [--anonymise] replaces every personal value as it writes
2588
- content export --root <path|uuid> --out <dir>
2589
- write a private bundle package: the subtree, its
2590
- dependencies and what the destination must already have
2591
- [--only] [--drafts] [--with-blueprints]
2592
- [--name <npm name>] [--package-version <version>]
2593
- Names and the tree only, unless [--include-content]:
2594
- values, media files [--with-blobs] and protected-page
2595
- usernames, encrypted under a passphrase (prompted, or
2596
- CROMBOLO_BUNDLE_PASSPHRASE); [--allow-content-unencrypted]
2597
- writes them readable. [--with-members] [--with-users]
2598
- add people, always encrypted
2599
- A bundle is an npm package. <pkg> is its directory, or the name of one this
2600
- site depends on.
2601
- content check <pkg> what installing its content here would do, and what it
2602
- needs from a person first; writes to the Changes dashboard
2603
- [--under <path|uuid>] [--resolve <key>=take-bundle|keep-local|skip]
2604
- [--resolve <key>/<property>[@<culture>]=take-bundle|keep-local]
2605
- [--resolve-all <choice>] [--allow-missing-blobs] [--save]
2606
- [--publish: also say which pages the import would not publish]
2607
- An encrypted bundle asks for its passphrase, or reads
2608
- CROMBOLO_BUNDLE_PASSPHRASE
2609
- content import <pkg> re-check, back up, then apply: documents and elements
2610
- arrive as drafts, media is live. One transaction, so a
2611
- failure leaves nothing behind
2612
- [--publish] [--label <text>] [--backup-taken] + the check flags
2613
- [--no-schema] [--no-files] decline a section it carries
2614
- [--replace-files] allow replacing files this site has;
2615
- refused without it, and the originals are kept for a revert
2616
- bundle check <pkg> the same as content check
2617
- bundle install <pkg> the same as content import; a bundle carrying its own
2618
- schema and views installs into a site that has neither
2619
- content runs [--limit N] the imports applied here, newest first
2620
- content publish <path|uuid> take it live; --descendants for the branch below it,
2621
- --culture <iso> per culture, --at/--until to schedule
2622
- content unpublish <path|uuid>
2623
- take it down; the same flags
2624
- content revert <run-id> put an import back: values and published state restored,
2625
- nodes it created unpublished and recycled. Itself a run,
2626
- so it can be reverted in turn
2627
- [--resolve <key>=discard|skip] [--resolve-all <choice>]
2628
- [--force] [--backup-taken]
2629
- dictionary export --out <file.udt> [--key <uuid>]
2630
- the dictionary as Umbraco's .udt, to seed another environment
2631
- dictionary import <file.udt>
2632
- apply one: items upsert by key, and a translation for a
2633
- language this site has not got is skipped
2634
- domains the hostnames domains.toml declares and what is bound here
2635
- domains set <path|uuid|/> --host <host>[=<culture>] [--host ...] [--default-culture <iso>]
2636
- write domains.toml and apply it; the page's hostnames are
2637
- replaced, and \${VAR} in a host is read from the environment
2638
- domains clear <path|uuid|/> unbind that page, in the file and in the database
2639
- domains apply apply domains.toml here, as a boot does
2640
- domains undo put back the file the last write replaced, and apply it
2641
- remote add <name> <url> [--env <id>]
2642
- a remote this site pulls from and pushes to, kept in
2643
- remotes.toml (URLs only; sign-ins are in your keychain)
2644
- remote list | remove <name>
2645
- login [<remote>] sign in with a code you approve in the remote's back office
2646
- logout [<remote>] | whoami [<remote>]
2647
- pull [<remote>] [--env <id>] [--full] [--yes]
2648
- a copy of the environment, sealed and anonymised unless
2649
- --full: started here in an empty folder, or replacing this
2650
- site (what it loses is recycled; --yes skips the question)
2651
- [--no-media] [--media-info-only] fetches media files one at a
2652
- time, only what changed since the last pull; [--lazy-media]
2653
- fetches each the first time it is asked for
2654
- push [<remote>] [--env <id>] [--force] [--auto-approve] [--media-info-only]
2655
- replace the environment with this site; shows what it would
2656
- replace first. A protected one needs --force and its name
2657
- typed back (--auto-approve skips that); the remote may also
2658
- hold it for someone else's approval
2659
- exit 0 done, 1 refused or failed, 2 refused before sending,
2660
- 3 gave up waiting
2661
- media fetch a lazily pulled site's media files, all of them now
2662
- import <file> [--replace | --replace-all]
2663
- replace this site with a whole-site file. Says first what
2664
- would be replaced, and changes nothing without a mode:
2665
- --replace recycles content and media the file lacks and
2666
- keeps this site's people; --replace-all deletes them, and
2667
- people the file lacks (typed confirmation, or --yes).
2668
- Domains, the secret key and settings always stay.
2669
- Undone with \`content revert <run>\`
2670
- [--allow-missing-media] [--backup-taken]
2671
- import umbraco report <backup> [--site <dir>] [--media <dir>] [--out <dir>] [--json]
2672
- read-only: what importing an Umbraco backup would bring,
2673
- and what it would not — packages, Razor, custom code.
2674
- <backup> is a .bacpac or Umbraco's SQLite file; --site is
2675
- the site's files, for its views, media and plugins.
2676
- Non-zero when something blocks the import
2677
- import umbraco apply <backup> --out <dir> [--site <dir>] [--media <dir>]
2678
- write a crombolo site: schema, view stubs, media and a
2679
- content bundle that \`bun start\` imports on first boot
2680
- [--name <site name>] [--drafts] [--postgres] [--force]
2681
- [--include-content] [--allow-content-unencrypted]
2682
- [--with-members] [--with-users], as content export
2683
- upgrade check [--set t.p=v] what the deployed schema/ would do to this database's data
2684
- upgrade check --fix back up; apply the additive part and every conversion early
2685
- upgrade [--force <reason>] re-check, back up, apply framework steps, cut over, ledger
2686
- upgrade --plan the DDL pending framework migrations would run
2687
- upgrade ledger print migration_history
2688
- bundle purge <id> [--yes] back up, then drop a bundle's tables and clear its state;
2689
- refused while crombolo.config.ts still wires it
2690
- maintenance pause [--reason <text>]
2691
- refuse every editor's save, on every node, until resumed;
2692
- readers are unaffected and no node drains
2693
- maintenance resume let editors save again
2694
- maintenance status [--json] whether editing is paused, by whom and why
2695
- (Postgres: --backup-taken or CROMBOLO_PG_DUMP before anything that writes)`)
2696
- }
2697
-
2698
- try {
2699
- // Before dispatching, which rewrites `bundle install` as `content import`.
2700
- const audited = commandAudit()
2701
- await dispatch()
2702
- if (audited) await ops.recordCommand(await siteConfig(), audited)
2703
- } catch (error) {
2704
- // These already say what happened and what to do; a stack trace would bury it.
2705
- if (error instanceof MigrationStepError || error instanceof UpgradeStateError) {
2706
- console.error(error.message)
2707
- process.exit(1)
2708
- }
2709
- if (!(error instanceof ops.CommandError)) throw error
2710
- for (const problem of error.problems) console.error(` ${problem}`)
2711
- console.error(error.message)
2712
- process.exit(1)
2713
- }
2714
-
2715
- // A declaration, not a const: the top-level dispatch above runs before this line.
2716
- function capital(word: string): string {
2717
- return `${word.charAt(0).toUpperCase()}${word.slice(1)}`
2718
- }
2719
-
2720
- /** The audit log entry for a command that changes the environment; undefined for one that only reads. */
2721
- function commandAudit(): ops.CommandAudit | undefined {
2722
- if (flags.has('--dry-run') || flags.has('--plan') || flags.has('--help')) return undefined
2723
- const sub = positional[0]
2724
- const typed = `crombolo ${command}${sub ? ` ${sub}` : ''}`
2725
- const entry = (
2726
- category: ops.CommandAudit['category'],
2727
- action: string,
2728
- entityType?: string,
2729
- entityKey?: string,
2730
- ): ops.CommandAudit => ({ category, action, command: typed, entityType, entityKey })
2731
- switch (command) {
2732
- case 'admin':
2733
- return sub === 'reset-password' ? entry('user', 'ResetPassword', 'User') : undefined
2734
- case 'schema':
2735
- if (sub === 'sync') return entry('schema', 'Sync', 'Schema')
2736
- return sub === 'purge' ? entry('schema', 'Purge', 'Schema') : undefined
2737
- case 'content':
2738
- case 'bundle':
2739
- if (sub === 'purge') return entry('system', 'Purge', 'Bundle', positional[1])
2740
- if (sub === 'import' || sub === 'install') return entry('content', 'Import', 'Bundle')
2741
- return sub === 'publish' || sub === 'unpublish' || sub === 'revert'
2742
- ? entry('content', capital(sub), 'Document', positional[1])
2743
- : undefined
2744
- case 'dictionary':
2745
- return sub === 'import' ? entry('settings', 'Import', 'DictionaryItem') : undefined
2746
- case 'domains':
2747
- return sub && sub !== 'list'
2748
- ? entry('settings', `${capital(sub)}Domains`, 'Domain')
2749
- : undefined
2750
- case 'upgrade':
2751
- if (sub === undefined) return entry('system', 'Upgrade')
2752
- return sub === 'check' && flags.has('--fix') ? entry('system', 'UpgradeFix') : undefined
2753
- case 'maintenance':
2754
- if (sub === 'pause') return entry('system', 'PauseEditing')
2755
- return sub === 'resume' ? entry('system', 'ResumeEditing') : undefined
2756
- default:
2757
- return undefined
2758
- }
2759
- }
2760
-
2761
- // ---------------------------------------------------------------- remotes (story 108)
2762
-
2763
- const keychain = () => osKeychain()
2764
-
2765
- /** Exit codes a pull or push states: 1 the remote refused or failed, 2 refused before sending, 3 gave up waiting. */
2766
- function remoteExit(error: unknown): never {
2767
- if (error instanceof RefusedBeforeSending) {
2768
- console.error(` ${error.message}`)
2769
- process.exit(2)
2770
- }
2771
- if (error instanceof TimedOut) {
2772
- console.error(` ${error.message} The job carries on at the remote.`)
2773
- process.exit(3)
2774
- }
2775
- if (error instanceof RemoteError) {
2776
- console.error(` ${error.message}`)
2777
- for (const item of error.problem.items ?? []) console.error(` ${item}`)
2778
- process.exit(error.problem.code === 'sign-in-required' ? 2 : 1)
2779
- }
2780
- console.error(` ${error instanceof Error ? error.message : String(error)}`)
2781
- process.exit(1)
2782
- }
2783
-
2784
- function openInBrowser(url: string): void {
2785
- if (flags.has('--no-browser')) return
2786
- const opener =
2787
- process.platform === 'darwin'
2788
- ? ['open']
2789
- : process.platform === 'win32'
2790
- ? ['cmd', '/c', 'start', '']
2791
- : ['xdg-open']
2792
- try {
2793
- Bun.spawn([...opener, url], { stdout: 'ignore', stderr: 'ignore' })
2794
- } catch {
2795
- // Printed above; opening it is a convenience.
2796
- }
2797
- }
2798
-
2799
- /** The remote a command names, signed in or not, with the environment it goes to. */
2800
- async function connectRemote(name: string | undefined) {
2801
- const entry = pickRemote(cwd, name)
2802
- if (!entry) {
2803
- const known = readRemotes(cwd)
2804
- console.error(
2805
- known.length === 0
2806
- ? ' No remotes yet: `crombolo remote add <name> <url>`.'
2807
- : ` Which remote? ${known.map((r) => r.name).join(', ')}`,
2808
- )
2809
- process.exit(2)
2810
- }
2811
- const discovery = await discover(entry.url).catch(remoteExit)
2812
- const client = new RemoteClient(discovery, keychain())
2813
- const environment =
2814
- flagValue('--env') ??
2815
- entry.environment ??
2816
- (discovery.environments.length === 1 ? discovery.environments[0]?.id : undefined)
2817
- return { entry, discovery, client, environment }
2818
- }
2819
-
2820
- async function remoteCommand(): Promise<void> {
2821
- const [sub, name, url] = positional
2822
- if (sub === 'add') {
2823
- if (!name || !url) {
2824
- console.error("remote add needs a name and the site's URL.")
2825
- process.exit(2)
2826
- }
2827
- const discovery = await discover(url).catch(remoteExit)
2828
- addRemote(cwd, { name, url: new URL(url).origin, environment: flagValue('--env') })
2829
- console.log(` added ${name} ${discovery.name} (${new URL(url).origin})`)
2830
- for (const env of discovery.environments)
2831
- console.log(` env ${env.id}${env.protected ? ' protected' : ''}`)
2832
- console.log(`\n\`crombolo login ${name}\` signs in.`)
2833
- return
2834
- }
2835
- if (sub === 'remove') {
2836
- if (!name || !removeRemote(cwd, name)) {
2837
- console.error(` There is no remote "${name ?? ''}".`)
2838
- process.exit(2)
2839
- }
2840
- console.log(` removed ${name}`)
2841
- return
2842
- }
2843
- const remotes = readRemotes(cwd)
2844
- if (remotes.length === 0) console.log(' No remotes yet: `crombolo remote add <name> <url>`.')
2845
- for (const remote of remotes) {
2846
- const signed = (await readSignIn(keychain(), remote.url).catch(() => undefined)) !== undefined
2847
- console.log(
2848
- ` ${remote.name.padEnd(12)} ${remote.url}${remote.environment ? ` env ${remote.environment}` : ''}${signed ? '' : ' (not signed in)'}`,
2849
- )
2850
- }
2851
- }
2852
-
2853
- async function loginCommand(): Promise<void> {
2854
- const { entry, discovery } = await connectRemote(positional[0])
2855
- await signIn(discovery, keychain(), {
2856
- clientName: `crombolo on ${hostname()}`,
2857
- onCode: (code, url) => {
2858
- console.log(` Approve this sign-in at ${url}`)
2859
- console.log(` and check it shows ${code}`)
2860
- openInBrowser(url)
2861
- },
2862
- }).catch(remoteExit)
2863
- console.log(` signed in to ${entry.name}`)
2864
- }
2865
-
2866
- async function logoutCommand(): Promise<void> {
2867
- const { entry, discovery } = await connectRemote(positional[0])
2868
- const signedIn = await readSignIn(keychain(), discovery.api)
2869
- // Ended at the remote too, best effort: the device leaves "Signed-in devices".
2870
- if (signedIn)
2871
- await fetch(signedIn.tokenUrl.replace(/\/token$/, '/revoke'), {
2872
- method: 'POST',
2873
- headers: { 'content-type': 'application/x-www-form-urlencoded' },
2874
- body: new URLSearchParams({ token: signedIn.accessToken }).toString(),
2875
- }).catch(() => undefined)
2876
- await forgetSignIn(keychain(), discovery.api)
2877
- console.log(` signed out of ${entry.name}`)
2878
- }
2879
-
2880
- async function whoamiCommand(): Promise<void> {
2881
- const { entry, client, discovery } = await connectRemote(positional[0])
2882
- const response = await client
2883
- .call(`${new URL(discovery.api).origin}/umbraco/management/api/v1/user/current`)
2884
- .catch(remoteExit)
2885
- if (!response.ok) remoteExit(await problemOf(response))
2886
- const me = (await response.json()) as { name: string; email: string }
2887
- console.log(` ${me.name} <${me.email}> on ${entry.name}`)
2888
- for (const env of await client.environments().catch(remoteExit)) {
2889
- const may = [env.may.pull && 'pull', env.may.pullFull && 'full pull', env.may.push && 'push']
2890
- .filter(Boolean)
2891
- .join(', ')
2892
- console.log(` ${env.id.padEnd(12)} ${may || 'nothing'}${env.protected ? ' (protected)' : ''}`)
2893
- }
2894
- }
2895
-
2896
- const describeJob = (job: Job) =>
2897
- ` ${job.kind.padEnd(5)} ${job.state}${job.stage ? `, ${job.stage}` : ''}${job.progress != null ? ` (${job.progress}%)` : ''}`
2898
-
2899
- async function pullCommand(): Promise<void> {
2900
- const { client, environment, discovery } = await connectRemote(positional[0])
2901
- if (!environment) {
2902
- console.error(
2903
- ` Which environment? ${discovery.environments.map((e) => e.id).join(', ')} (--env)`,
2904
- )
2905
- process.exit(2)
2906
- }
2907
- const media = flags.has('--no-media')
2908
- ? ('omitted' as const)
2909
- : flags.has('--media-info-only') || flags.has('--lazy-media')
2910
- ? ('referenced' as const)
2911
- : ('included' as const)
2912
- const incoming = join(cwd, '.crombolo', 'transfer', 'incoming')
2913
- await mkdir(incoming, { recursive: true })
2914
- const out = join(incoming, `pull-${environment}-${Date.now().toString(36)}.tgz`)
2915
- const pulled = await pullFile(client, {
2916
- environment,
2917
- out,
2918
- full: flags.has('--full'),
2919
- media,
2920
- onProgress: (job) => console.log(describeJob(job)),
2921
- }).catch(remoteExit)
2922
- console.log(
2923
- ` pulled ${describeBytes(pulled.size)}${flags.has('--full') ? '' : ', anonymised'}`,
2924
- )
2925
- if (media !== 'included') flags.add('--allow-missing-media')
2926
-
2927
- const existing = existsSync(join(cwd, 'crombolo.config.ts'))
2928
- if (!existing) {
2929
- await startFromFile(out, pulled.passphrase)
2930
- } else {
2931
- const config = await siteConfig()
2932
- const { summary } = await ops.siteReplaceCheck(config, {
2933
- bundle: out,
2934
- passphrase: pulled.passphrase,
2935
- replace: 'replace',
2936
- allowMissingBlobs: media !== 'included',
2937
- })
2938
- const leaving = Object.entries(summary.leaving)
2939
- if (leaving.length > 0)
2940
- console.log(
2941
- ` recycled ${leaving.map(([kind, n]) => `${n} ${kind}`).join(', ')} here, not in ${environment}`,
2942
- )
2943
- if (summary.filesRemoved.length > 0)
2944
- console.log(` removes ${summary.filesRemoved.length} file(s) here, kept for a revert`)
2945
- if (!flags.has('--yes')) {
2946
- if (!process.stdin.isTTY) {
2947
- console.error(`\nThis replaces this site with ${environment}: pass --yes to go on.`)
2948
- process.exit(2)
2949
- }
2950
- const answer = prompt(` Replace this site with ${environment}? [y/N]`)
2951
- if (!/^y(es)?$/i.test(answer?.trim() ?? '')) {
2952
- console.error(' Nothing was changed.')
2953
- process.exit(2)
2954
- }
2955
- }
2956
- const result = await ops.contentImport(config, {
2957
- bundle: out,
2958
- passphrase: pulled.passphrase,
2959
- replace: 'replace',
2960
- allowMissingBlobs: media !== 'included',
2961
- label: `pull from ${environment}`,
2962
- backupTaken: flags.has('--backup-taken'),
2963
- })
2964
- for (const problem of result.problems) console.error(` ${problem}`)
2965
- if (!result.runId) {
2966
- printTransferFindings(result.check)
2967
- console.error('\npull refused. Nothing was changed.')
2968
- process.exit(1)
2969
- }
2970
- console.log(
2971
- ` replaced, run ${result.runId}: \`crombolo content revert ${result.runId}\` puts it back`,
2972
- )
2973
- }
2974
-
2975
- if (media === 'referenced') {
2976
- const config = await siteConfig()
2977
- const blobs = await referencedBlobs(out)
2978
- if (flags.has('--lazy-media')) {
2979
- writeRemoteMedia(cwd, { api: discovery.api, environment })
2980
- await writeFile(
2981
- join(cwd, '.crombolo', 'remote-media-blobs.json'),
2982
- `${JSON.stringify(
2983
- blobs.map((b) => ({ key: b.key, etag: b.etag })),
2984
- null,
2985
- 2,
2986
- )}\n`,
2987
- )
2988
- console.log(
2989
- ` media ${blobs.length} file(s) fetched from ${environment} as they are first asked for`,
2990
- )
2991
- } else {
2992
- const fetched = await fetchReferencedMedia(client, {
2993
- environment,
2994
- blobs,
2995
- store: await mediaStoreFor(config),
2996
- siteDir: cwd,
2997
- }).catch(remoteExit)
2998
- console.log(
2999
- ` media ${fetched.fetched} fetched (${describeBytes(fetched.bytes)}), ${fetched.skipped} already here`,
3000
- )
3001
- for (const key of fetched.failed)
3002
- console.error(` missing ${key}: run the pull again to fetch it`)
3003
- }
3004
- }
3005
- if (!existing) await start()
3006
- }
3007
-
3008
- async function pushCommand(): Promise<void> {
3009
- const { client, environment, discovery } = await connectRemote(positional[0])
3010
- if (!environment) {
3011
- console.error(
3012
- ` Which environment? ${discovery.environments.map((e) => e.id).join(', ')} (--env)`,
3013
- )
3014
- process.exit(2)
3015
- }
3016
- const config = await siteConfig()
3017
- const result = await pushSite(config, client, {
3018
- environment,
3019
- force: flags.has('--force'),
3020
- autoApprove: flags.has('--auto-approve'),
3021
- mediaInfoOnly: flags.has('--media-info-only'),
3022
- onCheck: (check) => {
3023
- const counts = Object.entries(check.sending.counts)
3024
- console.log(
3025
- ` sending ${counts.map(([kind, n]) => `${n} ${kind}`).join(', ') || 'no content'}, ${check.sending.files} file(s)`,
3026
- )
3027
- if (check.fromRemote) {
3028
- const recycles = Object.entries(check.recycles)
3029
- if (recycles.length > 0)
3030
- console.log(
3031
- ` recycles ${recycles.map(([kind, n]) => `${n} ${kind}`).join(', ')} on ${environment}`,
3032
- )
3033
- if (check.removesFiles.length > 0)
3034
- console.log(` removes ${check.removesFiles.join(', ')} on ${environment}`)
3035
- }
3036
- },
3037
- confirm: async () => {
3038
- if (!process.stdin.isTTY) return false
3039
- const answer = prompt(` This replaces ${environment}. Type its name to go on:`)
3040
- return answer?.trim() === environment
3041
- },
3042
- onProgress: (job) => console.log(describeJob(job)),
3043
- onApproval: (job) => {
3044
- console.log(` waiting for someone else to approve it: ${job.approvalUrl}`)
3045
- if (job.approvalUrl) openInBrowser(job.approvalUrl)
3046
- },
3047
- }).catch(remoteExit)
3048
- const { job } = result
3049
- if (job.state === 'done') {
3050
- console.log(`\n pushed ${environment} replaced, run ${job.runId ?? '—'}`)
3051
- return
3052
- }
3053
- console.error(`\n ${job.problem?.title ?? `the push ended ${job.state}`}`)
3054
- for (const item of job.problem?.items ?? []) console.error(` ${item}`)
3055
- process.exit(1)
3056
- }
3057
-
3058
- async function mediaCommand(): Promise<void> {
3059
- if (positional[0] !== 'fetch') return help()
3060
- const source = readRemoteMedia(cwd)
3061
- const listed = join(cwd, '.crombolo', 'remote-media-blobs.json')
3062
- if (!source || !existsSync(listed)) {
3063
- console.error(' This site was not pulled with --lazy-media: there is nothing to fetch.')
3064
- process.exit(2)
3065
- }
3066
- const discovery = await discover(source.api).catch(remoteExit)
3067
- const client = new RemoteClient(discovery, keychain())
3068
- const config = await siteConfig()
3069
- const blobs = JSON.parse(await Bun.file(listed).text()) as { key: string; etag: string | null }[]
3070
- const fetched = await fetchReferencedMedia(client, {
3071
- environment: source.environment,
3072
- blobs,
3073
- store: await mediaStoreFor(config),
3074
- siteDir: cwd,
3075
- onProgress: (done, total) => {
3076
- if (done % 50 === 0 || done === total) console.log(` ${done}/${total}`)
3077
- },
3078
- }).catch(remoteExit)
3079
- console.log(
3080
- ` media ${fetched.fetched} fetched (${describeBytes(fetched.bytes)}), ${fetched.skipped} already here`,
3081
- )
3082
- for (const key of fetched.failed) console.error(` missing ${key}`)
3083
- if (fetched.failed.length > 0) process.exit(1)
3084
- }
3085
-
3086
- async function dispatch(): Promise<void> {
3087
- switch (command) {
3088
- case 'start':
3089
- await start()
3090
- break
3091
- case 'init':
3092
- await init()
3093
- break
3094
- case 'status':
3095
- await status()
3096
- break
3097
- case 'admin':
3098
- await admin()
3099
- break
3100
- case 'schema':
3101
- await schema()
3102
- break
3103
- case 'generate':
3104
- await generate()
3105
- break
3106
- case 'content':
3107
- await content()
3108
- break
3109
- /**
3110
- * The artifact's own name. A bundle that carries its own structure is what
3111
- * the Packages section builds (`docs/bundles.md`), and `install` reads
3112
- * better than `import` for one — but it is the same operation on the same
3113
- * artifact, so this is the same code path, not a second one.
3114
- */
3115
- case 'bundle':
3116
- if (positional[0] === 'purge') {
3117
- await bundlePurge()
3118
- break
3119
- }
3120
- if (positional[0] === 'install') positional[0] = 'import'
3121
- await content()
3122
- break
3123
- case 'dictionary':
3124
- await dictionary()
3125
- break
3126
- case 'domains':
3127
- await domains()
3128
- break
3129
- case 'components':
3130
- await components()
3131
- break
3132
- case 'assets':
3133
- await assets()
3134
- break
3135
- case 'export':
3136
- await exportCommand()
3137
- break
3138
- case 'remote':
3139
- await remoteCommand()
3140
- break
3141
- case 'login':
3142
- await loginCommand()
3143
- break
3144
- case 'logout':
3145
- await logoutCommand()
3146
- break
3147
- case 'whoami':
3148
- await whoamiCommand()
3149
- break
3150
- case 'pull':
3151
- await pullCommand()
3152
- break
3153
- case 'push':
3154
- await pushCommand()
3155
- break
3156
- case 'media':
3157
- await mediaCommand()
3158
- break
3159
- case 'import':
3160
- await importSite()
3161
- break
3162
- case 'upgrade':
3163
- await upgrade()
3164
- break
3165
- case 'maintenance':
3166
- await maintenance()
3167
- break
3168
- default:
3169
- help()
3170
- }
3171
- }
9
+ await main()