@beechcms/cms 0.6.0-preview.3 → 0.6.0-preview.4

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/bin/cli.mjs CHANGED
@@ -1,231 +1,270 @@
1
- #!/usr/bin/env node
2
- // @ts-check
3
-
4
- import { spawnSync } from 'node:child_process'
5
- import { existsSync } from 'node:fs'
6
- import { resolve } from 'node:path'
7
- import { pathToFileURL } from 'node:url'
8
-
9
-
10
- const [,, command, ...args] = process.argv
11
-
12
- const COMMANDS = {
13
- build: cmdBuild,
14
- 'seed:load': cmdSeedLoad,
15
- 'seed:create': cmdSeedCreate,
16
- 'init': cmdInit,
17
- 'validate': cmdValidate,
18
- 'deploy': cmdDeploy,
19
- 'update': cmdUpdate,
20
- 'onboard': cmdOnboard,
21
- 'reset': cmdReset,
22
- }
23
-
24
- function help() {
25
- console.log(`
26
- beech <command> [options]
27
-
28
- Commands:
29
- init Check project files and optionally initialise the database
30
- --db Also initialise the D1 database (system tables)
31
- --remote Target remote D1 instead of local (default: local)
32
- --db-name <n> Override D1 database name
33
-
34
- build Rebuild @beechcms/core after editing seeds.ts
35
-
36
- validate Validate SEED_REGISTRY for common errors (duplicate aliases,
37
- missing displayNameAlias, duplicate slugs). Exit code 1 on errors.
38
-
39
- seed:load Create/update content tables from SEED_REGISTRY
40
- --dry-run Print SQL without executing
41
- --diff Show schema differences vs current DB
42
- --remote Execute against remote D1 (default: local)
43
- --db <name> Override D1 database name
44
-
45
- seed:create Interactive wizard generate a new Seed definition and append
46
- it to seeds.ts, including SEED_REGISTRY entry
47
-
48
- deploy Deploy Worker, sync remote schema, and verify /admin
49
- --skip-seed Skip remote seed:load step
50
- --skip-check Skip /admin reachability check
51
-
52
- update Update @beechcms/api and @beechcms/core to latest, then
53
- apply any new system migrations to the local database
54
-
55
- onboard One-command local provisioning (init + seed:load). Designed
56
- for non-interactive use by agents and CI.
57
- --remote Target remote D1 instead of local (default: local)
58
- --yes Skip all interactive prompts (non-interactive mode)
59
- --db <name> Override D1 database name
60
-
61
- reset Reset database and/or Docker containers/volumes
62
- --db Wipe local Wrangler state & bootstrap D1 DB
63
- --docker Down Docker containers and wipe volumes
64
- --all Reset both (database & docker)
65
-
66
- Scaffold a new project (interactive, or pass --yes for non-interactive defaults):
67
- npm create @beechcms/cms [project-name] [--yes] [--with-examples]
68
-
69
- Golden path (local):
70
- npx beech onboard --local --yes # fully automated
71
- # or step by step:
72
- npx beech init --db --local
73
- npx beech seed:load --local
74
- npx wrangler dev
75
-
76
- Golden path (deploy):
77
- npx beech deploy
78
- npx beech init --db --remote # verify remote DB post-deploy
79
- `)
80
- }
81
-
82
- function cmdBuild() {
83
- console.log(
84
- '\nNo build step needed for BeechCMS projects.\n' +
85
- 'Edit seeds.ts then run `npx beech seed:load` to sync schema changes to D1.\n'
86
- )
87
- }
88
-
89
- async function tryLoadLocalRegistry() {
90
- const cwd = process.cwd()
91
-
92
- // Try compiled JS first (root or apps/api)
93
- const searchDirs = [cwd, resolve(cwd, 'apps', 'api')]
94
- for (const dir of searchDirs) {
95
- for (const name of ['seeds.js', 'seeds.mjs', 'seed.js', 'seed.mjs']) {
96
- const p = resolve(dir, name)
97
- if (existsSync(p)) {
98
- try {
99
- const mod = await import(pathToFileURL(p).href)
100
- if (mod.SEED_REGISTRY && typeof mod.SEED_REGISTRY === 'object') {
101
- return mod.SEED_REGISTRY
102
- }
103
- if (Array.isArray(mod.seeds)) {
104
- return Object.fromEntries(mod.seeds.map(s => [s.slug, s]))
105
- }
106
- if (Array.isArray(mod.default)) {
107
- return Object.fromEntries(mod.default.map(s => [s.slug, s]))
108
- }
109
- } catch {}
110
- }
111
- }
112
- }
113
-
114
- // Try seeds.ts / seed.ts (root or apps/api)
115
- let tsPath = null
116
- for (const dir of searchDirs) {
117
- const p = existsSync(resolve(dir, 'seeds.ts'))
118
- ? resolve(dir, 'seeds.ts')
119
- : resolve(dir, 'seed.ts')
120
- if (existsSync(p)) {
121
- tsPath = p
122
- break
123
- }
124
- }
125
-
126
- if (tsPath) {
127
- const result = spawnSync(process.execPath, [
128
- '--experimental-strip-types',
129
- '--input-type=module',
130
- '--eval',
131
- `
132
- import * as mod from ${JSON.stringify(pathToFileURL(tsPath).href)};
133
- let out = null;
134
- if (mod.SEED_REGISTRY && typeof mod.SEED_REGISTRY === 'object') out = mod.SEED_REGISTRY;
135
- else if (Array.isArray(mod.seeds)) out = Object.fromEntries(mod.seeds.map(s => [s.slug, s]));
136
- else if (Array.isArray(mod.default)) out = Object.fromEntries(mod.default.map(s => [s.slug, s]));
137
- if (out) process.stdout.write(JSON.stringify(out));
138
- `.trim(),
139
- ], { encoding: 'utf-8' })
140
-
141
- if (result.status === 0 && result.stdout) {
142
- try { return JSON.parse(result.stdout) } catch (err) {
143
- console.error(' Failed to parse seeds.ts output:', err)
144
- }
145
- } else if (result.status !== 0) {
146
- console.error(' Error loading seeds.ts:')
147
- console.error(result.stderr || result.stdout || 'Unknown error')
148
- if (process.version.slice(1).split('.')[0] < 22) {
149
- console.warn(' Note: Node.js 22.6+ is required to load .ts files directly. Current version:', process.version)
150
- }
151
- }
152
- }
153
-
154
- return null
155
- }
156
-
157
- async function cmdInit(args) {
158
- const initDb = args.includes('--db')
159
- const remote = args.includes('--remote')
160
- const dbIdx = args.indexOf('--db-name')
161
- const db = dbIdx !== -1 ? args[dbIdx + 1] : undefined
162
-
163
- const { init } = await import('@beechcms/cli')
164
- await init({ initDb, local: !remote, db })
165
- }
166
-
167
- async function cmdSeedLoad(args) {
168
- const dryRun = args.includes('--dry-run')
169
- const diff = args.includes('--diff')
170
- const remote = args.includes('--remote')
171
- const dbIdx = args.indexOf('--db')
172
- const db = dbIdx !== -1 ? args[dbIdx + 1] : undefined
173
-
174
- const registry = await tryLoadLocalRegistry()
175
-
176
- const { seedLoad } = await import('@beechcms/cli')
177
- await seedLoad({ dryRun, diff, local: !remote, db, registry })
178
- }
179
-
180
- async function cmdValidate(args) {
181
- const registry = await tryLoadLocalRegistry()
182
- const { validate } = await import('@beechcms/cli')
183
- await validate({ registry })
184
- }
185
-
186
- async function cmdSeedCreate(_args) {
187
- const { seedCreate } = await import('@beechcms/cli')
188
- await seedCreate({})
189
- }
190
-
191
- async function cmdDeploy(args) {
192
- const skipSeed = args.includes('--skip-seed')
193
- const skipCheck = args.includes('--skip-check')
194
- const registry = skipSeed ? null : await tryLoadLocalRegistry()
195
- const { deploy } = await import('@beechcms/cli')
196
- await deploy({ registry, skipSeed, skipCheck })
197
- }
198
-
199
- async function cmdUpdate(_args) {
200
- const { update } = await import('@beechcms/cli')
201
- await update({})
202
- }
203
-
204
- async function cmdOnboard(args) {
205
- const local = !args.includes('--remote')
206
- const yes = args.includes('--yes')
207
- const dbIdx = args.indexOf('--db')
208
- const db = dbIdx !== -1 ? args[dbIdx + 1] : undefined
209
- const registry = await tryLoadLocalRegistry()
210
- const { onboard } = await import('@beechcms/cli')
211
- await onboard({ local, yes, db, registry })
212
- }
213
-
214
- async function cmdReset(args) {
215
- const db = args.includes('--db')
216
- const docker = args.includes('--docker')
217
- const all = args.includes('--all')
218
-
219
- const { reset } = await import('@beechcms/cli')
220
- await reset({ db, docker, all })
221
- }
222
-
223
- const handler = COMMANDS[command]
224
- if (!handler) {
225
- help()
226
- if (command) process.exit(1)
227
- } else if (args.includes('--help') || args.includes('-h')) {
228
- help()
229
- } else {
230
- await handler(args)
231
- }
1
+ #!/usr/bin/env node
2
+ // @ts-check
3
+
4
+ import { spawnSync } from 'node:child_process'
5
+ import { existsSync } from 'node:fs'
6
+ import { resolve } from 'node:path'
7
+ import { pathToFileURL } from 'node:url'
8
+
9
+
10
+ const [,, command, ...args] = process.argv
11
+
12
+ const COMMANDS = {
13
+ build: cmdBuild,
14
+ 'seed:load': cmdSeedLoad,
15
+ 'seed:create': cmdSeedCreate,
16
+ 'schema:diff': cmdSchemaDiff,
17
+ 'init': cmdInit,
18
+ 'validate': cmdValidate,
19
+ 'deploy': cmdDeploy,
20
+ 'update': cmdUpdate,
21
+ 'onboard': cmdOnboard,
22
+ 'reset': cmdReset,
23
+ 'generate:types': cmdGenerateTypes,
24
+ }
25
+
26
+ function help() {
27
+ console.log(`
28
+ beech <command> [options]
29
+
30
+ Commands:
31
+ init Check project files and optionally initialise the database
32
+ --db Also initialise the D1 database (system tables)
33
+ --remote Target remote D1 instead of local (default: local)
34
+ --db-name <n> Override D1 database name
35
+
36
+ build Rebuild @beechcms/core after editing seeds.ts
37
+
38
+ validate Validate SEED_REGISTRY for common errors (duplicate aliases,
39
+ missing displayNameAlias, duplicate slugs). Exit code 1 on errors.
40
+
41
+ seed:load Create/update content tables from SEED_REGISTRY
42
+ --dry-run Print SQL without executing
43
+ --diff Show schema differences vs current DB
44
+ --remote Execute against remote D1 (default: local)
45
+ --db <name> Override D1 database name
46
+
47
+ seed:create Interactive wizard — generate a new Seed definition and append
48
+ it to seeds.ts, including SEED_REGISTRY entry
49
+
50
+ schema:diff Diff SEED_REGISTRY vs the live D1 schema and generate an
51
+ additive SQL migration in apps/api/migrations/
52
+ --write Write the migration file (default: preview only)
53
+ --name <name> Migration name used in the filename
54
+ --remote Diff against remote D1 (default: local)
55
+ --db <name> Override D1 database name
56
+
57
+ deploy Deploy Worker, sync remote schema, and verify /admin
58
+ --skip-seed Skip remote seed:load step
59
+ --skip-check Skip /admin reachability check
60
+
61
+ update Update @beechcms/api and @beechcms/core to latest, then
62
+ apply any new system migrations to the local database
63
+
64
+ onboard One-command local provisioning (init + seed:load). Designed
65
+ for non-interactive use by agents and CI.
66
+ --remote Target remote D1 instead of local (default: local)
67
+ --yes Skip all interactive prompts (non-interactive mode)
68
+ --db <name> Override D1 database name
69
+
70
+ reset Reset database and/or Docker containers/volumes
71
+ --db Wipe local Wrangler state & bootstrap D1 DB
72
+ --docker Down Docker containers and wipe volumes
73
+ --all Reset both (database & docker)
74
+
75
+ generate:types Generate TypeScript interfaces from the Seed registry
76
+ --out <path> Output file (default: src/types/beech.ts)
77
+ --local Read from seeds.ts instead of querying remote D1
78
+ --db <name> Override D1 database name (remote mode)
79
+
80
+ Scaffold a new project (interactive, or pass --yes for non-interactive defaults):
81
+ npm create @beechcms/cms [project-name] [--yes] [--with-examples]
82
+
83
+ Golden path (local):
84
+ npx beech onboard --local --yes # fully automated
85
+ # or step by step:
86
+ npx beech init --db --local
87
+ npx beech seed:load --local
88
+ npx wrangler dev
89
+
90
+ Golden path (deploy):
91
+ npx beech deploy
92
+ npx beech init --db --remote # verify remote DB post-deploy
93
+ `)
94
+ }
95
+
96
+ function cmdBuild() {
97
+ console.log(
98
+ '\nNo build step needed for BeechCMS projects.\n' +
99
+ 'Edit seeds.ts then run `npx beech seed:load` to sync schema changes to D1.\n'
100
+ )
101
+ }
102
+
103
+ async function tryLoadLocalRegistry() {
104
+ const cwd = process.cwd()
105
+
106
+ // Try compiled JS first (root or apps/api)
107
+ const searchDirs = [cwd, resolve(cwd, 'apps', 'api')]
108
+ for (const dir of searchDirs) {
109
+ for (const name of ['seeds.js', 'seeds.mjs', 'seed.js', 'seed.mjs']) {
110
+ const p = resolve(dir, name)
111
+ if (existsSync(p)) {
112
+ try {
113
+ const mod = await import(pathToFileURL(p).href)
114
+ if (mod.SEED_REGISTRY && typeof mod.SEED_REGISTRY === 'object') {
115
+ return mod.SEED_REGISTRY
116
+ }
117
+ if (Array.isArray(mod.seeds)) {
118
+ return Object.fromEntries(mod.seeds.map(s => [s.slug, s]))
119
+ }
120
+ if (Array.isArray(mod.default)) {
121
+ return Object.fromEntries(mod.default.map(s => [s.slug, s]))
122
+ }
123
+ } catch {}
124
+ }
125
+ }
126
+ }
127
+
128
+ // Try seeds.ts / seed.ts (root or apps/api)
129
+ let tsPath = null
130
+ for (const dir of searchDirs) {
131
+ const p = existsSync(resolve(dir, 'seeds.ts'))
132
+ ? resolve(dir, 'seeds.ts')
133
+ : resolve(dir, 'seed.ts')
134
+ if (existsSync(p)) {
135
+ tsPath = p
136
+ break
137
+ }
138
+ }
139
+
140
+ if (tsPath) {
141
+ const result = spawnSync(process.execPath, [
142
+ '--experimental-strip-types',
143
+ '--input-type=module',
144
+ '--eval',
145
+ `
146
+ import * as mod from ${JSON.stringify(pathToFileURL(tsPath).href)};
147
+ let out = null;
148
+ if (mod.SEED_REGISTRY && typeof mod.SEED_REGISTRY === 'object') out = mod.SEED_REGISTRY;
149
+ else if (Array.isArray(mod.seeds)) out = Object.fromEntries(mod.seeds.map(s => [s.slug, s]));
150
+ else if (Array.isArray(mod.default)) out = Object.fromEntries(mod.default.map(s => [s.slug, s]));
151
+ if (out) process.stdout.write(JSON.stringify(out));
152
+ `.trim(),
153
+ ], { encoding: 'utf-8' })
154
+
155
+ if (result.status === 0 && result.stdout) {
156
+ try { return JSON.parse(result.stdout) } catch (err) {
157
+ console.error(' Failed to parse seeds.ts output:', err)
158
+ }
159
+ } else if (result.status !== 0) {
160
+ console.error(' Error loading seeds.ts:')
161
+ console.error(result.stderr || result.stdout || 'Unknown error')
162
+ if (process.version.slice(1).split('.')[0] < 22) {
163
+ console.warn(' Note: Node.js 22.6+ is required to load .ts files directly. Current version:', process.version)
164
+ }
165
+ }
166
+ }
167
+
168
+ return null
169
+ }
170
+
171
+ async function cmdInit(args) {
172
+ const initDb = args.includes('--db')
173
+ const remote = args.includes('--remote')
174
+ const dbIdx = args.indexOf('--db-name')
175
+ const db = dbIdx !== -1 ? args[dbIdx + 1] : undefined
176
+
177
+ const { init } = await import('@beechcms/cli')
178
+ await init({ initDb, local: !remote, db })
179
+ }
180
+
181
+ async function cmdSeedLoad(args) {
182
+ const dryRun = args.includes('--dry-run')
183
+ const diff = args.includes('--diff')
184
+ const remote = args.includes('--remote')
185
+ const dbIdx = args.indexOf('--db')
186
+ const db = dbIdx !== -1 ? args[dbIdx + 1] : undefined
187
+
188
+ const registry = await tryLoadLocalRegistry()
189
+
190
+ const { seedLoad } = await import('@beechcms/cli')
191
+ await seedLoad({ dryRun, diff, local: !remote, db, registry })
192
+ }
193
+
194
+ async function cmdValidate(args) {
195
+ const registry = await tryLoadLocalRegistry()
196
+ const { validate } = await import('@beechcms/cli')
197
+ await validate({ registry })
198
+ }
199
+
200
+ async function cmdSeedCreate(_args) {
201
+ const { seedCreate } = await import('@beechcms/cli')
202
+ await seedCreate({})
203
+ }
204
+
205
+ async function cmdDeploy(args) {
206
+ const skipSeed = args.includes('--skip-seed')
207
+ const skipCheck = args.includes('--skip-check')
208
+ const registry = skipSeed ? null : await tryLoadLocalRegistry()
209
+ const { deploy } = await import('@beechcms/cli')
210
+ await deploy({ registry, skipSeed, skipCheck })
211
+ }
212
+
213
+ async function cmdUpdate(_args) {
214
+ const { update } = await import('@beechcms/cli')
215
+ await update({})
216
+ }
217
+
218
+ async function cmdOnboard(args) {
219
+ const local = !args.includes('--remote')
220
+ const yes = args.includes('--yes')
221
+ const dbIdx = args.indexOf('--db')
222
+ const db = dbIdx !== -1 ? args[dbIdx + 1] : undefined
223
+ const registry = await tryLoadLocalRegistry()
224
+ const { onboard } = await import('@beechcms/cli')
225
+ await onboard({ local, yes, db, registry })
226
+ }
227
+
228
+ async function cmdReset(args) {
229
+ const db = args.includes('--db')
230
+ const docker = args.includes('--docker')
231
+ const all = args.includes('--all')
232
+
233
+ const { reset } = await import('@beechcms/cli')
234
+ await reset({ db, docker, all })
235
+ }
236
+
237
+ async function cmdSchemaDiff(args) {
238
+ const remote = args.includes('--remote')
239
+ const write = args.includes('--write')
240
+ const nameIdx = args.indexOf('--name')
241
+ const name = nameIdx !== -1 ? args[nameIdx + 1] : undefined
242
+ const dbIdx = args.indexOf('--db')
243
+ const db = dbIdx !== -1 ? args[dbIdx + 1] : undefined
244
+ const registry = await tryLoadLocalRegistry()
245
+ const { schemaDiff } = await import('@beechcms/cli')
246
+ await schemaDiff({ local: !remote, write, name, db, registry })
247
+ }
248
+
249
+ async function cmdGenerateTypes(args) {
250
+ const outIdx = args.indexOf('--out')
251
+ const out = outIdx !== -1 ? args[outIdx + 1] : 'src/types/beech.ts'
252
+ const local = args.includes('--local')
253
+ const dbIdx = args.indexOf('--db')
254
+ const db = dbIdx !== -1 ? args[dbIdx + 1] : undefined
255
+
256
+ const registry = local ? await tryLoadLocalRegistry() : null
257
+
258
+ const { generateTypes } = await import('@beechcms/cli')
259
+ await generateTypes({ out, local, db, registry })
260
+ }
261
+
262
+ const handler = COMMANDS[command]
263
+ if (!handler) {
264
+ help()
265
+ if (command) process.exit(1)
266
+ } else if (args.includes('--help') || args.includes('-h')) {
267
+ help()
268
+ } else {
269
+ await handler(args)
270
+ }