UNPKG

1.64 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6const cli_color_1 = __importDefault(require("cli-color"));
7class Logger {
8 constructor(enableColor, stdout, stderr) {
9 this.color_ = !!enableColor;
10 this.messages_ = [];
11 this.stdout = stdout;
12 this.stderr = stderr;
13 }
14 raw(msg, opt_color) {
15 this.messages_.push(this.color_ && opt_color ? opt_color(msg) : msg);
16 }
17 info(msg) {
18 this.raw(msg);
19 }
20 warn(msg) {
21 this.raw(msg, cli_color_1.default.yellow);
22 }
23 error(msg) {
24 this.raw(msg, cli_color_1.default.red);
25 }
26 success(msg) {
27 this.raw(msg, cli_color_1.default.green);
28 }
29 /**
30 * Log items with black bright.
31 */
32 items(items) {
33 if (items.length === 0) {
34 items = ["(none)"];
35 }
36 this.messages_ = this.messages_.concat(items.map((item) => {
37 item = `- ${item}`;
38 return this.color_ ? cli_color_1.default.blackBright(item) : item;
39 }, this));
40 }
41 /**
42 * Flush out all stored messages.
43 * @param success If true, flush to stdout. Otherwise to stderr.
44 */
45 flush(success) {
46 const out = success ? this.stdout : this.stderr;
47 this.messages_.forEach((msg) => {
48 out.write(`${msg}\n`);
49 });
50 this.empty();
51 }
52 /**
53 * Clear all stored messages.
54 */
55 empty() {
56 this.messages_ = [];
57 }
58}
59exports.default = Logger;