UNPKG

1.49 kBJavaScriptView Raw
1
2///@ts-check
3"use strict";
4
5const colors = require("ansi-colors");
6
7const noColor = process.argv.indexOf('--no-color') > 0;
8
9// function addColor(str) {
10// if (process.argv.indexOf('--no-color') > 0) {
11// return str;
12// }
13
14// return colors.gray(str);
15// }
16
17function getTime(level) {
18 const time = new Date().toLocaleTimeString('en-US', { hour12: false });
19 if (noColor) {
20 return '[' + time + ']';
21 } else {
22 switch (level) {
23 case 1:
24 return colors.yellowBright('[') + colors.gray(time) + colors.yellowBright(']');
25 case 2:
26 return colors.red('[') + colors.gray(time) + colors.red(']');
27 case -1:
28 return colors.gray('[') + colors.gray(time) + colors.gray(']');
29 default:
30 return '[' + colors.gray(time) + ']';
31 }
32
33 }
34}
35
36
37function log() {
38 console.log.apply(console, Array.prototype.concat.apply([getTime()], arguments));
39 return this;
40}
41
42function info() {
43 console.info.apply(console, Array.prototype.concat.apply([getTime(-1)], arguments));
44 return this;
45}
46
47function warn() {
48 console.warn.apply(console, Array.prototype.concat.apply([getTime(1)], arguments));
49 return this;
50}
51
52function error() {
53 console.error.apply(console, Array.prototype.concat.apply([getTime(2)], arguments));
54 return this;
55}
56
57module.exports = log;
58module.exports.info = info;
59module.exports.warn = warn;
60module.exports.error = error;
\No newline at end of file