-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (69 loc) · 2.01 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
'use strict';
const chalk = require('chalk');
const ora = require('ora');
const LOG_COLOR = {
error: 'red',
info: 'cyan',
warn: 'yellow',
success: 'green'
};
const LOG_PREFIX = {
info: 'ℹ',
success: '✔',
warn: '⚠',
error: '✖'
};
class Logger {
constructor() {
this.levels = ['error', 'info', 'success', 'warn'];
this.loggerColor = LOG_COLOR;
this.logPrefix = LOG_PREFIX;
this.spinner = null;
this.initLog(this.levels);
}
initLog(levels) {
levels.forEach((level) => {
this[level] = (desc, prefixType) => { this.log(desc, prefixType ? level : false, level);};
});
}
log(desc, prefixType, level) {
if (prefixType) {
desc = `${chalk[this.loggerColor[prefixType] || 'info'](LOG_PREFIX[prefixType])} ${desc}`;
}
if (!level) {
console.log(desc); // eslint-disable-line no-console
}
else if (level === 'error') {
console.error(chalk[this.loggerColor[level]](desc)); // eslint-disable-line no-console
}
else {
console.log(chalk[this.loggerColor[level]](desc)); // eslint-disable-line no-console
}
}
bold(desc, level = 'info') {
if (level === 'error') {
console.error(chalk[this.loggerColor[level]].bold(desc)); // eslint-disable-line no-console
}
else {
console.log(chalk[this.loggerColor[level]].bold(desc)); // eslint-disable-line no-console
}
}
setColor(color) {
if (!color) this.error('invalid input');
this.loggerColor = Object.assign(LOG_COLOR, color);
}
spin(task, text) {
// wrap the task inside promise.
const promiseTask = task();
ora.promise(promiseTask, text);
return promiseTask;
}
startSpin(text, color) {
this.spinner = ora({ text, color}).start();
return this.spinner;
}
stopSpin() {
this.spinner.stop();
}
}
module.exports = Logger;