mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 12:37:20 +00:00
Merge pull request #86 from bknd-io/feat/cli-prefer-file
cli run will prefer data file now, improved console coloring
This commit is contained in:
@@ -32,7 +32,7 @@ export async function attachServeStatic(app: any, platform: Platform) {
|
||||
|
||||
export async function startServer(server: Platform, app: any, options: { port: number }) {
|
||||
const port = options.port;
|
||||
console.log(`(using ${server} serve)`);
|
||||
console.log(`Using ${server} serve`);
|
||||
|
||||
switch (server) {
|
||||
case "node": {
|
||||
@@ -54,7 +54,7 @@ export async function startServer(server: Platform, app: any, options: { port: n
|
||||
}
|
||||
|
||||
const url = `http://localhost:${port}`;
|
||||
console.log(`Server listening on ${url}`);
|
||||
console.info("Server listening on", url);
|
||||
await open(url);
|
||||
}
|
||||
|
||||
|
||||
@@ -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 });
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
import { isDebug } from "core";
|
||||
import c from "picocolors";
|
||||
import type { Formatter } from "picocolors/types";
|
||||
const _SPEEDUP = process.env.LOCAL;
|
||||
|
||||
const DEFAULT_WAIT = _SPEEDUP ? 0 : 250;
|
||||
@@ -54,3 +57,31 @@ export async function* typewriter(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function ifString(args: any[], c: Formatter) {
|
||||
return args.map((a) => (typeof a === "string" ? c(a) : a));
|
||||
}
|
||||
|
||||
const originalConsole = {
|
||||
log: console.log,
|
||||
info: console.info,
|
||||
debug: console.debug,
|
||||
warn: console.warn,
|
||||
error: console.error
|
||||
};
|
||||
|
||||
export const $console = {
|
||||
log: (...args: any[]) => originalConsole.info(c.gray("[LOG] "), ...ifString(args, c.dim)),
|
||||
info: (...args: any[]) => originalConsole.info(c.cyan("[INFO] "), ...args),
|
||||
debug: (...args: any[]) => isDebug() && originalConsole.info(c.yellow("[DEBUG]"), ...args),
|
||||
warn: (...args: any[]) => originalConsole.info(c.yellow("[WARN] "), ...ifString(args, c.yellow)),
|
||||
error: (...args: any[]) => originalConsole.info(c.red("[ERROR]"), ...ifString(args, c.red))
|
||||
};
|
||||
|
||||
export function replaceConsole() {
|
||||
console.log = $console.log;
|
||||
console.info = $console.info;
|
||||
console.debug = $console.debug;
|
||||
console.warn = $console.warn;
|
||||
console.error = $console.error;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user