-
Notifications
You must be signed in to change notification settings - Fork 11
/
log.js
72 lines (61 loc) · 1.67 KB
/
log.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const chalk = require('chalk');
const leftPad = require('left-pad');
const readline = require('readline');
const { INFO_COLUMN_WIDTH } = require('./constants');
const debugPrimaryColor = chalk.reset.cyan;
const debugSecondaryColor = chalk.reset.cyan.bold;
const errorPrimaryColor = chalk.reset.red;
const warnPrimaryColor = chalk.reset.yellow;
const promptPrimaryColor = chalk.reset.magenta;
const promptSecondaryColor = chalk.reset.magenta.bold;
function log(logger, level, ...args) {
if (logger) logger[level].apply(logger, args);
}
function info(logger, ...args) {
log(logger, 'info', ...args);
}
function debug(logger, ...args) {
let colorizeSecondaryText = msg =>
Array.isArray(msg)
? msg.map(colorizeSecondaryText)
: debugSecondaryColor(msg);
args = args.map(colorizeSecondaryText);
log(
logger,
'debug',
debugPrimaryColor(leftPad('[DEBUG]', INFO_COLUMN_WIDTH)),
...args
);
}
function warn(logger, ...args) {
log(
logger,
'warn',
warnPrimaryColor(leftPad('[WARN]', INFO_COLUMN_WIDTH)),
...args
);
}
function error(logger, ...args) {
log(
logger,
'error',
errorPrimaryColor(leftPad('[ERROR]', INFO_COLUMN_WIDTH)),
...args
);
}
function prompt(text) {
const inputInterface = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise((resolve, reject) => {
const question = `${promptPrimaryColor(
leftPad('[PROMPT]', INFO_COLUMN_WIDTH)
)} ${promptSecondaryColor(text)}`;
inputInterface.question(question, answer => {
inputInterface.close();
resolve(answer);
});
});
}
module.exports = { info, debug, error, warn, prompt };