1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | import type { Logger } from './types';
|
8 |
|
9 | const chalk = require('chalk');
|
10 | const stripAnsi = require('strip-ansi');
|
11 |
|
12 | let lastChar;
|
13 |
|
14 | const last = arr => arr[arr.length - 1];
|
15 |
|
16 | const section = (...args) => {
|
17 | if (lastChar !== '\n') {
|
18 | console.log();
|
19 | }
|
20 | log(...args);
|
21 | };
|
22 |
|
23 | const log = (...args) => {
|
24 | lastChar = last(stripAnsi(last(args)));
|
25 | console.log(...args);
|
26 | };
|
27 |
|
28 | function reset() {
|
29 | const logger = {
|
30 | info: (...args: any[]) => section(chalk.black.bgCyan(' INFO '), ...args),
|
31 | warn: (...args: any[]) => section(chalk.black.bgYellow(' WARN '), ...args),
|
32 | error: (...args: any[]) => section(chalk.black.bgRed(' ERROR '), ...args),
|
33 | done: (...args: any[]) => section(chalk.black.bgGreen(' DONE '), ...args),
|
34 | debug: (prefix: string, ...args: any[]) =>
|
35 | log(
|
36 | chalk.cyan(prefix.toUpperCase()),
|
37 | ...args.map(str => chalk.grey(str))
|
38 | ),
|
39 | reset,
|
40 | };
|
41 |
|
42 | Object.defineProperty(logger, 'reset', {
|
43 | ...Object.getOwnPropertyDescriptor(logger, 'reset').value,
|
44 | writable: false,
|
45 | });
|
46 |
|
47 | return logger;
|
48 | }
|
49 |
|
50 | const logger: Logger = reset();
|
51 |
|
52 | module.exports = logger;
|