-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint.js
66 lines (58 loc) · 2.01 KB
/
print.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
import chalk from 'chalk';
const dateTimeFormatOprions = {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false
};
/** Internal method: format a message so it shows with timestamp. */
function formatMessage(msg) {
return `[${new Intl.DateTimeFormat('en-US', dateTimeFormatOprions).format(new Date())}] ${typeof msg === 'string' ? msg : JSON.stringify(msg, null, 2)}`;
}
/**
* Prints a message in console.
* @param {string|Object} msg - The message to be printed. It can be a non-string type, in which case it will be serialized before printing.
* @param {Chalk} color - (Optional) The color of the message to be printed.
* If not provided, default color white is used.
*/
function print(msg, color = chalk.white) {
console.log(color(formatMessage(msg)));
}
/**
* Prints an error message in console.
* @param {string|Object} msg - The error message to be printed. It can be a non-string type, in which case it will be serialized before printing.
*/
function error(msg) {
console.log(chalk.red(formatMessage(msg)));
}
/**
* Prints a warning message in console.
* @param {string|Object} msg - The warning message to be printed. It can be a non-string type, in which case it will be serialized before printing.
*/
function warning(msg) {
console.log(chalk.yellow(formatMessage(msg)));
}
/**
* Prints an infomation message in console.
* @param {string|Object} msg - The information message to be printed. It can be a non-string type, in which case it will be serialized before printing.
*/
function info(msg) {
console.log(chalk.cyan(formatMessage(msg)));
}
/**
* Prints a verbose message in console.
* @param {string|Object} msg - The verbose message to be printed. It can be a non-string type, in which case it will be serialized before printing.
*/
function verbose(msg) {
console.log(chalk.green(formatMessage(msg)));
}
export {
print,
error,
warning,
info,
verbose,
};