1 | "use strict";
|
2 |
|
3 | const clc = require("cli-color");
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | const Logger = function(enableColor, stdout, stderr) {
|
12 | this.color_ = !!enableColor;
|
13 | this.messages_ = [];
|
14 | this.stdout = stdout;
|
15 | this.stderr = stderr;
|
16 | };
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | Logger.prototype.raw = function(msg, opt_color) {
|
23 | this.messages_.push(this.color_ && opt_color ? opt_color(msg) : msg);
|
24 | };
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | Logger.prototype.info = function(msg) {
|
30 | this.raw(msg);
|
31 | };
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | Logger.prototype.warn = function(msg) {
|
37 | this.raw(msg, clc.yellow);
|
38 | };
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | Logger.prototype.error = function(msg) {
|
44 | this.raw(msg, clc.red);
|
45 | };
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | Logger.prototype.success = function(msg) {
|
51 | this.raw(msg, clc.green);
|
52 | };
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | Logger.prototype.items = function(items) {
|
59 | if (items.length === 0) {
|
60 | items = ["(none)"];
|
61 | }
|
62 | this.messages_ = this.messages_.concat(
|
63 | items.map(function(item) {
|
64 | item = `- ${item}`;
|
65 | return this.color_ ? clc.blackBright(item) : item;
|
66 | }, this)
|
67 | );
|
68 | };
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | Logger.prototype.flush = function(success) {
|
75 | const out = success ? this.stdout : this.stderr;
|
76 | this.messages_.forEach(msg => {
|
77 | out.write(`${msg}\n`);
|
78 | });
|
79 | this.empty();
|
80 | };
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | Logger.prototype.empty = function() {
|
86 | this.messages_ = [];
|
87 | };
|
88 |
|
89 | module.exports = Logger;
|