-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
124 lines (103 loc) · 3.42 KB
/
index.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const strftime = require('strftime');
const chalk = require('chalk');
const format = require('util').format;
const lU = require('log-update');
const errsome = require('errsome');
const ansiRegex = require('ansi-regex')();
const _levels = {
trace: chalk.white.bold.bgBlack('<TRACE>'),
debug: chalk.white.bold.bgGreen('<DEBUG>'),
info: chalk.white.bold.bgBlue('<INFO>'),
warn: chalk.white.bold.bgYellow('<WARN>'),
error: chalk.white.bold.bgRed('<ERROR>'),
};
let _i = [];
let _text = '';
const _countStr = () => _text
.split('%s')
.map((v, i) => v + (i === _i.length ? '' : chalk.white(_i[i])))
.join('');
lU.show = () => lU(_countStr());
module.exports = function(t, df = '%T'){
let level = null;
let tag = t;
let dateFormat = df;
const _log = l => (...args) => {
const a = [];
const ll = _levels[l] || _levels[level];
if(dateFormat) a.push(chalk.white(`[${strftime(dateFormat)}]`));
if(ll) a.push(ll);
if(tag) a.push(chalk.cyan(`(${tag})`));
if(args.length > 0){
if(args.length === 1 && l === 'error' && args[0] instanceof Error){
a.push(chalk.gray(format('\n', errsome(args[0]))));
} else {
a.push(chalk.gray(format(...args)));
}
}
if(process.stdout.isTTY){
if(_text) lU.clear();
console.log(a.join(' '));
if(_text) lU.show();
} else {
console.log(a.map(s => s.replace(ansiRegex, '')).join(' '));
}
};
const log = _log();
log.t = log.trace = _log('trace');
log.d = log.debug = _log('debug');
log.i = log.info = _log('info');
log.w = log.warn = _log('warn');
log.e = log.error = _log('error');
log.level = l => {
level = _levels[l] ? l : null;
};
log.dateFormat = dF => {
dateFormat = typeof dF === 'string' || !dF ? dF : dateFormat;
};
log.tag = t => {
tag = t && t.id && t.exports && t.filename && t.paths ?
t.filename.split(/[/\\]/).slice(-2).join('/').split('.')[0] :
typeof t === 'string' ? t : undefined;
};
log.tag(tag);
log.start = (text = '%s', ...args) => {
if(!process.stdout.isTTY || !text || typeof text !== 'string') return;
_text = text;
const tl = _text.split('%s').length - 1;
const zeros = Array(tl).fill(0);
const steps = args.length > 0 ? args.map(v => +v || 0) : zeros;
_i = Object.assign(zeros, steps.slice(0, tl));
lU.show();
};
log.step = (...args) => {
if(!process.stdout.isTTY || !_text) return;
const steps = args.length > 0 ? args : [1];
_i = _i.map((v, i) => v + (+steps[i] || 0));
lU.show();
};
log.inc = n => {
if(!process.stdout.isTTY || !_text) return;
_i = _i.map((v, i) => v + (i === n - 1));
lU.show();
};
log.stop = () => {
if(!process.stdout.isTTY || !_text) return;
_text = '';
lU.clear();
};
log.finish = (text, ...args) => {
if(!process.stdout.isTTY || !_text) return;
if(text){
_text = text;
const tl = _text.split('%s').length - 1;
_i = Object.assign(Array(tl).fill(0), args.slice(0, tl).map(v => +v || 0));
}
lU.show();
_text = '';
lU.done();
};
log.counters = () => [..._i];
log.text = () => _text;
return log;
};