@beechcms/cli 0.6.0-preview.1 → 0.6.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/.turbo/turbo-build.log +5 -0
- package/coverage/index.html +21 -21
- package/coverage/lcov-report/index.html +21 -21
- package/coverage/lcov-report/validate.ts.html +99 -402
- package/coverage/lcov.info +76 -176
- package/coverage/validate.ts.html +99 -402
- package/dist/commands/seed-load.d.ts +8 -0
- package/dist/commands/seed-load.d.ts.map +1 -0
- package/dist/commands/seed-load.js +89 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +137 -0
- package/dist/lib/schema-diff.d.ts +15 -0
- package/dist/lib/schema-diff.d.ts.map +1 -0
- package/dist/lib/schema-diff.js +37 -0
- package/dist/lib/wrangler.d.ts +17 -0
- package/dist/lib/wrangler.d.ts.map +1 -0
- package/dist/lib/wrangler.js +65 -0
- package/package.json +11 -11
- package/src/commands/deploy.ts +126 -126
- package/src/commands/init.ts +599 -599
- package/src/commands/onboard.ts +32 -32
- package/src/commands/reset.ts +157 -0
- package/src/commands/seed-create.ts +192 -192
- package/src/commands/seed-load.ts +235 -235
- package/src/commands/update.ts +54 -54
- package/src/commands/validate.ts +80 -80
- package/src/index.ts +20 -17
- package/src/lib/schema-diff.ts +150 -150
- package/src/lib/wrangler.ts +129 -129
- package/src/test/reset.test.ts +128 -0
- package/src/test/seed-load.test.ts +158 -0
- package/src/test/validate.test.ts +261 -0
- package/tsconfig.json +16 -16
- package/tsconfig.tsbuildinfo +1 -1
- package/vitest.config.ts +33 -32
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import pc from 'picocolors';
|
|
2
|
+
import { SEED_REGISTRY, generateCreateTable, generateIndexes, generateFtsTable, generateFtsTriggers, } from '@beech/core';
|
|
3
|
+
import { executeD1File, findWranglerConfig, resolveDbName } from '../lib/wrangler.js';
|
|
4
|
+
import { diffSeed } from '../lib/schema-diff.js';
|
|
5
|
+
function buildStatements(seed) {
|
|
6
|
+
const stmts = [generateCreateTable(seed), ...generateIndexes(seed)];
|
|
7
|
+
const fts = generateFtsTable(seed);
|
|
8
|
+
if (fts) {
|
|
9
|
+
stmts.push(fts, ...generateFtsTriggers(seed));
|
|
10
|
+
}
|
|
11
|
+
return stmts;
|
|
12
|
+
}
|
|
13
|
+
async function runDiff(options) {
|
|
14
|
+
const seeds = Object.values(SEED_REGISTRY);
|
|
15
|
+
console.log(pc.cyan('\n Diffing schema…\n'));
|
|
16
|
+
let allOk = true;
|
|
17
|
+
for (const seed of seeds) {
|
|
18
|
+
const result = await diffSeed(seed, options);
|
|
19
|
+
const tableName = `content_${seed.slug}`;
|
|
20
|
+
if (!result.tableExists) {
|
|
21
|
+
console.log(pc.red(` ✗ ${tableName} — table missing`));
|
|
22
|
+
allOk = false;
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
const problems = result.columns.filter(c => c.status !== 'ok');
|
|
26
|
+
if (problems.length === 0) {
|
|
27
|
+
console.log(pc.green(` ✓ ${tableName}`));
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
allOk = false;
|
|
31
|
+
console.log(pc.yellow(` ⚠ ${tableName}`));
|
|
32
|
+
for (const col of problems) {
|
|
33
|
+
if (col.status === 'missing') {
|
|
34
|
+
console.log(pc.red(` + missing column: ${col.name} ${col.expectedType}`));
|
|
35
|
+
}
|
|
36
|
+
else if (col.status === 'extra') {
|
|
37
|
+
console.log(pc.dim(` ~ extra column: ${col.name} ${col.actualType}`));
|
|
38
|
+
}
|
|
39
|
+
else if (col.status === 'type_mismatch') {
|
|
40
|
+
console.log(pc.red(` ≠ type mismatch: ${col.name} (expected ${col.expectedType}, got ${col.actualType})`));
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
console.log('');
|
|
45
|
+
if (allOk) {
|
|
46
|
+
console.log(pc.green(' Schema matches seeds. No action needed.\n'));
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
console.log(pc.yellow(' Run `beech seed:load` to apply missing tables/columns.\n'));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
async function runLoad(options, dryRun) {
|
|
53
|
+
const seeds = Object.values(SEED_REGISTRY);
|
|
54
|
+
if (dryRun) {
|
|
55
|
+
console.log(pc.cyan('\n -- dry-run: SQL that would be executed\n'));
|
|
56
|
+
for (const seed of seeds) {
|
|
57
|
+
const stmts = buildStatements(seed);
|
|
58
|
+
console.log(pc.dim(` -- content_${seed.slug}`));
|
|
59
|
+
for (const stmt of stmts) {
|
|
60
|
+
console.log(stmt + '\n');
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
console.log(pc.cyan(`\n Loading seeds into ${options.local ? 'local' : 'remote'} D1 (${options.db})…\n`));
|
|
66
|
+
for (const seed of seeds) {
|
|
67
|
+
const stmts = buildStatements(seed);
|
|
68
|
+
const sql = stmts.join('\n\n') + '\n';
|
|
69
|
+
process.stdout.write(` ${pc.dim('→')} content_${seed.slug}… `);
|
|
70
|
+
executeD1File(sql, options);
|
|
71
|
+
console.log(pc.green('done'));
|
|
72
|
+
}
|
|
73
|
+
console.log(pc.green('\n All seeds loaded.\n'));
|
|
74
|
+
}
|
|
75
|
+
export async function seedLoad(args) {
|
|
76
|
+
const configPath = findWranglerConfig();
|
|
77
|
+
const db = args.db ?? resolveDbName(configPath);
|
|
78
|
+
const options = {
|
|
79
|
+
db,
|
|
80
|
+
local: args.local,
|
|
81
|
+
configPath,
|
|
82
|
+
};
|
|
83
|
+
if (args.diff) {
|
|
84
|
+
await runDiff(options);
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
await runLoad(options, args.dryRun);
|
|
88
|
+
}
|
|
89
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAA;AAClD,YAAY,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1248,10 +1248,147 @@ async function update(_args) {
|
|
|
1248
1248
|
console.log(pc7.cyan(" 3. npx beech seed:load"));
|
|
1249
1249
|
console.log(pc7.dim(" \u2192 sync remote schema\n"));
|
|
1250
1250
|
}
|
|
1251
|
+
|
|
1252
|
+
// src/commands/reset.ts
|
|
1253
|
+
import pc8 from "picocolors";
|
|
1254
|
+
import { spawnSync as spawnSync5 } from "node:child_process";
|
|
1255
|
+
import { existsSync as existsSync4, readFileSync as readFileSync5, rmSync as rmSync2 } from "node:fs";
|
|
1256
|
+
import { resolve as resolve4 } from "node:path";
|
|
1257
|
+
import { createInterface as createInterface3 } from "node:readline/promises";
|
|
1258
|
+
function isDockerInstalled() {
|
|
1259
|
+
try {
|
|
1260
|
+
const result = spawnSync5("docker", ["--version"], { stdio: "ignore", shell: true });
|
|
1261
|
+
return result.status === 0;
|
|
1262
|
+
} catch {
|
|
1263
|
+
return false;
|
|
1264
|
+
}
|
|
1265
|
+
}
|
|
1266
|
+
function isDockerRunning() {
|
|
1267
|
+
try {
|
|
1268
|
+
const result = spawnSync5("docker", ["info"], { stdio: "ignore", shell: true });
|
|
1269
|
+
return result.status === 0;
|
|
1270
|
+
} catch {
|
|
1271
|
+
return false;
|
|
1272
|
+
}
|
|
1273
|
+
}
|
|
1274
|
+
async function reset(args) {
|
|
1275
|
+
console.log(pc8.cyan("\n beech reset \u2014 cleanup environments\n"));
|
|
1276
|
+
let resetDb = args.db || args.all;
|
|
1277
|
+
let resetDocker = args.docker || args.all;
|
|
1278
|
+
if (!args.db && !args.docker && !args.all) {
|
|
1279
|
+
if (process.stdin.isTTY) {
|
|
1280
|
+
const rl = createInterface3({ input: process.stdin, output: process.stdout });
|
|
1281
|
+
try {
|
|
1282
|
+
const answer = (await rl.question(
|
|
1283
|
+
pc8.cyan(" \u2192 No options provided. Would you like to reset everything (DB & Docker)? (y/N): ")
|
|
1284
|
+
)).trim().toLowerCase();
|
|
1285
|
+
if (answer === "y" || answer === "yes") {
|
|
1286
|
+
resetDb = true;
|
|
1287
|
+
resetDocker = true;
|
|
1288
|
+
} else {
|
|
1289
|
+
console.log(pc8.dim("\n Reset cancelled. Use --db, --docker, or --all.\n"));
|
|
1290
|
+
return;
|
|
1291
|
+
}
|
|
1292
|
+
} finally {
|
|
1293
|
+
rl.close();
|
|
1294
|
+
}
|
|
1295
|
+
} else {
|
|
1296
|
+
console.log(pc8.red("\n \u2717 Error: Please specify what to reset using --db, --docker, or --all.\n"));
|
|
1297
|
+
process.exit(1);
|
|
1298
|
+
}
|
|
1299
|
+
}
|
|
1300
|
+
const cwd = process.cwd();
|
|
1301
|
+
if (resetDocker) {
|
|
1302
|
+
if (!isDockerInstalled()) {
|
|
1303
|
+
console.log(pc8.red(" \u2717 Docker is not installed or not found in your PATH."));
|
|
1304
|
+
console.log(pc8.dim(" Please install Docker to reset Docker containers and volumes.\n"));
|
|
1305
|
+
if (!args.all) {
|
|
1306
|
+
process.exit(1);
|
|
1307
|
+
}
|
|
1308
|
+
} else if (!isDockerRunning()) {
|
|
1309
|
+
console.log(pc8.yellow(" \u26A0 Docker is installed, but the Docker daemon is NOT running."));
|
|
1310
|
+
console.log(pc8.dim(" Please start Docker Desktop or your Docker daemon to reset containers.\n"));
|
|
1311
|
+
if (!args.all) {
|
|
1312
|
+
process.exit(1);
|
|
1313
|
+
}
|
|
1314
|
+
} else {
|
|
1315
|
+
console.log(pc8.dim(" Resetting Docker containers and volumes\u2026\n"));
|
|
1316
|
+
const result = spawnSync5("docker", ["compose", "down", "-v"], {
|
|
1317
|
+
stdio: "inherit",
|
|
1318
|
+
cwd,
|
|
1319
|
+
shell: true
|
|
1320
|
+
});
|
|
1321
|
+
if (result.status === 0) {
|
|
1322
|
+
console.log(pc8.green("\n \u2713 Docker containers stopped and volumes removed."));
|
|
1323
|
+
} else {
|
|
1324
|
+
console.log(pc8.red("\n \u2717 Docker reset failed."));
|
|
1325
|
+
}
|
|
1326
|
+
}
|
|
1327
|
+
}
|
|
1328
|
+
if (resetDb) {
|
|
1329
|
+
console.log(pc8.dim("\n Resetting local database\u2026\n"));
|
|
1330
|
+
let dbResetSuccess = false;
|
|
1331
|
+
const apiDir = resolve4(cwd, "apps", "api");
|
|
1332
|
+
if (existsSync4(resolve4(apiDir, "package.json"))) {
|
|
1333
|
+
const result = spawnSync5("npm", ["run", "db:reset:local"], {
|
|
1334
|
+
stdio: "inherit",
|
|
1335
|
+
cwd: apiDir,
|
|
1336
|
+
shell: true
|
|
1337
|
+
});
|
|
1338
|
+
dbResetSuccess = result.status === 0;
|
|
1339
|
+
} else if (existsSync4(resolve4(cwd, "package.json"))) {
|
|
1340
|
+
const pkg = JSON.parse(readFileSync5(resolve4(cwd, "package.json"), "utf-8"));
|
|
1341
|
+
if (pkg.scripts?.["db:reset:local"]) {
|
|
1342
|
+
const result = spawnSync5("npm", ["run", "db:reset:local"], {
|
|
1343
|
+
stdio: "inherit",
|
|
1344
|
+
cwd,
|
|
1345
|
+
shell: true
|
|
1346
|
+
});
|
|
1347
|
+
dbResetSuccess = result.status === 0;
|
|
1348
|
+
} else {
|
|
1349
|
+
const wranglerStateDir = resolve4(cwd, ".wrangler/state");
|
|
1350
|
+
if (existsSync4(wranglerStateDir)) {
|
|
1351
|
+
console.log(pc8.dim(" Removing .wrangler/state\u2026"));
|
|
1352
|
+
rmSync2(wranglerStateDir, { recursive: true, force: true });
|
|
1353
|
+
}
|
|
1354
|
+
if (existsSync4(resolve4(cwd, "scripts", "bootstrap-d1.mjs"))) {
|
|
1355
|
+
const result = spawnSync5("node", ["scripts/bootstrap-d1.mjs"], {
|
|
1356
|
+
stdio: "inherit",
|
|
1357
|
+
cwd,
|
|
1358
|
+
shell: true
|
|
1359
|
+
});
|
|
1360
|
+
dbResetSuccess = result.status === 0;
|
|
1361
|
+
} else {
|
|
1362
|
+
console.log(pc8.yellow(" \u26A0 Could not find database reset script."));
|
|
1363
|
+
const initResult = spawnSync5("npx", ["beech", "init", "--db", "--local"], {
|
|
1364
|
+
stdio: "inherit",
|
|
1365
|
+
cwd,
|
|
1366
|
+
shell: true
|
|
1367
|
+
});
|
|
1368
|
+
dbResetSuccess = initResult.status === 0;
|
|
1369
|
+
}
|
|
1370
|
+
}
|
|
1371
|
+
} else {
|
|
1372
|
+
const wranglerStateDir = resolve4(cwd, ".wrangler/state");
|
|
1373
|
+
if (existsSync4(wranglerStateDir)) {
|
|
1374
|
+
console.log(pc8.dim(" Removing .wrangler/state\u2026"));
|
|
1375
|
+
rmSync2(wranglerStateDir, { recursive: true, force: true });
|
|
1376
|
+
}
|
|
1377
|
+
dbResetSuccess = true;
|
|
1378
|
+
}
|
|
1379
|
+
if (dbResetSuccess) {
|
|
1380
|
+
console.log(pc8.green("\n \u2713 Local database reset completed."));
|
|
1381
|
+
} else {
|
|
1382
|
+
console.log(pc8.red("\n \u2717 Database reset failed."));
|
|
1383
|
+
}
|
|
1384
|
+
}
|
|
1385
|
+
console.log(pc8.dim("\n Reset process finished.\n"));
|
|
1386
|
+
}
|
|
1251
1387
|
export {
|
|
1252
1388
|
deploy,
|
|
1253
1389
|
init,
|
|
1254
1390
|
onboard,
|
|
1391
|
+
reset,
|
|
1255
1392
|
seedCreate,
|
|
1256
1393
|
seedLoad,
|
|
1257
1394
|
update,
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Seed } from '@beech/core';
|
|
2
|
+
import type { WranglerOptions } from './wrangler.js';
|
|
3
|
+
export interface ColumnDiff {
|
|
4
|
+
name: string;
|
|
5
|
+
status: 'ok' | 'missing' | 'extra' | 'type_mismatch';
|
|
6
|
+
expectedType?: string;
|
|
7
|
+
actualType?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface SeedDiff {
|
|
10
|
+
slug: string;
|
|
11
|
+
tableExists: boolean;
|
|
12
|
+
columns: ColumnDiff[];
|
|
13
|
+
}
|
|
14
|
+
export declare function diffSeed(seed: Seed, options: WranglerOptions): Promise<SeedDiff>;
|
|
15
|
+
//# sourceMappingURL=schema-diff.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema-diff.d.ts","sourceRoot":"","sources":["../../src/lib/schema-diff.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,aAAa,CAAA;AAEvC,OAAO,KAAK,EAAE,eAAe,EAAS,MAAM,eAAe,CAAA;AAU3D,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,MAAM,CAAA;IACZ,MAAM,EAAE,IAAI,GAAG,SAAS,GAAG,OAAO,GAAG,eAAe,CAAA;IACpD,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB;AAED,MAAM,WAAW,QAAQ;IACvB,IAAI,EAAE,MAAM,CAAA;IACZ,WAAW,EAAE,OAAO,CAAA;IACpB,OAAO,EAAE,UAAU,EAAE,CAAA;CACtB;AAED,wBAAsB,QAAQ,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,eAAe,GAAG,OAAO,CAAC,QAAQ,CAAC,CAsCtF"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { getExpectedColumns } from '@beech/core';
|
|
2
|
+
import { queryD1 } from './wrangler.js';
|
|
3
|
+
export async function diffSeed(seed, options) {
|
|
4
|
+
const tableName = `content_${seed.slug}`;
|
|
5
|
+
const expected = getExpectedColumns(seed);
|
|
6
|
+
let actual;
|
|
7
|
+
try {
|
|
8
|
+
actual = queryD1(`PRAGMA table_info(${tableName})`, options);
|
|
9
|
+
}
|
|
10
|
+
catch {
|
|
11
|
+
return { slug: seed.slug, tableExists: false, columns: expected.map(c => ({ name: c.name, status: 'missing', expectedType: c.sqlType })) };
|
|
12
|
+
}
|
|
13
|
+
if (actual.length === 0) {
|
|
14
|
+
return { slug: seed.slug, tableExists: false, columns: expected.map(c => ({ name: c.name, status: 'missing', expectedType: c.sqlType })) };
|
|
15
|
+
}
|
|
16
|
+
const actualMap = new Map(actual.map(r => [r.name, r]));
|
|
17
|
+
const expectedSet = new Set(expected.map(c => c.name));
|
|
18
|
+
const columns = [];
|
|
19
|
+
for (const col of expected) {
|
|
20
|
+
const actual = actualMap.get(col.name);
|
|
21
|
+
if (!actual) {
|
|
22
|
+
columns.push({ name: col.name, status: 'missing', expectedType: col.sqlType });
|
|
23
|
+
}
|
|
24
|
+
else if (actual.type.toUpperCase() !== col.sqlType) {
|
|
25
|
+
columns.push({ name: col.name, status: 'type_mismatch', expectedType: col.sqlType, actualType: actual.type });
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
columns.push({ name: col.name, status: 'ok' });
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
for (const row of actual) {
|
|
32
|
+
if (!expectedSet.has(row.name)) {
|
|
33
|
+
columns.push({ name: row.name, status: 'extra', actualType: row.type });
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return { slug: seed.slug, tableExists: true, columns };
|
|
37
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface WranglerOptions {
|
|
2
|
+
db: string;
|
|
3
|
+
local: boolean;
|
|
4
|
+
configPath: string | null;
|
|
5
|
+
}
|
|
6
|
+
export interface D1Row {
|
|
7
|
+
[key: string]: unknown;
|
|
8
|
+
}
|
|
9
|
+
/** Esegue SQL da file temporaneo via `wrangler d1 execute --file`. */
|
|
10
|
+
export declare function executeD1File(sql: string, options: WranglerOptions): void;
|
|
11
|
+
/** Esegue una query SQL e ritorna i risultati come array di oggetti (--json). */
|
|
12
|
+
export declare function queryD1<T extends D1Row = D1Row>(sql: string, options: WranglerOptions): T[];
|
|
13
|
+
/** Trova il path di wrangler.jsonc in apps/api/ relativo a cwd. */
|
|
14
|
+
export declare function findWranglerConfig(): string | null;
|
|
15
|
+
/** Risolve il nome del database D1 da wrangler.jsonc (grepping 'database_name'). */
|
|
16
|
+
export declare function resolveDbName(configPath: string | null): string;
|
|
17
|
+
//# sourceMappingURL=wrangler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wrangler.d.ts","sourceRoot":"","sources":["../../src/lib/wrangler.ts"],"names":[],"mappings":"AAKA,MAAM,WAAW,eAAe;IAC9B,EAAE,EAAE,MAAM,CAAA;IACV,KAAK,EAAE,OAAO,CAAA;IACd,UAAU,EAAE,MAAM,GAAG,IAAI,CAAA;CAC1B;AAED,MAAM,WAAW,KAAK;IACpB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CACvB;AAgBD,sEAAsE;AACtE,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,eAAe,GAAG,IAAI,CAYzE;AAED,iFAAiF;AACjF,wBAAgB,OAAO,CAAC,CAAC,SAAS,KAAK,GAAG,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,eAAe,GAAG,CAAC,EAAE,CAc3F;AAED,mEAAmE;AACnE,wBAAgB,kBAAkB,IAAI,MAAM,GAAG,IAAI,CAGlD;AAED,oFAAoF;AACpF,wBAAgB,aAAa,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI,GAAG,MAAM,CAS/D"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { execSync, spawnSync } from 'node:child_process';
|
|
2
|
+
import { writeFileSync, rmSync, existsSync } from 'node:fs';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
4
|
+
import { join, resolve } from 'node:path';
|
|
5
|
+
function buildArgs(options) {
|
|
6
|
+
const args = [];
|
|
7
|
+
if (options.configPath)
|
|
8
|
+
args.push('--config', options.configPath);
|
|
9
|
+
if (options.local)
|
|
10
|
+
args.push('--local');
|
|
11
|
+
else
|
|
12
|
+
args.push('--remote');
|
|
13
|
+
return args;
|
|
14
|
+
}
|
|
15
|
+
/** Esegue SQL da file temporaneo via `wrangler d1 execute --file`. */
|
|
16
|
+
export function executeD1File(sql, options) {
|
|
17
|
+
const tmpFile = join(tmpdir(), `beech-seed-${Date.now()}.sql`);
|
|
18
|
+
try {
|
|
19
|
+
writeFileSync(tmpFile, sql, 'utf-8');
|
|
20
|
+
const args = ['d1', 'execute', options.db, '--file', tmpFile, ...buildArgs(options)];
|
|
21
|
+
const result = spawnSync('npx', ['wrangler', ...args], { stdio: 'inherit', cwd: process.cwd() });
|
|
22
|
+
if (result.status !== 0) {
|
|
23
|
+
process.exit(result.status ?? 1);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
finally {
|
|
27
|
+
try {
|
|
28
|
+
rmSync(tmpFile);
|
|
29
|
+
}
|
|
30
|
+
catch { }
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/** Esegue una query SQL e ritorna i risultati come array di oggetti (--json). */
|
|
34
|
+
export function queryD1(sql, options) {
|
|
35
|
+
const args = ['d1', 'execute', options.db, '--command', sql, '--json', ...buildArgs(options)];
|
|
36
|
+
const result = spawnSync('npx', ['wrangler', ...args], { encoding: 'utf-8', cwd: process.cwd() });
|
|
37
|
+
if (result.status !== 0) {
|
|
38
|
+
throw new Error(`wrangler d1 execute failed:\n${result.stderr}`);
|
|
39
|
+
}
|
|
40
|
+
try {
|
|
41
|
+
const parsed = JSON.parse(result.stdout);
|
|
42
|
+
return (parsed[0]?.results ?? []);
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
throw new Error(`Failed to parse wrangler JSON output:\n${result.stdout}`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/** Trova il path di wrangler.jsonc in apps/api/ relativo a cwd. */
|
|
49
|
+
export function findWranglerConfig() {
|
|
50
|
+
const candidate = resolve(process.cwd(), 'apps', 'api', 'wrangler.jsonc');
|
|
51
|
+
return existsSync(candidate) ? candidate : null;
|
|
52
|
+
}
|
|
53
|
+
/** Risolve il nome del database D1 da wrangler.jsonc (grepping 'database_name'). */
|
|
54
|
+
export function resolveDbName(configPath) {
|
|
55
|
+
if (!configPath)
|
|
56
|
+
return 'beech-db';
|
|
57
|
+
try {
|
|
58
|
+
const content = execSync(`cat "${configPath}"`, { encoding: 'utf-8' });
|
|
59
|
+
const match = content.match(/"database_name"\s*:\s*"([^"]+)"/);
|
|
60
|
+
return match?.[1] ?? 'beech-db';
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return 'beech-db';
|
|
64
|
+
}
|
|
65
|
+
}
|
package/package.json
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@beechcms/cli",
|
|
3
|
-
"version": "0.6.0-preview.
|
|
3
|
+
"version": "0.6.0-preview.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"exports": {
|
|
7
7
|
".": "./dist/index.js"
|
|
8
8
|
},
|
|
9
|
-
"scripts": {
|
|
10
|
-
"build": "tsc --noEmit && esbuild src/index.ts --bundle --packages=external --platform=node --format=esm --outfile=dist/index.js",
|
|
11
|
-
"dev": "esbuild src/index.ts --bundle --packages=external --platform=node --format=esm --outfile=dist/index.js --watch",
|
|
12
|
-
"test": "vitest run",
|
|
13
|
-
"test:coverage": "vitest run --coverage"
|
|
14
|
-
},
|
|
15
9
|
"dependencies": {
|
|
16
|
-
"@beechcms/core": "^0.6.0-preview.
|
|
10
|
+
"@beechcms/core": "^0.6.0-preview.3",
|
|
17
11
|
"picocolors": "^1.1.1"
|
|
18
12
|
},
|
|
19
13
|
"devDependencies": {
|
|
20
|
-
"esbuild": "^0.
|
|
14
|
+
"esbuild": "^0.28.1",
|
|
21
15
|
"typescript": "^5.9.3",
|
|
22
16
|
"vitest": "^4.1.0"
|
|
23
17
|
},
|
|
24
|
-
"license": "MIT"
|
|
25
|
-
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsc --noEmit && esbuild src/index.ts --bundle --packages=external --platform=node --format=esm --outfile=dist/index.js",
|
|
21
|
+
"dev": "esbuild src/index.ts --bundle --packages=external --platform=node --format=esm --outfile=dist/index.js --watch",
|
|
22
|
+
"test": "vitest run",
|
|
23
|
+
"test:coverage": "vitest run --coverage"
|
|
24
|
+
}
|
|
25
|
+
}
|