From 514f5b3553ba7a6b98eb629bcc207bee18c16c03 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Mon, 10 Apr 2023 23:26:30 +0200 Subject: [PATCH] refactor!: move log levels and types to constants --- src/consola.ts | 23 +++++-- src/constants.ts | 99 +++++++++++++++++++++++++++ src/index.shared.ts | 4 +- src/index.ts | 4 +- src/log.types.ts | 59 ---------------- src/types.ts | 22 +----- src/{log.levels.ts => utils/level.ts} | 14 ---- 7 files changed, 121 insertions(+), 104 deletions(-) create mode 100644 src/constants.ts delete mode 100644 src/log.types.ts rename src/{log.levels.ts => utils/level.ts} (60%) diff --git a/src/consola.ts b/src/consola.ts index cc440db8..a862e8e0 100644 --- a/src/consola.ts +++ b/src/consola.ts @@ -1,11 +1,9 @@ -import { normalizeLogLevel } from "./log.levels"; -import { Types } from "./log.types"; +import { LogTypes, LogType } from "./constants"; import { isLogObj } from "./utils/index"; import type { ConsolaOptions, ConsolaReporter, ConsolaLogObject, - LogType, ConsolaReporterLogObject, } from "./types"; import type { PromptOptions } from "./prompt"; @@ -26,7 +24,7 @@ export class Consola { constructor(options: Partial = {}) { // Options - const types = options.types || Types; + const types = options.types || LogTypes; this.options = { // Defaults throttle: 1000, @@ -35,7 +33,7 @@ export class Consola { ...options, // Overrides, Normalizations and Clones defaults: { ...options.defaults }, - level: normalizeLogLevel(options.level, types), + level: _normalizeLogLevel(options.level, types), reporters: [...(options.reporters || [])], types, }; @@ -65,7 +63,7 @@ export class Consola { } set level(level) { - this.options.level = normalizeLogLevel( + this.options.level = _normalizeLogLevel( level, this.options.types, this.options.level @@ -353,6 +351,19 @@ export class Consola { } } +function _normalizeLogLevel(input: any, types: any = {}, defaultLevel = 3) { + if (input === undefined) { + return defaultLevel; + } + if (typeof input === "number") { + return input; + } + if (types[input] && types[input].level !== undefined) { + return types[input].level; + } + return defaultLevel; +} + export interface ConsolaInstance extends Consola { fatal(message: ConsolaLogObject | any, ...args: any[]): void; error(message: ConsolaLogObject | any, ...args: any[]): void; diff --git a/src/constants.ts b/src/constants.ts new file mode 100644 index 00000000..cb114021 --- /dev/null +++ b/src/constants.ts @@ -0,0 +1,99 @@ +import { ConsolaLogObject } from "./types"; + +// eslint-disable-next-line @typescript-eslint/ban-types +export type LogLevel = 0 | 1 | 2 | 3 | 4 | 5 | (number & {}); + +export const LogLevels: Record = { + silent: Number.NEGATIVE_INFINITY, + fatal: 0, + error: 0, + warn: 1, + log: 2, + info: 3, + success: 3, + fail: 3, + ready: 3, + start: 3, + debug: 4, + trace: 5, + verbose: Number.POSITIVE_INFINITY, +}; + +export type LogType = + // 0 + | "silent" + | "fatal" + | "error" + // 1 + | "warn" + // 2 + | "log" + // 3 + | "info" + | "success" + | "fail" + | "ready" + | "start" + // Verbose + | "debug" + | "trace" + | "verbose" + // eslint-disable-next-line @typescript-eslint/ban-types + | (string & {}); + +export const LogTypes: Record = { + // Silent + silent: { + level: -1, + }, + + // Level 0 + fatal: { + level: LogLevels.Fatal, + }, + error: { + level: LogLevels.Error, + }, + + // Level 1 + warn: { + level: LogLevels.Warn, + }, + + // Level 2 + log: { + level: LogLevels.Log, + }, + + // Level 3 + info: { + level: LogLevels.Info, + }, + success: { + level: LogLevels.Success, + }, + fail: { + level: LogLevels.Fail, + }, + ready: { + level: LogLevels.Info, + }, + start: { + level: LogLevels.Info, + }, + + // Level 4 + debug: { + level: LogLevels.Debug, + }, + + // Level 5 + trace: { + level: LogLevels.Trace, + }, + + // Verbose + verbose: { + level: LogLevels.Verbose, + }, +}; diff --git a/src/index.shared.ts b/src/index.shared.ts index 97f1dfba..35150404 100644 --- a/src/index.shared.ts +++ b/src/index.shared.ts @@ -1,4 +1,4 @@ export { Consola } from "./consola"; -export { Types } from "./log.types"; -export { LogLevels } from "./log.levels"; +export { LogLevels, LogTypes } from "./constants"; +export type { LogLevel, LogType } from "./constants"; export { isLogObj } from "./utils"; diff --git a/src/index.ts b/src/index.ts index b6d6f518..dbc75364 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ import { isDebug, isTest, isCI } from "std-env"; -import { LogLevels } from "./log.levels"; -import type { ConsolaOptions, LogLevel } from "./types"; +import { LogLevels, LogLevel } from "./constants"; +import type { ConsolaOptions } from "./types"; import { BasicReporter, FancyReporter } from "./reporters"; import { createConsola as _createConsola } from "./consola"; diff --git a/src/log.types.ts b/src/log.types.ts deleted file mode 100644 index 52b11c90..00000000 --- a/src/log.types.ts +++ /dev/null @@ -1,59 +0,0 @@ -import { LogLevels } from "./log.levels"; -import { ConsolaLogObject } from "./types"; - -export const Types: Record = { - // Silent - silent: { - level: -1, - }, - - // Level 0 - fatal: { - level: LogLevels.Fatal, - }, - error: { - level: LogLevels.Error, - }, - - // Level 1 - warn: { - level: LogLevels.Warn, - }, - - // Level 2 - log: { - level: LogLevels.Log, - }, - - // Level 3 - info: { - level: LogLevels.Info, - }, - success: { - level: LogLevels.Success, - }, - fail: { - level: LogLevels.Fail, - }, - ready: { - level: LogLevels.Info, - }, - start: { - level: LogLevels.Info, - }, - - // Level 4 - debug: { - level: LogLevels.Debug, - }, - - // Level 5 - trace: { - level: LogLevels.Trace, - }, - - // Verbose - verbose: { - level: LogLevels.Verbose, - }, -}; diff --git a/src/types.ts b/src/types.ts index 7a667386..2c2093f5 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,25 +1,5 @@ import { InspectOptions } from "node:util"; - -export type LogLevel = number; // Built-in: 0 | 1 | 2 | 3 | 4 | 5; - -export type LogType = - // 0 - | "silent" - | "fatal" - | "error" - // 1 - | "warn" - // 2 - | "log" - // 3 - | "info" - | "success" - | "ready" - | "start" - // Verbose - | "debug" - | "trace" - | "verbose"; +import type { LogLevel, LogType } from "./constants"; export interface ConsolaLogObject { level?: LogLevel | LogType; diff --git a/src/log.levels.ts b/src/utils/level.ts similarity index 60% rename from src/log.levels.ts rename to src/utils/level.ts index 8bfa268b..16fddb61 100644 --- a/src/log.levels.ts +++ b/src/utils/level.ts @@ -1,17 +1,3 @@ -export const LogLevels = { - Fatal: 0, - Error: 0, - Warn: 1, - Log: 2, - Info: 3, - Success: 3, - Fail: 3, - Debug: 4, - Trace: 5, - Silent: Number.NEGATIVE_INFINITY, - Verbose: Number.POSITIVE_INFINITY, -}; - export function normalizeLogLevel( input: any, types: any = {},