1 | const { EOL } = require('os');
|
2 | const chalk = require('chalk');
|
3 | const { isObject, last, filter, isString, lowerCase, upperFirst } = require('lodash');
|
4 |
|
5 | class Logger {
|
6 | constructor({ isCI = true, isVerbose = false, verbosityLevel = 0, isDryRun = false } = {}) {
|
7 | this.isCI = isCI;
|
8 | this.isVerbose = isVerbose;
|
9 | this.verbosityLevel = verbosityLevel;
|
10 | this.isDryRun = isDryRun;
|
11 | }
|
12 | log(...args) {
|
13 | console.log(...args);
|
14 | }
|
15 | error(...args) {
|
16 | console.error(chalk.red('ERROR'), ...args);
|
17 | }
|
18 | info(...args) {
|
19 | this.log(chalk.grey(...args));
|
20 | }
|
21 | warn(...args) {
|
22 | this.log(chalk.yellow('WARNING'), ...args);
|
23 | }
|
24 | verbose(...args) {
|
25 | const { isExternal } = isObject(last(args)) ? last(args) : {};
|
26 | if (this.verbosityLevel === 2 || (this.isVerbose && (isExternal || this.isDryRun))) {
|
27 | this.log(...filter(args, isString));
|
28 | }
|
29 | }
|
30 | exec(...args) {
|
31 | const { isDryRun: isExecutedInDryRun, isExternal } = isObject(last(args)) ? last(args) : {};
|
32 | if (this.verbosityLevel === 2 || this.isDryRun || (this.isVerbose && isExternal)) {
|
33 | const prefix = isExecutedInDryRun == null ? '$' : '!';
|
34 | this.log(prefix, ...filter(args, isString));
|
35 | }
|
36 | }
|
37 | obtrusive(...args) {
|
38 | if (!this.isCI) this.log();
|
39 | this.log(...args);
|
40 | if (!this.isCI) this.log();
|
41 | }
|
42 | preview({ title, text }) {
|
43 | if (text) {
|
44 | const header = chalk.bold(upperFirst(title));
|
45 | const body = text.replace(new RegExp(EOL + EOL, 'g'), EOL);
|
46 | this.obtrusive(`${header}:${EOL}${body}`);
|
47 | } else {
|
48 | this.obtrusive(`Empty ${lowerCase(title)}`);
|
49 | }
|
50 | }
|
51 | }
|
52 |
|
53 | module.exports = Logger;
|