-
Notifications
You must be signed in to change notification settings - Fork 167
/
console.js
166 lines (141 loc) · 3.59 KB
/
console.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
'use strict';
const Promise = require('bluebird');
const _ = require('lodash');
const chalk = require('chalk');
const Table = require('cli-table3');
const slog = require('log-update');
const Theme = require('./theme');
const defaultTheme = require('./themes/default');
const utils = require('../core/utils');
class Console {
constructor(logger) {
this._logger = logger || console;
this._theme = defaultTheme;
this._slogging = false;
this._debugging = false;
}
set theme(theme) {
if (!theme instanceof Theme) {
throw new Error('Fractal themes must inherit from the base Theme class.');
}
this._theme = theme;
}
get theme() {
return this._theme;
}
br() {
slog.clear();
this.write('');
return this;
}
log(text) {
this.write(text, 'log');
return this;
}
debug(text, data) {
if (this._debugging) {
if (this._slogging) {
this.unslog();
this.br();
}
this.write(text, 'debug');
if (data) this.dump(data);
}
return this;
}
success(text) {
this.write(text, 'success');
return this;
}
warn(text) {
this.write(text, 'warn');
return this;
}
update(text, type) {
slog(type ? this._format(text, type) : text);
return this;
}
clear() {
slog.clear();
return this;
}
persist() {
slog.done();
return this;
}
error(err, data) {
if (this.isSlogging()) {
this.unslog().br();
}
this.write(err, 'error');
if ((data || err instanceof Error) && this._debugging) {
data = data || err;
if (data.stack) {
this.log(' ' + _.trim(data.stack.toString().replace(err.toString(), '')));
} else {
this.dump(data);
}
}
return this;
}
dump(data) {
if (!data || !_.isObject(data)) {
return this.write(data);
}
if (!_.isFunction(data.then)) {
data = Promise.resolve(data);
}
data.then(data => this.write(utils.stringify(data)));
}
box(header, body, footer) {
const table = new Table({
head: [],
chars: { mid: chalk.dim('─'), 'left-mid': '│', 'mid-mid': chalk.dim('─'), 'right-mid': '│' },
});
if (header) {
table.push([header]);
}
body = [].concat(body);
for (const line of body) {
table.push([line]);
}
if (footer) {
table.push([chalk.dim(footer)]);
}
this.unslog().write(table.toString(), null);
return this;
}
write(str, type) {
str = _.isString(str) ? str : str.toString();
str = type ? this._format(str, type) : str;
if (this.isSlogging()) {
slog(str);
} else {
this._logger.log(str);
}
}
columns(data, options) {
const columnify = require('columnify');
this.write(columnify(data, options));
return this;
}
slog() {
this._slogging = true;
return this;
}
unslog() {
this._slogging = false;
slog.clear();
return this;
}
isSlogging() {
return this._slogging;
}
_format(text, type) {
return this._theme.format(text, type);
}
debugMode(status) {
this._debugging = !! status;
}
}
module.exports = Console;