@aiot-toolkit/shared-utils 2.0.2 → 2.0.3-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/ColorConsole.d.ts +22 -0
- package/lib/ColorConsole.js +57 -5
- package/lib/utils/CommonUtil.d.ts +0 -9
- package/lib/utils/CommonUtil.js +0 -18
- package/package.json +2 -2
package/lib/ColorConsole.d.ts
CHANGED
|
@@ -44,5 +44,27 @@ declare class ColorConsole {
|
|
|
44
44
|
* @returns 返回打印的日志信息
|
|
45
45
|
*/
|
|
46
46
|
static logger(log: ILog): void;
|
|
47
|
+
/**
|
|
48
|
+
* 日志转换为带颜色的字符串
|
|
49
|
+
*
|
|
50
|
+
* @param log 日志对象
|
|
51
|
+
* @param prefix 前缀列表
|
|
52
|
+
* false-不使用前缀
|
|
53
|
+
* string[]-自定义前缀
|
|
54
|
+
* 默认-[图标、时间、toolkit字样]
|
|
55
|
+
*
|
|
56
|
+
* @example logToString(log, ['myPrefix'])
|
|
57
|
+
*/
|
|
58
|
+
static logToString(log: ILog, prefix?: string[] | false): string;
|
|
59
|
+
/**
|
|
60
|
+
* 创建默认消息前缀列表
|
|
61
|
+
*
|
|
62
|
+
* 1. 图标
|
|
63
|
+
* 2. 时间
|
|
64
|
+
* 3. toolkit 字样
|
|
65
|
+
*
|
|
66
|
+
* @param log
|
|
67
|
+
*/
|
|
68
|
+
private static createDefaultPrefix;
|
|
47
69
|
}
|
|
48
70
|
export default ColorConsole;
|
package/lib/ColorConsole.js
CHANGED
|
@@ -55,9 +55,7 @@ class ColorConsole {
|
|
|
55
55
|
}
|
|
56
56
|
static _log(logLevel, ...message) {
|
|
57
57
|
const style = this.styleDic[logLevel] || this.defaultStyle;
|
|
58
|
-
|
|
59
|
-
const icon = style.icon ? style.icon : '';
|
|
60
|
-
console.log(icon, time, this.createMessage(message.map((item) => {
|
|
58
|
+
console.log(...this.createDefaultPrefix({ level: logLevel, message }), this.createMessage(message.map((item) => {
|
|
61
59
|
if (typeof item === 'object') {
|
|
62
60
|
return Object.assign(Object.assign({}, item), { style: item.style || style });
|
|
63
61
|
}
|
|
@@ -90,10 +88,26 @@ class ColorConsole {
|
|
|
90
88
|
* @returns 返回打印的日志信息
|
|
91
89
|
*/
|
|
92
90
|
static logger(log) {
|
|
91
|
+
const str = this.logToString(log);
|
|
92
|
+
console.log(str);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* 日志转换为带颜色的字符串
|
|
96
|
+
*
|
|
97
|
+
* @param log 日志对象
|
|
98
|
+
* @param prefix 前缀列表
|
|
99
|
+
* false-不使用前缀
|
|
100
|
+
* string[]-自定义前缀
|
|
101
|
+
* 默认-[图标、时间、toolkit字样]
|
|
102
|
+
*
|
|
103
|
+
* @example logToString(log, ['myPrefix'])
|
|
104
|
+
*/
|
|
105
|
+
static logToString(log, prefix) {
|
|
93
106
|
const { message, position, filePath, level = ILog_1.Loglevel.INFO } = log;
|
|
107
|
+
// 位置消息
|
|
94
108
|
let positionMessage;
|
|
95
109
|
positionMessage = {
|
|
96
|
-
style: this.getStyle(
|
|
110
|
+
style: this.getStyle(level),
|
|
97
111
|
word: [filePath, position === null || position === void 0 ? void 0 : position.startLine, position === null || position === void 0 ? void 0 : position.startColumn]
|
|
98
112
|
.filter((item) => Boolean(item))
|
|
99
113
|
.join(':')
|
|
@@ -102,7 +116,45 @@ class ColorConsole {
|
|
|
102
116
|
if (positionMessage) {
|
|
103
117
|
logs.unshift(positionMessage);
|
|
104
118
|
}
|
|
105
|
-
|
|
119
|
+
// 前缀
|
|
120
|
+
let effectPrefix;
|
|
121
|
+
if (prefix === false) {
|
|
122
|
+
prefix = [];
|
|
123
|
+
}
|
|
124
|
+
if (prefix === undefined) {
|
|
125
|
+
effectPrefix = _a.createDefaultPrefix(log);
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
effectPrefix = prefix;
|
|
129
|
+
}
|
|
130
|
+
const style = this.styleDic[level] || this.defaultStyle;
|
|
131
|
+
return [
|
|
132
|
+
...effectPrefix,
|
|
133
|
+
this.createMessage(logs.map((item) => {
|
|
134
|
+
if (typeof item === 'object') {
|
|
135
|
+
return Object.assign(Object.assign({}, item), {
|
|
136
|
+
// 对象格式消息的样式依次使用:[消息自身样式, log样式, 默认样式]
|
|
137
|
+
style: item.style || style });
|
|
138
|
+
}
|
|
139
|
+
return item;
|
|
140
|
+
}))
|
|
141
|
+
].join(' ');
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* 创建默认消息前缀列表
|
|
145
|
+
*
|
|
146
|
+
* 1. 图标
|
|
147
|
+
* 2. 时间
|
|
148
|
+
* 3. toolkit 字样
|
|
149
|
+
*
|
|
150
|
+
* @param log
|
|
151
|
+
*/
|
|
152
|
+
static createDefaultPrefix(log) {
|
|
153
|
+
const { level = ILog_1.Loglevel.INFO } = log;
|
|
154
|
+
const style = this.styleDic[level] || this.defaultStyle;
|
|
155
|
+
const time = this.formatWord(`[${(0, dayjs_1.default)().format('MM-DD HH:mm:ss')}] [toolkit]:`, style);
|
|
156
|
+
const icon = style.icon ? style.icon : '';
|
|
157
|
+
return [icon, time];
|
|
106
158
|
}
|
|
107
159
|
}
|
|
108
160
|
_a = ColorConsole;
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { Schema, SomeJTDSchemaType } from 'ajv/dist/core';
|
|
2
|
-
import { ILog } from '../interface/ILog';
|
|
3
2
|
export type JSONSchemaType = Schema & SomeJTDSchemaType;
|
|
4
3
|
/**
|
|
5
4
|
* 不好分类的工具函数
|
|
@@ -41,13 +40,5 @@ declare class CommonUtil {
|
|
|
41
40
|
*/
|
|
42
41
|
static validateJson(json: JSON, schema: JSONSchemaType): Error[] | undefined;
|
|
43
42
|
static outputQRCodeOnTerminal(text: string): Promise<void>;
|
|
44
|
-
/**
|
|
45
|
-
* 终端环境下处理日志列表
|
|
46
|
-
*
|
|
47
|
-
* 1. 用ColorConsole输出日志
|
|
48
|
-
* 2. 如果日志中有Error/Throw,则终止程序
|
|
49
|
-
* @param logs
|
|
50
|
-
*/
|
|
51
|
-
static processingLogsInTerminal(logs?: ILog[]): void;
|
|
52
43
|
}
|
|
53
44
|
export default CommonUtil;
|
package/lib/utils/CommonUtil.js
CHANGED
|
@@ -112,23 +112,5 @@ class CommonUtil {
|
|
|
112
112
|
console.log(qrcode);
|
|
113
113
|
});
|
|
114
114
|
}
|
|
115
|
-
/**
|
|
116
|
-
* 终端环境下处理日志列表
|
|
117
|
-
*
|
|
118
|
-
* 1. 用ColorConsole输出日志
|
|
119
|
-
* 2. 如果日志中有Error/Throw,则终止程序
|
|
120
|
-
* @param logs
|
|
121
|
-
*/
|
|
122
|
-
static processingLogsInTerminal(logs) {
|
|
123
|
-
if (!logs) {
|
|
124
|
-
return;
|
|
125
|
-
}
|
|
126
|
-
logs.forEach((log) => {
|
|
127
|
-
ColorConsole_1.default.logger(log);
|
|
128
|
-
});
|
|
129
|
-
if (logs.some((log) => log.level === ILog_1.Loglevel.ERROR || log.level === ILog_1.Loglevel.THROW)) {
|
|
130
|
-
process.exit(1);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
115
|
}
|
|
134
116
|
exports.default = CommonUtil;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aiot-toolkit/shared-utils",
|
|
3
|
-
"version": "2.0.2",
|
|
3
|
+
"version": "2.0.3-beta.2",
|
|
4
4
|
"description": "The common method functions of the aiot-toolkit.",
|
|
5
5
|
"homepage": "",
|
|
6
6
|
"license": "ISC",
|
|
@@ -41,5 +41,5 @@
|
|
|
41
41
|
"@types/minimatch": "^5.1.2",
|
|
42
42
|
"@types/qrcode": "^1.5.5"
|
|
43
43
|
},
|
|
44
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "7776973f6a81d7f13eb330be8df5801d9143c83c"
|
|
45
45
|
}
|