-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
101 lines (100 loc) · 2.28 KB
/
index.ts
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import chalk, {
BackgroundColor as BackgroundColorName,
Color as ColorName,
} from "chalk";
import ora, { Ora } from "ora";
import boxen from "boxen";
export interface LoggerConfigI {
color?: string;
bgColor?: string;
box?: string;
title?: string;
status?: "warn" | "success" | "fail" | "info" | "end";
}
function makeText(msg: string, config?: LoggerConfigI): string {
let txt = msg;
if (!config) {
return txt;
}
if (config.color || config.bgColor) {
let c = chalk;
if (config.color) {
c.Color = config.color as typeof ColorName;
}
if (config.bgColor) {
c.BackgroundColor = config.bgColor as typeof BackgroundColorName;
}
txt = c(txt);
}
if (config.box || config.title) {
let b: { [key: string]: any } = {
borderStyle: config.box ?? "single",
padding: 1,
margin: 1,
};
if (config.title) {
b["title"] = config.title;
}
txt = boxen(txt, b);
}
return txt;
}
export default class Logger {
static log(msg: string, config?: LoggerConfigI) {
const txt = makeText(msg, config);
console.log(txt);
}
static startLog(msg: string, config?: LoggerConfigI): Ora {
const txt = makeText(msg, config);
return ora(txt).start();
}
static endLog(oraPtr: Ora, msg: string, config?: LoggerConfigI) {
const txt = makeText(msg, config);
switch (config?.status) {
case "warn":
oraPtr.warn(txt);
break;
case "success":
oraPtr.succeed(txt);
break;
case "info":
oraPtr.info(txt);
break;
case "fail":
oraPtr.fail(txt);
break;
default:
oraPtr.stopAndPersist({ text: txt });
break;
}
oraPtr.stop();
}
static error(msg: string, config?: LoggerConfigI) {
const conf = {
...config,
color: "red",
};
Logger.log(msg, conf);
}
static warn(msg: string, config?: LoggerConfigI) {
const conf = {
...config,
color: "yellow",
};
Logger.log(msg, conf);
}
static info(msg: string, config?: LoggerConfigI) {
const conf = {
...config,
color: "blue",
};
Logger.log(msg, conf);
}
static debug(msg: string, config?: LoggerConfigI) {
const conf = {
...config,
color: "green",
};
Logger.log(msg, conf);
}
}