Skip to content

Commit

Permalink
refactor: improve types and exports
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Apr 10, 2023
1 parent b92d23b commit b380d21
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 129 deletions.
63 changes: 33 additions & 30 deletions src/consola.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
import { LogTypes, LogType } from "./constants";
import { LogTypes, LogType, LogLevel } from "./constants";
import { isLogObj } from "./utils/index";
import type {
ConsolaOptions,
ConsolaReporter,
ConsolaLogObject,
ConsolaReporterLogObject,
} from "./types";
import type { ConsolaReporter, InputLogObject, LogObject } from "./types";
import type { PromptOptions } from "./prompt";

let paused = false;
const queue: any[] = [];

export interface ConsolaOptions {
reporters: ConsolaReporter[];
types: Record<LogType, InputLogObject>;
level: LogLevel;
defaults: InputLogObject;
throttle: number;
throttleMin: number;
stdout?: NodeJS.WritableStream;
stderr?: NodeJS.WritableStream;
mockFn?: (type: LogType, defaults: InputLogObject) => (...args: any) => void;
prompt?: typeof import("./prompt").prompt | undefined;
}

export class Consola {
options: ConsolaOptions;

_lastLog: {
serialized?: string;
object?: ConsolaReporterLogObject;
object?: LogObject;
count?: number;
time?: Date;
timeout?: ReturnType<typeof setTimeout>;
Expand All @@ -40,7 +48,7 @@ export class Consola {

// Create logger functions for current instance
for (const type in types) {
const defaults: ConsolaLogObject = {
const defaults: InputLogObject = {
type: type as LogType,
...types[type as LogType],
...this.options.defaults,
Expand Down Expand Up @@ -94,7 +102,7 @@ export class Consola {
}) as ConsolaInstance;
}

withDefaults(defaults: ConsolaLogObject): ConsolaInstance {
withDefaults(defaults: InputLogObject): ConsolaInstance {
return this.create({
...this.options,
defaults: {
Expand Down Expand Up @@ -233,7 +241,7 @@ export class Consola {
}
}

_wrapLogFn(defaults: ConsolaLogObject, isRaw?: boolean) {
_wrapLogFn(defaults: InputLogObject, isRaw?: boolean) {
return (...args: any[]) => {
if (paused) {
queue.push([this, defaults, args, isRaw]);
Expand All @@ -243,16 +251,17 @@ export class Consola {
};
}

_logFn(defaults: ConsolaLogObject, args: any[], isRaw?: boolean) {
_logFn(defaults: InputLogObject, args: any[], isRaw?: boolean) {
if (((defaults.level as number) || 0) > this.level) {
return false;
}

// Construct a new log object
const logObj: ConsolaReporterLogObject | ConsolaLogObject = {
const logObj: Partial<LogObject> = {
date: new Date(),
args: [],
...defaults,
level: _normalizeLogLevel(defaults.level, this.options.types),
};

// Consume arguments
Expand All @@ -279,7 +288,7 @@ export class Consola {

// Normalize type and tag to lowercase
logObj.type = (
typeof logObj.type === "string" ? logObj.type.toLowerCase() : ""
typeof logObj.type === "string" ? logObj.type.toLowerCase() : "log"
) as LogType;
logObj.tag = typeof logObj.tag === "string" ? logObj.tag.toLowerCase() : "";

Expand All @@ -301,8 +310,8 @@ export class Consola {

// Log
if (newLog) {
this._lastLog.object = logObj as ConsolaReporterLogObject;
this._log(logObj as ConsolaReporterLogObject);
this._lastLog.object = logObj as LogObject;
this._log(logObj as LogObject);
}
};

Expand Down Expand Up @@ -341,7 +350,7 @@ export class Consola {
resolveLog(true);
}

_log(logObj: ConsolaReporterLogObject) {
_log(logObj: LogObject) {
for (const reporter of this.options.reporters) {
reporter.log(logObj, {
stdout: this.stdout,
Expand All @@ -351,7 +360,11 @@ export class Consola {
}
}

function _normalizeLogLevel(input: any, types: any = {}, defaultLevel = 3) {
function _normalizeLogLevel(
input: LogLevel | LogType | undefined,
types: any = {},
defaultLevel = 3
) {
if (input === undefined) {
return defaultLevel;
}
Expand All @@ -364,18 +377,8 @@ function _normalizeLogLevel(input: any, types: any = {}, defaultLevel = 3) {
return defaultLevel;
}

export interface ConsolaInstance extends Consola {
fatal(message: ConsolaLogObject | any, ...args: any[]): void;
error(message: ConsolaLogObject | any, ...args: any[]): void;
warn(message: ConsolaLogObject | any, ...args: any[]): void;
log(message: ConsolaLogObject | any, ...args: any[]): void;
info(message: ConsolaLogObject | any, ...args: any[]): void;
start(message: ConsolaLogObject | any, ...args: any[]): void;
success(message: ConsolaLogObject | any, ...args: any[]): void;
ready(message: ConsolaLogObject | any, ...args: any[]): void;
debug(message: ConsolaLogObject | any, ...args: any[]): void;
trace(message: ConsolaLogObject | any, ...args: any[]): void;
}
export type ConsolaInstance = Consola &
Record<LogType, (message: InputLogObject | any, ...args: any[]) => void>;

// Legacy support
// @ts-expect-error
Expand Down
32 changes: 15 additions & 17 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConsolaLogObject } from "./types";
import { LogObject } from "./types";

// eslint-disable-next-line @typescript-eslint/ban-types
export type LogLevel = 0 | 1 | 2 | 3 | 4 | 5 | (number & {});
Expand Down Expand Up @@ -37,63 +37,61 @@ export type LogType =
// Verbose
| "debug"
| "trace"
| "verbose"
// eslint-disable-next-line @typescript-eslint/ban-types
| (string & {});
| "verbose";

export const LogTypes: Record<LogType, ConsolaLogObject> = {
export const LogTypes: Record<LogType, Partial<LogObject>> = {
// Silent
silent: {
level: -1,
},

// Level 0
fatal: {
level: LogLevels.Fatal,
level: LogLevels.fatal,
},
error: {
level: LogLevels.Error,
level: LogLevels.error,
},

// Level 1
warn: {
level: LogLevels.Warn,
level: LogLevels.warn,
},

// Level 2
log: {
level: LogLevels.Log,
level: LogLevels.log,
},

// Level 3
info: {
level: LogLevels.Info,
level: LogLevels.info,
},
success: {
level: LogLevels.Success,
level: LogLevels.success,
},
fail: {
level: LogLevels.Fail,
level: LogLevels.fail,
},
ready: {
level: LogLevels.Info,
level: LogLevels.info,
},
start: {
level: LogLevels.Info,
level: LogLevels.info,
},

// Level 4
debug: {
level: LogLevels.Debug,
level: LogLevels.debug,
},

// Level 5
trace: {
level: LogLevels.Trace,
level: LogLevels.trace,
},

// Verbose
verbose: {
level: LogLevels.Verbose,
level: LogLevels.verbose,
},
};
2 changes: 1 addition & 1 deletion src/index.browser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import BrowserReporter from "./reporters/browser";
import { createConsola as _createConsola } from "./consola";
import type { ConsolaOptions } from "./types";
import type { ConsolaOptions } from "./consola";

export * from "./index.shared";

Expand Down
8 changes: 4 additions & 4 deletions src/index.node.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isDebug, isTest, isCI } from "std-env";
import { LogLevels, LogLevel } from "./constants";
import type { ConsolaOptions } from "./types";
import type { ConsolaOptions } from "./consola";
import { BasicReporter, FancyReporter } from "./reporters";
import { createConsola as _createConsola } from "./consola";

Expand Down Expand Up @@ -29,12 +29,12 @@ export function createConsola(options: Partial<ConsolaOptions> = {}) {

function _getDefaultLogLevel() {
if (isDebug) {
return LogLevels.Debug;
return LogLevels.debug;
}
if (isTest) {
return LogLevels.Warn;
return LogLevels.warn;
}
return LogLevels.Info;
return LogLevels.info;
}

export const consola = createConsola();
Expand Down
4 changes: 3 additions & 1 deletion src/index.shared.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export { Consola } from "./consola";
export { LogLevels, LogTypes } from "./constants";

export type { ConsolaInstance, ConsolaOptions } from "./consola";
export type { LogLevel, LogType } from "./constants";
export { isLogObj } from "./utils";
export type * from "./types";
6 changes: 3 additions & 3 deletions src/reporters/basic.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import util from "node:util";
import { ConsolaReporterLogObject } from "../types";
import { LogObject } from "../types";
import { parseStack } from "../utils/error";
import { writeStream } from "../utils/stream";

Expand Down Expand Up @@ -47,7 +47,7 @@ export default class BasicReporter {
return arr.filter(Boolean).join(" ");
}

formatLogObj(logObj: ConsolaReporterLogObject, _opts: any) {
formatLogObj(logObj: LogObject, _opts: any) {
const message = this.formatArgs(logObj.args);

return this.filterAndJoin([
Expand All @@ -57,7 +57,7 @@ export default class BasicReporter {
]);
}

log(logObj: ConsolaReporterLogObject, { stdout, stderr }: any = {}) {
log(logObj: LogObject, { stdout, stderr }: any = {}) {
const line = this.formatLogObj(logObj, {
width: stdout.columns || 0,
});
Expand Down
4 changes: 2 additions & 2 deletions src/reporters/browser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConsolaReporterLogObject } from "../types";
import { LogObject } from "../types";

export default class BrowserReporter {
options: any;
Expand Down Expand Up @@ -30,7 +30,7 @@ export default class BrowserReporter {
return (console as any).__log || console.log;
}

log(logObj: ConsolaReporterLogObject) {
log(logObj: LogObject) {
const consoleLogFn = this._getLogFn(logObj.level);

// Type
Expand Down
6 changes: 3 additions & 3 deletions src/reporters/fancy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { mainSymbols } from "figures";
import * as colors from "colorette";
import { parseStack } from "../utils/error";
import { TYPE_COLOR_MAP, LEVEL_COLOR_MAP } from "../utils/fancy";
import { ConsolaReporterLogObject } from "../types";
import { LogObject } from "../types";
import BasicReporter from "./basic";

const DEFAULTS = {
Expand Down Expand Up @@ -43,7 +43,7 @@ export default class FancyReporter extends BasicReporter {
);
}

formatType(logObj: ConsolaReporterLogObject, isBadge: boolean) {
formatType(logObj: LogObject, isBadge: boolean) {
const typeColor =
(TYPE_COLOR_MAP as any)[logObj.type] ||
(LEVEL_COLOR_MAP as any)[logObj.level] ||
Expand All @@ -62,7 +62,7 @@ export default class FancyReporter extends BasicReporter {
return _type ? getColor(typeColor)(_type) : "";
}

formatLogObj(logObj: ConsolaReporterLogObject, opts: { width: number }) {
formatLogObj(logObj: LogObject, opts: { width: number }) {
const [message, ...additional] = this.formatArgs(logObj.args).split("\n");

const isBadge =
Expand Down
Loading

0 comments on commit b380d21

Please sign in to comment.