@aiot-toolkit/shared-utils 2.0.1-alpha.12 → 2.0.1-alpha.13
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 +37 -2
- package/lib/ColorConsole.js +98 -1
- package/lib/ColorConsole.js.map +1 -1
- package/lib/index.js +1 -3
- package/lib/index.js.map +1 -1
- package/lib/utils/CommonUtil.d.ts +23 -1
- package/lib/utils/CommonUtil.js +56 -8
- package/lib/utils/CommonUtil.js.map +1 -1
- package/lib/utils/FileUtil.js +2 -2
- package/lib/utils/FileUtil.js.map +1 -1
- package/lib/utils/StringUtil.d.ts +3 -1
- package/lib/utils/StringUtil.js +3 -1
- package/lib/utils/StringUtil.js.map +1 -1
- package/package.json +3 -2
- package/lib/ColorConsole2.d.ts +0 -41
- package/lib/ColorConsole2.js +0 -107
- package/lib/ColorConsole2.js.map +0 -1
package/lib/ColorConsole.d.ts
CHANGED
|
@@ -1,13 +1,48 @@
|
|
|
1
|
-
import { ILog } from './interface/ILog';
|
|
1
|
+
import { ILog, LOG_LEVEL } from './interface/ILog';
|
|
2
|
+
interface IStyle {
|
|
3
|
+
color?: string;
|
|
4
|
+
bgColor?: string;
|
|
5
|
+
bold?: boolean;
|
|
6
|
+
italic?: boolean;
|
|
7
|
+
icon?: string;
|
|
8
|
+
}
|
|
9
|
+
type Message = string | number | boolean | undefined | {
|
|
10
|
+
word: string;
|
|
11
|
+
style?: IStyle;
|
|
12
|
+
};
|
|
2
13
|
/**
|
|
3
14
|
* Log
|
|
4
15
|
*/
|
|
5
16
|
declare class ColorConsole {
|
|
17
|
+
static defaultStyle: IStyle;
|
|
18
|
+
static styleDic: {
|
|
19
|
+
[key in LOG_LEVEL]: IStyle;
|
|
20
|
+
};
|
|
21
|
+
static getStyle(logLevel: LOG_LEVEL): IStyle;
|
|
22
|
+
static log(...message: Message[]): void;
|
|
23
|
+
static error(...message: Message[]): void;
|
|
24
|
+
static info(...message: Message[]): void;
|
|
25
|
+
static success(...message: Message[]): void;
|
|
26
|
+
static throw(...message: Message[]): void;
|
|
27
|
+
static warn(...message: Message[]): void;
|
|
28
|
+
/**
|
|
29
|
+
* 创建复杂格式的消息,可单独设置词语样式
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```js
|
|
33
|
+
* createMessage("my name is", {message:'****', style:{ color:'red' }})
|
|
34
|
+
* ```
|
|
35
|
+
* @param words
|
|
36
|
+
* @returns
|
|
37
|
+
*/
|
|
38
|
+
static createMessage(...words: Message[]): string;
|
|
39
|
+
private static _log;
|
|
40
|
+
private static formatWord;
|
|
6
41
|
/**
|
|
7
42
|
* 按照日志类型打印不同颜色的日志
|
|
8
43
|
* @param log 默认日志类型为info
|
|
9
44
|
* @returns 返回打印的日志信息
|
|
10
45
|
*/
|
|
11
|
-
static
|
|
46
|
+
static logger(log: ILog): string;
|
|
12
47
|
}
|
|
13
48
|
export default ColorConsole;
|
package/lib/ColorConsole.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
|
+
var _a;
|
|
5
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
7
|
const chalk_1 = __importDefault(require("chalk"));
|
|
7
8
|
const dayjs_1 = __importDefault(require("dayjs"));
|
|
@@ -10,12 +11,80 @@ const ILog_1 = require("./interface/ILog");
|
|
|
10
11
|
* Log
|
|
11
12
|
*/
|
|
12
13
|
class ColorConsole {
|
|
14
|
+
static getStyle(logLevel) {
|
|
15
|
+
return this.styleDic[logLevel] || this.defaultStyle;
|
|
16
|
+
}
|
|
17
|
+
static log(...message) {
|
|
18
|
+
return this._log(ILog_1.LOG_LEVEL.Log, ...message);
|
|
19
|
+
}
|
|
20
|
+
static error(...message) {
|
|
21
|
+
return this._log(ILog_1.LOG_LEVEL.Error, ...message);
|
|
22
|
+
}
|
|
23
|
+
static info(...message) {
|
|
24
|
+
return this._log(ILog_1.LOG_LEVEL.Info, ...message);
|
|
25
|
+
}
|
|
26
|
+
static success(...message) {
|
|
27
|
+
return this._log(ILog_1.LOG_LEVEL.Success, ...message);
|
|
28
|
+
}
|
|
29
|
+
static throw(...message) {
|
|
30
|
+
this._log(ILog_1.LOG_LEVEL.Throw, ...message);
|
|
31
|
+
process.exit();
|
|
32
|
+
}
|
|
33
|
+
static warn(...message) {
|
|
34
|
+
return this._log(ILog_1.LOG_LEVEL.Warn, ...message);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* 创建复杂格式的消息,可单独设置词语样式
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```js
|
|
41
|
+
* createMessage("my name is", {message:'****', style:{ color:'red' }})
|
|
42
|
+
* ```
|
|
43
|
+
* @param words
|
|
44
|
+
* @returns
|
|
45
|
+
*/
|
|
46
|
+
static createMessage(...words) {
|
|
47
|
+
return words
|
|
48
|
+
.map((item) => {
|
|
49
|
+
if (typeof item === 'object') {
|
|
50
|
+
return this.formatWord(item.word, item.style);
|
|
51
|
+
}
|
|
52
|
+
return this.formatWord(item, this.defaultStyle);
|
|
53
|
+
})
|
|
54
|
+
.join(' ');
|
|
55
|
+
}
|
|
56
|
+
static _log(logLevel, ...message) {
|
|
57
|
+
const style = this.styleDic[logLevel] || this.defaultStyle;
|
|
58
|
+
const time = this.formatWord(`[${(0, dayjs_1.default)().format('YYYY-MM-DD HH:mm:ss')}]:`, style);
|
|
59
|
+
const icon = style.icon ? style.icon : '';
|
|
60
|
+
console.log(icon, time, this.createMessage(...message));
|
|
61
|
+
}
|
|
62
|
+
static formatWord(word, style) {
|
|
63
|
+
if (word === undefined || word === null) {
|
|
64
|
+
return '';
|
|
65
|
+
}
|
|
66
|
+
const { color, bgColor, bold, italic } = style || this.defaultStyle;
|
|
67
|
+
let result = word;
|
|
68
|
+
if (color) {
|
|
69
|
+
result = chalk_1.default.hex(color)(result);
|
|
70
|
+
}
|
|
71
|
+
if (bgColor) {
|
|
72
|
+
result = chalk_1.default.bgHex(bgColor)(result);
|
|
73
|
+
}
|
|
74
|
+
if (bold) {
|
|
75
|
+
result = chalk_1.default.bold(result);
|
|
76
|
+
}
|
|
77
|
+
if (italic) {
|
|
78
|
+
result = chalk_1.default.italic(result);
|
|
79
|
+
}
|
|
80
|
+
return result;
|
|
81
|
+
}
|
|
13
82
|
/**
|
|
14
83
|
* 按照日志类型打印不同颜色的日志
|
|
15
84
|
* @param log 默认日志类型为info
|
|
16
85
|
* @returns 返回打印的日志信息
|
|
17
86
|
*/
|
|
18
|
-
static
|
|
87
|
+
static logger(log) {
|
|
19
88
|
const time = (0, dayjs_1.default)().format('YYYY-MM-DD HH:mm:ss');
|
|
20
89
|
const { message, position, level = ILog_1.LOG_LEVEL.Info, isOnlyPrintError = false } = log;
|
|
21
90
|
let result = position ? `${message}\n${JSON.stringify(position)}` : `${message}`;
|
|
@@ -40,6 +109,34 @@ class ColorConsole {
|
|
|
40
109
|
return result;
|
|
41
110
|
}
|
|
42
111
|
}
|
|
112
|
+
_a = ColorConsole;
|
|
113
|
+
ColorConsole.defaultStyle = {
|
|
114
|
+
color: '#cccccc'
|
|
115
|
+
};
|
|
116
|
+
ColorConsole.styleDic = {
|
|
117
|
+
[ILog_1.LOG_LEVEL.Success]: {
|
|
118
|
+
color: '#008000',
|
|
119
|
+
icon: '✅'
|
|
120
|
+
},
|
|
121
|
+
[ILog_1.LOG_LEVEL.Info]: {
|
|
122
|
+
color: '#03A9F4',
|
|
123
|
+
icon: 'ℹ️ '
|
|
124
|
+
},
|
|
125
|
+
[ILog_1.LOG_LEVEL.Warn]: {
|
|
126
|
+
color: '#FF9800',
|
|
127
|
+
icon: '🟠'
|
|
128
|
+
},
|
|
129
|
+
[ILog_1.LOG_LEVEL.Error]: {
|
|
130
|
+
color: '#ff0000',
|
|
131
|
+
icon: '❌'
|
|
132
|
+
},
|
|
133
|
+
[ILog_1.LOG_LEVEL.Throw]: {
|
|
134
|
+
color: '#ff0000',
|
|
135
|
+
bold: true,
|
|
136
|
+
icon: '❌'
|
|
137
|
+
},
|
|
138
|
+
[ILog_1.LOG_LEVEL.Log]: _a.defaultStyle
|
|
139
|
+
};
|
|
43
140
|
exports.default = ColorConsole;
|
|
44
141
|
|
|
45
142
|
//# sourceMappingURL=ColorConsole.js.map
|
package/lib/ColorConsole.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["ColorConsole.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"sources":["ColorConsole.ts"],"names":[],"mappings":";;;;;;AAAA,kDAAyB;AACzB,kDAAyB;AACzB,2CAAkD;AAWlD;;GAEG;AACH,MAAM,YAAY;IA+BhB,MAAM,CAAC,QAAQ,CAAC,QAAmB;QACjC,OAAO,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,IAAI,IAAI,CAAC,YAAY,CAAA;IACrD,CAAC;IAED,MAAM,CAAC,GAAG,CAAC,GAAG,OAAkB;QAC9B,OAAO,IAAI,CAAC,IAAI,CAAC,gBAAS,CAAC,GAAG,EAAE,GAAG,OAAO,CAAC,CAAA;IAC7C,CAAC;IACD,MAAM,CAAC,KAAK,CAAC,GAAG,OAAkB;QAChC,OAAO,IAAI,CAAC,IAAI,CAAC,gBAAS,CAAC,KAAK,EAAE,GAAG,OAAO,CAAC,CAAA;IAC/C,CAAC;IACD,MAAM,CAAC,IAAI,CAAC,GAAG,OAAkB;QAC/B,OAAO,IAAI,CAAC,IAAI,CAAC,gBAAS,CAAC,IAAI,EAAE,GAAG,OAAO,CAAC,CAAA;IAC9C,CAAC;IACD,MAAM,CAAC,OAAO,CAAC,GAAG,OAAkB;QAClC,OAAO,IAAI,CAAC,IAAI,CAAC,gBAAS,CAAC,OAAO,EAAE,GAAG,OAAO,CAAC,CAAA;IACjD,CAAC;IACD,MAAM,CAAC,KAAK,CAAC,GAAG,OAAkB;QAChC,IAA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,IAAI,EAAE,IAAI;QACV,IAAI,EAAE,GAAG;KACV;IACD,CAAC,gBAAS,CAAC,GAAG,CAAC,EAAE,EAAI,CAAC,YAAY;CAxBrB,AAyBd,CAAA;AA2GH,kBAAe,YAAY,CAAA","file":"ColorConsole.js","sourcesContent":["import chalk from 'chalk'\nimport dayjs from 'dayjs'\nimport { ILog, LOG_LEVEL } from './interface/ILog'\n\ninterface IStyle {\n color?: string\n bgColor?: string\n bold?: boolean\n italic?: boolean\n icon?: string\n}\n\ntype Message = string | number | boolean | undefined | { word: string; style?: IStyle }\n/**\n * Log\n */\nclass ColorConsole {\n static defaultStyle: IStyle = {\n color: '#cccccc'\n }\n static styleDic: {\n [key in LOG_LEVEL]: IStyle\n } = {\n [LOG_LEVEL.Success]: {\n color: '#008000',\n icon: '✅'\n },\n [LOG_LEVEL.Info]: {\n color: '#03A9F4',\n icon: 'ℹ️ '\n },\n [LOG_LEVEL.Warn]: {\n color: '#FF9800',\n icon: '🟠'\n },\n [LOG_LEVEL.Error]: {\n color: '#ff0000',\n icon: '❌'\n },\n [LOG_LEVEL.Throw]: {\n color: '#ff0000',\n bold: true,\n icon: '❌'\n },\n [LOG_LEVEL.Log]: this.defaultStyle\n }\n\n static getStyle(logLevel: LOG_LEVEL): IStyle {\n return this.styleDic[logLevel] || this.defaultStyle\n }\n\n static log(...message: Message[]) {\n return this._log(LOG_LEVEL.Log, ...message)\n }\n static error(...message: Message[]) {\n return this._log(LOG_LEVEL.Error, ...message)\n }\n static info(...message: Message[]) {\n return this._log(LOG_LEVEL.Info, ...message)\n }\n static success(...message: Message[]) {\n return this._log(LOG_LEVEL.Success, ...message)\n }\n static throw(...message: Message[]) {\n this._log(LOG_LEVEL.Throw, ...message)\n process.exit()\n }\n static warn(...message: Message[]) {\n return this._log(LOG_LEVEL.Warn, ...message)\n }\n\n /**\n * 创建复杂格式的消息,可单独设置词语样式\n *\n * @example\n * ```js\n * createMessage(\"my name is\", {message:'****', style:{ color:'red' }})\n * ```\n * @param words\n * @returns\n */\n public static createMessage(...words: Message[]) {\n return words\n .map((item) => {\n if (typeof item === 'object') {\n return this.formatWord(item.word, item.style)\n }\n return this.formatWord(item, this.defaultStyle)\n })\n .join(' ')\n }\n\n private static _log(logLevel: LOG_LEVEL, ...message: Message[]) {\n const style = this.styleDic[logLevel] || this.defaultStyle\n const time = this.formatWord(`[${dayjs().format('YYYY-MM-DD HH:mm:ss')}]:`, style)\n const icon = style.icon ? style.icon : ''\n console.log(icon, time, this.createMessage(...message))\n }\n\n private static formatWord(word?: string | number | boolean, style?: IStyle) {\n if (word === undefined || word === null) {\n return ''\n }\n const { color, bgColor, bold, italic } = style || this.defaultStyle\n\n let result = word\n if (color) {\n result = chalk.hex(color)(result)\n }\n if (bgColor) {\n result = chalk.bgHex(bgColor)(result)\n }\n\n if (bold) {\n result = chalk.bold(result)\n }\n\n if (italic) {\n result = chalk.italic(result)\n }\n return result\n }\n\n /**\n * 按照日志类型打印不同颜色的日志\n * @param log 默认日志类型为info\n * @returns 返回打印的日志信息\n */\n static logger(log: ILog): string {\n const time = dayjs().format('YYYY-MM-DD HH:mm:ss')\n const { message, position, level = LOG_LEVEL.Info, isOnlyPrintError = false } = log\n let result = position ? `${message}\\n${JSON.stringify(position)}` : `${message}`\n switch (level) {\n case LOG_LEVEL.Success:\n case LOG_LEVEL.Info:\n console.log(`${chalk.green.bold(`[${time} ${LOG_LEVEL[level]}]`)}: ${result}`)\n break\n case LOG_LEVEL.Warn:\n console.log(`${chalk.yellow.bold(`[${time} ${LOG_LEVEL[level]}]`)}: ${result}`)\n break\n case LOG_LEVEL.Error:\n case LOG_LEVEL.Throw:\n result = message\n if (isOnlyPrintError) {\n console.log(`${chalk.red.bold(`[${time} ${LOG_LEVEL[level]}]`)}: ${result}`)\n } else {\n throw new Error(result)\n }\n }\n return result\n }\n}\nexport default ColorConsole\n"],"sourceRoot":"../src"}
|
package/lib/index.js
CHANGED
|
@@ -15,9 +15,7 @@ Object.defineProperty(exports, "LOG_LEVEL", { enumerable: true, get: function ()
|
|
|
15
15
|
const WrapCallback_1 = __importDefault(require("./utils/WrapCallback"));
|
|
16
16
|
exports.WrapCallback = WrapCallback_1.default;
|
|
17
17
|
function outputQRCodeOnTerminal(text) {
|
|
18
|
-
ColorConsole_1.default.log({
|
|
19
|
-
message: `\nGenerate QR code for HTTP server: ${text}`
|
|
20
|
-
});
|
|
18
|
+
ColorConsole_1.default.log(`Generate QR code for HTTP server: ${text}`);
|
|
21
19
|
qrcode_terminal_1.default.generate(text, { small: true });
|
|
22
20
|
}
|
|
23
21
|
exports.outputQRCodeOnTerminal = outputQRCodeOnTerminal;
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["index.ts"],"names":[],"mappings":";;;;;;AAAA,sEAAwC;AACxC,kEAAyC;AACzC,6DAAoC;AAWlC,kBAXK,iBAAO,CAWL;AAVT,yEAAgD;AAW9C,wBAXK,uBAAa,CAWL;AAVf,2CAA0D;AAOxD,0FAPa,gBAAS,OAOb;AALX,wEAA+C;AAS7C,uBATK,sBAAY,CASL;AAId,SAAS,sBAAsB,CAAC,IAAY;IAC1C,sBAAY,CAAC,GAAG,CAAC
|
|
1
|
+
{"version":3,"sources":["index.ts"],"names":[],"mappings":";;;;;;AAAA,sEAAwC;AACxC,kEAAyC;AACzC,6DAAoC;AAWlC,kBAXK,iBAAO,CAWL;AAVT,yEAAgD;AAW9C,wBAXK,uBAAa,CAWL;AAVf,2CAA0D;AAOxD,0FAPa,gBAAS,OAOb;AALX,wEAA+C;AAS7C,uBATK,sBAAY,CASL;AAId,SAAS,sBAAsB,CAAC,IAAY;IAC1C,sBAAY,CAAC,GAAG,CAAC,qCAAqC,IAAI,EAAE,CAAC,CAAA;IAC7D,yBAAU,CAAC,QAAQ,CAAC,IAAI,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAA;AAC5C,CAAC;AANC,wDAAsB","file":"index.js","sourcesContent":["import qrTerminal from 'qrcode-terminal'\nimport ColorConsole from './ColorConsole'\nimport MapData from './data/MapData'\nimport PositionError from './data/PositionError'\nimport { ILog, LOG_LEVEL, Logger } from './interface/ILog'\nimport IPosition from './interface/IPosition'\nimport WrapCallback from './utils/WrapCallback'\n\nexport {\n ILog,\n IPosition,\n LOG_LEVEL,\n Logger,\n MapData,\n PositionError,\n WrapCallback,\n outputQRCodeOnTerminal\n}\n\nfunction outputQRCodeOnTerminal(text: string) {\n ColorConsole.log(`Generate QR code for HTTP server: ${text}`)\n qrTerminal.generate(text, { small: true })\n}\n"],"sourceRoot":"../src"}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
+
import { Schema, SomeJTDSchemaType } from 'ajv/dist/core';
|
|
3
|
+
export type JSONSchemaType = Schema & SomeJTDSchemaType;
|
|
2
4
|
/**
|
|
3
5
|
* 不好分类的工具函数
|
|
4
6
|
*/
|
|
@@ -16,6 +18,26 @@ declare class CommonUtil {
|
|
|
16
18
|
* 如果不存在,提示下载;存在则返回加载的依赖
|
|
17
19
|
* @param packageName
|
|
18
20
|
*/
|
|
19
|
-
static
|
|
21
|
+
static requireNodeModule(projectPath: string, packageName: string): Promise<any>;
|
|
22
|
+
static requireModule(path: string, throwError?: boolean): any;
|
|
23
|
+
/**
|
|
24
|
+
* 验证 json 内容
|
|
25
|
+
* @param json json 内容
|
|
26
|
+
* @param schema 验证配置, 详见:https://github.com/ajv-validator/ajv
|
|
27
|
+
*
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```
|
|
31
|
+
* CommonUtil.validateJson({a:123},
|
|
32
|
+
* {
|
|
33
|
+
* a:{
|
|
34
|
+
* type:'number'
|
|
35
|
+
* }
|
|
36
|
+
* }
|
|
37
|
+
* )
|
|
38
|
+
* ```
|
|
39
|
+
* @returns
|
|
40
|
+
*/
|
|
41
|
+
static validateJson(json: JSON, schema: JSONSchemaType): Error[] | undefined;
|
|
20
42
|
}
|
|
21
43
|
export default CommonUtil;
|
package/lib/utils/CommonUtil.js
CHANGED
|
@@ -4,9 +4,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const crypto_1 = __importDefault(require("crypto"));
|
|
7
|
+
const ajv_1 = __importDefault(require("ajv"));
|
|
7
8
|
const fs_1 = __importDefault(require("fs"));
|
|
8
9
|
const path_1 = __importDefault(require("path"));
|
|
9
|
-
const
|
|
10
|
+
const ColorConsole_1 = __importDefault(require("../ColorConsole"));
|
|
10
11
|
const ILog_1 = require("../interface/ILog");
|
|
11
12
|
/**
|
|
12
13
|
* 不好分类的工具函数
|
|
@@ -29,23 +30,70 @@ class CommonUtil {
|
|
|
29
30
|
* 如果不存在,提示下载;存在则返回加载的依赖
|
|
30
31
|
* @param packageName
|
|
31
32
|
*/
|
|
32
|
-
static
|
|
33
|
+
static requireNodeModule(projectPath, packageName) {
|
|
33
34
|
const modulePath = path_1.default.resolve(projectPath, `./node_modules/${packageName}`);
|
|
34
35
|
if (!fs_1.default.existsSync(modulePath)) {
|
|
35
|
-
return Promise.reject(new Error(
|
|
36
|
+
return Promise.reject(new Error(ColorConsole_1.default.createMessage({
|
|
36
37
|
word: packageName,
|
|
37
|
-
style:
|
|
38
|
+
style: ColorConsole_1.default.getStyle(ILog_1.LOG_LEVEL.Error)
|
|
38
39
|
}, 'is missing, run', {
|
|
39
40
|
word: `npm i ${packageName}`,
|
|
40
|
-
style:
|
|
41
|
+
style: ColorConsole_1.default.getStyle(ILog_1.LOG_LEVEL.Error)
|
|
41
42
|
}, 'to install it')));
|
|
42
43
|
}
|
|
44
|
+
return require(modulePath);
|
|
45
|
+
}
|
|
46
|
+
static requireModule(path, throwError = false) {
|
|
47
|
+
if (!fs_1.default.existsSync(path)) {
|
|
48
|
+
if (throwError) {
|
|
49
|
+
throw new Error(`${path} is missing`);
|
|
50
|
+
}
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
43
53
|
try {
|
|
44
|
-
|
|
45
|
-
return Promise.resolve(module);
|
|
54
|
+
return require(path);
|
|
46
55
|
}
|
|
47
56
|
catch (error) {
|
|
48
|
-
|
|
57
|
+
if (throwError) {
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* 验证 json 内容
|
|
65
|
+
* @param json json 内容
|
|
66
|
+
* @param schema 验证配置, 详见:https://github.com/ajv-validator/ajv
|
|
67
|
+
*
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```
|
|
71
|
+
* CommonUtil.validateJson({a:123},
|
|
72
|
+
* {
|
|
73
|
+
* a:{
|
|
74
|
+
* type:'number'
|
|
75
|
+
* }
|
|
76
|
+
* }
|
|
77
|
+
* )
|
|
78
|
+
* ```
|
|
79
|
+
* @returns
|
|
80
|
+
*/
|
|
81
|
+
static validateJson(json, schema) {
|
|
82
|
+
if (!json || !schema) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
const ajv = new ajv_1.default({ allErrors: true, allowUnionTypes: true });
|
|
86
|
+
ajv.validate(schema, json);
|
|
87
|
+
if (ajv.errors) {
|
|
88
|
+
const errors = ajv.errors.map((item) => {
|
|
89
|
+
const { message, instancePath, keyword } = item;
|
|
90
|
+
const errorMessage = `${instancePath ? `${instancePath}: ` : ''} ${message}`;
|
|
91
|
+
if (keyword === 'required') {
|
|
92
|
+
return new TypeError(errorMessage);
|
|
93
|
+
}
|
|
94
|
+
return new Error(errorMessage);
|
|
95
|
+
});
|
|
96
|
+
return errors;
|
|
49
97
|
}
|
|
50
98
|
}
|
|
51
99
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["utils/CommonUtil.ts"],"names":[],"mappings":";;;;;AAAA,oDAA2B;AAE3B,4CAAmB;AACnB,gDAAuB;AACvB,
|
|
1
|
+
{"version":3,"sources":["utils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ED,IAAI;YACF,OAAO,OAAO,CAAC,IAAI,CAAC,CAAA;SACrB;QAAC,OAAO,KAAK,EAAE;YACd,IAAI,UAAU,EAAE;gBACd,MAAM,KAAK,CAAA;aACZ;YACD,OAAM;SACP;IACH,CAAC;IAED;;;;;;;;;;;;;;;;;OAiBG;IACH,MAAM,CAAC,YAAY,CAAC,IAAU,EAAE,MAAsB;QACpD,IAAI,CAAC,IAAI,IAAI,CAAC,MAAM,EAAE;YACpB,OAAM;SACP;QAED,MAAM,GAAG,GAAG,IAAI,aAAG,CAAC,EAAE,SAAS,EAAE,IAAI,EAAE,eAAe,EAAE,IAAI,EAAE,CAAC,CAAA;QAC/D,GAAG,CAAC,QAAQ,CAAC,MAAM,EAAE,IAAI,CAAC,CAAA;QAE1B,IAAI,GAAG,CAAC,MAAM,EAAE;YACd,MAAM,MAAM,GAAY,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE;gBAC9C,MAAM,EAAE,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,GAAG,IAAI,CAAA;gBAC/C,MAAM,YAAY,GAAG,GAAG,YAAY,CAAC,CAAC,CAAC,GAAG,YAAY,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,OAAO,EAAE,CAAA;gBAC5E,IAAI,OAAO,KAAK,UAAU,EAAE;oBAC1B,OAAO,IAAI,SAAS,CAAC,YAAY,CAAC,CAAA;iBACnC;gBACD,OAAO,IAAI,KAAK,CAAC,YAAY,CAAC,CAAA;YAChC,CAAC,CAAC,CAAA;YACF,OAAO,MAAM,CAAA;SACd;IACH,CAAC;CACF;AACD,kBAAe,UAAU,CAAA","file":"CommonUtil.js","sourcesContent":["import crypto from 'crypto'\n\nimport Ajv from 'ajv'\nimport { Schema, SomeJTDSchemaType } from 'ajv/dist/core'\nimport Fs from 'fs'\nimport Path from 'path'\nimport ColorConsole from '../ColorConsole'\nimport { LOG_LEVEL } from '../interface/ILog'\n\nexport type JSONSchemaType = Schema & SomeJTDSchemaType\n/**\n * 不好分类的工具函数\n */\nclass CommonUtil {\n /**\n * 根据 buffer 生成 hash 值\n *\n * 用于唯一标识流内容,通常用于文件校验\n * @param buffer\n * @returns hash 值\n */\n static calcDataDigest(buffer: Buffer) {\n const hash = crypto.createHash('SHA256')\n hash.update(buffer)\n return hash.digest()\n }\n\n /**\n * 从开发者项目的 node_module 中加载指定依赖;\n * 如果不存在,提示下载;存在则返回加载的依赖\n * @param packageName\n */\n static requireNodeModule(projectPath: string, packageName: string): Promise<any> {\n const modulePath = Path.resolve(projectPath, `./node_modules/${packageName}`)\n if (!Fs.existsSync(modulePath)) {\n return Promise.reject(\n new Error(\n ColorConsole.createMessage(\n {\n word: packageName,\n style: ColorConsole.getStyle(LOG_LEVEL.Error)\n },\n 'is missing, run',\n {\n word: `npm i ${packageName}`,\n style: ColorConsole.getStyle(LOG_LEVEL.Error)\n },\n 'to install it'\n )\n )\n )\n }\n return require(modulePath)\n }\n\n static requireModule(path: string, throwError = false) {\n if (!Fs.existsSync(path)) {\n if (throwError) {\n throw new Error(`${path} is missing`)\n }\n return\n }\n\n try {\n return require(path)\n } catch (error) {\n if (throwError) {\n throw error\n }\n return\n }\n }\n\n /**\n * 验证 json 内容\n * @param json json 内容\n * @param schema 验证配置, 详见:https://github.com/ajv-validator/ajv\n *\n *\n * @example\n * ```\n * CommonUtil.validateJson({a:123},\n * {\n * a:{\n * type:'number'\n * }\n * }\n * )\n * ```\n * @returns\n */\n static validateJson(json: JSON, schema: JSONSchemaType): Error[] | undefined {\n if (!json || !schema) {\n return\n }\n\n const ajv = new Ajv({ allErrors: true, allowUnionTypes: true })\n ajv.validate(schema, json)\n\n if (ajv.errors) {\n const errors: Error[] = ajv.errors.map((item) => {\n const { message, instancePath, keyword } = item\n const errorMessage = `${instancePath ? `${instancePath}: ` : ''} ${message}`\n if (keyword === 'required') {\n return new TypeError(errorMessage)\n }\n return new Error(errorMessage)\n })\n return errors\n }\n }\n}\nexport default CommonUtil\n"],"sourceRoot":"../../src"}
|
package/lib/utils/FileUtil.js
CHANGED
|
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
7
7
|
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const ColorConsole_1 = __importDefault(require("../ColorConsole"));
|
|
8
9
|
/**
|
|
9
10
|
* FileUtil
|
|
10
11
|
*/
|
|
@@ -144,8 +145,7 @@ class FileUtil {
|
|
|
144
145
|
if (!fs_extra_1.default.existsSync(filePath)) {
|
|
145
146
|
const fileName = path_1.default.basename(filePath);
|
|
146
147
|
const dirPath = path_1.default.dirname(filePath);
|
|
147
|
-
|
|
148
|
-
console.error(`Error: File ${fileName} does not exist in the path:${dirPath}`);
|
|
148
|
+
ColorConsole_1.default.error(`Error: File ${fileName} does not exist in the path:${dirPath}`);
|
|
149
149
|
checkPass = false;
|
|
150
150
|
}
|
|
151
151
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["utils/FileUtil.ts"],"names":[],"mappings":";;;;;AAAA,wDAAyB;AACzB,gDAAuB;
|
|
1
|
+
{"version":3,"sources":["utils/FileUtil.ts"],"names":[],"mappings":";;;;;AAAA,wDAAyB;AACzB,gDAAuB;AACvB,mEAA0C;AAI1C;;GAEG;AACH,MAAM,QAAQ;IACZ;;;;;;;;;;OAUG;IACH,MAAM,CAAC,OAAO,CAAC,IAAY,EAAE,OAAmB,EAAE,OAAmB;QACnE,IAAI,OAAO,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,OAAO,CAAC,EAAE;YACxC,OAAO,KAAK,CAAA;SACb;QAED,IAAI,OAAO,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,OAAO,CAAC,EAAE;YACzC,OAAO,KAAK,CAAA;SACb;QAED,OAAO,IAAI,CAAA;IACb,CAAC;IACD;;;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QAEpB,MAAM,KAAK,GAAG,IAAI,CAAC,cAAc,CAAC,SAAS,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAA;QAChE,KAAK,IAAI,IAAI,IAAI,KAAK,EAAE;YACtB,MAAM,UAAU,GAAG,cAAI,CAAC,IAAI,CAAC,SAAS,EAAE,cAAI,CAAC,QAAQ,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC,CAAA;YACvE,kBAAE,CAAC,QAAQ,CAAC,IAAI,EAAE,UAAU,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAA;SACnD;IACH,CAAC;IACD;;;OAGG;IACH,MAAM,CAAC,aAAa,CAAC,QAAkB;QACrC,IAAI,SAAS,GAAG,IAAI,CAAA;QACpB,QAAQ,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE;YAC5B,IAAI,CAAC,kBAAE,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE;gBAC5B,MAAM,QAAQ,GAAG,cAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAA;gBACxC,MAAM,OAAO,GAAG,cAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAA;gBACtC,sBAAY,CAAC,KAAK,CAAC,eAAe,QAAQ,+BAA+B,OAAO,EAAE,CAAC,CAAA;gBACnF,SAAS,GAAG,KAAK,CAAA;aAClB;QACH,CAAC,CAAC,CAAA;QACF,OAAO,SAAS,CAAA;IAClB,CAAC;CACF;AACD,kBAAe,QAAQ,CAAA","file":"FileUtil.js","sourcesContent":["import Fs from 'fs-extra'\nimport Path from 'path'\nimport ColorConsole from '../ColorConsole'\ntype OneMatchType = string | RegExp | ((path: string) => boolean)\n\nexport type MatchType = OneMatchType | OneMatchType[]\n/**\n * FileUtil\n */\nclass FileUtil {\n /**\n * 判断字符串是否符合包含、不包含的条件\n *\n * 规则为:\n * 1. 如果设置了不包含且匹配,则不包含\n * 2. 否则,如果设置了包含且不匹配,则不包含\n * 3. 否则,包含\n * @param value\n * @param include\n * @param exclude\n */\n static include(path: string, include?: MatchType, exclude?: MatchType) {\n if (exclude && this.match(path, exclude)) {\n return false\n }\n\n if (include && !this.match(path, include)) {\n return false\n }\n\n return true\n }\n /**\n * 判断字符串是否匹配指定规则\n * @param value\n * @param matchType\n * @returns\n */\n static match(value: string, matchType: MatchType) {\n switch (typeof matchType) {\n case 'function':\n return matchType(value)\n case 'string':\n return matchType === value\n case 'object':\n if (matchType instanceof RegExp) {\n return matchType.test(value)\n } else if (matchType instanceof Array) {\n // 递归检查每一项\n let result = matchType.find((item) => this.match(value, item))\n if (result) {\n return true\n }\n }\n return false\n default:\n return false\n }\n }\n /**\n * 获取指定目录所有层级的文件\n * @param dir\n * @returns\n */\n\n static readAlldirSync(dir: string, includes?: MatchType, excludes?: MatchType) {\n const files = Fs.readdirSync(dir, { withFileTypes: true })\n const result: string[] = []\n files.forEach((item) => {\n const itemPath = Path.join(dir, item.name)\n if (item.isDirectory() && item) {\n const children = this.readAlldirSync(itemPath, includes, excludes)\n result.push(...children)\n } else {\n if (this.include(itemPath, includes, excludes)) {\n result.push(itemPath)\n }\n }\n })\n return result\n }\n\n /**\n * 替换文件内容\n *\n * @see replaceKeywords\n * @param filePath\n * @param keywordDic\n */\n static replaceFileContent(filePath: string, keywordDic: Record<string, string>) {\n let content = Fs.readFileSync(filePath, 'utf-8')\n content = this.replaceKeywords(content, keywordDic)\n Fs.writeFileSync(filePath, content)\n }\n\n /**\n * 替换字符串内容\n *\n * @example\n * // 替换 abc{{value}} 为 abc123\n * replaceKeywords(`abc{{value}}`, {value: '123'})\n *\n * @param content\n * @param keywordDic\n * @returns\n */\n static replaceKeywords(content: string, keywordDic: Record<string, string>) {\n return content.replace(/{{(.*?)}}/gm, (origin, key) => {\n key = key.trim()\n return keywordDic[key] !== undefined ? keywordDic[key] : origin\n })\n }\n /**\n * 更新文件路径\n * @param path\n * @param fullName\n * @returns\n */\n static updatePath(path: string, fullName: string) {\n if (Fs.statSync(path).isFile()) {\n return Path.join(Path.dirname(path), fullName)\n }\n return path\n }\n\n /**\n * 复制文件\n * @param sourceDir 源目录\n * @param targetDir 目标目录\n * @param excludes 不包含的文件\n * @param includes 包含的文件\n */\n static copyFiles(\n sourceDir: string,\n targetDir: string,\n excludes?: MatchType,\n includes?: MatchType\n ) {\n const files = this.readAlldirSync(sourceDir, includes, excludes)\n for (let file of files) {\n const targetPath = Path.join(targetDir, Path.relative(sourceDir, file))\n Fs.copySync(file, targetPath, { overwrite: true })\n }\n }\n /**\n * 校验文件\n * @param fileList\n */\n static checkFilePath(fileList: string[]): boolean {\n let checkPass = true\n fileList.forEach((filePath) => {\n if (!Fs.existsSync(filePath)) {\n const fileName = Path.basename(filePath)\n const dirPath = Path.dirname(filePath)\n ColorConsole.error(`Error: File ${fileName} does not exist in the path:${dirPath}`)\n checkPass = false\n }\n })\n return checkPass\n }\n}\nexport default FileUtil\n"],"sourceRoot":"../../src"}
|
|
@@ -6,10 +6,12 @@ declare class StringUtil {
|
|
|
6
6
|
static removeQuotes(str: string): string;
|
|
7
7
|
/**
|
|
8
8
|
* 中划线文本转为驼峰
|
|
9
|
+
*
|
|
10
|
+
* aaa-bbb 转换为 aaaBbb
|
|
9
11
|
* @param value
|
|
10
12
|
* @returns
|
|
11
13
|
*/
|
|
12
|
-
static
|
|
14
|
+
static hyphenedToCamel(value: string): string;
|
|
13
15
|
/** 首字母大写 */
|
|
14
16
|
static capitalizeFirstLetter([first, ...rest]: string): string;
|
|
15
17
|
/** 逗号分割的字符串转数组 */
|
package/lib/utils/StringUtil.js
CHANGED
|
@@ -10,10 +10,12 @@ class StringUtil {
|
|
|
10
10
|
}
|
|
11
11
|
/**
|
|
12
12
|
* 中划线文本转为驼峰
|
|
13
|
+
*
|
|
14
|
+
* aaa-bbb 转换为 aaaBbb
|
|
13
15
|
* @param value
|
|
14
16
|
* @returns
|
|
15
17
|
*/
|
|
16
|
-
static
|
|
18
|
+
static hyphenedToCamel(value) {
|
|
17
19
|
return value === null || value === void 0 ? void 0 : value.replace(/-([a-z])/g, (_, match) => match.toUpperCase());
|
|
18
20
|
}
|
|
19
21
|
/** 首字母大写 */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["utils/StringUtil.ts"],"names":[],"mappings":";;AAAA;;GAEG;AACH,MAAM,UAAU;IACd,6BAA6B;IAC7B,MAAM,CAAC,YAAY,CAAC,GAAW;QAC7B,OAAO,GAAG,CAAC,OAAO,CAAC,kBAAkB,EAAE,EAAE,CAAC,CAAA;IAC5C,CAAC;IAED
|
|
1
|
+
{"version":3,"sources":["utils/StringUtil.ts"],"names":[],"mappings":";;AAAA;;GAEG;AACH,MAAM,UAAU;IACd,6BAA6B;IAC7B,MAAM,CAAC,YAAY,CAAC,GAAW;QAC7B,OAAO,GAAG,CAAC,OAAO,CAAC,kBAAkB,EAAE,EAAE,CAAC,CAAA;IAC5C,CAAC;IAED;;;;;;OAMG;IACH,MAAM,CAAC,eAAe,CAAC,KAAa;QAClC,OAAO,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,OAAO,CAAC,WAAW,EAAE,CAAC,CAAC,EAAE,KAAa,EAAE,EAAE,CAAC,KAAK,CAAC,WAAW,EAAE,CAAC,CAAA;IAC/E,CAAC;IAED,YAAY;IACZ,MAAM,CAAC,qBAAqB,CAAC,CAAC,KAAK,EAAE,GAAG,IAAI,CAAS;QACnD,OAAO,CAAC,KAAK,CAAC,WAAW,EAAE,EAAE,GAAG,IAAI,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;IAChD,CAAC;IAED,kBAAkB;IAClB,MAAM,CAAC,mBAAmB,CAAC,KAAc;QACvC,IAAI,CAAC,KAAK;YAAE,OAAO,EAAE,CAAA;QACrB,OAAO,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IAC1C,CAAC;CACF;AACD,kBAAe,UAAU,CAAA","file":"StringUtil.js","sourcesContent":["/**\n * StringUtil\n */\nclass StringUtil {\n /** 使用正则表达式去除开头和结尾的 ' \" ` */\n static removeQuotes(str: string) {\n return str.replace(/^['\"`]+|['\"`]+$/g, '')\n }\n\n /**\n * 中划线文本转为驼峰\n *\n * aaa-bbb 转换为 aaaBbb\n * @param value\n * @returns\n */\n static hyphenedToCamel(value: string) {\n return value?.replace(/-([a-z])/g, (_, match: string) => match.toUpperCase())\n }\n\n /** 首字母大写 */\n static capitalizeFirstLetter([first, ...rest]: string) {\n return [first.toUpperCase(), ...rest].join('')\n }\n\n /** 逗号分割的字符串转数组 */\n static string2arrayByComma(value?: string) {\n if (!value) return []\n return value.replace(' ', '').split(',')\n }\n}\nexport default StringUtil\n"],"sourceRoot":"../../src"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aiot-toolkit/shared-utils",
|
|
3
|
-
"version": "2.0.1-alpha.
|
|
3
|
+
"version": "2.0.1-alpha.13",
|
|
4
4
|
"description": "The common method functions of the aiot-toolkit.",
|
|
5
5
|
"homepage": "",
|
|
6
6
|
"license": "ISC",
|
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
"test": "node ./__tests__/shared-utils.test.js"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
+
"ajv": "^8.12.0",
|
|
23
24
|
"chalk": "^4.0.0",
|
|
24
25
|
"crypto": "^1.0.1",
|
|
25
26
|
"dayjs": "^1.11.9",
|
|
@@ -29,5 +30,5 @@
|
|
|
29
30
|
"devDependencies": {
|
|
30
31
|
"@types/qrcode-terminal": "^0.12.0"
|
|
31
32
|
},
|
|
32
|
-
"gitHead": "
|
|
33
|
+
"gitHead": "49040b26a267943df4d0b202b200d4df5bcbc1ef"
|
|
33
34
|
}
|
package/lib/ColorConsole2.d.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { LOG_LEVEL } from './interface/ILog';
|
|
2
|
-
interface IStyle {
|
|
3
|
-
color?: string;
|
|
4
|
-
bgColor?: string;
|
|
5
|
-
bold?: boolean;
|
|
6
|
-
italic?: boolean;
|
|
7
|
-
}
|
|
8
|
-
type Message = string | number | boolean | undefined | {
|
|
9
|
-
word: string;
|
|
10
|
-
style?: IStyle;
|
|
11
|
-
};
|
|
12
|
-
/**
|
|
13
|
-
* 带颜色的输出
|
|
14
|
-
*/
|
|
15
|
-
declare class ColorConsole2 {
|
|
16
|
-
static defaultStyle: IStyle;
|
|
17
|
-
static styleDic: {
|
|
18
|
-
[key in LOG_LEVEL]: IStyle;
|
|
19
|
-
};
|
|
20
|
-
static getStyle(logLevel: LOG_LEVEL): IStyle;
|
|
21
|
-
static log(...message: Message[]): void;
|
|
22
|
-
static error(...message: Message[]): void;
|
|
23
|
-
static info(...message: Message[]): void;
|
|
24
|
-
static success(...message: Message[]): void;
|
|
25
|
-
static throw(...message: Message[]): void;
|
|
26
|
-
static warn(...message: Message[]): void;
|
|
27
|
-
/**
|
|
28
|
-
* 创建复杂格式的消息,可单独设置词语样式
|
|
29
|
-
*
|
|
30
|
-
* @example
|
|
31
|
-
* ```js
|
|
32
|
-
* createMessage("my name is", {message:'****', style:{ color:'red' }})
|
|
33
|
-
* ```
|
|
34
|
-
* @param words
|
|
35
|
-
* @returns
|
|
36
|
-
*/
|
|
37
|
-
static createMessage(...words: Message[]): string;
|
|
38
|
-
private static _log;
|
|
39
|
-
private static formatWord;
|
|
40
|
-
}
|
|
41
|
-
export default ColorConsole2;
|
package/lib/ColorConsole2.js
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
var _a;
|
|
6
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
const chalk_1 = __importDefault(require("chalk"));
|
|
8
|
-
const dayjs_1 = __importDefault(require("dayjs"));
|
|
9
|
-
const ILog_1 = require("./interface/ILog");
|
|
10
|
-
/**
|
|
11
|
-
* 带颜色的输出
|
|
12
|
-
*/
|
|
13
|
-
class ColorConsole2 {
|
|
14
|
-
static getStyle(logLevel) {
|
|
15
|
-
return this.styleDic[logLevel] || this.defaultStyle;
|
|
16
|
-
}
|
|
17
|
-
static log(...message) {
|
|
18
|
-
return this._log(ILog_1.LOG_LEVEL.Log, ...message);
|
|
19
|
-
}
|
|
20
|
-
static error(...message) {
|
|
21
|
-
return this._log(ILog_1.LOG_LEVEL.Error, ...message);
|
|
22
|
-
}
|
|
23
|
-
static info(...message) {
|
|
24
|
-
return this._log(ILog_1.LOG_LEVEL.Info, ...message);
|
|
25
|
-
}
|
|
26
|
-
static success(...message) {
|
|
27
|
-
return this._log(ILog_1.LOG_LEVEL.Success, ...message);
|
|
28
|
-
}
|
|
29
|
-
static throw(...message) {
|
|
30
|
-
this._log(ILog_1.LOG_LEVEL.Throw, ...message);
|
|
31
|
-
process.exit();
|
|
32
|
-
}
|
|
33
|
-
static warn(...message) {
|
|
34
|
-
return this._log(ILog_1.LOG_LEVEL.Warn, ...message);
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* 创建复杂格式的消息,可单独设置词语样式
|
|
38
|
-
*
|
|
39
|
-
* @example
|
|
40
|
-
* ```js
|
|
41
|
-
* createMessage("my name is", {message:'****', style:{ color:'red' }})
|
|
42
|
-
* ```
|
|
43
|
-
* @param words
|
|
44
|
-
* @returns
|
|
45
|
-
*/
|
|
46
|
-
static createMessage(...words) {
|
|
47
|
-
return words
|
|
48
|
-
.map((item) => {
|
|
49
|
-
if (typeof item === 'object') {
|
|
50
|
-
return this.formatWord(item.word, item.style);
|
|
51
|
-
}
|
|
52
|
-
return this.formatWord(item, this.defaultStyle);
|
|
53
|
-
})
|
|
54
|
-
.join(' ');
|
|
55
|
-
}
|
|
56
|
-
static _log(logLevel, ...message) {
|
|
57
|
-
const style = this.styleDic[logLevel] || this.defaultStyle;
|
|
58
|
-
const time = this.formatWord((0, dayjs_1.default)().format('YYYY-MM-DD HH:mm:ss'), style);
|
|
59
|
-
console.log(`[${time}]: `, this.createMessage(...message));
|
|
60
|
-
}
|
|
61
|
-
static formatWord(word, style) {
|
|
62
|
-
if (word === undefined || word === null) {
|
|
63
|
-
return '';
|
|
64
|
-
}
|
|
65
|
-
const { color, bgColor, bold, italic } = style || this.defaultStyle;
|
|
66
|
-
let result = word;
|
|
67
|
-
if (color) {
|
|
68
|
-
result = chalk_1.default.hex(color)(result);
|
|
69
|
-
}
|
|
70
|
-
if (bgColor) {
|
|
71
|
-
result = chalk_1.default.bgHex(bgColor)(result);
|
|
72
|
-
}
|
|
73
|
-
if (bold) {
|
|
74
|
-
result = chalk_1.default.bold(result);
|
|
75
|
-
}
|
|
76
|
-
if (italic) {
|
|
77
|
-
result = chalk_1.default.italic(result);
|
|
78
|
-
}
|
|
79
|
-
return result;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
_a = ColorConsole2;
|
|
83
|
-
ColorConsole2.defaultStyle = {
|
|
84
|
-
color: 'black'
|
|
85
|
-
};
|
|
86
|
-
ColorConsole2.styleDic = {
|
|
87
|
-
[ILog_1.LOG_LEVEL.Success]: {
|
|
88
|
-
color: '#008000'
|
|
89
|
-
},
|
|
90
|
-
[ILog_1.LOG_LEVEL.Info]: {
|
|
91
|
-
color: '#03A9F4'
|
|
92
|
-
},
|
|
93
|
-
[ILog_1.LOG_LEVEL.Warn]: {
|
|
94
|
-
color: '#FF9800'
|
|
95
|
-
},
|
|
96
|
-
[ILog_1.LOG_LEVEL.Error]: {
|
|
97
|
-
color: '#ff0000'
|
|
98
|
-
},
|
|
99
|
-
[ILog_1.LOG_LEVEL.Throw]: {
|
|
100
|
-
color: '#ff0000',
|
|
101
|
-
bold: true
|
|
102
|
-
},
|
|
103
|
-
[ILog_1.LOG_LEVEL.Log]: _a.defaultStyle
|
|
104
|
-
};
|
|
105
|
-
exports.default = ColorConsole2;
|
|
106
|
-
|
|
107
|
-
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,KAAK,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,CAAA;SACtC;QAED,IAAI,IAAI,EAAE;YACR,MAAM,GAAG,eAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;SAC5B;QAED,IAAI,MAAM,EAAE;YACV,MAAM,GAAG,eAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAA;SAC9B;QACD,OAAO,MAAM,CAAA;IACf,CAAC;;;AAlGM,0BAAY,GAAW;IAC5B,KAAK,EAAE,OAAO;CADG,AAElB,CAAA;AACM,sBAAQ,GAEX;IACF,CAAC,gBAAS,CAAC,OAAO,CAAC,EAAE;QACnB,KAAK,EAAE,SAAS;KACjB;IACD,CAAC,gBAAS,CAAC,IAAI,CAAC,EAAE;QAChB,KAAK,EAAE,SAAS;KACjB;IACD,CAAC,gBAAS,CAAC,IAAI,CAAC,EAAE;QAChB,KAAK,EAAE,SAAS;KACjB;IACD,CAAC,gBAAS,CAAC,KAAK,CAAC,EAAE;QACjB,KAAK,EAAE,SAAS;KACjB;IACD,CAAC,gBAAS,CAAC,KAAK,CAAC,EAAE;QACjB,KAAK,EAAE,SAAS;QAChB,IAAI,EAAE,IAAI;KACX;IACD,CAAC,gBAAS,CAAC,GAAG,CAAC,EAAE,EAAI,CAAC,YAAY;CAnBrB,AAoBd,CAAA;AA6EH,kBAAe,aAAa,CAAA","file":"ColorConsole2.js","sourcesContent":["import chalk from 'chalk'\nimport dayjs from 'dayjs'\nimport { LOG_LEVEL } from './interface/ILog'\n\ninterface IStyle {\n color?: string\n bgColor?: string\n bold?: boolean\n italic?: boolean\n}\n\ntype Message = string | number | boolean | undefined | { word: string; style?: IStyle }\n/**\n * 带颜色的输出\n */\nclass ColorConsole2 {\n static defaultStyle: IStyle = {\n color: 'black'\n }\n static styleDic: {\n [key in LOG_LEVEL]: IStyle\n } = {\n [LOG_LEVEL.Success]: {\n color: '#008000'\n },\n [LOG_LEVEL.Info]: {\n color: '#03A9F4'\n },\n [LOG_LEVEL.Warn]: {\n color: '#FF9800'\n },\n [LOG_LEVEL.Error]: {\n color: '#ff0000'\n },\n [LOG_LEVEL.Throw]: {\n color: '#ff0000',\n bold: true\n },\n [LOG_LEVEL.Log]: this.defaultStyle\n }\n\n static getStyle(logLevel: LOG_LEVEL): IStyle {\n return this.styleDic[logLevel] || this.defaultStyle\n }\n\n static log(...message: Message[]) {\n return this._log(LOG_LEVEL.Log, ...message)\n }\n static error(...message: Message[]) {\n return this._log(LOG_LEVEL.Error, ...message)\n }\n static info(...message: Message[]) {\n return this._log(LOG_LEVEL.Info, ...message)\n }\n static success(...message: Message[]) {\n return this._log(LOG_LEVEL.Success, ...message)\n }\n static throw(...message: Message[]) {\n this._log(LOG_LEVEL.Throw, ...message)\n process.exit()\n }\n static warn(...message: Message[]) {\n return this._log(LOG_LEVEL.Warn, ...message)\n }\n\n /**\n * 创建复杂格式的消息,可单独设置词语样式\n *\n * @example\n * ```js\n * createMessage(\"my name is\", {message:'****', style:{ color:'red' }})\n * ```\n * @param words\n * @returns\n */\n public static createMessage(...words: Message[]) {\n return words\n .map((item) => {\n if (typeof item === 'object') {\n return this.formatWord(item.word, item.style)\n }\n return this.formatWord(item, this.defaultStyle)\n })\n .join(' ')\n }\n\n private static _log(logLevel: LOG_LEVEL, ...message: Message[]) {\n const style = this.styleDic[logLevel] || this.defaultStyle\n const time = this.formatWord(dayjs().format('YYYY-MM-DD HH:mm:ss'), style)\n console.log(`[${time}]: `, this.createMessage(...message))\n }\n\n private static formatWord(word?: string | number | boolean, style?: IStyle) {\n if (word === undefined || word === null) {\n return ''\n }\n const { color, bgColor, bold, italic } = style || this.defaultStyle\n\n let result = word\n if (color) {\n result = chalk.hex(color)(result)\n }\n if (bgColor) {\n result = chalk.bgHex(bgColor)(result)\n }\n\n if (bold) {\n result = chalk.bold(result)\n }\n\n if (italic) {\n result = chalk.italic(result)\n }\n return result\n }\n}\nexport default ColorConsole2\n"],"sourceRoot":"../src"}
|