mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
cli run will prefer data file now, improved console coloring
This commit is contained in:
@@ -2,10 +2,12 @@ import type { Config } from "@libsql/client/node";
|
||||
import { App, type CreateAppConfig } from "App";
|
||||
import { StorageLocalAdapter } from "adapter/node";
|
||||
import type { CliBkndConfig, CliCommand } from "cli/types";
|
||||
import { replaceConsole } from "cli/utils/cli";
|
||||
import { Option } from "commander";
|
||||
import { config } from "core";
|
||||
import dotenv from "dotenv";
|
||||
import { registries } from "modules/registries";
|
||||
import c from "picocolors";
|
||||
import {
|
||||
PLATFORMS,
|
||||
type Platform,
|
||||
@@ -27,6 +29,13 @@ export const run: CliCommand = (program) => {
|
||||
.default(config.server.default_port)
|
||||
.argParser((v) => Number.parseInt(v))
|
||||
)
|
||||
.addOption(
|
||||
new Option("-m, --memory", "use in-memory database").conflicts([
|
||||
"config",
|
||||
"db-url",
|
||||
"db-token"
|
||||
])
|
||||
)
|
||||
.addOption(new Option("-c, --config <config>", "config file"))
|
||||
.addOption(
|
||||
new Option("--db-url <db>", "database url, can be any valid libsql url").conflicts(
|
||||
@@ -97,33 +106,44 @@ export async function makeConfigApp(config: CliBkndConfig, platform?: Platform)
|
||||
|
||||
async function action(options: {
|
||||
port: number;
|
||||
memory?: boolean;
|
||||
config?: string;
|
||||
dbUrl?: string;
|
||||
dbToken?: string;
|
||||
server: Platform;
|
||||
}) {
|
||||
replaceConsole();
|
||||
const configFilePath = await getConfigPath(options.config);
|
||||
|
||||
let app: App | undefined = undefined;
|
||||
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 } });
|
||||
} else if (configFilePath) {
|
||||
console.log("[INFO] Using config from:", 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);
|
||||
} else if (options.memory) {
|
||||
console.info("Using", c.cyan("in-memory"), "connection");
|
||||
app = await makeApp({ server: { platform: options.server } });
|
||||
} else {
|
||||
const credentials = getConnectionCredentialsFromEnv();
|
||||
if (credentials) {
|
||||
console.log("[INFO] Using connection from environment");
|
||||
console.info("Using connection from env", c.cyan(credentials.url));
|
||||
app = await makeConfigApp({ app: { connection: credentials } }, options.server);
|
||||
}
|
||||
}
|
||||
|
||||
if (!app) {
|
||||
app = await makeApp({ server: { platform: options.server } });
|
||||
const connection = { url: "file:data.db" } as Config;
|
||||
console.info("Using connection", c.cyan(connection.url));
|
||||
app = await makeApp({
|
||||
connection,
|
||||
server: { platform: options.server }
|
||||
});
|
||||
}
|
||||
|
||||
await startServer(options.server, app, { port: options.port });
|
||||
|
||||
Reference in New Issue
Block a user