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