Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use better typings of consola once they are fixed #63 #73

Merged
merged 5 commits into from
May 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"simplegit",
"lcov",
"toolbelt",
"Conosla",
"packument"
],
"ignorePaths": [
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,5 @@ typings/
# stryker temp files
.stryker-tmp
/reports
!@types/**
!/tools

23 changes: 0 additions & 23 deletions @types/consola/index.d.ts

This file was deleted.

38 changes: 19 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,55 +87,55 @@
"@types/cross-spawn": "^6.0.1",
"@types/jest": "^25.2.1",
"@types/jest-when": "^2.7.1",
"@types/node": "^13.13.4",
"@types/node": "^13.13.5",
"@types/node-fetch": "^2.5.7",
"@types/pacote": "^11.1.0",
"@types/semver": "^7.1.0",
"@types/shell-quote": "^1.6.1",
"@types/tmp": "^0.1.0",
"@types/tmp": "^0.2.0",
"@types/yargs-parser": "^15.0.0",
"@typescript-eslint/eslint-plugin": "^2.29.0",
"@typescript-eslint/parser": "^2.29.0",
"@typescript-eslint/eslint-plugin": "^2.31.0",
"@typescript-eslint/parser": "^2.31.0",
"all-contributors-cli": "^6.14.2",
"cspell": "^4.0.57",
"cspell": "^4.0.61",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-jest": "^23.8.2",
"eslint-plugin-prefer-arrow": "^1.2.0",
"eslint-plugin-jest": "^23.9.0",
"eslint-plugin-prefer-arrow": "^1.2.1",
"eslint-plugin-prettier": "^3.1.3",
"expect": "^25.4.0",
"expect": "^26.0.1",
"generate-changelog": "^1.8.0",
"husky": "^4.2.5",
"jest": "^25.4.0",
"jest": "^26.0.1",
"jest-mock-extended": "^1.0.8",
"jest-serial-runner": "file:./tools/jest-serial-runner",
"jest-when": "^2.7.1",
"lint-staged": "^10.1.7",
"lint-staged": "^10.2.2",
"mock-spawn": "^0.2.6",
"node-fetch": "^2.6.0",
"npm-check-updates": "^4.1.2",
"prettier": "^2.0.5",
"semver": "^7.3.2",
"ts-jest": "^25.4.0",
"ts-node": "^8.9.1",
"ts-toolbelt": "^6.5.1",
"ts-jest": "^25.5.0",
"ts-node": "^8.10.1",
"ts-toolbelt": "^6.6.2",
"typescript": "^3.8.3"
},
"dependencies": {
"array-flat-polyfill": "~1.0.1",
"axios": "~0.19.2",
"chalk": "~4.0.0",
"consola": "~2.11.3",
"consola": "~2.12.1",
"cross-spawn": "~7.0.2",
"figures": "~3.2.0",
"inversify": "~5.0.1",
"moment": "~2.24.0",
"pacote": "~11.1.4",
"moment": "~2.25.3",
"pacote": "~11.1.9",
"reflect-metadata": "~0.1.13",
"shell-quote": "~1.7.2",
"simple-git": "~1.132.0",
"sqlite3": "~4.1.1",
"tmp": "~0.2.0",
"simple-git": "~2.4.0",
"sqlite3": "~4.2.0",
"tmp": "~0.2.1",
"ts-type-guards": "~0.6.1",
"typeorm": "~0.2.24",
"yaml": "~1.9.2",
Expand Down
14 changes: 14 additions & 0 deletions src/utils/logger/impl/logLevel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { LogLevel as ConsolaLogLevel } from 'consola';
import { LogLevel } from '../interfaces/ILogger';

const logLevelMap = {
[LogLevel.ERROR]: ConsolaLogLevel.Error,
[LogLevel.WARN]: ConsolaLogLevel.Warn,
[LogLevel.LOG]: ConsolaLogLevel.Log,
[LogLevel.INFO]: ConsolaLogLevel.Info,
[LogLevel.DEBUG]: ConsolaLogLevel.Debug,
[LogLevel.TRACE]: ConsolaLogLevel.Trace,
[LogLevel.SILENT]: -ConsolaLogLevel.Silent, // Hack until the issue of fixed
};

export const getConoslaLogLevel = (level: LogLevel): ConsolaLogLevel => logLevelMap[level];
4 changes: 2 additions & 2 deletions src/utils/logger/impl/logReporter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConsolaLogObject, ConsolaReporter, FancyReporter, ReporterOptions } from 'consola';
import { ConsolaLogObject, ConsolaReporter, FancyReporter, FancyReporterOptions } from 'consola';
import chalk = require('chalk');
import * as figures from 'figures';
import type { Chalk, BackgroundColor, ForegroundColor } from 'chalk';
Expand Down Expand Up @@ -35,7 +35,7 @@ const TYPE_ICONS: Record<string, Nullable<string>> = {
export type BGColor = typeof BackgroundColor;
export type Color = typeof ForegroundColor;

export interface LogReporterOptions extends ReporterOptions {
export interface LogReporterOptions extends FancyReporterOptions {
secondaryColor: Color;
bgColor: BGColor;
}
Expand Down
21 changes: 17 additions & 4 deletions src/utils/logger/impl/logger.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { Consola, ConsolaOptions } from 'consola';
import { ILogger, LogLevel } from '../interfaces/ILogger';
import { getConoslaLogLevel } from './logLevel';

export class Logger extends Consola implements ILogger {
constructor(options: ConsolaOptions) {
super(options);
}

public isLevelEnabled(level: LogLevel): boolean {
return this.level >= level;
}

public isTraceEnabled(): boolean {
return this.isLevelEnabled(LogLevel.TRACE);
}
Expand All @@ -25,4 +22,20 @@ export class Logger extends Consola implements ILogger {
public isWarnEnabled(): boolean {
return this.isLevelEnabled(LogLevel.WARN);
}

public isLogEnabled(): boolean {
return this.isLevelEnabled(LogLevel.LOG);
}

public isErrorEnabled(): boolean {
return this.isLevelEnabled(LogLevel.ERROR);
}

public isSilent(): boolean {
return this.level === getConoslaLogLevel(LogLevel.SILENT);
}

private isLevelEnabled(level: LogLevel): boolean {
return this.level >= getConoslaLogLevel(level);
}
}
8 changes: 3 additions & 5 deletions src/utils/logger/impl/loggerFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@ import * as chalk from 'chalk';
import { ILogger, LogLevel } from '../interfaces/ILogger';
import { Logger } from './logger';
import { LogReporter } from './logReporter';
import { getConoslaLogLevel } from './logLevel';

@injectable()
export class LoggerFactory extends ILoggerFactory {
private readonly options: ConsolaOptions;
constructor(settings: ILoggerSettings) {
constructor({ logLevel }: ILoggerSettings) {
super();
const isDebug = settings.debugMode;
const isTrace = settings.traceMode;
const level = isTrace ? LogLevel.TRACE : isDebug ? LogLevel.DEBUG : LogLevel.INFO;
this.options = {
level,
level: getConoslaLogLevel(logLevel ?? LogLevel.INFO),
reporters: [
new LogReporter({
secondaryColor: `grey`,
Expand Down
9 changes: 6 additions & 3 deletions src/utils/logger/interfaces/ILogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export enum LogLevel {
INFO,
DEBUG,
TRACE,
SILENT,
}

export interface ILogger {
Expand All @@ -24,9 +25,11 @@ export interface ILogger {

trace(...args: unknown[]): void;

isErrorEnabled(): boolean;
isWarnEnabled(): boolean;
isLogEnabled(): boolean;
isInfoEnabled(): boolean;
isDebugEnabled(): boolean;
isTraceEnabled(): boolean;
isInfoEnabled(): boolean;
isWarnEnabled(): boolean;
isLevelEnabled(level: LogLevel): boolean;
isSilent(): boolean;
}
5 changes: 3 additions & 2 deletions src/utils/logger/interfaces/ILoggerSettings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { LogLevel } from './ILogger';

export abstract class ILoggerSettings {
public abstract readonly debugMode: boolean;
public abstract readonly traceMode: boolean;
public abstract readonly logLevel: LogLevel | undefined;
}
4 changes: 2 additions & 2 deletions test/common/logger.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { LoggerFactory } from '../../src/utils/logger/impl/loggerFactory';
import { ILoggerFactory, ILoggerSettings } from '../../src/utils/logger';
import { LogLevel } from '../../src/utils/logger/interfaces/ILogger';

export const loggerSettings: ILoggerSettings = {
debugMode: true,
traceMode: true,
logLevel: LogLevel.TRACE,
};
export const loggerFactory: ILoggerFactory = new LoggerFactory(loggerSettings);
17 changes: 17 additions & 0 deletions test/src/utils/logger/impl/logLevel.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { LogLevel } from '../../../../../src/utils/logger/interfaces/ILogger';
import { LogLevel as ConsolaLogLevel } from 'consola';
import { getConoslaLogLevel } from '../../../../../src/utils/logger/impl/logLevel';

describe(`log level`, () => {
describe(`get conosla log level`, () => {
it(`Should have proper mappings`, () => {
expect(getConoslaLogLevel(LogLevel.ERROR)).toEqual(ConsolaLogLevel.Error);
expect(getConoslaLogLevel(LogLevel.WARN)).toEqual(ConsolaLogLevel.Warn);
expect(getConoslaLogLevel(LogLevel.LOG)).toEqual(ConsolaLogLevel.Log);
expect(getConoslaLogLevel(LogLevel.INFO)).toEqual(ConsolaLogLevel.Info);
expect(getConoslaLogLevel(LogLevel.DEBUG)).toEqual(ConsolaLogLevel.Debug);
expect(getConoslaLogLevel(LogLevel.TRACE)).toEqual(ConsolaLogLevel.Trace);
expect(getConoslaLogLevel(LogLevel.SILENT)).toEqual(-ConsolaLogLevel.Silent);
});
});
});