-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger.js
76 lines (60 loc) · 1.77 KB
/
logger.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
const tty = require('node:tty');
const isDisabled = 'NO_COLOR' in process.env;
const isForced = 'FORCE_COLOR' in process.env;
const isWindows = process.platform === 'win32';
const isCompatibleTerminal = tty && tty.isatty(1) && process.env.TERM && process.env.TERM !== 'dumb';
const isCI = 'CI' in process.env && ('GITHUB_ACTIONS' in process.env || 'GITLAB_CI' in process.env || 'CIRCLECI' in process.env);
const isColorSupported = !isDisabled && (isForced || isWindows || isCompatibleTerminal || isCI);
const colorCodes = {
greenStart: '\x1b[32m',
greenEnd: '\x1b[39m',
redStart: '\x1b[31m',
redEnd: '\x1b[39m',
};
function colorize(string, start, end) {
return typeof string === 'string' ? `${start}${string}${end}` : string;
}
module.exports = class Logger {
constructor({ isVerbose = false, isColorEnabled = true }) {
this.isVerbose = isVerbose;
this.isColorEnabled = isColorSupported && isColorEnabled;
}
newline() {
console.log();
}
verboseNewline() {
if (!this.isVerbose) return;
console.log();
}
info(...args) {
console.log(...args);
}
verboseInfo(...args) {
if (!this.isVerbose) return;
console.log(...args);
}
noVerboseInfo(...args) {
if (this.isVerbose) return;
console.log(...args);
}
success(...args) {
if (this.isColorEnabled) {
args = args.map(arg => colorize(arg, colorCodes.greenStart, colorCodes.greenEnd));
}
console.log(...args);
}
error(...args) {
if (this.isColorEnabled) {
args = args.map(arg => colorize(arg, colorCodes.redStart, colorCodes.redEnd));
}
console.error(...args);
}
verboseError(...args) {
if (!this.isVerbose) return;
this.error(...args);
}
noVerboseError(...args) {
if (this.isVerbose) return;
this.error(...args);
}
};