From bea311ae506e2e419f9dce3c2a3c15f79ceae6fe Mon Sep 17 00:00:00 2001 From: Florian Bouillon Date: Wed, 1 Jul 2020 10:48:43 +0200 Subject: [PATCH] Added base support for object --- LICENSE | 21 +++++++++++++++ packages/logger/src/Logger.ts | 44 ++++++++++++++++++++++++++----- packages/logger/webpack.config.js | 2 +- 3 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9ee0e21 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Florian Bouillon + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/packages/logger/src/Logger.ts b/packages/logger/src/Logger.ts index 9ff9392..8f2a09e 100644 --- a/packages/logger/src/Logger.ts +++ b/packages/logger/src/Logger.ts @@ -1,23 +1,43 @@ -import chalk from 'chalk' +import { white, blue, yellow, green } from 'chalk' +/** + * Logger Class + */ export default class Logger { + + /** + * If it is set to true all message won't be shown until it is set to false + */ public static isBlocked = false - private static queue: Array = [] + private static queue: Array> = [] private static prefixLen = 0 + /** + * Log a message into the console + * @param prefix the prefix used + * @param message the message to log + */ public static log(prefix: string, ...message: Array) { - this.queue.push(this.formatMessage(prefix, ...message)) while (this.queue.length > 0 && !this.isBlocked) { - console.log(this.queue.shift()) + const item = this.queue.shift() + if (!item) { + continue + } + console.log(...item) } } - public static urgent(prefix: string, ...message: Array) { + /** + * Log a message into the console (passthrough the `Logger.isBlocked` boolean) + * @param prefix The prefix used + * @param message the message to log + */ + public static urgent(prefix: string, ...message: Array) { console.log(this.formatMessage(prefix, ...message)) } - private static formatMessage(prefix: string, ...message: Array): string { + private static formatMessage(prefix: string, ...message: Array): Array { if (this.prefixLen < prefix.length) { this.prefixLen = prefix.length } @@ -27,7 +47,17 @@ export default class Logger { els[0] = this.buildSpace(diff / 2 - (diff % 2 !== 0 ? 1 : 0)) els[1] = this.buildSpace(diff / 2) } - return `${chalk.white('[ ')}${els[0]}${chalk.blue(prefix)}${els[1]}${chalk.white(' ]')}: ${message.map((el) => typeof el === 'number' ? chalk.yellow(el.toString()) : chalk.white(el)).join(' ')}` + const res: Array = [ + `${white('[ ')}${els[0]}${blue(prefix)}${els[1]}${white(' ]')}:` // prefix + ].concat( + message.map((el) => { + if (typeof el === 'object') { + return el + } + return typeof el !== 'string' ? yellow(el.toString()) : green(el) + }) + ) + return res } private static buildSpace(count: number): string { diff --git a/packages/logger/webpack.config.js b/packages/logger/webpack.config.js index 3c676c1..6273b45 100644 --- a/packages/logger/webpack.config.js +++ b/packages/logger/webpack.config.js @@ -14,4 +14,4 @@ module.exports = { } ] } - }; +}