1 | 'use strict';
|
2 |
|
3 | const { indent } = require('./string');
|
4 | const { msDiff } = require('./stopwatch');
|
5 | const chalk = require('chalk');
|
6 |
|
7 | const TOO_LONG = 10;
|
8 |
|
9 | const start = process.hrtime();
|
10 | let last = null;
|
11 |
|
12 | exports.BELL = '\x07';
|
13 |
|
14 | exports.silent = process.env.NODE_ENV == 'test';
|
15 | exports.verbose = false;
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | exports.print = function(msg, column) {
|
23 | if (column == null) column = 0;
|
24 | if (!exports.silent) console.log(indent(msg, column));
|
25 | };
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | exports.error = function(err, column, throws) {
|
35 | if (exports.silent) return;
|
36 | if (column == null) column = 0;
|
37 | if (throws == null) throws = true;
|
38 | if ('string' == typeof err) err = new Error(err);
|
39 |
|
40 | if (!throws) err.message += exports.BELL;
|
41 | exports.print(
|
42 | chalk.red.inverse(' error ') + ' ' + err.message + (err.filepath ? ' in ' + chalk.bold.grey(err.filepath) : ''),
|
43 | column
|
44 | );
|
45 | if (!throws) return console.log(err);
|
46 | throw err;
|
47 | };
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | exports.warn = function(msg, column) {
|
55 | if (column == null) column = 0;
|
56 | if ('string' instanceof Error) msg = msg.message;
|
57 | exports.print(`${chalk.yellow.inverse(' warning ')} ${msg}`, column);
|
58 | };
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | exports.debug = function(msg, column) {
|
66 | const now = process.hrtime();
|
67 |
|
68 | if (!last) last = now;
|
69 |
|
70 | const ellapsed = msDiff(now, last);
|
71 |
|
72 | if (column == null) column = 0;
|
73 | if (exports.verbose) {
|
74 | msg = chalk.cyan('+') +
|
75 | chalk.bold.grey((ellapsed > TOO_LONG ? chalk.red(ellapsed) : ellapsed) + 'ms') +
|
76 | chalk.cyan('::') +
|
77 | chalk.bold.grey(msDiff(now, start) + 'ms') +
|
78 | chalk.cyan('=') +
|
79 | msg;
|
80 | exports.print(msg, column);
|
81 | }
|
82 | last = now;
|
83 | };
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | exports.strong = function(string) {
|
91 | return chalk.bold.grey(string);
|
92 | };
|