mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
improve cli creds extraction
This commit is contained in:
3
app/.gitignore
vendored
Normal file
3
app/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
test-results
|
||||||
|
playwright-report
|
||||||
|
bknd.config.*
|
||||||
@@ -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) {
|
for (const p of paths) {
|
||||||
const _p = path.resolve(process.cwd(), p);
|
const _p = path.relative(process.cwd(), p);
|
||||||
if (await fileExists(_p)) {
|
if (await fileExists(_p)) {
|
||||||
return _p;
|
return _p;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import { colorizeConsole, config } from "core";
|
|||||||
import dotenv from "dotenv";
|
import dotenv from "dotenv";
|
||||||
import { registries } from "modules/registries";
|
import { registries } from "modules/registries";
|
||||||
import c from "picocolors";
|
import c from "picocolors";
|
||||||
|
import path from "node:path";
|
||||||
import {
|
import {
|
||||||
PLATFORMS,
|
PLATFORMS,
|
||||||
type Platform,
|
type Platform,
|
||||||
@@ -15,8 +16,12 @@ import {
|
|||||||
getConnectionCredentialsFromEnv,
|
getConnectionCredentialsFromEnv,
|
||||||
startServer,
|
startServer,
|
||||||
} from "./platform";
|
} 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";
|
const isBun = typeof Bun !== "undefined";
|
||||||
|
|
||||||
export const run: CliCommand = (program) => {
|
export const run: CliCommand = (program) => {
|
||||||
@@ -85,24 +90,12 @@ async function makeApp(config: MakeAppConfig) {
|
|||||||
return app;
|
return app;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function makeConfigApp(config: CliBkndConfig, platform?: Platform) {
|
export async function makeConfigApp(_config: CliBkndConfig, platform?: Platform) {
|
||||||
const appConfig = typeof config.app === "function" ? config.app(process.env) : config.app;
|
const config = makeConfig(_config, process.env);
|
||||||
const app = App.create(appConfig);
|
return makeApp({
|
||||||
|
...config,
|
||||||
app.emgr.onEvent(
|
server: { platform },
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function action(options: {
|
async function action(options: {
|
||||||
@@ -118,19 +111,31 @@ async function action(options: {
|
|||||||
const configFilePath = await getConfigPath(options.config);
|
const configFilePath = await getConfigPath(options.config);
|
||||||
|
|
||||||
let app: App | undefined = undefined;
|
let app: App | undefined = undefined;
|
||||||
|
// first start from arguments if given
|
||||||
if (options.dbUrl) {
|
if (options.dbUrl) {
|
||||||
console.info("Using connection from", c.cyan("--db-url"));
|
console.info("Using connection from", c.cyan("--db-url"));
|
||||||
const connection = options.dbUrl
|
const connection = options.dbUrl
|
||||||
? { url: options.dbUrl, authToken: options.dbToken }
|
? { url: options.dbUrl, authToken: options.dbToken }
|
||||||
: undefined;
|
: undefined;
|
||||||
app = await makeApp({ connection, server: { platform: options.server } });
|
app = await makeApp({ connection, server: { platform: options.server } });
|
||||||
|
|
||||||
|
// check configuration file to be present
|
||||||
} else if (configFilePath) {
|
} else if (configFilePath) {
|
||||||
console.info("Using config from", c.cyan(configFilePath));
|
console.info("Using config from", c.cyan(configFilePath));
|
||||||
const config = (await import(configFilePath).then((m) => m.default)) as CliBkndConfig;
|
try {
|
||||||
app = await makeConfigApp(config, options.server);
|
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) {
|
} else if (options.memory) {
|
||||||
console.info("Using", c.cyan("in-memory"), "connection");
|
console.info("Using", c.cyan("in-memory"), "connection");
|
||||||
app = await makeApp({ server: { platform: options.server } });
|
app = await makeApp({ server: { platform: options.server } });
|
||||||
|
|
||||||
|
// finally try to use env variables
|
||||||
} else {
|
} else {
|
||||||
const credentials = getConnectionCredentialsFromEnv();
|
const credentials = getConnectionCredentialsFromEnv();
|
||||||
if (credentials) {
|
if (credentials) {
|
||||||
@@ -139,6 +144,7 @@ async function action(options: {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if nothing helps, create a file based app
|
||||||
if (!app) {
|
if (!app) {
|
||||||
const connection = { url: "file:data.db" } as Config;
|
const connection = { url: "file:data.db" } as Config;
|
||||||
console.info("Using connection", c.cyan(connection.url));
|
console.info("Using connection", c.cyan(connection.url));
|
||||||
|
|||||||
7
app/src/cli/types.d.ts
vendored
7
app/src/cli/types.d.ts
vendored
@@ -1,12 +1,9 @@
|
|||||||
import type { CreateAppConfig } from "App";
|
import type { BkndConfig } from "adapter";
|
||||||
import type { FrameworkBkndConfig } from "adapter";
|
|
||||||
import type { Command } from "commander";
|
import type { Command } from "commander";
|
||||||
|
|
||||||
export type CliCommand = (program: Command) => void;
|
export type CliCommand = (program: Command) => void;
|
||||||
|
|
||||||
export type CliBkndConfig<Env = any> = FrameworkBkndConfig & {
|
export type CliBkndConfig<Env = any> = BkndConfig & {
|
||||||
app: CreateAppConfig | ((env: Env) => CreateAppConfig);
|
|
||||||
setAdminHtml?: boolean;
|
|
||||||
server?: {
|
server?: {
|
||||||
port?: number;
|
port?: number;
|
||||||
platform?: "node" | "bun";
|
platform?: "node" | "bun";
|
||||||
|
|||||||
Reference in New Issue
Block a user