@aiot-toolkit/shared-utils 1.0.17-binary-beta.2 → 1.0.17-ets-beta.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.
- package/lib/buildMode/BuildModeManager.js +1 -273
- package/lib/buildMode/util.js +1 -83
- package/lib/index.js +1 -488
- package/lib/record-client.js +1 -150
- package/package.json +2 -3
- package/lib/protobuf/protobuf.js +0 -284
- package/lib/protobuf/vdom_pb.js +0 -2786
package/lib/index.js
CHANGED
|
@@ -1,489 +1,2 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.logWarn = logWarn;
|
|
7
|
-
exports.mkdirsSync = mkdirsSync;
|
|
8
|
-
exports.getIPv4IPAddress = getIPv4IPAddress;
|
|
9
|
-
exports.getServerIPAndPort = getServerIPAndPort;
|
|
10
|
-
exports.getDefaultServerHost = getDefaultServerHost;
|
|
11
|
-
exports.getClientIPAddress = getClientIPAddress;
|
|
12
|
-
exports.stripPrefixForIPV4MappedIPV6Address = stripPrefixForIPV4MappedIPV6Address;
|
|
13
|
-
exports.outputQRCodeOnTerminal = outputQRCodeOnTerminal;
|
|
14
|
-
exports.relateCwd = relateCwd;
|
|
15
|
-
exports.equals = equals;
|
|
16
|
-
exports.extend = extend;
|
|
17
|
-
exports.renderString = renderString;
|
|
18
|
-
exports.KnownError = KnownError;
|
|
19
|
-
exports.setCustomConfig = setCustomConfig;
|
|
20
|
-
exports.getProjectDslName = getProjectDslName;
|
|
21
|
-
exports.getDeviceInfo = getDeviceInfo;
|
|
22
|
-
exports.readJson = readJson;
|
|
23
|
-
exports.getManifest = getManifest;
|
|
24
|
-
exports.getUxExtensionList = getUxExtensionList;
|
|
25
|
-
exports.mergeJsonFile = mergeJsonFile;
|
|
26
|
-
exports.filterContentByTag = filterContentByTag;
|
|
27
|
-
Object.defineProperty(exports, "getLaunchPage", {
|
|
28
|
-
enumerable: true,
|
|
29
|
-
get: function () {
|
|
30
|
-
return _util.getLaunchPage;
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
exports.colorconsole = exports.logger = void 0;
|
|
34
|
-
var _os = _interopRequireDefault(require("os"));
|
|
35
|
-
var _fs = _interopRequireDefault(require("fs"));
|
|
36
|
-
var _path = _interopRequireDefault(require("path"));
|
|
37
|
-
var _http = _interopRequireDefault(require("http"));
|
|
38
|
-
var _console = require("console");
|
|
39
|
-
var _chalk = _interopRequireDefault(require("chalk"));
|
|
40
|
-
var _qrcodeTerminal = _interopRequireDefault(require("qrcode-terminal"));
|
|
41
|
-
var _config = _interopRequireDefault(require("../config"));
|
|
42
|
-
var _parse = _interopRequireDefault(require("parse5"));
|
|
43
|
-
var _index = _interopRequireDefault(require("parse5/lib/serializer/index.js"));
|
|
44
|
-
var _util = require("./buildMode/util");
|
|
45
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
46
|
-
/**
|
|
47
|
-
* Copyright (C) 2017, hapjs.org. All rights reserved.
|
|
48
|
-
*/
|
|
49
|
-
|
|
50
|
-
const logLevelMap = {};
|
|
51
|
-
function prependLevel(levelName, args) {
|
|
52
|
-
if (!logLevelMap[levelName]) {
|
|
53
|
-
const logLevel = levelName.toUpperCase();
|
|
54
|
-
logLevelMap[levelName] = logLevel;
|
|
55
|
-
}
|
|
56
|
-
if (typeof args[0] === 'string' && args[0].length > 1 && args[0][0] !== '[') {
|
|
57
|
-
args[0] = `[${logLevelMap[levelName]}] ${args[0]}`;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
const logger = {
|
|
61
|
-
logs: [],
|
|
62
|
-
add(args) {
|
|
63
|
-
this.logs.push(args);
|
|
64
|
-
},
|
|
65
|
-
clear() {
|
|
66
|
-
this.logs = [];
|
|
67
|
-
},
|
|
68
|
-
get() {
|
|
69
|
-
return this.logs.join('\n');
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* 带颜色的 info, log, warn, error, trace 的打印日志输出流
|
|
75
|
-
*/
|
|
76
|
-
exports.logger = logger;
|
|
77
|
-
let originConsole = global.console;
|
|
78
|
-
let console = _console.Console ? new _console.Console(process.stdout, process.stderr) : originConsole;
|
|
79
|
-
const colorconsole = {
|
|
80
|
-
attach(writable) {
|
|
81
|
-
if (writable && _console.Console) {
|
|
82
|
-
console = new _console.Console(writable, writable);
|
|
83
|
-
}
|
|
84
|
-
},
|
|
85
|
-
trace(...args) {
|
|
86
|
-
prependLevel(`trace`, args);
|
|
87
|
-
console.trace(...args);
|
|
88
|
-
},
|
|
89
|
-
log(...args) {
|
|
90
|
-
prependLevel(`log`, args);
|
|
91
|
-
console.log(_chalk.default.green(...args));
|
|
92
|
-
},
|
|
93
|
-
info(...args) {
|
|
94
|
-
prependLevel(`info`, args);
|
|
95
|
-
console.info(_chalk.default.green(...args));
|
|
96
|
-
},
|
|
97
|
-
warn(...args) {
|
|
98
|
-
prependLevel(`warn`, args);
|
|
99
|
-
console.warn(_chalk.default.yellow.bold(...args));
|
|
100
|
-
},
|
|
101
|
-
error(...args) {
|
|
102
|
-
prependLevel(`error`, args);
|
|
103
|
-
console.error(_chalk.default.red.bold(...args));
|
|
104
|
-
},
|
|
105
|
-
throw(...args) {
|
|
106
|
-
throw new Error(_chalk.default.red.bold(...args));
|
|
107
|
-
}
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* 打印日志
|
|
112
|
-
* @param loader
|
|
113
|
-
* @param logs
|
|
114
|
-
* @param suppresslog
|
|
115
|
-
*/
|
|
116
|
-
exports.colorconsole = colorconsole;
|
|
117
|
-
function logWarn(loader, logs, suppresslog) {
|
|
118
|
-
if (logs && logs.length) {
|
|
119
|
-
logs.forEach(log => {
|
|
120
|
-
const logAddr = log.line && log.column ? '\t@' + log.line + ':' + log.column : '';
|
|
121
|
-
if (suppresslog) return;
|
|
122
|
-
if (log.reason.startsWith('ERROR')) {
|
|
123
|
-
colorconsole.error(loader.resourcePath, log.reason + logAddr);
|
|
124
|
-
} else {
|
|
125
|
-
colorconsole.warn(loader.resourcePath, log.reason + logAddr);
|
|
126
|
-
}
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* 创建任意深度的路径的文件夹
|
|
133
|
-
* @param dirname
|
|
134
|
-
* @returns {boolean}
|
|
135
|
-
*/
|
|
136
|
-
function mkdirsSync(dirname) {
|
|
137
|
-
if (_fs.default.existsSync(dirname)) {
|
|
138
|
-
return true;
|
|
139
|
-
} else {
|
|
140
|
-
if (mkdirsSync(_path.default.dirname(dirname))) {
|
|
141
|
-
_fs.default.mkdirSync(dirname);
|
|
142
|
-
return true;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* 获取服务器端的IP
|
|
149
|
-
*/
|
|
150
|
-
function getIPv4IPAddress() {
|
|
151
|
-
const ifaces = _os.default.networkInterfaces();
|
|
152
|
-
let result;
|
|
153
|
-
for (const prop in ifaces) {
|
|
154
|
-
if (Object.prototype.hasOwnProperty.call(ifaces, prop)) {
|
|
155
|
-
const iface = ifaces[prop];
|
|
156
|
-
iface.every(eachAlias => {
|
|
157
|
-
if (eachAlias.family === 'IPv4' && !eachAlias.internal && eachAlias.address !== '127.0.0.1') {
|
|
158
|
-
result = eachAlias;
|
|
159
|
-
return false;
|
|
160
|
-
}
|
|
161
|
-
return true;
|
|
162
|
-
});
|
|
163
|
-
if (result !== void 0) {
|
|
164
|
-
break;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
return result && result.address;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* 取得server端的IP和port
|
|
173
|
-
* @param port {number}
|
|
174
|
-
* @returns {string}
|
|
175
|
-
*/
|
|
176
|
-
function getServerIPAndPort(port) {
|
|
177
|
-
return (getIPv4IPAddress() || '127.0.0.1') + `${port === 80 ? '' : ':' + port}`;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* 获取默认的服务器地址
|
|
182
|
-
*/
|
|
183
|
-
function getDefaultServerHost() {
|
|
184
|
-
return getServerIPAndPort(_config.default.server.port);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* 获取客户端ip
|
|
189
|
-
* @param req node Http IncomingRequest Object
|
|
190
|
-
* @returns {any|*|string}
|
|
191
|
-
*/
|
|
192
|
-
function getClientIPAddress(req) {
|
|
193
|
-
const ip = req.headers['x-forwarded-for'] || req.connection && req.connection.remoteAddress || req.socket && req.socket.remoteAddress || req.connection && req.connection.socket && req.connection.socket.remoteAddress;
|
|
194
|
-
return stripPrefixForIPV4MappedIPV6Address(ip);
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* 检测可能是IpV4-mapped IpV6 格式的ip
|
|
199
|
-
* - https://en.wikipedia.org/wiki/IPv6#IPv4-mapped_IPv6_addresses
|
|
200
|
-
*
|
|
201
|
-
* @param ip IpV4-mapped IpV6 string
|
|
202
|
-
* @returns {*}
|
|
203
|
-
*/
|
|
204
|
-
function stripPrefixForIPV4MappedIPV6Address(ip) {
|
|
205
|
-
if (/^::1$/.test(ip)) {
|
|
206
|
-
ip = '127.0.0.1';
|
|
207
|
-
}
|
|
208
|
-
if (/^::.{0,4}:(\d{1,3}\.){3}\d{1,3}/.test(ip)) {
|
|
209
|
-
ip = ip.replace(/^.*:/, '');
|
|
210
|
-
}
|
|
211
|
-
return ip;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/**
|
|
215
|
-
* 命令行输出二维码
|
|
216
|
-
* @param text
|
|
217
|
-
*/
|
|
218
|
-
function outputQRCodeOnTerminal(text) {
|
|
219
|
-
console.info(`\nGenerate QR code for HTTP server: ${text}`);
|
|
220
|
-
_qrcodeTerminal.default.generate(text, {
|
|
221
|
-
small: true
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
/**
|
|
226
|
-
* 相对工作目录的路径
|
|
227
|
-
*
|
|
228
|
-
* @param {String} filepath - 输入路径
|
|
229
|
-
* @returns {String} - 输出相对路径
|
|
230
|
-
*/
|
|
231
|
-
function relateCwd(filepath) {
|
|
232
|
-
const CWD = _config.default.projectPath;
|
|
233
|
-
return _path.default.relative(CWD, filepath);
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
* 比较两个对象是否相同
|
|
238
|
-
* @param o1
|
|
239
|
-
* @param o2
|
|
240
|
-
* @param fn {Function} 对比函数
|
|
241
|
-
*/
|
|
242
|
-
function equals(o1, o2, fn, ...args) {
|
|
243
|
-
if (fn) {
|
|
244
|
-
const fnRet = fn(o1, o2, ...args);
|
|
245
|
-
if (fnRet) {
|
|
246
|
-
return true;
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
const toStr1 = Object.prototype.toString.call(o1);
|
|
250
|
-
const toStr2 = Object.prototype.toString.call(o2);
|
|
251
|
-
if (toStr1 !== toStr2) {
|
|
252
|
-
return false;
|
|
253
|
-
}
|
|
254
|
-
if (toStr1 === '[object Null]' || toStr1 === '[object Undefined]') {
|
|
255
|
-
return true;
|
|
256
|
-
}
|
|
257
|
-
if (toStr1 !== '[object Object]' && toStr1 !== '[object Array]') {
|
|
258
|
-
return Object(o1).toString() === Object(o2).toString();
|
|
259
|
-
}
|
|
260
|
-
const keyMap = {};
|
|
261
|
-
Object.keys(o1).forEach(k => keyMap[k] = true);
|
|
262
|
-
Object.keys(o2).forEach(k => keyMap[k] = true);
|
|
263
|
-
const keyList = Object.keys(keyMap);
|
|
264
|
-
for (let i = 0; i < keyList.length; i++) {
|
|
265
|
-
const keyName = keyList[i];
|
|
266
|
-
const isEqual = equals(o1[keyName], o2[keyName], fn, keyName);
|
|
267
|
-
if (!isEqual) {
|
|
268
|
-
return false;
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
return true;
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
/**
|
|
275
|
-
* 扩展对象属性
|
|
276
|
-
* @param dest
|
|
277
|
-
* @param src
|
|
278
|
-
*/
|
|
279
|
-
function extend(target, ...src) {
|
|
280
|
-
if (typeof Object.assign === 'function') {
|
|
281
|
-
Object.assign(target, ...src);
|
|
282
|
-
} else {
|
|
283
|
-
const first = src.shift();
|
|
284
|
-
// 覆盖旧值
|
|
285
|
-
for (const key in first) {
|
|
286
|
-
target[key] = first[key];
|
|
287
|
-
}
|
|
288
|
-
if (src.length) {
|
|
289
|
-
extend(target, ...src);
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
return target;
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
/**
|
|
296
|
-
* 简单的模板字符串渲染函数
|
|
297
|
-
*
|
|
298
|
-
* @param {String} tpl - 模板字符串
|
|
299
|
-
* @param {Object} data - 渲染数据
|
|
300
|
-
* @returns {String}
|
|
301
|
-
*/
|
|
302
|
-
function renderString(tpl, data) {
|
|
303
|
-
return tpl.replace(/{{(.*?)}}/gm, (_, key) => {
|
|
304
|
-
key = key.trim();
|
|
305
|
-
return data[key] !== undefined ? data[key] : key;
|
|
306
|
-
});
|
|
307
|
-
}
|
|
308
|
-
function KnownError(message) {
|
|
309
|
-
const err = new Error(message);
|
|
310
|
-
err.name = 'KnownError';
|
|
311
|
-
err.__KNOWN = true;
|
|
312
|
-
return err;
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
/**
|
|
316
|
-
* 获取并设置自定义配置项
|
|
317
|
-
* @param {String} projectPath - 项目路径
|
|
318
|
-
* @param {String} port - server端口
|
|
319
|
-
*/
|
|
320
|
-
function setCustomConfig(projectPath, port) {
|
|
321
|
-
projectPath = _config.default.projectPath = projectPath || _config.default.projectPath;
|
|
322
|
-
const defaultConfig = 'quickapp.config.js';
|
|
323
|
-
const defaultConfigPath = _path.default.join(projectPath, defaultConfig);
|
|
324
|
-
if (_fs.default.existsSync(defaultConfigPath)) {
|
|
325
|
-
let customConf = {};
|
|
326
|
-
try {
|
|
327
|
-
customConf = require(defaultConfigPath);
|
|
328
|
-
} catch (err) {
|
|
329
|
-
colorconsole.error(`Error reading project custom configuration file: ${err.message}`);
|
|
330
|
-
}
|
|
331
|
-
// 保证 customConf server 字段无内容也可保证默认值
|
|
332
|
-
const server = Object.assign(_config.default.server, customConf.server);
|
|
333
|
-
Object.assign(_config.default, customConf, {
|
|
334
|
-
server
|
|
335
|
-
});
|
|
336
|
-
}
|
|
337
|
-
port && (_config.default.server.port = port);
|
|
338
|
-
}
|
|
339
|
-
const illegalExtsList = ['.css', '.less', '.scss', '.styl', '.sass', '.log', '.json', '.js'].map(e => 'app' + e);
|
|
340
|
-
|
|
341
|
-
/**
|
|
342
|
-
* 获取当前项目所使用的DSL
|
|
343
|
-
* @param projectPath
|
|
344
|
-
*/
|
|
345
|
-
function getProjectDslName(projectPath) {
|
|
346
|
-
// 根据项目中src/app的后缀名判断
|
|
347
|
-
const srcFiles = _fs.default.readdirSync(_path.default.join(projectPath, _config.default.sourceRoot));
|
|
348
|
-
const appFileNames = srcFiles.filter(f => /^app\..*/.test(f) && !illegalExtsList.includes(f));
|
|
349
|
-
let appFileExt;
|
|
350
|
-
if (appFileNames && appFileNames[0]) {
|
|
351
|
-
appFileExt = _path.default.extname(appFileNames[0]).slice(1);
|
|
352
|
-
colorconsole.info(`Get the app file suffix: ${appFileExt}`);
|
|
353
|
-
} else {
|
|
354
|
-
colorconsole.error(`Cannot get the correct app file suffix`);
|
|
355
|
-
}
|
|
356
|
-
return appFileExt === 'ux' ? 'xvm' : appFileExt;
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
/**
|
|
360
|
-
* 获取手机设备信息(1020新增接口)
|
|
361
|
-
* @param client {{ ip, port }}
|
|
362
|
-
* @param callback
|
|
363
|
-
*/
|
|
364
|
-
function getDeviceInfo(client, callback) {
|
|
365
|
-
const req = _http.default.request({
|
|
366
|
-
path: '/deviceinfo',
|
|
367
|
-
host: client.ip,
|
|
368
|
-
port: client.port,
|
|
369
|
-
timeout: 3000
|
|
370
|
-
}, res => {
|
|
371
|
-
res.on('data', data => {
|
|
372
|
-
callback(null, JSON.parse(data));
|
|
373
|
-
});
|
|
374
|
-
}).on('error', err => {
|
|
375
|
-
callback(err);
|
|
376
|
-
}).on('timeout', function () {
|
|
377
|
-
// abort方法会触发error事件
|
|
378
|
-
req.abort();
|
|
379
|
-
});
|
|
380
|
-
req.end();
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
/**
|
|
384
|
-
* 读取一份 json 内容
|
|
385
|
-
*
|
|
386
|
-
* @param {String} jsonpath - json 路径
|
|
387
|
-
* @returns {Object} json
|
|
388
|
-
*/
|
|
389
|
-
function readJson(jsonpath) {
|
|
390
|
-
try {
|
|
391
|
-
return JSON.parse(_fs.default.readFileSync(jsonpath).toString());
|
|
392
|
-
} catch (err) {
|
|
393
|
-
if (err instanceof SyntaxError) {
|
|
394
|
-
throw new SyntaxError(`Failed to parse ${jsonpath}, format error`);
|
|
395
|
-
} else if (err.message.startsWith('ENOENT:')) {
|
|
396
|
-
err.message = `Failed to read ${jsonpath}, the file cannot be found`;
|
|
397
|
-
throw err;
|
|
398
|
-
} else {
|
|
399
|
-
err.message = `Failed to read ${jsonpath} ${err.message}`;
|
|
400
|
-
throw err;
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
/**
|
|
406
|
-
* 获取 manifest.json 内容
|
|
407
|
-
*
|
|
408
|
-
* @returns {Object} json
|
|
409
|
-
*/
|
|
410
|
-
function getManifest() {
|
|
411
|
-
try {
|
|
412
|
-
// 需要特殊处理打包mina-h5时,manifest.json文件在根目录下
|
|
413
|
-
let manifestFile = _path.default.resolve(_config.default.projectPath, 'manifest.json');
|
|
414
|
-
if (!_fs.default.existsSync(manifestFile)) {
|
|
415
|
-
manifestFile = _path.default.resolve(_config.default.projectPath, _config.default.sourceRoot, 'manifest.json');
|
|
416
|
-
}
|
|
417
|
-
const manifest = readJson(manifestFile);
|
|
418
|
-
return manifest;
|
|
419
|
-
} catch (err) {
|
|
420
|
-
throw err;
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
/**
|
|
425
|
-
* 获取识别为.ux的文件后缀列表
|
|
426
|
-
* @param {*} withDot 扩展名是否包含.
|
|
427
|
-
* @returns string[]
|
|
428
|
-
*/
|
|
429
|
-
function getUxExtensionList(withDot = true) {
|
|
430
|
-
const result = ['ux', 'mix', 'hml'];
|
|
431
|
-
if (withDot) {
|
|
432
|
-
return result.map(item => `.${item}`);
|
|
433
|
-
}
|
|
434
|
-
return result;
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
/**
|
|
438
|
-
* 合并json文件的内容,把fromPath的内容合并到toPath
|
|
439
|
-
* @param {*} fromPath
|
|
440
|
-
* @param {*} toPath
|
|
441
|
-
*/
|
|
442
|
-
function mergeJsonFile(fromPath, toPath) {
|
|
443
|
-
if (!_fs.default.existsSync(fromPath) || !_fs.default.existsSync(toPath)) {
|
|
444
|
-
return;
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
// 读取两个文件的数据-->转换成json-->frompath的json属性覆盖topath的同名属性
|
|
448
|
-
const fromStr = _fs.default.readFileSync(fromPath).toString();
|
|
449
|
-
const toStr = _fs.default.readFileSync(toPath).toString();
|
|
450
|
-
try {
|
|
451
|
-
const fromObj = JSON.parse(fromStr);
|
|
452
|
-
const toObj = JSON.parse(toStr);
|
|
453
|
-
const result = Object.assign({}, toObj, fromObj);
|
|
454
|
-
_fs.default.writeFileSync(toPath, JSON.stringify(result, null, 2));
|
|
455
|
-
} catch (error) {
|
|
456
|
-
colorconsole.error(`Parse JSON Error: ${error.message}`);
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
/**
|
|
461
|
-
* 通过tag过滤内容
|
|
462
|
-
* @param {string} source 源文本
|
|
463
|
-
* @param {(tagName)=>boolean} includeFun 过滤方法,返回true表示保留
|
|
464
|
-
*
|
|
465
|
-
* @return string
|
|
466
|
-
*/
|
|
467
|
-
function filterContentByTag(source, includeFun) {
|
|
468
|
-
if (!source) {
|
|
469
|
-
return '';
|
|
470
|
-
}
|
|
471
|
-
const nodes = _parse.default.parseFragment(source, {
|
|
472
|
-
treeAdapter: _parse.default.treeAdapters.default,
|
|
473
|
-
locationInfo: true
|
|
474
|
-
}).childNodes;
|
|
475
|
-
if (!nodes || !nodes.length) {
|
|
476
|
-
return '';
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
// 过滤childrenNodes
|
|
480
|
-
const effectNodes = nodes.filter(item => includeFun(item.nodeName));
|
|
481
|
-
|
|
482
|
-
// 生成html
|
|
483
|
-
const serializer = new _index.default({
|
|
484
|
-
childNodes: effectNodes
|
|
485
|
-
});
|
|
486
|
-
serializer.serialize();
|
|
487
|
-
return serializer.html;
|
|
488
|
-
}
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.logWarn=logWarn,exports.mkdirsSync=mkdirsSync,exports.getIPv4IPAddress=getIPv4IPAddress,exports.getServerIPAndPort=getServerIPAndPort,exports.getDefaultServerHost=getDefaultServerHost,exports.getClientIPAddress=getClientIPAddress,exports.stripPrefixForIPV4MappedIPV6Address=stripPrefixForIPV4MappedIPV6Address,exports.outputQRCodeOnTerminal=outputQRCodeOnTerminal,exports.relateCwd=relateCwd,exports.equals=equals,exports.extend=extend,exports.renderString=renderString,exports.KnownError=KnownError,exports.setCustomConfig=setCustomConfig,exports.getProjectDslName=getProjectDslName,exports.getDeviceInfo=getDeviceInfo,exports.readJson=readJson,exports.getManifest=getManifest,exports.getUxExtensionList=getUxExtensionList,exports.mergeJsonFile=mergeJsonFile,exports.filterContentByTag=filterContentByTag,Object.defineProperty(exports,"getLaunchPage",{enumerable:!0,get:function(){return _util.getLaunchPage}}),exports.colorconsole=exports.logger=void 0;var _os=_interopRequireDefault(require("os")),_fs=_interopRequireDefault(require("fs")),_path=_interopRequireDefault(require("path")),_http=_interopRequireDefault(require("http")),_console=require("console"),_chalk=_interopRequireDefault(require("chalk")),_qrcodeTerminal=_interopRequireDefault(require("qrcode-terminal")),_config=_interopRequireDefault(require("../config")),_parse=_interopRequireDefault(require("parse5")),_index=_interopRequireDefault(require("parse5/lib/serializer/index.js")),_util=require("./buildMode/util");function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}const logLevelMap={};function prependLevel(e,t){if(!logLevelMap[e]){const t=e.toUpperCase();logLevelMap[e]=t}"string"==typeof t[0]&&t[0].length>1&&"["!==t[0][0]&&(t[0]=`[${logLevelMap[e]}] ${t[0]}`)}const logger={logs:[],add(e){this.logs.push(e)},clear(){this.logs=[]},get(){return this.logs.join("\n")}};exports.logger=logger;let originConsole=global.console,console=_console.Console?new _console.Console(process.stdout,process.stderr):originConsole;const colorconsole={attach(e){e&&_console.Console&&(console=new _console.Console(e,e))},trace(...e){prependLevel("trace",e),console.trace(...e)},log(...e){prependLevel("log",e),console.log(_chalk.default.green(...e))},info(...e){prependLevel("info",e),console.info(_chalk.default.green(...e))},warn(...e){prependLevel("warn",e),console.warn(_chalk.default.yellow.bold(...e))},error(...e){prependLevel("error",e),console.error(_chalk.default.red.bold(...e))},throw(...e){throw new Error(_chalk.default.red.bold(...e))}};function logWarn(e,t,r){t&&t.length&&t.forEach((t=>{const o=t.line&&t.column?"\t@"+t.line+":"+t.column:"";r||(t.reason.startsWith("ERROR")?colorconsole.error(e.resourcePath,t.reason+o):colorconsole.warn(e.resourcePath,t.reason+o))}))}function mkdirsSync(e){return!!_fs.default.existsSync(e)||(mkdirsSync(_path.default.dirname(e))?(_fs.default.mkdirSync(e),!0):void 0)}function getIPv4IPAddress(){const e=_os.default.networkInterfaces();let t;for(const r in e)if(Object.prototype.hasOwnProperty.call(e,r)){if(e[r].every((e=>!("IPv4"===e.family&&!e.internal&&"127.0.0.1"!==e.address)||(t=e,!1))),void 0!==t)break}return t&&t.address}function getServerIPAndPort(e){return(getIPv4IPAddress()||"127.0.0.1")+""+(80===e?"":":"+e)}function getDefaultServerHost(){return getServerIPAndPort(_config.default.server.port)}function getClientIPAddress(e){return stripPrefixForIPV4MappedIPV6Address(e.headers["x-forwarded-for"]||e.connection&&e.connection.remoteAddress||e.socket&&e.socket.remoteAddress||e.connection&&e.connection.socket&&e.connection.socket.remoteAddress)}function stripPrefixForIPV4MappedIPV6Address(e){return/^::1$/.test(e)&&(e="127.0.0.1"),/^::.{0,4}:(\d{1,3}\.){3}\d{1,3}/.test(e)&&(e=e.replace(/^.*:/,"")),e}function outputQRCodeOnTerminal(e){console.info(`\nGenerate QR code for HTTP server: ${e}`),_qrcodeTerminal.default.generate(e,{small:!0})}function relateCwd(e){const t=_config.default.projectPath;return _path.default.relative(t,e)}function equals(e,t,r,...o){if(r){if(r(e,t,...o))return!0}const n=Object.prototype.toString.call(e);if(n!==Object.prototype.toString.call(t))return!1;if("[object Null]"===n||"[object Undefined]"===n)return!0;if("[object Object]"!==n&&"[object Array]"!==n)return Object(e).toString()===Object(t).toString();const s={};Object.keys(e).forEach((e=>s[e]=!0)),Object.keys(t).forEach((e=>s[e]=!0));const i=Object.keys(s);for(let o=0;o<i.length;o++){const n=i[o];if(!equals(e[n],t[n],r,n))return!1}return!0}function extend(e,...t){if("function"==typeof Object.assign)Object.assign(e,...t);else{const r=t.shift();for(const t in r)e[t]=r[t];t.length&&extend(e,...t)}return e}function renderString(e,t){return e.replace(/{{(.*?)}}/gm,((e,r)=>(r=r.trim(),void 0!==t[r]?t[r]:r)))}function KnownError(e){const t=new Error(e);return t.name="KnownError",t.__KNOWN=!0,t}function setCustomConfig(e,t){e=_config.default.projectPath=e||_config.default.projectPath;const r=_path.default.join(e,"quickapp.config.js");if(_fs.default.existsSync(r)){let e={};try{e=require(r)}catch(e){colorconsole.error(`Error reading project custom configuration file: ${e.message}`)}const t=Object.assign(_config.default.server,e.server);Object.assign(_config.default,e,{server:t})}t&&(_config.default.server.port=t)}exports.colorconsole=colorconsole;const illegalExtsList=[".css",".less",".scss",".styl",".sass",".log",".json",".js"].map((e=>"app"+e));function getProjectDslName(e){const t=_fs.default.readdirSync(_path.default.join(e,_config.default.sourceRoot)).filter((e=>/^app\..*/.test(e)&&!illegalExtsList.includes(e)));let r;return t&&t[0]?(r=_path.default.extname(t[0]).slice(1),colorconsole.info(`Get the app file suffix: ${r}`)):colorconsole.error("Cannot get the correct app file suffix"),"ux"===r?"xvm":r}function getDeviceInfo(e,t){const r=_http.default.request({path:"/deviceinfo",host:e.ip,port:e.port,timeout:3e3},(e=>{e.on("data",(e=>{t(null,JSON.parse(e))}))})).on("error",(e=>{t(e)})).on("timeout",(function(){r.abort()}));r.end()}function readJson(e){try{return JSON.parse(_fs.default.readFileSync(e).toString())}catch(t){throw t instanceof SyntaxError?new SyntaxError(`Failed to parse ${e}, format error`):t.message.startsWith("ENOENT:")?(t.message=`Failed to read ${e}, the file cannot be found`,t):(t.message=`Failed to read ${e} ${t.message}`,t)}}function getManifest(){try{let e=_path.default.resolve(_config.default.projectPath,"manifest.json");_fs.default.existsSync(e)||(e=_path.default.resolve(_config.default.projectPath,_config.default.sourceRoot,"manifest.json"));return readJson(e)}catch(e){throw e}}function getUxExtensionList(e=!0){const t=["ux","mix","hml"];return e?t.map((e=>`.${e}`)):t}function mergeJsonFile(e,t){if(!_fs.default.existsSync(e)||!_fs.default.existsSync(t))return;const r=_fs.default.readFileSync(e).toString(),o=_fs.default.readFileSync(t).toString();try{const e=JSON.parse(r),n=JSON.parse(o),s=Object.assign({},n,e);_fs.default.writeFileSync(t,JSON.stringify(s,null,2))}catch(e){colorconsole.error(`Parse JSON Error: ${e.message}`)}}function filterContentByTag(e,t){if(!e)return"";const r=_parse.default.parseFragment(e,{treeAdapter:_parse.default.treeAdapters.default,locationInfo:!0}).childNodes;if(!r||!r.length)return"";const o=r.filter((e=>t(e.nodeName))),n=new _index.default({childNodes:o});return n.serialize(),n.html}
|
|
489
2
|
//# sourceMappingURL=index.js.map
|
package/lib/record-client.js
CHANGED
|
@@ -1,151 +1,2 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.getRecords = getRecords;
|
|
7
|
-
exports.getProjectClients = getProjectClients;
|
|
8
|
-
exports.recordClient = recordClient;
|
|
9
|
-
exports.getRecordClient = getRecordClient;
|
|
10
|
-
exports.clearProjectRecord = clearProjectRecord;
|
|
11
|
-
exports.removeClientBySn = removeClientBySn;
|
|
12
|
-
var _fs = _interopRequireDefault(require("fs"));
|
|
13
|
-
var _path = _interopRequireDefault(require("path"));
|
|
14
|
-
var _index = require("./index");
|
|
15
|
-
var _config = _interopRequireDefault(require("../config"));
|
|
16
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
17
|
-
/**
|
|
18
|
-
* Copyright (C) 2017, hapjs.org. All rights reserved.
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* 读取记录文件
|
|
23
|
-
* @param {String} clientRecordPath - 记录文件的地址
|
|
24
|
-
* @returns {Object} - 记录的全部设备信息
|
|
25
|
-
*/
|
|
26
|
-
function getRecords(clientRecordPath) {
|
|
27
|
-
let recordData = {
|
|
28
|
-
records: {}
|
|
29
|
-
};
|
|
30
|
-
try {
|
|
31
|
-
recordData = JSON.parse(_fs.default.readFileSync(clientRecordPath).toString());
|
|
32
|
-
} catch (err) {
|
|
33
|
-
_index.colorconsole.error(`Error reading hap-toolkit-client-records.json file: ${err.message}`);
|
|
34
|
-
}
|
|
35
|
-
return recordData;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* 将设备记录写入记录文件
|
|
40
|
-
* @param {String} clientRecordPath - 记录文件的地址
|
|
41
|
-
* @param {Object} recordData - 要写入的全部设备记录
|
|
42
|
-
*/
|
|
43
|
-
function writeRecords(clientRecordPath, recordData) {
|
|
44
|
-
_fs.default.writeFileSync(clientRecordPath, JSON.stringify(recordData, null, 2));
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* 读取一个项目的设备记录
|
|
49
|
-
* @param {Object} recordData - 全部设备信息
|
|
50
|
-
* @returns {Array} - 设备记录
|
|
51
|
-
*/
|
|
52
|
-
function getProjectClients(recordData) {
|
|
53
|
-
const projectPath = _config.default.projectPath;
|
|
54
|
-
let records = recordData.records;
|
|
55
|
-
records = records instanceof Object ? records : {};
|
|
56
|
-
const clients = records[projectPath];
|
|
57
|
-
if (clients && clients instanceof Array) {
|
|
58
|
-
return clients;
|
|
59
|
-
}
|
|
60
|
-
return [];
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* 保存设备记录
|
|
65
|
-
* @param {String} clientRecordPath - 记录文件的地址
|
|
66
|
-
* @param {String} newClient - 新的设备记录
|
|
67
|
-
* @param {callback} logHook - log信息的钩子,参数为要打印的信息
|
|
68
|
-
*/
|
|
69
|
-
function recordClient(clientRecordPath, newClient, logHook) {
|
|
70
|
-
const pathParentDir = _path.default.dirname(clientRecordPath);
|
|
71
|
-
const projectPath = _config.default.projectPath;
|
|
72
|
-
let recordData;
|
|
73
|
-
(0, _index.mkdirsSync)(pathParentDir);
|
|
74
|
-
if (_fs.default.existsSync(clientRecordPath)) {
|
|
75
|
-
recordData = getRecords(clientRecordPath);
|
|
76
|
-
let clients = getProjectClients(recordData);
|
|
77
|
-
logHook && logHook(`writeClientLogFile(): before: ${JSON.stringify(recordData.records)}`);
|
|
78
|
-
clients = clients.filter(client => {
|
|
79
|
-
return client.ip !== newClient.ip || client.port !== newClient.port;
|
|
80
|
-
});
|
|
81
|
-
// 保留最后的4条记录,最多记录5条
|
|
82
|
-
while (clients.length > 4) {
|
|
83
|
-
clients.shift();
|
|
84
|
-
}
|
|
85
|
-
recordData.records[projectPath] = clients;
|
|
86
|
-
} else {
|
|
87
|
-
recordData = {
|
|
88
|
-
records: {}
|
|
89
|
-
};
|
|
90
|
-
recordData.records[projectPath] = [];
|
|
91
|
-
}
|
|
92
|
-
// 写入文件
|
|
93
|
-
recordData.records[projectPath].push(newClient);
|
|
94
|
-
writeRecords(clientRecordPath, recordData);
|
|
95
|
-
logHook && logHook(`writeClientLogFile(): after: ${JSON.stringify(recordData.records)}`);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* 根据设备sn和ip获取设备的记录
|
|
100
|
-
* @param {String} clientRecordPath - 记录文件地址
|
|
101
|
-
* @param {String} sn - 设备序列号
|
|
102
|
-
* @param {String} ip
|
|
103
|
-
* @returns {Object} - 匹配的设备记录
|
|
104
|
-
*/
|
|
105
|
-
function getRecordClient(clientRecordPath, sn, ip) {
|
|
106
|
-
if (_fs.default.existsSync(clientRecordPath)) {
|
|
107
|
-
const recordData = getRecords(clientRecordPath);
|
|
108
|
-
const clients = getProjectClients(recordData);
|
|
109
|
-
return clients.find(client => {
|
|
110
|
-
return client.sn === sn && client.ip === ip && client.port;
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* 清除一个项目的设备记录
|
|
117
|
-
* @param {String} clientRecordPath
|
|
118
|
-
*/
|
|
119
|
-
function clearProjectRecord(clientRecordPath) {
|
|
120
|
-
if (_fs.default.existsSync(clientRecordPath)) {
|
|
121
|
-
const recordData = getRecords(clientRecordPath);
|
|
122
|
-
const projectPath = _config.default.projectPath;
|
|
123
|
-
recordData.records[projectPath] = [];
|
|
124
|
-
writeRecords(clientRecordPath, recordData);
|
|
125
|
-
_index.colorconsole.info('### App Server ### Clear debugging equipment records');
|
|
126
|
-
} else {
|
|
127
|
-
_index.colorconsole.info('### App Server ### There are no debugging equipment records that need to be cleared');
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* 从端口映射记录文件中移除项目的一个设备记录
|
|
133
|
-
* @param {String} clientRecordPath - 记录文件的地址
|
|
134
|
-
* @param {String} sn - 设备序列号
|
|
135
|
-
* @param {callback} logHook - log信息的钩子,参数为要打印的信息
|
|
136
|
-
*/
|
|
137
|
-
function removeClientBySn(clientRecordPath, sn, logHook) {
|
|
138
|
-
if (_fs.default.existsSync(clientRecordPath)) {
|
|
139
|
-
const projectPath = _config.default.projectPath;
|
|
140
|
-
const recordData = getRecords(clientRecordPath);
|
|
141
|
-
const records = recordData.records;
|
|
142
|
-
const clients = getProjectClients(recordData);
|
|
143
|
-
logHook && logHook(`_removeItemFromClientLogFile(): before: ${JSON.stringify(records)}`);
|
|
144
|
-
records[projectPath] = clients.filter(it => {
|
|
145
|
-
return it.sn !== sn;
|
|
146
|
-
});
|
|
147
|
-
writeRecords(clientRecordPath, recordData);
|
|
148
|
-
logHook && logHook(`_removeItemFromClientLogFile(): after: ${JSON.stringify(records)}`);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.getRecords=getRecords,exports.getProjectClients=getProjectClients,exports.recordClient=recordClient,exports.getRecordClient=getRecordClient,exports.clearProjectRecord=clearProjectRecord,exports.removeClientBySn=removeClientBySn;var _fs=_interopRequireDefault(require("fs")),_path=_interopRequireDefault(require("path")),_index=require("./index"),_config=_interopRequireDefault(require("../config"));function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}function getRecords(e){let r={records:{}};try{r=JSON.parse(_fs.default.readFileSync(e).toString())}catch(e){_index.colorconsole.error(`Error reading hap-toolkit-client-records.json file: ${e.message}`)}return r}function writeRecords(e,r){_fs.default.writeFileSync(e,JSON.stringify(r,null,2))}function getProjectClients(e){const r=_config.default.projectPath;let t=e.records;t=t instanceof Object?t:{};const o=t[r];return o&&o instanceof Array?o:[]}function recordClient(e,r,t){const o=_path.default.dirname(e),i=_config.default.projectPath;let n;if((0,_index.mkdirsSync)(o),_fs.default.existsSync(e)){n=getRecords(e);let o=getProjectClients(n);for(t&&t(`writeClientLogFile(): before: ${JSON.stringify(n.records)}`),o=o.filter((e=>e.ip!==r.ip||e.port!==r.port));o.length>4;)o.shift();n.records[i]=o}else n={records:{}},n.records[i]=[];n.records[i].push(r),writeRecords(e,n),t&&t(`writeClientLogFile(): after: ${JSON.stringify(n.records)}`)}function getRecordClient(e,r,t){if(_fs.default.existsSync(e)){return getProjectClients(getRecords(e)).find((e=>e.sn===r&&e.ip===t&&e.port))}}function clearProjectRecord(e){if(_fs.default.existsSync(e)){const r=getRecords(e),t=_config.default.projectPath;r.records[t]=[],writeRecords(e,r),_index.colorconsole.info("### App Server ### Clear debugging equipment records")}else _index.colorconsole.info("### App Server ### There are no debugging equipment records that need to be cleared")}function removeClientBySn(e,r,t){if(_fs.default.existsSync(e)){const o=_config.default.projectPath,i=getRecords(e),n=i.records,c=getProjectClients(i);t&&t(`_removeItemFromClientLogFile(): before: ${JSON.stringify(n)}`),n[o]=c.filter((e=>e.sn!==r)),writeRecords(e,i),t&&t(`_removeItemFromClientLogFile(): after: ${JSON.stringify(n)}`)}}
|
|
151
2
|
//# sourceMappingURL=record-client.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aiot-toolkit/shared-utils",
|
|
3
|
-
"version": "1.0.17-
|
|
3
|
+
"version": "1.0.17-ets-beta.1",
|
|
4
4
|
"description": "aiot shared utils",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=8.0.0"
|
|
@@ -25,11 +25,10 @@
|
|
|
25
25
|
],
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"chalk": "^2.4.2",
|
|
28
|
-
"google-protobuf": "^3.21.2",
|
|
29
28
|
"qrcode-terminal": "^0.12.0"
|
|
30
29
|
},
|
|
31
30
|
"devDependencies": {
|
|
32
31
|
"strip-ansi": "^5.2.0"
|
|
33
32
|
},
|
|
34
|
-
"gitHead": "
|
|
33
|
+
"gitHead": "06619701b7322969a14636f5ee5f260a41579e05"
|
|
35
34
|
}
|