@otwdesign/cli 1.0.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@otwdesign/cli",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
4
4
  "description": "The cms command for OTW CMS sites: generate, migrate, update and serve.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "OTW SRL",
@@ -41,9 +41,9 @@
41
41
  "@types/semver": "^7.7.0",
42
42
  "semver": "^7.8.0",
43
43
  "tsx": "^4.23.15",
44
- "@otwdesign/next": "1.0.2",
45
- "@otwdesign/core": "1.0.2",
46
- "@otwdesign/sdk": "1.0.2"
44
+ "@otwdesign/next": "1.1.0",
45
+ "@otwdesign/core": "1.1.0",
46
+ "@otwdesign/sdk": "1.1.0"
47
47
  },
48
48
  "devDependencies": {
49
49
  "@opentelemetry/api": "~1.9.0",
package/src/migrate.ts CHANGED
@@ -37,14 +37,19 @@ async function pgDumpAvailable(): Promise<boolean> {
37
37
  * Copies the embedded database's directory, or dumps Postgres with `pg_dump`. The password goes
38
38
  * through the environment, not the command line, where other users could read it.
39
39
  */
40
- async function backUp(options: MigrateOptions): Promise<string | null> {
40
+ export async function backUp(
41
+ options: Pick<MigrateOptions, 'root' | 'env' | 'now' | 'hasPgDump'> & {
42
+ /** The command that skips the backup, for the error when `pg_dump` is missing. */
43
+ withoutBackup?: string
44
+ },
45
+ ): Promise<string | null> {
41
46
  const { env, root } = options
42
47
  const stamp = (options.now ?? (() => new Date()))().toISOString().replace(/[:.]/g, '-')
43
48
  if (env.DATABASE_URL) {
44
49
  const dir = join(root, '.data', 'backups')
45
50
  if (!(await (options.hasPgDump ?? pgDumpAvailable)())) {
46
51
  throw new Error(
47
- 'pg_dump is not installed, so the database cannot be backed up first. Take a backup (many hosts do this for you), then run `cms migrate --no-backup`.',
52
+ `pg_dump is not installed, so the database cannot be backed up first. Take a backup (many hosts do this for you), then run \`${options.withoutBackup ?? 'cms migrate --no-backup'}\`.`,
48
53
  )
49
54
  }
50
55
  const url = new URL(env.DATABASE_URL)
@@ -9,7 +9,7 @@ import { detectPackageManager, planSite } from '../update.ts'
9
9
  import { ControlError, createControlServer } from './control.ts'
10
10
  import { startRelease } from './processes.ts'
11
11
  import { createProxy } from './proxy.ts'
12
- import { buildRelease, removeRelease, step, writeBack } from './releases.ts'
12
+ import { buildRelease, migrateRelease, removeRelease, writeBack } from './releases.ts'
13
13
  import { backupDatabase, loadState, restoreDatabase, saveState } from './state.ts'
14
14
  import { Supervisor, type Log } from './supervisor.ts'
15
15
 
@@ -92,20 +92,12 @@ export async function serve(options: ServeOptions): Promise<void> {
92
92
  plan: (release, scope) => planSite(release.dir, scope),
93
93
  build: (id, changes, log) => buildRelease(builder, id, changes, log),
94
94
  migrate: (release, log) =>
95
- step(
96
- process.execPath,
97
- [
98
- join(release.dir, 'node_modules', '@otwdesign', 'cli', 'bin', 'cms.js'),
99
- 'migrate',
100
- '--root',
101
- release.dir,
102
- // The supervisor copies the embedded database itself, and puts it back on failure.
103
- ...(database === 'embedded' || !options.backup ? ['--no-backup'] : []),
104
- ],
105
- release.dir,
106
- log,
95
+ migrateRelease(release, log, {
96
+ site: root,
97
+ siteEnv: env,
98
+ backup: options.backup,
107
99
  releaseEnv,
108
- ),
100
+ }),
109
101
  backup: async () =>
110
102
  database === 'embedded'
111
103
  ? backupDatabase(dataDir, join(root, '.cms', 'backups'), new Date())
@@ -1,8 +1,9 @@
1
1
  import { spawn } from 'node:child_process'
2
2
  import { copyFile, cp, mkdir, readdir, readFile, rm, writeFile } from 'node:fs/promises'
3
3
  import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path'
4
- import type { PackageChange } from '@otwdesign/core'
4
+ import type { Env, PackageChange } from '@otwdesign/core'
5
5
  import { MARKER, removeRoute } from '../generate.ts'
6
+ import { backUp } from '../migrate.ts'
6
7
  import { CORE_PACKAGES, readSitePackages, writeChanges, type PackageManager } from '../update.ts'
7
8
  import type { BuiltRelease, Log } from './supervisor.ts'
8
9
 
@@ -137,6 +138,52 @@ export async function buildRelease(
137
138
  return { id, dir, createdAt: new Date().toISOString(), versions }
138
139
  }
139
140
 
141
+ export interface ReleaseMigration {
142
+ site: string
143
+ /** The site's environment, which has its DATABASE_URL. */
144
+ siteEnv: Env
145
+ /** False for Postgres hosts that take their own backups. */
146
+ backup: boolean
147
+ /** The environment releases run with. */
148
+ releaseEnv: NodeJS.ProcessEnv
149
+ now?: () => Date
150
+ }
151
+
152
+ /**
153
+ * Runs the release's own `cms migrate`, which knows the release's plugins and migrations, but
154
+ * never lets it back up: it would write into the release's folder, deleted when the release is
155
+ * pruned. Postgres is dumped from here into the site's `.data/backups`, as `cms migrate` does in
156
+ * the site; the supervisor copies the embedded database itself, and puts it back on failure.
157
+ */
158
+ export async function migrateRelease(
159
+ release: BuiltRelease,
160
+ log: Log,
161
+ { site, siteEnv, backup, releaseEnv, now }: ReleaseMigration,
162
+ ): Promise<void> {
163
+ if (siteEnv.DATABASE_URL && backup) {
164
+ const file = await backUp({
165
+ root: site,
166
+ env: siteEnv,
167
+ now,
168
+ withoutBackup: 'cms serve --no-backup',
169
+ })
170
+ if (file) log(`Backed up the database to ${relative(site, file)}`)
171
+ }
172
+ await step(
173
+ process.execPath,
174
+ [
175
+ join(release.dir, 'node_modules', '@otwdesign', 'cli', 'bin', 'cms.js'),
176
+ 'migrate',
177
+ '--root',
178
+ release.dir,
179
+ '--no-backup',
180
+ ],
181
+ release.dir,
182
+ log,
183
+ releaseEnv,
184
+ )
185
+ }
186
+
140
187
  /** Files `cms generate` owns in a tree, relative to it. */
141
188
  async function managedFiles(root: string, dir = root): Promise<string[]> {
142
189
  const found: string[] = []