refactor console imports, added config update event

This commit is contained in:
dswbx
2025-07-02 14:02:50 +02:00
parent d41fd5541f
commit 70737c04cd
48 changed files with 100 additions and 63 deletions

View File

@@ -1,5 +1,5 @@
import type { CreateUserPayload } from "auth/AppAuth";
import { $console } from "core";
import { $console } from "core/utils";
import { Event } from "core/events";
import type { em as prototypeEm } from "data/prototype";
import { Connection } from "data/connection/Connection";
@@ -34,7 +34,10 @@ export type AppPluginConfig = {
export type AppPlugin = (app: App) => AppPluginConfig;
abstract class AppEvent<A = {}> extends Event<{ app: App } & A> {}
export class AppConfigUpdatedEvent extends AppEvent {
export class AppConfigUpdatedEvent extends AppEvent<{
module: string;
config: ModuleConfigs[keyof ModuleConfigs];
}> {
static override slug = "app-config-updated";
}
export class AppBuiltEvent extends AppEvent {
@@ -265,7 +268,7 @@ export class App<C extends Connection = Connection, Options extends AppOptions =
$console.log("App config updated", module);
// @todo: potentially double syncing
await this.build({ sync: true });
await this.emgr.emit(new AppConfigUpdatedEvent({ app: this }));
await this.emgr.emit(new AppConfigUpdatedEvent({ app: this, module, config }));
}
protected async onFirstBoot() {

View File

@@ -7,7 +7,7 @@ import { getFresh } from "./modes/fresh";
import { getCached } from "./modes/cached";
import { getDurable } from "./modes/durable";
import type { App } from "bknd";
import { $console } from "core";
import { $console } from "core/utils";
declare global {
namespace Cloudflare {

View File

@@ -8,7 +8,7 @@ import type { CloudflareBkndConfig, CloudflareEnv } from ".";
import { App } from "bknd";
import { makeConfig as makeAdapterConfig } from "bknd/adapter";
import type { Context, ExecutionContext } from "hono";
import { $console } from "core";
import { $console } from "core/utils";
import { setCookie } from "hono/cookie";
import { sqlite } from "bknd/adapter/sqlite";

View File

@@ -3,7 +3,7 @@ import type { App, CreateAppConfig } from "bknd";
import { createRuntimeApp, makeConfig } from "bknd/adapter";
import type { CloudflareBkndConfig, Context, CloudflareEnv } from "../index";
import { constants, registerAsyncsExecutionContext } from "../config";
import { $console } from "core";
import { $console } from "core/utils";
export async function getDurable<Env extends CloudflareEnv = CloudflareEnv>(
config: CloudflareBkndConfig<Env>,

View File

@@ -1,5 +1,6 @@
import { App, type CreateAppConfig } from "bknd";
import { config as $config, $console } from "bknd/core";
import { config as $config } from "bknd/core";
import { $console } from "bknd/utils";
import type { MiddlewareHandler } from "hono";
import type { AdminControllerOptions } from "modules/server/AdminController";
import { Connection } from "bknd/data";

View File

@@ -4,7 +4,7 @@ import { serveStatic } from "@hono/node-server/serve-static";
import { registerLocalMediaAdapter } from "adapter/node/storage";
import { type RuntimeBkndConfig, createRuntimeApp, type RuntimeOptions } from "bknd/adapter";
import { config as $config } from "bknd/core";
import { $console } from "core";
import { $console } from "core/utils";
import type { App } from "App";
type NodeEnv = NodeJS.ProcessEnv;

View File

@@ -1,7 +1,7 @@
import { Authenticator, AuthPermissions, Role, type Strategy } from "auth";
import type { PasswordStrategy } from "auth/authenticate/strategies";
import { $console, type DB } from "core";
import { secureRandomString, transformObject } from "core/utils";
import type { DB } from "core";
import { $console, secureRandomString, transformObject } from "core/utils";
import type { Entity, EntityManager } from "data";
import { em, entity, enumm, type FieldSchema, text } from "data/prototype";
import { Module } from "modules/Module";

View File

@@ -1,6 +1,6 @@
import { AppAuth } from "auth/AppAuth";
import type { CreateUser, SafeUser, User, UserPool } from "auth/authenticate/Authenticator";
import { $console } from "core";
import { $console } from "core/utils";
import { pick } from "lodash-es";
import {
InvalidConditionsException,

View File

@@ -1,6 +1,7 @@
import { $console, type DB, Exception } from "core";
import { type DB, Exception } from "core";
import { addFlashMessage } from "core/server/flash";
import {
$console,
type Static,
StringEnum,
type TObject,

View File

@@ -1,6 +1,6 @@
import { type Authenticator, InvalidCredentialsException, type User } from "auth";
import { $console, tbValidator as tb } from "core";
import { hash, parse, type Static, StrictObject, StringEnum } from "core/utils";
import { tbValidator as tb } from "core";
import { $console, hash, parse, type Static, StrictObject, StringEnum } from "core/utils";
import { Hono } from "hono";
import { compare as bcryptCompare, genSalt as bcryptGenSalt, hash as bcryptHash } from "bcryptjs";
import * as tbbox from "@sinclair/typebox";

View File

@@ -1,5 +1,5 @@
import { $console, Exception, Permission } from "core";
import { objectTransform } from "core/utils";
import { Exception, Permission } from "core";
import { $console, objectTransform } from "core/utils";
import type { Context } from "hono";
import type { ServerEnv } from "modules/Controller";
import { Role } from "./Role";

View File

@@ -1,5 +1,5 @@
import { $console, type Permission } from "core";
import { patternMatch } from "core/utils";
import type { Permission } from "core";
import { $console, patternMatch } from "core/utils";
import type { Context } from "hono";
import { createMiddleware } from "hono/factory";
import type { ServerEnv } from "modules/Controller";

View File

@@ -1,5 +1,5 @@
import path from "node:path";
import { $console } from "core";
import { $console } from "core/utils";
import type { MiddlewareHandler } from "hono";
import open from "open";
import { fileExists, getRelativeDistPath } from "../../utils/sys";

View File

@@ -3,7 +3,7 @@ import type { App, CreateAppConfig } from "App";
import { StorageLocalAdapter } from "adapter/node/storage";
import type { CliBkndConfig, CliCommand } from "cli/types";
import { Option } from "commander";
import { colorizeConsole, config } from "core";
import { config } from "core";
import dotenv from "dotenv";
import { registries } from "modules/registries";
import c from "picocolors";
@@ -17,7 +17,7 @@ import {
startServer,
} from "./platform";
import { createRuntimeApp, makeConfig } from "adapter";
import { isBun } from "core/utils";
import { colorizeConsole, isBun } from "core/utils";
const env_files = [".env", ".dev.vars"];
dotenv.config({

View File

@@ -9,7 +9,7 @@ import type { PasswordStrategy } from "auth/authenticate/strategies";
import { makeAppFromEnv } from "cli/commands/run";
import type { CliCommand } from "cli/types";
import { Argument } from "commander";
import { $console } from "core";
import { $console } from "core/utils";
import c from "picocolors";
import { isBun } from "core/utils";

View File

@@ -1,4 +1,4 @@
import { $console } from "core";
import { $console } from "core/utils";
import { execSync, exec as nodeExec } from "node:child_process";
import { readFile, writeFile as nodeWriteFile } from "node:fs/promises";
import path from "node:path";

View File

@@ -1,6 +1,7 @@
import { PostHog } from "posthog-js-lite";
import { getVersion } from "cli/utils/sys";
import { $console, env, isDebug } from "core";
import { env, isDebug } from "core";
import { $console } from "core/utils";
type Properties = { [p: string]: any };

View File

@@ -1,6 +1,6 @@
import { type Event, type EventClass, InvalidEventReturn } from "./Event";
import { EventListener, type ListenerHandler, type ListenerMode } from "./EventListener";
import { $console } from "core";
import { $console } from "core/utils";
export type RegisterListenerConfig =
| ListenerMode

View File

@@ -38,7 +38,6 @@ export {
} from "./object/schema";
export * from "./drivers";
export * from "./console";
export * from "./events";
// compatibility

View File

@@ -2,7 +2,7 @@ import { extension, guess, isMimeType } from "media/storage/mime-types-tiny";
import { randomString } from "core/utils/strings";
import type { Context } from "hono";
import { invariant } from "core/utils/runtime";
import { $console } from "../console";
import { $console } from "./console";
export function getContentName(request: Request): string | undefined;
export function getContentName(contentDisposition: string): string | undefined;

View File

@@ -1,3 +1,4 @@
export * from "./console";
export * from "./browser";
export * from "./objects";
export * from "./strings";

View File

@@ -1,4 +1,4 @@
import { $console } from "core";
import { $console } from "./console";
type ConsoleSeverity = "log" | "warn" | "error";
const _oldConsoles = {
@@ -36,14 +36,14 @@ export function disableConsoleLog(severities: ConsoleSeverity[] = ["log", "warn"
severities.forEach((severity) => {
console[severity] = () => null;
});
$console.setLevel("critical");
$console?.setLevel("critical");
}
export function enableConsoleLog() {
Object.entries(_oldConsoles).forEach(([severity, fn]) => {
console[severity as ConsoleSeverity] = fn;
});
$console.resetLevel();
$console?.resetLevel();
}
export function formatMemoryUsage() {

View File

@@ -1,4 +1,3 @@
import { $console, isDebug } from "core";
import {
DataPermissions,
type EntityData,

View File

@@ -1,5 +1,6 @@
import { $console, config } from "core";
import { config } from "core";
import {
$console,
type Static,
StringEnum,
parse,

View File

@@ -1,4 +1,5 @@
import { $console, type DB as DefaultDB } from "core";
import type { DB as DefaultDB } from "core";
import { $console } from "core/utils";
import { EventManager } from "core/events";
import { sql } from "kysely";
import { Connection } from "../connection/Connection";

View File

@@ -1,4 +1,4 @@
import { $console } from "core/console";
import { $console } from "core/utils";
import type { Entity, EntityData } from "../Entity";
import type { EntityManager } from "../EntityManager";
import { Result, type ResultJSON, type ResultOptions } from "../Result";
@@ -32,6 +32,7 @@ export class MutatorResult<T = EntityData[]> extends Result<T> {
onError: (error) => {
if (!options?.silent) {
$console.error("[ERROR] Mutator:", error.message);
throw error;
}
},
...options,

View File

@@ -1,5 +1,5 @@
import type { DB as DefaultDB, PrimaryFieldType } from "core";
import { $console } from "core";
import { $console } from "core/utils";
import { type EmitsEvents, EventManager } from "core/events";
import { type SelectQueryBuilder, sql } from "kysely";
import { InvalidSearchParamsException } from "../../errors";

View File

@@ -1,9 +1,8 @@
import { $console } from "core/console";
import type { Entity, EntityData } from "../Entity";
import type { EntityManager } from "../EntityManager";
import { Result, type ResultJSON, type ResultOptions } from "../Result";
import type { Compilable, SelectQueryBuilder } from "kysely";
import { ensureInt } from "core/utils";
import { $console, ensureInt } from "core/utils";
export type RepositoryResultOptions = ResultOptions & {
silent?: boolean;

View File

@@ -1,4 +1,5 @@
import { $console, type PrimaryFieldType } from "core";
import type { PrimaryFieldType } from "core";
import { $console } from "core/utils";
import { Event, InvalidEventReturn } from "core/events";
import type { Entity, EntityData } from "../entities";
import type { RepoQuery } from "data/server/query";

View File

@@ -1,7 +1,6 @@
import { type Static, StringEnum, dayjs } from "core/utils";
import { $console, type Static, StringEnum, dayjs } from "core/utils";
import type { EntityManager } from "../entities";
import { Field, type TActionContext, type TRenderContext, baseFieldConfigSchema } from "./Field";
import { $console } from "core";
import * as tbbox from "@sinclair/typebox";
import type { TFieldTSType } from "data/entities/EntityTypescript";
const { Type } = tbbox;

View File

@@ -2,7 +2,7 @@ import type { CompiledQuery, TableMetadata } from "kysely";
import type { IndexMetadata, SchemaResponse } from "../connection/Connection";
import type { Entity, EntityManager } from "../entities";
import { PrimaryField } from "../fields";
import { $console } from "core";
import { $console } from "core/utils";
type IntrospectedTable = TableMetadata & {
indices: IndexMetadata[];

View File

@@ -1,7 +1,6 @@
import { s } from "core/object/schema";
import { WhereBuilder, type WhereQuery } from "data/entities/query/WhereBuilder";
import { $console } from "core";
import { isObject } from "core/utils";
import { isObject, $console } from "core/utils";
import type { CoercionOptions, TAnyOf } from "jsonv-ts";
// -------

View File

@@ -2,7 +2,7 @@ import { Event, EventManager, type ListenerHandler } from "core/events";
import type { EmitsEvents } from "core/events";
import type { Task, TaskResult } from "../tasks/Task";
import type { Flow } from "./Flow";
import { $console } from "core";
import { $console } from "core/utils";
export type TaskLog = TaskResult & {
task: Task;

View File

@@ -1,11 +1,10 @@
import { objectTransform, transformObject } from "core/utils";
import { $console, transformObject } from "core/utils";
import { type TaskMapType, TriggerMap } from "../index";
import type { Task } from "../tasks/Task";
import { Condition, TaskConnection } from "../tasks/TaskConnection";
import { Execution } from "./Execution";
import { FlowTaskConnector } from "./FlowTaskConnector";
import { Trigger } from "./triggers/Trigger";
import { $console } from "core";
type Jsoned<T extends { toJSON: () => object }> = ReturnType<T["toJSON"]>;

View File

@@ -1,5 +1,5 @@
import type { Task } from "../../tasks/Task";
import { $console } from "core";
import { $console } from "core/utils";
export class RuntimeExecutor {
async run(

View File

@@ -1,7 +1,7 @@
import type { EventManager } from "core/events";
import type { Flow } from "../Flow";
import { Trigger } from "./Trigger";
import { $console } from "core";
import { $console } from "core/utils";
import * as tbbox from "@sinclair/typebox";
const { Type } = tbbox;

View File

@@ -1,5 +1,5 @@
import { Task } from "../Task";
import { $console } from "core";
import { $console } from "core/utils";
import * as tbbox from "@sinclair/typebox";
const { Type } = tbbox;

View File

@@ -16,6 +16,7 @@ export {
type ModuleManagerOptions,
type ModuleBuildContext,
type InitialModuleConfigs,
ModuleManagerEvents,
} from "./modules/ModuleManager";
export type { ServerEnv } from "modules/Controller";

View File

@@ -1,4 +1,5 @@
import { $console, type AppEntity } from "core";
import type { AppEntity } from "core";
import { $console } from "core/utils";
import type { Entity, EntityManager } from "data";
import { type FileUploadedEventData, Storage, type StorageAdapter, MediaPermissions } from "media";
import { Module } from "modules/Module";

View File

@@ -1,9 +1,8 @@
import { type EmitsEvents, EventManager } from "core/events";
import { isFile, detectImageDimensions } from "core/utils";
import { $console, isFile, detectImageDimensions } from "core/utils";
import { isMimeType } from "media/storage/mime-types-tiny";
import * as StorageEvents from "./events";
import type { FileUploadedEventData } from "./events";
import { $console } from "core";
import type { StorageAdapter } from "./StorageAdapter";
export type FileListObject = {

View File

@@ -1,4 +1,5 @@
import { $console, type PrimaryFieldType } from "core";
import type { PrimaryFieldType } from "core";
import { $console } from "core/utils";
import { isDebug } from "core/env";
import { encodeSearch } from "core/utils/reqres";
import type { ApiFetcher } from "Api";

View File

@@ -1,6 +1,7 @@
import { Guard } from "auth";
import { $console, BkndError, DebugLogger, env } from "core";
import { EventManager } from "core/events";
import { BkndError, DebugLogger, env } from "core";
import { $console } from "core/utils";
import { EventManager, Event } from "core/events";
import * as $diff from "core/object/diff";
import {
Default,
@@ -126,9 +127,24 @@ interface T_INTERNAL_EM {
const debug_modules = env("modules_debug");
abstract class ModuleManagerEvent<A = {}> extends Event<{ ctx: ModuleBuildContext } & A> {}
export class ModuleManagerConfigUpdateEvent<
Module extends keyof ModuleConfigs,
> extends ModuleManagerEvent<{
module: Module;
config: ModuleConfigs[Module];
}> {
static override slug = "mm-config-update";
}
export const ModuleManagerEvents = {
ModuleManagerConfigUpdateEvent,
};
// @todo: cleanup old diffs on upgrade
// @todo: cleanup multiple backups on upgrade
export class ModuleManager {
static Events = ModuleManagerEvents;
protected modules: Modules;
// internal em for __bknd config table
__em!: EntityManager<T_INTERNAL_EM>;
@@ -151,7 +167,7 @@ export class ModuleManager {
) {
this.__em = new EntityManager([__bknd], this.connection);
this.modules = {} as Modules;
this.emgr = new EventManager();
this.emgr = new EventManager({ ...ModuleManagerEvents });
this.logger = new DebugLogger(debug_modules);
let initial = {} as Partial<ModuleConfigs>;
@@ -628,6 +644,13 @@ export class ModuleManager {
try {
// overwrite listener to run build inside this try/catch
module.setListener(async () => {
await this.emgr.emit(
new ModuleManagerConfigUpdateEvent({
ctx: this.ctx(),
module: name,
config: module.config as any,
}),
);
await this.buildModules();
});

View File

@@ -1,7 +1,8 @@
/** @jsxImportSource hono/jsx */
import type { App } from "App";
import { $console, config, isDebug } from "core";
import { config, isDebug } from "core";
import { $console } from "core/utils";
import { addFlashMessage } from "core/server/flash";
import { html } from "hono/html";
import { Fragment } from "hono/jsx";

View File

@@ -1,5 +1,5 @@
import { Exception, isDebug, $console } from "core";
import { type Static, StringEnum } from "core/utils";
import { Exception, isDebug } from "core";
import { type Static, StringEnum, $console } from "core/utils";
import { cors } from "hono/cors";
import { Module } from "modules/Module";
import * as tbbox from "@sinclair/typebox";

View File

@@ -1,9 +1,8 @@
/// <reference types="@cloudflare/workers-types" />
import type { App } from "App";
import { $console, tbValidator as tb } from "core";
import {
StringEnum,
$console,
TypeInvalidError,
datetimeStringLocal,
datetimeStringUTC,