improve cli creds extraction

This commit is contained in:
dswbx
2025-03-28 18:03:09 +01:00
parent 9e3c081e50
commit 11a28eba88
4 changed files with 35 additions and 28 deletions

3
app/.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
test-results
playwright-report
bknd.config.*

View File

@@ -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;
}

View File

@@ -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));
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));

View File

@@ -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<Env = any> = FrameworkBkndConfig & {
app: CreateAppConfig | ((env: Env) => CreateAppConfig);
setAdminHtml?: boolean;
export type CliBkndConfig<Env = any> = BkndConfig & {
server?: {
port?: number;
platform?: "node" | "bun";