mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
updated remix adapter for non-middleware env, fixed console mute
This commit is contained in:
105
app/src/core/console.ts
Normal file
105
app/src/core/console.ts
Normal file
@@ -0,0 +1,105 @@
|
||||
import colors from "picocolors";
|
||||
|
||||
function hasColors() {
|
||||
try {
|
||||
// biome-ignore lint/style/useSingleVarDeclarator: <explanation>
|
||||
const p = process || {},
|
||||
argv = p.argv || [],
|
||||
env = p.env || {};
|
||||
return (
|
||||
!(!!env.NO_COLOR || argv.includes("--no-color")) &&
|
||||
// biome-ignore lint/complexity/useOptionalChain: <explanation>
|
||||
(!!env.FORCE_COLOR ||
|
||||
argv.includes("--color") ||
|
||||
p.platform === "win32" ||
|
||||
((p.stdout || {}).isTTY && env.TERM !== "dumb") ||
|
||||
!!env.CI)
|
||||
);
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const originalConsoles = {
|
||||
error: console.error,
|
||||
warn: console.warn,
|
||||
info: console.info,
|
||||
log: console.log,
|
||||
debug: console.debug
|
||||
} as typeof console;
|
||||
|
||||
function __tty(type: any, args: any[]) {
|
||||
const has = hasColors();
|
||||
const styles = {
|
||||
error: {
|
||||
prefix: colors.red,
|
||||
args: colors.red
|
||||
},
|
||||
warn: {
|
||||
prefix: colors.yellow,
|
||||
args: colors.yellow
|
||||
},
|
||||
info: {
|
||||
prefix: colors.cyan
|
||||
},
|
||||
log: {
|
||||
prefix: colors.gray
|
||||
},
|
||||
debug: {
|
||||
prefix: colors.yellow
|
||||
}
|
||||
} as const;
|
||||
const prefix = styles[type].prefix(
|
||||
`[${type.toUpperCase()}]${has ? " ".repeat(5 - type.length) : ""}`
|
||||
);
|
||||
const _args = args.map((a) =>
|
||||
"args" in styles[type] && has && typeof a === "string" ? styles[type].args(a) : a
|
||||
);
|
||||
return originalConsoles[type](prefix, ..._args);
|
||||
}
|
||||
|
||||
export type TConsoleSeverity = keyof typeof originalConsoles;
|
||||
const severities = Object.keys(originalConsoles) as TConsoleSeverity[];
|
||||
|
||||
let enabled = [...severities];
|
||||
|
||||
export function disableConsole(severities: TConsoleSeverity[] = enabled) {
|
||||
enabled = enabled.filter((s) => !severities.includes(s));
|
||||
}
|
||||
|
||||
export function enableConsole() {
|
||||
enabled = [...severities];
|
||||
}
|
||||
|
||||
export const $console = new Proxy(
|
||||
{},
|
||||
{
|
||||
get: (_, prop) => {
|
||||
if (prop in originalConsoles && enabled.includes(prop as TConsoleSeverity)) {
|
||||
return (...args: any[]) => __tty(prop, args);
|
||||
}
|
||||
return () => null;
|
||||
}
|
||||
}
|
||||
) as typeof console;
|
||||
|
||||
export async function withDisabledConsole<R>(
|
||||
fn: () => Promise<R>,
|
||||
sev?: TConsoleSeverity[]
|
||||
): Promise<R> {
|
||||
disableConsole(sev);
|
||||
try {
|
||||
const result = await fn();
|
||||
enableConsole();
|
||||
return result;
|
||||
} catch (e) {
|
||||
enableConsole();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
export function colorizeConsole(con: typeof console) {
|
||||
for (const [key] of Object.entries(originalConsoles)) {
|
||||
con[key] = $console[key];
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,8 @@ export {
|
||||
} from "./object/query/query";
|
||||
export { Registry, type Constructor } from "./registry/Registry";
|
||||
|
||||
export * from "./console";
|
||||
|
||||
// compatibility
|
||||
export type Middleware = MiddlewareHandler<any, any, any>;
|
||||
export interface ClassController {
|
||||
|
||||
Reference in New Issue
Block a user