mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-15 20:17:22 +00:00
fix: cli: user command now uses the same app env setup as run
This commit is contained in:
@@ -38,6 +38,7 @@
|
||||
"test:vitest:watch": "vitest",
|
||||
"test:vitest:coverage": "vitest run --coverage",
|
||||
"test:e2e": "playwright test",
|
||||
"test:e2e:adapters": "bun run e2e/adapters.ts",
|
||||
"test:e2e:ui": "playwright test --ui",
|
||||
"test:e2e:debug": "playwright test --debug",
|
||||
"test:e2e:report": "playwright show-report"
|
||||
|
||||
@@ -17,12 +17,13 @@ import {
|
||||
startServer,
|
||||
} from "./platform";
|
||||
import { makeConfig } from "adapter";
|
||||
import { isBun as $isBun } from "cli/utils/sys";
|
||||
|
||||
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 = $isBun();
|
||||
|
||||
export const run: CliCommand = (program) => {
|
||||
program
|
||||
@@ -98,7 +99,7 @@ export async function makeConfigApp(_config: CliBkndConfig, platform?: Platform)
|
||||
});
|
||||
}
|
||||
|
||||
async function action(options: {
|
||||
type RunOptions = {
|
||||
port: number;
|
||||
memory?: boolean;
|
||||
config?: string;
|
||||
@@ -106,8 +107,9 @@ async function action(options: {
|
||||
dbToken?: string;
|
||||
server: Platform;
|
||||
open?: boolean;
|
||||
}) {
|
||||
colorizeConsole(console);
|
||||
};
|
||||
|
||||
export async function makeAppFromEnv(options: Partial<RunOptions> = {}) {
|
||||
const configFilePath = await getConfigPath(options.config);
|
||||
|
||||
let app: App | undefined = undefined;
|
||||
@@ -147,12 +149,19 @@ 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));
|
||||
console.info("Using fallback connection", c.cyan(connection.url));
|
||||
app = await makeApp({
|
||||
connection,
|
||||
server: { platform: options.server },
|
||||
});
|
||||
}
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
async function action(options: RunOptions) {
|
||||
colorizeConsole(console);
|
||||
|
||||
const app = await makeAppFromEnv(options);
|
||||
await startServer(options.server, app, { port: options.port, open: options.open });
|
||||
}
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
import {
|
||||
isCancel as $isCancel,
|
||||
log as $log,
|
||||
password as $password,
|
||||
text as $text,
|
||||
log as $log,
|
||||
isCancel as $isCancel,
|
||||
} from "@clack/prompts";
|
||||
import type { App } from "App";
|
||||
import type { PasswordStrategy } from "auth/authenticate/strategies";
|
||||
import { makeConfigApp } from "cli/commands/run";
|
||||
import { getConfigPath } from "cli/commands/run/platform";
|
||||
import type { CliBkndConfig, CliCommand } from "cli/types";
|
||||
import { makeAppFromEnv } from "cli/commands/run";
|
||||
import type { CliCommand } from "cli/types";
|
||||
import { Argument } from "commander";
|
||||
import { $console } from "core";
|
||||
import c from "picocolors";
|
||||
import { isBun } from "cli/utils/sys";
|
||||
|
||||
export const user: CliCommand = (program) => {
|
||||
program
|
||||
@@ -24,14 +24,9 @@ export const user: CliCommand = (program) => {
|
||||
};
|
||||
|
||||
async function action(action: "create" | "update" | "token", options: any) {
|
||||
const configFilePath = await getConfigPath();
|
||||
if (!configFilePath) {
|
||||
console.error("config file not found");
|
||||
return;
|
||||
}
|
||||
|
||||
const config = (await import(configFilePath).then((m) => m.default)) as CliBkndConfig;
|
||||
const app = await makeConfigApp(config, options.server);
|
||||
const app = await makeAppFromEnv({
|
||||
server: "node",
|
||||
});
|
||||
|
||||
switch (action) {
|
||||
case "create":
|
||||
@@ -147,6 +142,11 @@ async function update(app: App, options: any) {
|
||||
}
|
||||
|
||||
async function token(app: App, options: any) {
|
||||
if (isBun()) {
|
||||
$log.error("Please use node to generate tokens");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const config = app.module.auth.toJSON(true);
|
||||
const users_entity = config.entity_name as "users";
|
||||
const em = app.modules.ctx().em;
|
||||
|
||||
@@ -3,6 +3,14 @@ import { readFile } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import url from "node:url";
|
||||
|
||||
export function isBun(): boolean {
|
||||
try {
|
||||
return typeof Bun !== "undefined";
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function getRootPath() {
|
||||
const _path = path.dirname(url.fileURLToPath(import.meta.url));
|
||||
// because of "src", local needs one more level up
|
||||
|
||||
Reference in New Issue
Block a user