@beechcms/cli 0.4.0-preview.2 → 0.4.0-preview.3

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/dist/index.js CHANGED
@@ -10,8 +10,8 @@ import {
10
10
  } from "@beechcms/core";
11
11
 
12
12
  // src/lib/wrangler.ts
13
- import { execSync, spawnSync } from "node:child_process";
14
- import { writeFileSync, rmSync, existsSync } from "node:fs";
13
+ import { spawnSync } from "node:child_process";
14
+ import { writeFileSync, rmSync, existsSync, readFileSync } from "node:fs";
15
15
  import { tmpdir } from "node:os";
16
16
  import { join, resolve } from "node:path";
17
17
  function buildArgs(options) {
@@ -53,13 +53,18 @@ ${result.stdout}`);
53
53
  }
54
54
  }
55
55
  function findWranglerConfig() {
56
- const candidate = resolve(process.cwd(), "apps", "api", "wrangler.jsonc");
57
- return existsSync(candidate) ? candidate : null;
56
+ const candidates = [
57
+ resolve(process.cwd(), "wrangler.jsonc"),
58
+ resolve(process.cwd(), "wrangler.json"),
59
+ resolve(process.cwd(), "apps", "api", "wrangler.jsonc"),
60
+ resolve(process.cwd(), "apps", "api", "wrangler.json")
61
+ ];
62
+ return candidates.find(existsSync) ?? null;
58
63
  }
59
64
  function resolveDbName(configPath) {
60
65
  if (!configPath) return "beech-db";
61
66
  try {
62
- const content = execSync(`cat "${configPath}"`, { encoding: "utf-8" });
67
+ const content = readFileSync(configPath, "utf-8");
63
68
  const match = content.match(/"database_name"\s*:\s*"([^"]+)"/);
64
69
  return match?.[1] ?? "beech-db";
65
70
  } catch {
@@ -113,8 +118,8 @@ function buildStatements(seed) {
113
118
  }
114
119
  return stmts;
115
120
  }
116
- async function runDiff(options) {
117
- const seeds = Object.values(SEED_REGISTRY);
121
+ async function runDiff(options, registry) {
122
+ const seeds = Object.values(registry);
118
123
  console.log(pc.cyan("\n Diffing schema\u2026\n"));
119
124
  let allOk = true;
120
125
  for (const seed of seeds) {
@@ -149,8 +154,8 @@ async function runDiff(options) {
149
154
  console.log(pc.yellow(" Run `beech seed:load` to apply missing tables/columns.\n"));
150
155
  }
151
156
  }
152
- async function runLoad(options, dryRun) {
153
- const seeds = Object.values(SEED_REGISTRY);
157
+ async function runLoad(options, dryRun, registry) {
158
+ const seeds = Object.values(registry);
154
159
  if (dryRun) {
155
160
  console.log(pc.cyan("\n -- dry-run: SQL that would be executed\n"));
156
161
  for (const seed of seeds) {
@@ -175,6 +180,11 @@ async function runLoad(options, dryRun) {
175
180
  console.log(pc.green("\n All seeds loaded.\n"));
176
181
  }
177
182
  async function seedLoad(args) {
183
+ const registry = args.registry ?? SEED_REGISTRY;
184
+ if (Object.keys(registry).length === 0) {
185
+ console.warn(pc.yellow("\n Warning: SEED_REGISTRY is empty. Create a seeds.ts in your project root.\n"));
186
+ return;
187
+ }
178
188
  const configPath = findWranglerConfig();
179
189
  const db = args.db ?? resolveDbName(configPath);
180
190
  const options = {
@@ -183,9 +193,9 @@ async function seedLoad(args) {
183
193
  configPath
184
194
  };
185
195
  if (args.diff) {
186
- await runDiff(options);
196
+ await runDiff(options, registry);
187
197
  } else {
188
- await runLoad(options, args.dryRun);
198
+ await runLoad(options, args.dryRun, registry);
189
199
  }
190
200
  }
191
201
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@beechcms/cli",
3
- "version": "0.4.0-preview.2",
3
+ "version": "0.4.0-preview.3",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "exports": {
@@ -11,7 +11,7 @@
11
11
  "dev": "esbuild src/index.ts --bundle --packages=external --platform=node --format=esm --outfile=dist/index.js --watch"
12
12
  },
13
13
  "dependencies": {
14
- "@beechcms/core": "^0.4.0-preview.2",
14
+ "@beechcms/core": "^0.4.0-preview.3",
15
15
  "picocolors": "^1.1.1"
16
16
  },
17
17
  "devDependencies": {
@@ -16,6 +16,7 @@ export interface SeedLoadOptions {
16
16
  diff: boolean
17
17
  local: boolean
18
18
  db?: string
19
+ registry?: Record<string, Seed> | null
19
20
  }
20
21
 
21
22
  function buildStatements(seed: Seed): string[] {
@@ -32,8 +33,8 @@ function buildStatements(seed: Seed): string[] {
32
33
  return stmts
33
34
  }
34
35
 
35
- async function runDiff(options: WranglerOptions): Promise<void> {
36
- const seeds = Object.values(SEED_REGISTRY)
36
+ async function runDiff(options: WranglerOptions, registry: Record<string, Seed>): Promise<void> {
37
+ const seeds = Object.values(registry)
37
38
  console.log(pc.cyan('\n Diffing schema…\n'))
38
39
 
39
40
  let allOk = true
@@ -74,8 +75,8 @@ async function runDiff(options: WranglerOptions): Promise<void> {
74
75
  }
75
76
  }
76
77
 
77
- async function runLoad(options: WranglerOptions, dryRun: boolean): Promise<void> {
78
- const seeds = Object.values(SEED_REGISTRY)
78
+ async function runLoad(options: WranglerOptions, dryRun: boolean, registry: Record<string, Seed>): Promise<void> {
79
+ const seeds = Object.values(registry)
79
80
 
80
81
  if (dryRun) {
81
82
  console.log(pc.cyan('\n -- dry-run: SQL that would be executed\n'))
@@ -103,6 +104,13 @@ async function runLoad(options: WranglerOptions, dryRun: boolean): Promise<void>
103
104
  }
104
105
 
105
106
  export async function seedLoad(args: SeedLoadOptions): Promise<void> {
107
+ const registry = args.registry ?? SEED_REGISTRY
108
+
109
+ if (Object.keys(registry).length === 0) {
110
+ console.warn(pc.yellow('\n Warning: SEED_REGISTRY is empty. Create a seeds.ts in your project root.\n'))
111
+ return
112
+ }
113
+
106
114
  const configPath = findWranglerConfig()
107
115
  const db = args.db ?? resolveDbName(configPath)
108
116
 
@@ -113,8 +121,8 @@ export async function seedLoad(args: SeedLoadOptions): Promise<void> {
113
121
  }
114
122
 
115
123
  if (args.diff) {
116
- await runDiff(options)
124
+ await runDiff(options, registry)
117
125
  } else {
118
- await runLoad(options, args.dryRun)
126
+ await runLoad(options, args.dryRun, registry)
119
127
  }
120
128
  }
@@ -1,5 +1,5 @@
1
- import { execSync, spawnSync } from 'node:child_process'
2
- import { writeFileSync, rmSync, existsSync } from 'node:fs'
1
+ import { spawnSync } from 'node:child_process'
2
+ import { writeFileSync, rmSync, existsSync, readFileSync } from 'node:fs'
3
3
  import { tmpdir } from 'node:os'
4
4
  import { join, resolve } from 'node:path'
5
5
 
@@ -59,17 +59,22 @@ export function queryD1<T extends D1Row = D1Row>(sql: string, options: WranglerO
59
59
  }
60
60
  }
61
61
 
62
- /** Trova il path di wrangler.jsonc in apps/api/ relativo a cwd. */
62
+ /** Trova il path di wrangler.jsonc — prima in CWD (user project), poi in apps/api/ (monorepo). */
63
63
  export function findWranglerConfig(): string | null {
64
- const candidate = resolve(process.cwd(), 'apps', 'api', 'wrangler.jsonc')
65
- return existsSync(candidate) ? candidate : null
64
+ const candidates = [
65
+ resolve(process.cwd(), 'wrangler.jsonc'),
66
+ resolve(process.cwd(), 'wrangler.json'),
67
+ resolve(process.cwd(), 'apps', 'api', 'wrangler.jsonc'),
68
+ resolve(process.cwd(), 'apps', 'api', 'wrangler.json'),
69
+ ]
70
+ return candidates.find(existsSync) ?? null
66
71
  }
67
72
 
68
- /** Risolve il nome del database D1 da wrangler.jsonc (grepping 'database_name'). */
73
+ /** Risolve il nome del database D1 da wrangler.jsonc (cerca 'database_name'). */
69
74
  export function resolveDbName(configPath: string | null): string {
70
75
  if (!configPath) return 'beech-db'
71
76
  try {
72
- const content = execSync(`cat "${configPath}"`, { encoding: 'utf-8' })
77
+ const content = readFileSync(configPath, 'utf-8')
73
78
  const match = content.match(/"database_name"\s*:\s*"([^"]+)"/)
74
79
  return match?.[1] ?? 'beech-db'
75
80
  } catch {