/** * emdash export-seed * * Export current database schema (and optionally content) as a seed file */ import { resolve } from "node:path"; import { defineCommand } from "citty"; import consola from "consola"; import { createDatabase } from "../../database/connection.js"; import { runMigrations } from "../../database/migrations/runner.js"; import { exportSeed } from "../../seed/export.js"; export const exportSeedCommand = defineCommand({ meta: { name: "export-seed", description: "Export database schema and content as a seed file", }, args: { database: { type: "string", alias: "d", description: "Database path", default: "./data.db", }, cwd: { type: "string", description: "Working directory", default: process.cwd(), }, "with-content": { type: "string", description: "Include content (all or comma-separated collection names)", required: false, }, pretty: { type: "boolean", description: "Pretty print JSON output", default: true, }, }, async run({ args }) { const cwd = resolve(args.cwd); // Connect to database const dbPath = resolve(cwd, args.database); consola.info(`Database: ${dbPath}`); const db = createDatabase({ url: `file:${dbPath}` }); // Run migrations to ensure tables exist try { await runMigrations(db); } catch (error) { consola.error("Migration failed:", error); await db.destroy(); process.exit(1); } try { const seed = await exportSeed(db, args["with-content"]); // Output to stdout const output = args.pretty ? JSON.stringify(seed, null, "\t") : JSON.stringify(seed); console.log(output); } catch (error) { consola.error("Export failed:", error); await db.destroy(); process.exit(1); } await db.destroy(); }, });