-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
25 lines (22 loc) · 801 Bytes
/
utils.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
const colors = Object.entries({
gray: '\x1b[90m',
green: '\x1b[32m',
red: '\x1b[31m',
yellow: '\x1b[33m',
}).reduce((map, [key, value]) => Object.assign(map, {[key]: (text) => `${value}${text}\x1b[0m`}), {});
function logWithTime(text) {
const now = new Date();
const hours = now.getHours();
const minutes = now.getMinutes();
const seconds = now.getSeconds();
const leftpad = (n) => String(n).padStart(2, '0');
return console.log(`${colors.gray([hours, minutes, seconds].map(leftpad).join(':'))} ${text}`);
}
const log = Object.assign((text) => logWithTime(text), {
ok: (text) => logWithTime(colors.green(text)),
warn: (text) => logWithTime(colors.yellow(text)),
error: (text) => logWithTime(colors.red(text)),
});
module.exports = {
log,
};