Skip to content

Commit

Permalink
refactor(types): merge LogTypeLiteral and logtype types to LogType
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Apr 10, 2023
1 parent 6833945 commit da1bc73
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 69 deletions.
10 changes: 5 additions & 5 deletions src/consola.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
ConsolaOptions,
ConsolaReporter,
ConsolaLogObject,
logType,
LogType,
ConsolaReporterLogObject,
} from "./types";
import type { PromptOptions } from "./prompt";
Expand Down Expand Up @@ -43,8 +43,8 @@ export class Consola {
// Create logger functions for current instance
for (const type in types) {
const defaults: ConsolaLogObject = {
type: type as logType,
...types[type as logType],
type: type as LogType,
...types[type as LogType],
...this.options.defaults,
};
(this as any)[type] = this._wrapLogFn(defaults);
Expand Down Expand Up @@ -229,7 +229,7 @@ export class Consola {

for (const type in this.options.types) {
(this as any)[type] =
_mockFn(type as logType, (this as any)._types[type]) ||
_mockFn(type as LogType, (this as any)._types[type]) ||
(this as any)[type];
(this as any)[type].raw = (this as any)[type];
}
Expand Down Expand Up @@ -282,7 +282,7 @@ export class Consola {
// Normalize type and tag to lowercase
logObj.type = (
typeof logObj.type === "string" ? logObj.type.toLowerCase() : ""
) as logType;
) as LogType;
logObj.tag = typeof logObj.tag === "string" ? logObj.tag.toLowerCase() : "";

// Resolve log
Expand Down
70 changes: 6 additions & 64 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
import { InspectOptions } from "node:util";

export type LogLevelLiteral =
| "fatal"
| "error"
| "warn"
| "log"
| "info"
| "success"
| "fail"
| "debug"
| "trace"
| "silent"
| "verbose";

export type LogLevel = number; // Built-in: 0 | 1 | 2 | 3 | 4 | 5;

export type logType =
export type LogType =
// 0
| "silent"
| "fatal"
Expand All @@ -35,9 +22,9 @@ export type logType =
| "verbose";

export interface ConsolaLogObject {
level?: LogLevel | LogLevelLiteral;
level?: LogLevel | LogType;
tag?: string;
type?: logType;
type?: LogType;
message?: string;
additional?: string | string[];
args?: any[];
Expand All @@ -46,7 +33,7 @@ export interface ConsolaLogObject {

export interface ConsolaReporterLogObject {
level: LogLevel;
type: logType;
type: LogType;
tag: string;
args: any[];
date: Date;
Expand All @@ -55,7 +42,7 @@ export interface ConsolaReporterLogObject {
export type ConsolaMock = (...args: any) => void;

export type ConsolaMockFn = (
type: logType,
type: LogType,
defaults: ConsolaLogObject
) => ConsolaMock;

Expand All @@ -70,7 +57,7 @@ export interface ConsolaReporter {

export interface ConsolaOptions {
reporters: ConsolaReporter[];
types: Record<logType, ConsolaLogObject>;
types: Record<LogType, ConsolaLogObject>;
level: LogLevel;
defaults: ConsolaLogObject;
throttle: number;
Expand All @@ -84,50 +71,5 @@ export interface ConsolaOptions {
export interface BasicReporterOptions {
dateFormat?: string;
formatOptions?: InspectOptions;
}

export declare class BasicReporter implements ConsolaReporter {
protected options: BasicReporterOptions;

constructor(options?: BasicReporterOptions);

public log(logObj: ConsolaReporterLogObject, args: ConsolaReporterArgs): void;

protected formatStack(stack: string): string;
protected formatArgs(args: any[]): string;
protected formatDate(date: Date): string;
protected filterAndJoin(arr: Array<string | undefined>): string;
protected formatLogObj(logObj: ConsolaReporterLogObject): string;
}

export interface FancyReporterOptions extends BasicReporterOptions {
secondaryColor?: string;
}

export declare class FancyReporter extends BasicReporter {
constructor(options?: FancyReporterOptions);

protected formatType(logObj: ConsolaReporterLogObject): void;
}

export type BrowserReporterOptions = Record<string, any>;

export declare class BrowserReporter implements ConsolaReporter {
public log(logObj: ConsolaReporterLogObject, args: ConsolaReporterArgs): void;
}

export type JSONReporterOptions = {
stream?: NodeJS.WritableStream;
};

export declare class JSONReporter implements ConsolaReporter {
constructor(options?: JSONReporterOptions);
public log(logObj: ConsolaReporterLogObject, args: ConsolaReporterArgs): void;
}

export type Winston = any;

export declare class WinstonReporter implements ConsolaReporter {
constructor(logger?: Winston);
public log(logObj: ConsolaReporterLogObject, args: ConsolaReporterArgs): void;
}

0 comments on commit da1bc73

Please sign in to comment.