From 11a28eba887195eb333ef7c7ee4023f76789526d Mon Sep 17 00:00:00 2001 From: dswbx Date: Fri, 28 Mar 2025 18:03:09 +0100 Subject: [PATCH] improve cli creds extraction --- app/.gitignore | 3 ++ app/src/cli/commands/run/platform.ts | 5 +-- app/src/cli/commands/run/run.ts | 48 ++++++++++++++++------------ app/src/cli/types.d.ts | 7 ++-- 4 files changed, 35 insertions(+), 28 deletions(-) create mode 100644 app/.gitignore diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..74f7dc3 --- /dev/null +++ b/app/.gitignore @@ -0,0 +1,3 @@ +test-results +playwright-report +bknd.config.* \ No newline at end of file diff --git a/app/src/cli/commands/run/platform.ts b/app/src/cli/commands/run/platform.ts index 62707bd..90b8358 100644 --- a/app/src/cli/commands/run/platform.ts +++ b/app/src/cli/commands/run/platform.ts @@ -72,9 +72,10 @@ export async function getConfigPath(filePath?: string) { } } - const paths = ["./bknd.config", "./bknd.config.ts", "./bknd.config.js"]; + const exts = ["", ".js", ".ts", ".mjs", ".cjs", ".json"]; + const paths = exts.map((e) => `bknd.config${e}`); for (const p of paths) { - const _p = path.resolve(process.cwd(), p); + const _p = path.relative(process.cwd(), p); if (await fileExists(_p)) { return _p; } diff --git a/app/src/cli/commands/run/run.ts b/app/src/cli/commands/run/run.ts index ff15f10..2d156e1 100644 --- a/app/src/cli/commands/run/run.ts +++ b/app/src/cli/commands/run/run.ts @@ -7,6 +7,7 @@ import { colorizeConsole, config } from "core"; import dotenv from "dotenv"; import { registries } from "modules/registries"; import c from "picocolors"; +import path from "node:path"; import { PLATFORMS, type Platform, @@ -15,8 +16,12 @@ import { getConnectionCredentialsFromEnv, startServer, } from "./platform"; +import { makeConfig } from "adapter"; -dotenv.config(); +const env_files = [".env", ".dev.vars"]; +dotenv.config({ + path: env_files.map((file) => path.resolve(process.cwd(), file)), +}); const isBun = typeof Bun !== "undefined"; export const run: CliCommand = (program) => { @@ -85,24 +90,12 @@ async function makeApp(config: MakeAppConfig) { return app; } -export async function makeConfigApp(config: CliBkndConfig, platform?: Platform) { - const appConfig = typeof config.app === "function" ? config.app(process.env) : config.app; - const app = App.create(appConfig); - - app.emgr.onEvent( - App.Events.AppBuiltEvent, - async () => { - await attachServeStatic(app, platform ?? "node"); - app.registerAdminController(); - - await config.onBuilt?.(app); - }, - "sync", - ); - - await config.beforeBuild?.(app); - await app.build(config.buildConfig); - return app; +export async function makeConfigApp(_config: CliBkndConfig, platform?: Platform) { + const config = makeConfig(_config, process.env); + return makeApp({ + ...config, + server: { platform }, + }); } async function action(options: { @@ -118,19 +111,31 @@ async function action(options: { const configFilePath = await getConfigPath(options.config); let app: App | undefined = undefined; + // first start from arguments if given if (options.dbUrl) { console.info("Using connection from", c.cyan("--db-url")); const connection = options.dbUrl ? { url: options.dbUrl, authToken: options.dbToken } : undefined; app = await makeApp({ connection, server: { platform: options.server } }); + + // check configuration file to be present } else if (configFilePath) { console.info("Using config from", c.cyan(configFilePath)); - const config = (await import(configFilePath).then((m) => m.default)) as CliBkndConfig; - app = await makeConfigApp(config, options.server); + try { + const config = (await import(configFilePath).then((m) => m.default)) as CliBkndConfig; + app = await makeConfigApp(config, options.server); + } catch (e) { + console.error("Failed to load config:", e); + process.exit(1); + } + + // try to use an in-memory connection } else if (options.memory) { console.info("Using", c.cyan("in-memory"), "connection"); app = await makeApp({ server: { platform: options.server } }); + + // finally try to use env variables } else { const credentials = getConnectionCredentialsFromEnv(); if (credentials) { @@ -139,6 +144,7 @@ async function action(options: { } } + // if nothing helps, create a file based app if (!app) { const connection = { url: "file:data.db" } as Config; console.info("Using connection", c.cyan(connection.url)); diff --git a/app/src/cli/types.d.ts b/app/src/cli/types.d.ts index 30bde3a..b1c20ea 100644 --- a/app/src/cli/types.d.ts +++ b/app/src/cli/types.d.ts @@ -1,12 +1,9 @@ -import type { CreateAppConfig } from "App"; -import type { FrameworkBkndConfig } from "adapter"; +import type { BkndConfig } from "adapter"; import type { Command } from "commander"; export type CliCommand = (program: Command) => void; -export type CliBkndConfig = FrameworkBkndConfig & { - app: CreateAppConfig | ((env: Env) => CreateAppConfig); - setAdminHtml?: boolean; +export type CliBkndConfig = BkndConfig & { server?: { port?: number; platform?: "node" | "bun";