Skip to content

Commit fda9f85

Browse files
authored
Merge pull request #1 from boxpositron:feature/add-logger-modes
Feature/add-logger-modes
2 parents 26af8e9 + 190cdb2 commit fda9f85

File tree

5 files changed

+224
-70
lines changed

5 files changed

+224
-70
lines changed

package-lock.json

Lines changed: 29 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "moment-logger",
3-
"version": "1.0.5-4",
3+
"version": "1.0.5-5",
44
"description": "Logging with timestamps",
55
"main": "dist/index.js",
66
"files": [
@@ -15,10 +15,12 @@
1515
"private": false,
1616
"dependencies": {
1717
"chalk": "^4.1.2",
18+
"figlet": "^1.5.2",
1819
"tslib": "^2.3.1"
1920
},
2021
"devDependencies": {
2122
"@types/chai": "^4.3.0",
23+
"@types/figlet": "^1.5.4",
2224
"@types/mocha": "^9.0.0",
2325
"@types/node": "^16.11.19",
2426
"chai": "^4.3.4",
@@ -35,7 +37,7 @@
3537
"bugs": {
3638
"url": "https://github.com/boxpositron/moment-logger/issues"
3739
},
38-
"homepage": "https://github.com/boxpositron/moment-logger#readme",
40+
"homepage": "https://momentlogger.com",
3941
"keywords": [
4042
"terminal",
4143
"cli",

src/core/index.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ export enum Environments {
77
NODE = 'node'
88
}
99

10-
const isProcessAvailable = typeof process !== 'undefined'
10+
// check if process is avaiable
11+
// https://stackoverflow.com/a/34550964/5844218
12+
13+
const isProcessAvailable = typeof window === 'undefined' || typeof process === 'object' && process.title === 'node'
1114

1215
export const DEFAULTS = {
1316
environment: isProcessAvailable ? Environments.NODE : Environments.BROWSER,
@@ -48,8 +51,10 @@ const format = (prefix: string, message: string, suffix: string, noTimestamp: bo
4851
.map(({ value, padding }) => pad(value, padding))
4952
.join(':')
5053

54+
5155
const padded = parsed.padEnd(20 - parsed.length)
52-
const composed = `${prefix} ${message} ${suffix}`.trim()
56+
57+
const composed = `${prefix} ${message} ${suffix}`
5358

5459
if (noTimestamp) {
5560
return composed
@@ -86,15 +91,17 @@ const toConsole = (message: string, mode: ConsoleTypes): void => {
8691

8792
const output = isProcessAvailable ? process : process_
8893

94+
const escaped: string = message.replace(/\\n/g, '\n\t\t')
95+
96+
8997
switch (mode) {
9098
case ConsoleTypes.LOG: {
91-
message.split('\n').forEach(x => output.stdout.write(x + '\n'))
99+
output.stdout.write(escaped)
92100
break;
93101
}
94102

95103
case ConsoleTypes.ERROR: {
96-
const escaped: string = message.replace(/\\n/g, '\n\t\t')
97-
escaped.split('\n').forEach(x => output.stderr.write(x + '\n'))
104+
output.stderr.write(escaped)
98105
break
99106
}
100107

@@ -128,9 +135,16 @@ export const display = (payload: string | DisplayOptions) => {
128135

129136
let header = ''
130137

138+
const isArt = type == LogTypes.ART
131139
const isBlank = type == LogTypes.BLANK
132140
const isError = type == LogTypes.ERROR
133141

142+
143+
if (isArt) {
144+
toConsole(body, ConsoleTypes.LOG)
145+
return body
146+
}
147+
134148
if (!isBlank && !noType) {
135149
const castedType: string = type.toString()
136150

@@ -153,6 +167,7 @@ export const display = (payload: string | DisplayOptions) => {
153167
export const clear = (): void => {
154168

155169
if (DEFAULTS.isTTY) {
170+
// this is available in all environments
156171
console.clear()
157172
}
158173
}

src/logger/index.ts

Lines changed: 112 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
import Events, { EventEmitter } from 'events'
2+
23
import chalk from 'chalk';
4+
import figlet from 'figlet';
35

46
import { collapse, throttle } from './utils'
57
import { Environments, DisplayOptions, display, clear, DEFAULTS } from '../core'
68

9+
export interface FigletConfiguration extends figlet.Options {
10+
delay?: number
11+
}
12+
13+
14+
const defaultFigletConfiguration: FigletConfiguration = {
15+
horizontalLayout: 'default',
16+
verticalLayout: 'fitted',
17+
delay: 0
18+
};
19+
720
export interface LoggerOptions {
821
prefix?: string;
922
suffix?: string;
@@ -12,6 +25,7 @@ export interface LoggerOptions {
1225
pluginPassthrough?: boolean;
1326
pluginThrottle?: number;
1427
showErrorStack?: boolean;
28+
figlet?: FigletConfiguration;
1529
}
1630

1731

@@ -20,10 +34,12 @@ export enum LogTypes {
2034
INFO = 'info',
2135
WARN = 'warn',
2236
ERROR = 'error',
23-
BLANK = 'blank'
37+
BLANK = 'blank',
38+
ART = 'art'
2439
}
2540

2641
type LoggerFunction = (...args: any[]) => string | void;
42+
type PromisedLoggerFunction = (...args: any[]) => Promise<string | void>;
2743

2844

2945

@@ -34,7 +50,8 @@ export interface LoggerInstance extends EventEmitter {
3450
error: LoggerFunction;
3551
blank: LoggerFunction;
3652
clear: LoggerFunction;
37-
pause: LoggerFunction;
53+
pause: PromisedLoggerFunction;
54+
art: PromisedLoggerFunction;
3855
}
3956

4057

@@ -55,13 +72,14 @@ export class Logger extends Events implements LoggerInstance {
5572
*/
5673

5774

58-
prefix: string;
59-
suffix: string;
60-
noType: boolean;
61-
noTimestamp: boolean;
62-
showErrorStack: boolean;
63-
pluginPassthrough: boolean;
64-
pluginThrottle: number;
75+
private prefix: string;
76+
private suffix: string;
77+
private noType: boolean;
78+
private noTimestamp: boolean;
79+
private showErrorStack: boolean;
80+
private pluginPassthrough: boolean;
81+
private pluginThrottle: number;
82+
private figlet: FigletConfiguration;
6583

6684

6785
constructor(config: LoggerOptions = {}) {
@@ -78,6 +96,7 @@ export class Logger extends Events implements LoggerInstance {
7896
this.showErrorStack = config.showErrorStack || false
7997
this.pluginPassthrough = config.pluginPassthrough || false
8098
this.pluginThrottle = config.pluginThrottle || 0
99+
this.figlet = config.figlet || defaultFigletConfiguration
81100
}
82101

83102
get options(): LoggerOptions {
@@ -293,10 +312,12 @@ export class Logger extends Events implements LoggerInstance {
293312
* Wait for input
294313
* @returns {String}
295314
*/
296-
pause = (): void => {
315+
pause = (): Promise<string> => new Promise((resolve, reject) => {
316+
317+
const startTimestamp = Date.now()
297318

298319
if (DEFAULTS.environment === Environments.BROWSER) {
299-
throw new Error('Pause is not supported in browser environment')
320+
return reject(new Error('Pause is not supported in browser environment'))
300321
}
301322

302323
if (DEFAULTS.environment === Environments.NODE) {
@@ -306,9 +327,87 @@ export class Logger extends Events implements LoggerInstance {
306327
}
307328

308329
process.stdin.resume()
309-
process.stdin.on('data', process.exit.bind(process, 0))
330+
process.stdin.on('data', () => {
331+
332+
const endTimestamp = Date.now()
333+
334+
const diff = endTimestamp - startTimestamp
335+
process.stdout.clearLine(0);
336+
process.stdout.cursorTo(0);
337+
resolve(diff.toLocaleString())
338+
})
310339

311340
}
312341

313-
}
342+
})
343+
344+
art = async (...args: any[]): Promise<string> => new Promise((resolve) => {
345+
const startTimestamp = Date.now()
346+
347+
const message = collapse(args, this.collapseOptions)
348+
349+
const feedback = (m: any): void => {
350+
this.emit('logger-art', m)
351+
}
352+
353+
354+
let feedback_ = feedback
355+
356+
if (this.pluginThrottle) {
357+
feedback_ = throttle(feedback, this.pluginThrottle)
358+
}
359+
360+
if (this.pluginPassthrough) {
361+
feedback_(args)
362+
} else {
363+
feedback_(message)
364+
}
365+
366+
367+
368+
const options: figlet.Options = {
369+
...this.figlet
370+
}
371+
372+
373+
figlet.text(message, options, (err: Error | null, data: string | undefined) => {
374+
if (err) {
375+
this.log(message)
376+
const endTimestamp = Date.now()
377+
const diff = endTimestamp - startTimestamp
378+
resolve(diff.toLocaleString())
379+
return
380+
}
381+
382+
383+
const payload: DisplayOptions = {
384+
noTimestamp: true,
385+
noType: true,
386+
type: LogTypes.ART,
387+
start: 8,
388+
end: 10,
389+
modifier: (...t) => chalk.white.bgGray.dim(t),
390+
message: `\n${data}\n`
391+
}
392+
393+
394+
display(payload)
395+
396+
397+
if (this.figlet.delay) {
398+
setTimeout(() => {
399+
const endTimestamp = Date.now()
400+
const diff = endTimestamp - startTimestamp
401+
resolve(diff.toLocaleString())
402+
403+
}, this.figlet.delay)
404+
} else {
405+
const endTimestamp = Date.now()
406+
const diff = endTimestamp - startTimestamp
407+
408+
resolve(diff.toLocaleString())
409+
}
410+
})
411+
412+
})
314413
}

0 commit comments

Comments
 (0)