UNPKG

1.71 kBJavaScriptView Raw
1let { log, warn, error, info, time, timeEnd } = console;
2let { color } = new (require('./string'))();
3let { NODE_ENV } = process.env;
4let testing = (NODE_ENV === 'test') || (NODE_ENV === 'testing');
5let anon = () => {};
6let config;
7let disabled;
8let enabled;
9
10try {
11 config = require(path.join(process.cwd(), 'config'));
12} catch (err) {
13 config = {};
14}
15
16disabled = testing && !config.enableConsoleMethodsWhenTesting;
17enabled = !disabled;
18
19exports.log = disabled ? anon : log;
20exports.warn = disabled ? anon : warn;
21exports.error = disabled ? anon : error;
22exports.info = disabled ? anon : info;
23exports.time = disabled ? anon : time;
24exports.timeEnd = disabled ? anon : timeEnd;
25
26exports.verbose = function (...args) {
27 if (disabled) return;
28 if (process.verbose) {
29 log(args);
30 }
31}
32
33exports.chalk = {
34
35 ok (...args) {
36 if (disabled) return;
37 log(color(args.map(a => {
38 return (typeof a === typeof {}) ? JSON.stringify(a, null, 2) : a;
39 }).join('\n'), 'green'));
40 },
41
42 warn (...args) {
43 if (disabled) return;
44 log(color(args.map(a => {
45 return (typeof a === typeof {}) ? JSON.stringify(a, null, 2) : a;
46 }).join('\n'), 'yellow'));
47 },
48
49 error (...args) {
50 if (disabled) return;
51 error(color(args.map(a => {
52 return (typeof a === typeof {}) ? JSON.stringify(a, null, 2) : a;
53 }).join('\n'), 'red'));
54 },
55
56 info (...args) {
57 if (disabled) return;
58 info(color(args.map(a => {
59 return (typeof a === typeof {}) ? JSON.stringify(a, null, 2) : a;
60 }).join('\n'), 'grey'));
61 },
62
63 verbose (information, _color = 'grey') {
64 if (disabled) return;
65 if (process.verbose) {
66 info(color(information, _color));
67 }
68 }
69
70}