mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-17 12:56:05 +00:00
cli run will prefer data file now, improved console coloring
This commit is contained in:
@@ -62,17 +62,17 @@ export class App {
|
|||||||
// respond to events, such as "onUpdated".
|
// respond to events, such as "onUpdated".
|
||||||
// this is important if multiple changes are done, and then build() is called manually
|
// this is important if multiple changes are done, and then build() is called manually
|
||||||
if (!this.emgr.enabled) {
|
if (!this.emgr.enabled) {
|
||||||
console.warn("[APP] config updated, but event manager is disabled, skip.");
|
console.warn("App config updated, but event manager is disabled, skip.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("[APP] config updated", key);
|
console.log("App config updated", key);
|
||||||
// @todo: potentially double syncing
|
// @todo: potentially double syncing
|
||||||
await this.build({ sync: true });
|
await this.build({ sync: true });
|
||||||
await this.emgr.emit(new AppConfigUpdatedEvent({ app: this }));
|
await this.emgr.emit(new AppConfigUpdatedEvent({ app: this }));
|
||||||
},
|
},
|
||||||
onFirstBoot: async () => {
|
onFirstBoot: async () => {
|
||||||
console.log("[APP] first boot");
|
console.log("App first boot");
|
||||||
this.trigger_first_boot = true;
|
this.trigger_first_boot = true;
|
||||||
},
|
},
|
||||||
onServerInit: async (server) => {
|
onServerInit: async (server) => {
|
||||||
@@ -183,7 +183,7 @@ export function createApp(config: CreateAppConfig = {}) {
|
|||||||
} else if (typeof config.connection === "object") {
|
} else if (typeof config.connection === "object") {
|
||||||
if ("type" in config.connection) {
|
if ("type" in config.connection) {
|
||||||
console.warn(
|
console.warn(
|
||||||
"[WARN] Using deprecated connection type 'libsql', use the 'config' object directly."
|
"Using deprecated connection type 'libsql', use the 'config' object directly."
|
||||||
);
|
);
|
||||||
connection = new LibsqlConnection(config.connection.config);
|
connection = new LibsqlConnection(config.connection.config);
|
||||||
} else {
|
} else {
|
||||||
@@ -191,7 +191,7 @@ export function createApp(config: CreateAppConfig = {}) {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
connection = new LibsqlConnection({ url: ":memory:" });
|
connection = new LibsqlConnection({ url: ":memory:" });
|
||||||
console.warn("[!] No connection provided, using in-memory database");
|
console.warn("No connection provided, using in-memory database");
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("Could not create connection", e);
|
console.error("Could not create connection", e);
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ export async function attachServeStatic(app: any, platform: Platform) {
|
|||||||
|
|
||||||
export async function startServer(server: Platform, app: any, options: { port: number }) {
|
export async function startServer(server: Platform, app: any, options: { port: number }) {
|
||||||
const port = options.port;
|
const port = options.port;
|
||||||
console.log(`(using ${server} serve)`);
|
console.log(`Using ${server} serve`);
|
||||||
|
|
||||||
switch (server) {
|
switch (server) {
|
||||||
case "node": {
|
case "node": {
|
||||||
@@ -54,7 +54,7 @@ export async function startServer(server: Platform, app: any, options: { port: n
|
|||||||
}
|
}
|
||||||
|
|
||||||
const url = `http://localhost:${port}`;
|
const url = `http://localhost:${port}`;
|
||||||
console.log(`Server listening on ${url}`);
|
console.info("Server listening on", url);
|
||||||
await open(url);
|
await open(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,10 +2,12 @@ import type { Config } from "@libsql/client/node";
|
|||||||
import { App, type CreateAppConfig } from "App";
|
import { App, type CreateAppConfig } from "App";
|
||||||
import { StorageLocalAdapter } from "adapter/node";
|
import { StorageLocalAdapter } from "adapter/node";
|
||||||
import type { CliBkndConfig, CliCommand } from "cli/types";
|
import type { CliBkndConfig, CliCommand } from "cli/types";
|
||||||
|
import { replaceConsole } from "cli/utils/cli";
|
||||||
import { Option } from "commander";
|
import { Option } from "commander";
|
||||||
import { config } from "core";
|
import { 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 {
|
import {
|
||||||
PLATFORMS,
|
PLATFORMS,
|
||||||
type Platform,
|
type Platform,
|
||||||
@@ -27,6 +29,13 @@ export const run: CliCommand = (program) => {
|
|||||||
.default(config.server.default_port)
|
.default(config.server.default_port)
|
||||||
.argParser((v) => Number.parseInt(v))
|
.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("-c, --config <config>", "config file"))
|
||||||
.addOption(
|
.addOption(
|
||||||
new Option("--db-url <db>", "database url, can be any valid libsql url").conflicts(
|
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: {
|
async function action(options: {
|
||||||
port: number;
|
port: number;
|
||||||
|
memory?: boolean;
|
||||||
config?: string;
|
config?: string;
|
||||||
dbUrl?: string;
|
dbUrl?: string;
|
||||||
dbToken?: string;
|
dbToken?: string;
|
||||||
server: Platform;
|
server: Platform;
|
||||||
}) {
|
}) {
|
||||||
|
replaceConsole();
|
||||||
const configFilePath = await getConfigPath(options.config);
|
const configFilePath = await getConfigPath(options.config);
|
||||||
|
|
||||||
let app: App | undefined = undefined;
|
let app: App | undefined = undefined;
|
||||||
if (options.dbUrl) {
|
if (options.dbUrl) {
|
||||||
|
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 } });
|
||||||
} else if (configFilePath) {
|
} 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;
|
const config = (await import(configFilePath).then((m) => m.default)) as CliBkndConfig;
|
||||||
app = await makeConfigApp(config, options.server);
|
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 {
|
} else {
|
||||||
const credentials = getConnectionCredentialsFromEnv();
|
const credentials = getConnectionCredentialsFromEnv();
|
||||||
if (credentials) {
|
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);
|
app = await makeConfigApp({ app: { connection: credentials } }, options.server);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!app) {
|
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 });
|
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 _SPEEDUP = process.env.LOCAL;
|
||||||
|
|
||||||
const DEFAULT_WAIT = _SPEEDUP ? 0 : 250;
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -181,7 +181,7 @@ export class DataController extends Controller {
|
|||||||
//console.log("request", c.req.raw);
|
//console.log("request", c.req.raw);
|
||||||
const { entity, context } = c.req.param();
|
const { entity, context } = c.req.param();
|
||||||
if (!this.entityExists(entity)) {
|
if (!this.entityExists(entity)) {
|
||||||
console.log("not found", entity, definedEntities);
|
console.warn("not found:", entity, definedEntities);
|
||||||
return c.notFound();
|
return c.notFound();
|
||||||
}
|
}
|
||||||
const _entity = this.em.entity(entity);
|
const _entity = this.em.entity(entity);
|
||||||
@@ -208,7 +208,7 @@ export class DataController extends Controller {
|
|||||||
//console.log("request", c.req.raw);
|
//console.log("request", c.req.raw);
|
||||||
const { entity } = c.req.param();
|
const { entity } = c.req.param();
|
||||||
if (!this.entityExists(entity)) {
|
if (!this.entityExists(entity)) {
|
||||||
console.log("not found", entity, definedEntities);
|
console.warn("not found:", entity, definedEntities);
|
||||||
return c.notFound();
|
return c.notFound();
|
||||||
}
|
}
|
||||||
const options = c.req.valid("query") as RepoQuery;
|
const options = c.req.valid("query") as RepoQuery;
|
||||||
|
|||||||
Reference in New Issue
Block a user