@aiot-toolkit/shared-utils 2.0.3-beta.6 → 2.0.3-beta.8
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.js +208 -169
- package/lib/data/MapData.js +11 -7
- package/lib/data/PositionError.js +10 -6
- package/lib/enum/ProjectType.js +30 -39
- package/lib/index.js +134 -25
- package/lib/interface/ILog.js +16 -10
- package/lib/interface/IPosition.js +4 -1
- package/lib/interface/IRouter.js +4 -1
- package/lib/type/Type.js +4 -1
- package/lib/utils/CommonUtil.js +113 -104
- package/lib/utils/FileUtil.js +170 -165
- package/lib/utils/NetworkUtil.js +134 -131
- package/lib/utils/StringUtil.js +98 -99
- package/lib/utils/WrapCallback.js +33 -27
- package/package.json +2 -2
package/lib/index.js
CHANGED
|
@@ -1,26 +1,135 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
exports
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
exports
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
exports
|
|
25
|
-
|
|
26
|
-
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
Object.defineProperty(exports, "ColorConsole", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function () {
|
|
9
|
+
return _ColorConsole.default;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
Object.defineProperty(exports, "CommonUtil", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return _CommonUtil.default;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
Object.defineProperty(exports, "Dictionary", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
get: function () {
|
|
21
|
+
return _Type.Dictionary;
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
Object.defineProperty(exports, "FileUtil", {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
get: function () {
|
|
27
|
+
return _FileUtil.default;
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
Object.defineProperty(exports, "ILog", {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
get: function () {
|
|
33
|
+
return _ILog.ILog;
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
Object.defineProperty(exports, "IPosition", {
|
|
37
|
+
enumerable: true,
|
|
38
|
+
get: function () {
|
|
39
|
+
return _IPosition.default;
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
Object.defineProperty(exports, "IRouteItem", {
|
|
43
|
+
enumerable: true,
|
|
44
|
+
get: function () {
|
|
45
|
+
return _IRouter.IRouteItem;
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
Object.defineProperty(exports, "IRouter", {
|
|
49
|
+
enumerable: true,
|
|
50
|
+
get: function () {
|
|
51
|
+
return _IRouter.default;
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
Object.defineProperty(exports, "JSONSchemaType", {
|
|
55
|
+
enumerable: true,
|
|
56
|
+
get: function () {
|
|
57
|
+
return _CommonUtil.JSONSchemaType;
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
Object.defineProperty(exports, "Logger", {
|
|
61
|
+
enumerable: true,
|
|
62
|
+
get: function () {
|
|
63
|
+
return _ILog.Logger;
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
Object.defineProperty(exports, "Loglevel", {
|
|
67
|
+
enumerable: true,
|
|
68
|
+
get: function () {
|
|
69
|
+
return _ILog.Loglevel;
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
Object.defineProperty(exports, "MapData", {
|
|
73
|
+
enumerable: true,
|
|
74
|
+
get: function () {
|
|
75
|
+
return _MapData.default;
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
Object.defineProperty(exports, "MatchType", {
|
|
79
|
+
enumerable: true,
|
|
80
|
+
get: function () {
|
|
81
|
+
return _FileUtil.MatchType;
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
Object.defineProperty(exports, "NetworkUtil", {
|
|
85
|
+
enumerable: true,
|
|
86
|
+
get: function () {
|
|
87
|
+
return _NetworkUtil.default;
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
Object.defineProperty(exports, "PositionError", {
|
|
91
|
+
enumerable: true,
|
|
92
|
+
get: function () {
|
|
93
|
+
return _PositionError.default;
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
Object.defineProperty(exports, "ProjectType", {
|
|
97
|
+
enumerable: true,
|
|
98
|
+
get: function () {
|
|
99
|
+
return _ProjectType.default;
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
Object.defineProperty(exports, "StringUtil", {
|
|
103
|
+
enumerable: true,
|
|
104
|
+
get: function () {
|
|
105
|
+
return _StringUtil.default;
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
Object.defineProperty(exports, "StyleMessage", {
|
|
109
|
+
enumerable: true,
|
|
110
|
+
get: function () {
|
|
111
|
+
return _ColorConsole.StyleMessage;
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
Object.defineProperty(exports, "WrapCallback", {
|
|
115
|
+
enumerable: true,
|
|
116
|
+
get: function () {
|
|
117
|
+
return _WrapCallback.default;
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
var _ColorConsole = _interopRequireWildcard(require("./ColorConsole"));
|
|
121
|
+
var _MapData = _interopRequireDefault(require("./data/MapData"));
|
|
122
|
+
var _PositionError = _interopRequireDefault(require("./data/PositionError"));
|
|
123
|
+
var _ProjectType = _interopRequireDefault(require("./enum/ProjectType"));
|
|
124
|
+
var _ILog = require("./interface/ILog");
|
|
125
|
+
var _IPosition = _interopRequireDefault(require("./interface/IPosition"));
|
|
126
|
+
var _IRouter = _interopRequireWildcard(require("./interface/IRouter"));
|
|
127
|
+
var _Type = require("./type/Type");
|
|
128
|
+
var _CommonUtil = _interopRequireWildcard(require("./utils/CommonUtil"));
|
|
129
|
+
var _FileUtil = _interopRequireWildcard(require("./utils/FileUtil"));
|
|
130
|
+
var _NetworkUtil = _interopRequireDefault(require("./utils/NetworkUtil"));
|
|
131
|
+
var _StringUtil = _interopRequireDefault(require("./utils/StringUtil"));
|
|
132
|
+
var _WrapCallback = _interopRequireDefault(require("./utils/WrapCallback"));
|
|
133
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
134
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
135
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
package/lib/interface/ILog.js
CHANGED
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
3
6
|
exports.Loglevel = void 0;
|
|
4
7
|
// 日志类别
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
8
|
+
let Loglevel = exports.Loglevel = /*#__PURE__*/function (Loglevel) {
|
|
9
|
+
Loglevel[Loglevel["LOG"] = 0] = "LOG";
|
|
10
|
+
Loglevel[Loglevel["SUCCESS"] = 1] = "SUCCESS";
|
|
11
|
+
Loglevel[Loglevel["INFO"] = 2] = "INFO";
|
|
12
|
+
Loglevel[Loglevel["WARN"] = 3] = "WARN";
|
|
13
|
+
Loglevel[Loglevel["ERROR"] = 4] = "ERROR";
|
|
14
|
+
Loglevel[Loglevel["THROW"] = 5] = "THROW";
|
|
15
|
+
return Loglevel;
|
|
16
|
+
}({});
|
|
17
|
+
/**
|
|
18
|
+
* ILog
|
|
19
|
+
*/
|
package/lib/interface/IRouter.js
CHANGED
package/lib/type/Type.js
CHANGED
package/lib/utils/CommonUtil.js
CHANGED
|
@@ -1,116 +1,125 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _crypto = _interopRequireDefault(require("crypto"));
|
|
8
|
+
var _ajv = _interopRequireDefault(require("ajv"));
|
|
9
|
+
var _fs = _interopRequireDefault(require("fs"));
|
|
10
|
+
var _path = _interopRequireDefault(require("path"));
|
|
11
|
+
var _qrcode = _interopRequireDefault(require("qrcode"));
|
|
12
|
+
var _ColorConsole = _interopRequireDefault(require("../ColorConsole"));
|
|
13
|
+
var _ILog = require("../interface/ILog");
|
|
14
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
13
15
|
/**
|
|
14
16
|
* 不好分类的工具函数
|
|
15
17
|
*/
|
|
16
18
|
class CommonUtil {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
19
|
+
/**
|
|
20
|
+
* 根据 buffer 生成 hash 值
|
|
21
|
+
*
|
|
22
|
+
* 用于唯一标识流内容,通常用于文件校验
|
|
23
|
+
* @param buffer
|
|
24
|
+
* @returns hash 值
|
|
25
|
+
*/
|
|
26
|
+
static calcDataDigest(buffer) {
|
|
27
|
+
const hash = _crypto.default.createHash('SHA256');
|
|
28
|
+
hash.update(buffer);
|
|
29
|
+
return hash.digest();
|
|
30
|
+
}
|
|
31
|
+
static calcImageDigest(filePath) {
|
|
32
|
+
const data = _fs.default.readFileSync(filePath);
|
|
33
|
+
const hash = _crypto.default.createHash('md5').update(data);
|
|
34
|
+
return hash.digest('hex').substring(0, 8);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 从开发者项目的 node_module 中加载指定依赖;
|
|
39
|
+
* 如果不存在,提示下载;存在则返回加载的依赖
|
|
40
|
+
* @param packageName
|
|
41
|
+
*/
|
|
42
|
+
static requireNodeModule(projectPath, packageName) {
|
|
43
|
+
const modulePath = _path.default.resolve(projectPath, `./node_modules/${packageName}`);
|
|
44
|
+
if (!_fs.default.existsSync(modulePath)) {
|
|
45
|
+
return Promise.reject(new Error(_ColorConsole.default.createMessage([{
|
|
46
|
+
word: packageName,
|
|
47
|
+
style: _ColorConsole.default.getStyle(_ILog.Loglevel.ERROR)
|
|
48
|
+
}, 'is missing, run', {
|
|
49
|
+
word: `npm i ${packageName}`,
|
|
50
|
+
style: _ColorConsole.default.getStyle(_ILog.Loglevel.ERROR)
|
|
51
|
+
}, 'to install it'])));
|
|
28
52
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
53
|
+
return Promise.resolve(require(modulePath));
|
|
54
|
+
}
|
|
55
|
+
static requireModule(path) {
|
|
56
|
+
let throwError = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
57
|
+
if (!_fs.default.existsSync(path)) {
|
|
58
|
+
if (throwError) {
|
|
59
|
+
throw new Error(`${path} is missing`);
|
|
60
|
+
}
|
|
61
|
+
return;
|
|
33
62
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
if (!fs_1.default.existsSync(modulePath)) {
|
|
42
|
-
return Promise.reject(new Error(ColorConsole_1.default.createMessage([
|
|
43
|
-
{
|
|
44
|
-
word: packageName,
|
|
45
|
-
style: ColorConsole_1.default.getStyle(ILog_1.Loglevel.ERROR)
|
|
46
|
-
},
|
|
47
|
-
'is missing, run',
|
|
48
|
-
{
|
|
49
|
-
word: `npm i ${packageName}`,
|
|
50
|
-
style: ColorConsole_1.default.getStyle(ILog_1.Loglevel.ERROR)
|
|
51
|
-
},
|
|
52
|
-
'to install it'
|
|
53
|
-
])));
|
|
54
|
-
}
|
|
55
|
-
return Promise.resolve(require(modulePath));
|
|
63
|
+
try {
|
|
64
|
+
return require(path);
|
|
65
|
+
} catch (error) {
|
|
66
|
+
if (throwError) {
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
return;
|
|
56
70
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* 验证 json 内容
|
|
75
|
+
* @param json json 内容
|
|
76
|
+
* @param schema 验证配置, 详见:https://github.com/ajv-validator/ajv
|
|
77
|
+
*
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```
|
|
81
|
+
* CommonUtil.validateJson({a:123},
|
|
82
|
+
* {
|
|
83
|
+
* a:{
|
|
84
|
+
* type:'number'
|
|
85
|
+
* }
|
|
86
|
+
* }
|
|
87
|
+
* )
|
|
88
|
+
* ```
|
|
89
|
+
* @returns
|
|
90
|
+
*/
|
|
91
|
+
static validateJson(json, schema) {
|
|
92
|
+
if (!json || !schema) {
|
|
93
|
+
return;
|
|
73
94
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
* ```
|
|
90
|
-
* @returns
|
|
91
|
-
*/
|
|
92
|
-
static validateJson(json, schema) {
|
|
93
|
-
if (!json || !schema) {
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
const ajv = new ajv_1.default({ allErrors: true, allowUnionTypes: true });
|
|
97
|
-
ajv.validate(schema, json);
|
|
98
|
-
if (ajv.errors) {
|
|
99
|
-
const errors = ajv.errors.map((item) => {
|
|
100
|
-
const { message, instancePath, keyword } = item;
|
|
101
|
-
const errorMessage = `${instancePath ? `${instancePath}: ` : ''}${message}`;
|
|
102
|
-
if (keyword === 'required') {
|
|
103
|
-
return new TypeError(errorMessage);
|
|
104
|
-
}
|
|
105
|
-
return new Error(errorMessage);
|
|
106
|
-
});
|
|
107
|
-
return errors;
|
|
95
|
+
const ajv = new _ajv.default({
|
|
96
|
+
allErrors: true,
|
|
97
|
+
allowUnionTypes: true
|
|
98
|
+
});
|
|
99
|
+
ajv.validate(schema, json);
|
|
100
|
+
if (ajv.errors) {
|
|
101
|
+
const errors = ajv.errors.map(item => {
|
|
102
|
+
const {
|
|
103
|
+
message,
|
|
104
|
+
instancePath,
|
|
105
|
+
keyword
|
|
106
|
+
} = item;
|
|
107
|
+
const errorMessage = `${instancePath ? `${instancePath}: ` : ''}${message}`;
|
|
108
|
+
if (keyword === 'required') {
|
|
109
|
+
return new TypeError(errorMessage);
|
|
108
110
|
}
|
|
111
|
+
return new Error(errorMessage);
|
|
112
|
+
});
|
|
113
|
+
return errors;
|
|
109
114
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
+
}
|
|
116
|
+
static outputQRCodeOnTerminal(text) {
|
|
117
|
+
return _qrcode.default.toString(text, {
|
|
118
|
+
type: 'utf8',
|
|
119
|
+
margin: 0
|
|
120
|
+
}).then(qrcode => {
|
|
121
|
+
console.log(qrcode);
|
|
122
|
+
});
|
|
123
|
+
}
|
|
115
124
|
}
|
|
116
|
-
exports.default = CommonUtil;
|
|
125
|
+
var _default = exports.default = CommonUtil;
|