@aiot-toolkit/shared-utils 2.0.1-alpha.9 → 2.0.2-dev.1

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.
@@ -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
+ export type StyleMessage = 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: StyleMessage[]): void;
23
+ static error(...message: StyleMessage[]): void;
24
+ static info(...message: StyleMessage[]): void;
25
+ static success(...message: StyleMessage[]): void;
26
+ static throw(...message: StyleMessage[]): void;
27
+ static warn(...message: StyleMessage[]): 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: StyleMessage | StyleMessage[]): 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 log(log: ILog): string;
46
+ static logger(log: ILog): void;
12
47
  }
13
48
  export default ColorConsole;
@@ -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,36 +11,126 @@ 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
+ }
32
+ static warn(...message) {
33
+ return this._log(ILog_1.LOG_LEVEL.Warn, ...message);
34
+ }
35
+ /**
36
+ * 创建复杂格式的消息,可单独设置词语样式
37
+ *
38
+ * @example
39
+ * ```js
40
+ * createMessage("my name is", {message:'****', style:{ color:'red' }})
41
+ * ```
42
+ * @param words
43
+ * @returns
44
+ */
45
+ static createMessage(words) {
46
+ const list = Array.isArray(words) ? words : [words];
47
+ return list
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.map((item) => {
61
+ if (typeof item === 'object') {
62
+ return Object.assign(Object.assign({}, item), { style: item.style || style });
63
+ }
64
+ return item;
65
+ })));
66
+ }
67
+ static formatWord(word, style) {
68
+ if (word === undefined || word === null) {
69
+ return '';
70
+ }
71
+ const { color, bgColor, bold, italic } = style || this.defaultStyle;
72
+ let result = word;
73
+ if (color) {
74
+ result = chalk_1.default.hex(color)(result);
75
+ }
76
+ if (bgColor) {
77
+ result = chalk_1.default.bgHex(bgColor)(result);
78
+ }
79
+ if (bold) {
80
+ result = chalk_1.default.bold(result);
81
+ }
82
+ if (italic) {
83
+ result = chalk_1.default.italic(result);
84
+ }
85
+ return result;
86
+ }
13
87
  /**
14
88
  * 按照日志类型打印不同颜色的日志
15
89
  * @param log 默认日志类型为info
16
90
  * @returns 返回打印的日志信息
17
91
  */
18
- static log(log) {
19
- const time = (0, dayjs_1.default)().format('YYYY-MM-DD HH:mm:ss');
20
- const { message, position, level = ILog_1.LOG_LEVEL.Info, isOnlyPrintError = false } = log;
21
- let result = position ? `${message}\n${JSON.stringify(position)}` : `${message}`;
22
- switch (level) {
23
- case ILog_1.LOG_LEVEL.Success:
24
- case ILog_1.LOG_LEVEL.Info:
25
- console.log(`${chalk_1.default.green.bold(`[${time} ${ILog_1.LOG_LEVEL[level]}]`)}: ${result}`);
26
- break;
27
- case ILog_1.LOG_LEVEL.Warn:
28
- console.log(`${chalk_1.default.yellow.bold(`[${time} ${ILog_1.LOG_LEVEL[level]}]`)}: ${result}`);
29
- break;
30
- case ILog_1.LOG_LEVEL.Error:
31
- case ILog_1.LOG_LEVEL.Throw:
32
- result = message;
33
- if (isOnlyPrintError) {
34
- console.log(`${chalk_1.default.red.bold(`[${time} ${ILog_1.LOG_LEVEL[level]}]`)}: ${result}`);
35
- }
36
- else {
37
- throw new Error(result);
38
- }
92
+ static logger(log) {
93
+ const { message, position, filePath, level = ILog_1.LOG_LEVEL.Info } = log;
94
+ let positionMessage;
95
+ positionMessage = {
96
+ style: this.getStyle(ILog_1.LOG_LEVEL.Info),
97
+ word: [filePath, position === null || position === void 0 ? void 0 : position.startLine, position === null || position === void 0 ? void 0 : position.startColumn]
98
+ .filter((item) => Boolean(item))
99
+ .join(':')
100
+ };
101
+ const logs = Array.isArray(message) ? message : [message];
102
+ if (positionMessage) {
103
+ logs.unshift(positionMessage);
39
104
  }
40
- return result;
105
+ this._log(level, ...logs);
41
106
  }
42
107
  }
108
+ _a = ColorConsole;
109
+ ColorConsole.defaultStyle = {
110
+ color: '#cccccc'
111
+ };
112
+ ColorConsole.styleDic = {
113
+ [ILog_1.LOG_LEVEL.Success]: {
114
+ color: '#008000',
115
+ icon: '✅'
116
+ },
117
+ [ILog_1.LOG_LEVEL.Info]: {
118
+ color: '#03A9F4',
119
+ icon: 'ℹ️ '
120
+ },
121
+ [ILog_1.LOG_LEVEL.Warn]: {
122
+ color: '#FF9800',
123
+ icon: '🟠'
124
+ },
125
+ [ILog_1.LOG_LEVEL.Error]: {
126
+ color: '#ff0000',
127
+ icon: '❌'
128
+ },
129
+ [ILog_1.LOG_LEVEL.Throw]: {
130
+ color: '#ff0000',
131
+ bold: true,
132
+ icon: '❌'
133
+ },
134
+ [ILog_1.LOG_LEVEL.Log]: _a.defaultStyle
135
+ };
43
136
  exports.default = ColorConsole;
44
-
45
- //# sourceMappingURL=ColorConsole.js.map
@@ -8,5 +8,3 @@ class MapData {
8
8
  }
9
9
  }
10
10
  exports.default = MapData;
11
-
12
- //# sourceMappingURL=MapData.js.map
@@ -10,5 +10,3 @@ class PositionError extends Error {
10
10
  }
11
11
  }
12
12
  exports.default = PositionError;
13
-
14
- //# sourceMappingURL=PositionError.js.map
package/lib/index.js CHANGED
@@ -15,11 +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;
24
-
25
- //# sourceMappingURL=index.js.map
@@ -1,3 +1,4 @@
1
+ import { StyleMessage } from '../ColorConsole';
1
2
  import IPosition from './IPosition';
2
3
  export declare enum LOG_LEVEL {
3
4
  Log = 0,
@@ -12,8 +13,8 @@ export declare enum LOG_LEVEL {
12
13
  */
13
14
  export interface ILog {
14
15
  level?: LOG_LEVEL;
15
- message: string;
16
+ message: StyleMessage[] | StyleMessage;
16
17
  position?: IPosition;
17
- isOnlyPrintError?: boolean;
18
+ filePath?: string;
18
19
  }
19
20
  export type Logger = (log: ILog) => void;
@@ -11,5 +11,3 @@ var LOG_LEVEL;
11
11
  LOG_LEVEL[LOG_LEVEL["Error"] = 4] = "Error";
12
12
  LOG_LEVEL[LOG_LEVEL["Throw"] = 5] = "Throw";
13
13
  })(LOG_LEVEL || (exports.LOG_LEVEL = LOG_LEVEL = {}));
14
-
15
- //# sourceMappingURL=ILog.js.map
@@ -1,4 +1,2 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
-
4
- //# sourceMappingURL=IPosition.js.map
package/lib/type/Type.js CHANGED
@@ -1,4 +1,2 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
-
4
- //# sourceMappingURL=Type.js.map
@@ -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 requireModule(projectPath: string, packageName: string): Promise<any>;
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;
@@ -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 ColorConsole2_1 = __importDefault(require("../ColorConsole2"));
10
+ const ColorConsole_1 = __importDefault(require("../ColorConsole"));
10
11
  const ILog_1 = require("../interface/ILog");
11
12
  /**
12
13
  * 不好分类的工具函数
@@ -29,26 +30,76 @@ class CommonUtil {
29
30
  * 如果不存在,提示下载;存在则返回加载的依赖
30
31
  * @param packageName
31
32
  */
32
- static requireModule(projectPath, packageName) {
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(ColorConsole2_1.default.createMessage({
36
- word: packageName,
37
- style: ColorConsole2_1.default.getStyle(ILog_1.LOG_LEVEL.Error)
38
- }, 'is missing, run', {
39
- word: `npm i ${packageName}`,
40
- style: ColorConsole2_1.default.getStyle(ILog_1.LOG_LEVEL.Error)
41
- }, 'to install it')));
36
+ return Promise.reject(new Error(ColorConsole_1.default.createMessage([
37
+ {
38
+ word: packageName,
39
+ style: ColorConsole_1.default.getStyle(ILog_1.LOG_LEVEL.Error)
40
+ },
41
+ 'is missing, run',
42
+ {
43
+ word: `npm i ${packageName}`,
44
+ style: ColorConsole_1.default.getStyle(ILog_1.LOG_LEVEL.Error)
45
+ },
46
+ 'to install it'
47
+ ])));
48
+ }
49
+ return require(modulePath);
50
+ }
51
+ static requireModule(path, throwError = false) {
52
+ if (!fs_1.default.existsSync(path)) {
53
+ if (throwError) {
54
+ throw new Error(`${path} is missing`);
55
+ }
56
+ return;
42
57
  }
43
58
  try {
44
- const module = require(modulePath);
45
- return Promise.resolve(module);
59
+ return require(path);
46
60
  }
47
61
  catch (error) {
48
- return Promise.reject(error);
62
+ if (throwError) {
63
+ throw error;
64
+ }
65
+ return;
66
+ }
67
+ }
68
+ /**
69
+ * 验证 json 内容
70
+ * @param json json 内容
71
+ * @param schema 验证配置, 详见:https://github.com/ajv-validator/ajv
72
+ *
73
+ *
74
+ * @example
75
+ * ```
76
+ * CommonUtil.validateJson({a:123},
77
+ * {
78
+ * a:{
79
+ * type:'number'
80
+ * }
81
+ * }
82
+ * )
83
+ * ```
84
+ * @returns
85
+ */
86
+ static validateJson(json, schema) {
87
+ if (!json || !schema) {
88
+ return;
89
+ }
90
+ const ajv = new ajv_1.default({ allErrors: true, allowUnionTypes: true });
91
+ ajv.validate(schema, json);
92
+ if (ajv.errors) {
93
+ const errors = ajv.errors.map((item) => {
94
+ const { message, instancePath, keyword } = item;
95
+ const errorMessage = `${instancePath ? `${instancePath}: ` : ''} ${message}`;
96
+ if (keyword === 'required') {
97
+ return new TypeError(errorMessage);
98
+ }
99
+ return new Error(errorMessage);
100
+ });
101
+ return errors;
49
102
  }
50
103
  }
51
104
  }
52
105
  exports.default = CommonUtil;
53
-
54
- //# sourceMappingURL=CommonUtil.js.map
@@ -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
  */
@@ -103,9 +104,9 @@ class FileUtil {
103
104
  * @returns
104
105
  */
105
106
  static replaceKeywords(content, keywordDic) {
106
- return content.replace(/{{(.*?)}}/gm, (_, key) => {
107
+ return content.replace(/{{(.*?)}}/gm, (origin, key) => {
107
108
  key = key.trim();
108
- return keywordDic[key] !== undefined ? keywordDic[key] : key;
109
+ return keywordDic[key] !== undefined ? keywordDic[key] : origin;
109
110
  });
110
111
  }
111
112
  /**
@@ -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
  });
@@ -153,5 +153,3 @@ class FileUtil {
153
153
  }
154
154
  }
155
155
  exports.default = FileUtil;
156
-
157
- //# sourceMappingURL=FileUtil.js.map
@@ -50,5 +50,3 @@ class NetworkUtil {
50
50
  }
51
51
  }
52
52
  exports.default = NetworkUtil;
53
-
54
- //# sourceMappingURL=NetworkUtil.js.map
@@ -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 middleLinetoCame(value: string): string;
14
+ static hyphenedToCamel(value: string): string;
13
15
  /** 首字母大写 */
14
16
  static capitalizeFirstLetter([first, ...rest]: string): string;
15
17
  /** 逗号分割的字符串转数组 */
@@ -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 middleLinetoCame(value) {
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
  /** 首字母大写 */
@@ -28,5 +30,3 @@ class StringUtil {
28
30
  }
29
31
  }
30
32
  exports.default = StringUtil;
31
-
32
- //# sourceMappingURL=StringUtil.js.map
@@ -3,6 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ const ColorConsole_1 = __importDefault(require("../ColorConsole"));
6
7
  const PositionError_1 = __importDefault(require("../data/PositionError"));
7
8
  const ILog_1 = require("../interface/ILog");
8
9
  /**
@@ -22,13 +23,11 @@ class WrapCallback {
22
23
  const { level, message, position } = log;
23
24
  if (level === ILog_1.LOG_LEVEL.Error || level === ILog_1.LOG_LEVEL.Throw) {
24
25
  if (position) {
25
- throw new PositionError_1.default(position, message);
26
+ throw new PositionError_1.default(position, ColorConsole_1.default.createMessage(message));
26
27
  }
27
- throw new Error(message);
28
+ throw new Error(ColorConsole_1.default.createMessage(message));
28
29
  }
29
30
  };
30
31
  }
31
32
  }
32
33
  exports.default = WrapCallback;
33
-
34
- //# sourceMappingURL=WrapCallback.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiot-toolkit/shared-utils",
3
- "version": "2.0.1-alpha.9",
3
+ "version": "2.0.2-dev.1",
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": "d593999b9ca345bce8b3ba40a7d11f6685ac03b5"
33
+ "gitHead": "0beecc786c41b73ab2dabeaad06fb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AAC,IAAI,IAAI,IAAI,gBAAS,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,MAAM,EAAE,CAAC,CAAA;iBAC7E;qBAAM;oBACL,MAAM,IAAI,KAAK,CAAC,MAAM,CAAC,CAAA;iBACxB;SACJ;QACD,OAAO,MAAM,CAAA;IACf,CAAC;CACF;AACD,kBAAe,YAAY,CAAA","file":"ColorConsole.js","sourcesContent":["import chalk from 'chalk'\nimport dayjs from 'dayjs'\nimport { ILog, LOG_LEVEL } from './interface/ILog'\n/**\n * Log\n */\nclass ColorConsole {\n /**\n * 按照日志类型打印不同颜色的日志\n * @param log 默认日志类型为info\n * @returns 返回打印的日志信息\n */\n static log(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"}
@@ -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;
@@ -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
- //# sourceMappingURL=ColorConsole2.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["ColorConsole2.ts"],"names":[],"mappings":";;;;;;AAAA,kDAAyB;AACzB,kDAAyB;AACzB,2CAA4C;AAU5C;;GAEG;AACH,MAAM,aAAa;IA0BjB,MAAM,CAAC,QAAQ,CAAC,QAAmB;QACjC,OAAO,IAAI,CAA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M,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"}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["data/MapData.ts"],"names":[],"mappings":";;AAEA,MAAM,OAAO;IAKX,YAAY,IAAY,EAAE,MAAiB,EAAE,MAAiB;QAC5D,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QAChB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;QACpB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;IACtB,CAAC;CACF;AAED,kBAAe,OAAO,CAAA","file":"MapData.js","sourcesContent":["import IPosition from '../interface/IPosition'\n\nclass MapData {\n public readonly source: IPosition\n public readonly target: IPosition\n public readonly name: string\n\n constructor(name: string, source: IPosition, target: IPosition) {\n this.name = name\n this.source = source\n this.target = target\n }\n}\n\nexport default MapData\n"],"sourceRoot":"../../src"}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["data/PositionError.ts"],"names":[],"mappings":";;AAEA;;GAEG;AACH,MAAM,aAAc,SAAQ,KAAK;IAC/B,YACS,QAAoB,EAC3B,OAAgB;QAEhB,KAAK,CAAC,OAAO,CAAC,CAAA;QAHP,aAAQ,GAAR,QAAQ,CAAY;IAI7B,CAAC;CACF;AACD,kBAAe,aAAa,CAAA","file":"PositionError.js","sourcesContent":["import IPosition from '../interface/IPosition'\n\n/**\n * PositionError\n */\nclass PositionError extends Error {\n constructor(\n public position?: IPosition,\n message?: string\n ) {\n super(message)\n }\n}\nexport default PositionError\n"],"sourceRoot":"../../src"}
package/lib/index.js.map DELETED
@@ -1 +0,0 @@
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;QACf,OAAO,EAAE,uCAAuC,IAAI,EAAE;KACvD,CAAC,CAAA;IACF,yBAAU,CAAC,QAAQ,CAAC,IAAI,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAA;AAC5C,CAAC;AARC,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({\n message: `\\nGenerate QR code for HTTP server: ${text}`\n })\n qrTerminal.generate(text, { small: true })\n}\n"],"sourceRoot":"../src"}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["interface/ILog.ts"],"names":[],"mappings":";;;AAEA,OAAO;AACP,IAAY,SAOX;AAPD,WAAY,SAAS;IACnB,uCAAG,CAAA;IACH,+CAAO,CAAA;IACP,yCAAI,CAAA;IACJ,yCAAI,CAAA;IACJ,2CAAK,CAAA;IACL,2CAAK,CAAA;AACP,CAAC,EAPW,SAAS,yBAAT,SAAS,QAOpB","file":"ILog.js","sourcesContent":["import IPosition from './IPosition'\n\n// 日志类别\nexport enum LOG_LEVEL {\n Log,\n Success,\n Info,\n Warn,\n Error,\n Throw\n}\n/**\n * ILog\n */\nexport interface ILog {\n level?: LOG_LEVEL\n message: string\n position?: IPosition\n isOnlyPrintError?: boolean\n}\nexport type Logger = (log: ILog) => void\n"],"sourceRoot":"../../src"}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["interface/IPosition.ts"],"names":[],"mappings":"","file":"IPosition.js","sourcesContent":["/**\n * 节点位置数据结构\n */\ninterface IPosition {\n /**\n * 起始位置\n */\n pos: number\n /**\n * 结束位置\n */\n end: number\n\n /**\n * 起始行\n */\n startLine: number\n\n /**\n * 起始列\n */\n startColumn: number\n\n /**\n * 结束行\n */\n endLine: number\n\n /**\n * 结束列\n */\n endColumn: number\n}\n\nexport default IPosition\n"],"sourceRoot":"../../src"}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["type/Type.ts"],"names":[],"mappings":"","file":"Type.js","sourcesContent":["type Dictionary<T = any> = { [key: string | number]: T }\n\nexport { Dictionary }\n"],"sourceRoot":"../../src"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.js","sourcesContent":["import crypto from 'crypto'\n\nimport Fs from 'fs'\nimport Path from 'path'\nimport ColorConsole2 from '../ColorConsole2'\nimport { LOG_LEVEL } from '../interface/ILog'\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 requireModule(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 ColorConsole2.createMessage(\n {\n word: packageName,\n style: ColorConsole2.getStyle(LOG_LEVEL.Error)\n },\n 'is missing, run',\n {\n word: `npm i ${packageName}`,\n style: ColorConsole2.getStyle(LOG_LEVEL.Error)\n },\n 'to install it'\n )\n )\n )\n }\n try {\n const module = require(modulePath)\n return Promise.resolve(module)\n } catch (error) {\n return Promise.reject(error)\n }\n }\n}\nexport default CommonUtil\n"],"sourceRoot":"../../src"}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["utils/FileUtil.ts"],"names":[],"mappings":";;;;;AAAA,wDAAyB;AACzB,gDAAuB;AAIvB;;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,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AAiB,EACjB,QAAoB,EACpB,QAAoB;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,mBAAmB;gBACnB,OAAO,CAAC,KAAK,CAAC,eAAe,QAAQ,+BAA+B,OAAO,EAAE,CAAC,CAAA;gBAC9E,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'\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, (_, key) => {\n key = key.trim()\n return keywordDic[key] !== undefined ? keywordDic[key] : key\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 // 此处需要报错,但是不需要结束程序\n console.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"}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["utils/NetworkUtil.ts"],"names":[],"mappings":";;;;;AAAA,8DAAgC;AAChC,gEAAsC;AACtC,4DAAkC;AAElC;;GAEG;AACH,MAAM,WAAW;IACf;;;;;;OAMG;IACH,MAAM,CAAC,QAAQ,CAAC,GAAW,EAAE,SAAiB,EAAE,YAAY,GAAG,IAAI;QACjE,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACrC,IAAI,GAA0B,CAAA;YAC9B,IAAI,YAAY,EAAE;gBAChB,GAAG,GAAG,IAAI,sBAAW,CAAC,SAAS,CAC7B;oBACE,MAAM,EAAE,2BAA2B,qBAAM,CAAC,KAAK,CAAC,OAAO,CAAC,mBAAmB;iBAC5E,EACD,sBAAW,CAAC,OAAO,CAAC,cAAc,CACnC,CAAA;gBACD,GAAG,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,CAAC,CAAA;aAClB;YAED,IAAA,oBAAS,EAAC;gBACR,QAAQ,EAAE,CAAC,IAAI,EAAE,EAAE;oBACjB,IAAI,GAAG,EAAE;wBACP,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;wBACxB,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAA;qBAC3B;gBACH,CAAC;aACF,CAAC;iBACC,KAAK,CAAC,GAAG,EAAE,SAAS,CAAC;iBACrB,IAAI,CAAC,GAAG,EAAE;gBACT,OAAO,EAAE,CAAA;YACX,CAAC,CAAC;iBACD,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;gBACf,MAAM,CAAC,KAAK,CAAC,CAAA;YACf,CAAC,CAAC;iBACD,OAAO,CAAC,GAAG,EAAE;gBACZ,IAAI,GAAG,EAAE;oBACP,GAAG,CAAC,IAAI,EAAE,CAAA;iBACX;YACH,CAAC,CAAC,CAAA;QACN,CAAC,CAAC,CAAA;IACJ,CAAC;CACF;AACD,kBAAe,WAAW,CAAA","file":"NetworkUtil.js","sourcesContent":["import Colors from 'ansi-colors'\nimport CliProgress from 'cli-progress'\nimport SimpleGit from 'simple-git'\n\n/**\n * NetworkUtil\n */\nclass NetworkUtil {\n /**\n * clone git 项目\n * @param url\n * @param localPath\n * @param showProgress 是否显示进度条,默认为 true,会在命令行工具中显示绿色进度条\n * @returns\n */\n static gitClone(url: string, localPath: string, showProgress = true): Promise<void> {\n return new Promise((resolve, reject) => {\n let bar: CliProgress.SingleBar\n if (showProgress) {\n bar = new CliProgress.SingleBar(\n {\n format: `progress: {percentage}% ${Colors.green(`{bar}`)} {value}/{total}`\n },\n CliProgress.Presets.shades_classic\n )\n bar.start(100, 0)\n }\n\n SimpleGit({\n progress: (data) => {\n if (bar) {\n bar.setTotal(data.total)\n bar.update(data.processed)\n }\n }\n })\n .clone(url, localPath)\n .then(() => {\n resolve()\n })\n .catch((error) => {\n reject(error)\n })\n .finally(() => {\n if (bar) {\n bar.stop()\n }\n })\n })\n }\n}\nexport default NetworkUtil\n"],"sourceRoot":"../../src"}
@@ -1 +0,0 @@
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;;;;OAIG;IACH,MAAM,CAAC,gBAAgB,CAAC,KAAa;QACnC,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 * @param value\n * @returns\n */\n static middleLinetoCame(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"}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["utils/WrapCallback.ts"],"names":[],"mappings":";;;;;AAAA,0EAAiD;AACjD,4CAA2D;AAE3D;;GAEG;AACH,MAAM,YAAY;IAChB;;;;OAIG;IACH,MAAM,CAAC,SAAS,CAAC,KAAa;QAC5B,OAAO,CAAC,GAAS,EAAE,EAAE;YACnB,cAAc;YACd,KAAK,CAAC,GAAG,CAAC,CAAA;YACV,wBAAwB;YACxB,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,GAAG,GAAG,CAAA;YACxC,IAAI,KAAK,KAAK,gBAAS,CAAC,KAAK,IAAI,KAAK,KAAK,gBAAS,CAAC,KAAK,EAAE;gBAC1D,IAAI,QAAQ,EAAE;oBACZ,MAAM,IAAI,uBAAa,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAA;iBAC3C;gBACD,MAAM,IAAI,KAAK,CAAC,OAAO,CAAC,CAAA;aACzB;QACH,CAAC,CAAA;IACH,CAAC;CACF;AACD,kBAAe,YAAY,CAAA","file":"WrapCallback.js","sourcesContent":["import PositionError from '../data/PositionError'\nimport { ILog, LOG_LEVEL, Logger } from '../interface/ILog'\n\n/**\n * 包装回调函数\n */\nclass WrapCallback {\n /**\n * 包装日志回调函数\n * @param onLog\n * @returns\n */\n static wrapOnLog(onLog: Logger): Logger {\n return (log: ILog) => {\n // 1. 传递给外部log\n onLog(log)\n // 2. 对throw和error进行错误抛出\n const { level, message, position } = log\n if (level === LOG_LEVEL.Error || level === LOG_LEVEL.Throw) {\n if (position) {\n throw new PositionError(position, message)\n }\n throw new Error(message)\n }\n }\n }\n}\nexport default WrapCallback\n"],"sourceRoot":"../../src"}