From 93019827b0b03f9e07945ac8dcbf91e9f2223e6d Mon Sep 17 00:00:00 2001 From: dswbx Date: Fri, 29 Aug 2025 08:40:14 +0200 Subject: [PATCH] cli: updated config to get config from app, added sync command (#241) * cli: updated config to get config from app, added sync command * updated command description --- app/src/cli/commands/config.ts | 32 +++++++++++++++++++---- app/src/cli/commands/index.ts | 1 + app/src/cli/commands/sync.ts | 47 ++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 app/src/cli/commands/sync.ts diff --git a/app/src/cli/commands/config.ts b/app/src/cli/commands/config.ts index 81e6cb7..24d7e49 100644 --- a/app/src/cli/commands/config.ts +++ b/app/src/cli/commands/config.ts @@ -1,15 +1,37 @@ import { getDefaultConfig } from "modules/ModuleManager"; import type { CliCommand } from "../types"; +import { makeAppFromEnv } from "cli/commands/run"; +import { writeFile } from "node:fs/promises"; +import c from "picocolors"; export const config: CliCommand = (program) => { program .command("config") - .description("get default config") + .description("get app config") .option("--pretty", "pretty print") - .action((options) => { - const config = getDefaultConfig(); + .option("--default", "use default config") + .option("--secrets", "include secrets in output") + .option("--config ", "config file") + .option("--db-url ", "database url, can be any valid sqlite url") + .option("--out ", "output file") + .action(async (options) => { + let config: any = {}; - // biome-ignore lint/suspicious/noConsoleLog: - console.log(options.pretty ? JSON.stringify(config, null, 2) : JSON.stringify(config)); + if (options.default) { + config = getDefaultConfig(); + } else { + const app = await makeAppFromEnv(options); + config = app.toJSON(options.secrets); + } + + config = options.pretty ? JSON.stringify(config, null, 2) : JSON.stringify(config); + + console.info(""); + if (options.out) { + await writeFile(options.out, config); + console.info(`Config written to ${c.cyan(options.out)}`); + } else { + console.info(JSON.parse(config)); + } }); }; diff --git a/app/src/cli/commands/index.ts b/app/src/cli/commands/index.ts index 8e5b8b9..ad014fb 100644 --- a/app/src/cli/commands/index.ts +++ b/app/src/cli/commands/index.ts @@ -7,3 +7,4 @@ export { create } from "./create"; export { copyAssets } from "./copy-assets"; export { types } from "./types"; export { mcp } from "./mcp/mcp"; +export { sync } from "./sync"; diff --git a/app/src/cli/commands/sync.ts b/app/src/cli/commands/sync.ts new file mode 100644 index 0000000..abb16ec --- /dev/null +++ b/app/src/cli/commands/sync.ts @@ -0,0 +1,47 @@ +import type { CliCommand } from "../types"; +import { makeAppFromEnv } from "cli/commands/run"; +import { writeFile } from "node:fs/promises"; +import c from "picocolors"; + +export const sync: CliCommand = (program) => { + program + .command("sync") + .description("sync database") + .option("--config ", "config file") + .option("--db-url ", "database url, can be any valid sqlite url") + .option("--dump", "dump operations to console instead of executing them") + .option("--drop", "include destructive DDL operations") + .option("--out ", "output file") + .option("--sql", "use sql output") + .action(async (options) => { + const app = await makeAppFromEnv(options); + const schema = app.em.schema(); + const stmts = await schema.sync({ drop: options.drop }); + + console.info(""); + if (stmts.length === 0) { + console.info(c.yellow("No changes to sync")); + process.exit(0); + } + // @todo: currently assuming parameters aren't used + const sql = stmts.map((d) => d.sql).join(";\n") + ";"; + + if (options.dump) { + if (options.out) { + const output = options.sql ? sql : JSON.stringify(stmts, null, 2); + await writeFile(options.out, output); + console.info(`SQL written to ${c.cyan(options.out)}`); + } else { + console.info(options.sql ? c.cyan(sql) : stmts); + } + + process.exit(0); + } + + await schema.sync({ force: true, drop: options.drop }); + console.info(c.cyan(sql)); + + console.info(`${c.gray(`Executed ${c.cyan(stmts.length)} statement(s)`)}`); + console.info(`${c.green("Database synced")}`); + }); +};