mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
cli: automatically register local adapter
This commit is contained in:
@@ -6,14 +6,25 @@ import type { Serve, ServeOptions } from "bun";
|
||||
import { serveStatic } from "hono/bun";
|
||||
|
||||
let app: App;
|
||||
export async function createApp(_config: Partial<CreateAppConfig> = {}, distPath?: string) {
|
||||
export type ExtendedAppCreateConfig = Partial<CreateAppConfig> & {
|
||||
distPath?: string;
|
||||
onBuilt?: (app: App) => Promise<void>;
|
||||
buildOptions?: Parameters<App["build"]>[0];
|
||||
};
|
||||
|
||||
export async function createApp({
|
||||
distPath,
|
||||
onBuilt,
|
||||
buildOptions,
|
||||
...config
|
||||
}: ExtendedAppCreateConfig) {
|
||||
const root = path.resolve(distPath ?? "./node_modules/bknd/dist", "static");
|
||||
|
||||
if (!app) {
|
||||
app = App.create(_config);
|
||||
app = App.create(config);
|
||||
|
||||
app.emgr.on(
|
||||
"app-built",
|
||||
app.emgr.onEvent(
|
||||
App.Events.AppBuiltEvent,
|
||||
async () => {
|
||||
app.modules.server.get(
|
||||
"/*",
|
||||
@@ -22,20 +33,18 @@ export async function createApp(_config: Partial<CreateAppConfig> = {}, distPath
|
||||
})
|
||||
);
|
||||
app.registerAdminController();
|
||||
await onBuilt?.(app);
|
||||
},
|
||||
"sync"
|
||||
);
|
||||
|
||||
await app.build();
|
||||
await app.build(buildOptions);
|
||||
}
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
export type BunAdapterOptions = Omit<ServeOptions, "fetch"> &
|
||||
CreateAppConfig & {
|
||||
distPath?: string;
|
||||
};
|
||||
export type BunAdapterOptions = Omit<ServeOptions, "fetch"> & ExtendedAppCreateConfig;
|
||||
|
||||
export function serve({
|
||||
distPath,
|
||||
@@ -44,13 +53,23 @@ export function serve({
|
||||
plugins,
|
||||
options,
|
||||
port = 1337,
|
||||
onBuilt,
|
||||
buildOptions,
|
||||
...serveOptions
|
||||
}: BunAdapterOptions = {}) {
|
||||
Bun.serve({
|
||||
...serveOptions,
|
||||
port,
|
||||
fetch: async (request: Request) => {
|
||||
const app = await createApp({ connection, initialConfig, plugins, options }, distPath);
|
||||
const app = await createApp({
|
||||
connection,
|
||||
initialConfig,
|
||||
plugins,
|
||||
options,
|
||||
onBuilt,
|
||||
buildOptions,
|
||||
distPath
|
||||
});
|
||||
return app.fetch(request);
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user