UNPKG

@yuebai008/cli

Version:

Command line interface for rapid qg-minigame development

240 lines (221 loc) 8.79 kB
"use strict";var _Object$defineProperty = require("@babel/runtime-corejs2/core-js/object/define-property");var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");_Object$defineProperty(exports, "__esModule", { value: true });exports.LINK_MODE = exports.CLIENT_PORT = void 0;exports.clearProjectRecord = clearProjectRecord;exports.clientRecordPath = void 0;exports.getClientFromRequest = getClientFromRequest;exports.getDebugInfoFromRequest = getDebugInfoFromRequest;exports.getIPv4IPAddress = getIPv4IPAddress;exports.getProjectClients = getProjectClients;exports.getRecordClient = getRecordClient;exports.getRecords = getRecords;exports.ipConvert = ipConvert;exports.mkdirsSync = mkdirsSync;exports.recordClient = recordClient;exports.removeClientBySn = removeClientBySn;var _stringify = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/json/stringify")); var _os = _interopRequireDefault(require("os")); var _fs = _interopRequireDefault(require("fs")); var _path = _interopRequireDefault(require("path")); var _ipaddr = _interopRequireDefault(require("ipaddr.js")); var _cliSharedUtils = require("../cli-shared-utils"); /** * Copyright (C) 2017, hapjs.org. All rights reserved. */var home = require('os').homedir(); var CLIENT_PORT = exports.CLIENT_PORT = 39517; var clientRecordPath = exports.clientRecordPath = _path["default"].join(home, 'quickgame-toolkit-client-records.json'); var LINK_MODE = exports.LINK_MODE = { NULL: 0, WIFI: 1, ADB: 2 }; /** * 读取记录文件 * @param {String} clientRecordPath - 记录文件的地址 * @returns {Object} - 记录的全部设备信息 */ function getRecords(clientRecordPath) { var recordData = { records: {} }; try { recordData = JSON.parse(_fs["default"].readFileSync(clientRecordPath).toString()); } catch (err) { console.error("\u8BFB\u53D6client.json\u6587\u4EF6\u51FA\u9519: ".concat(err.message)); } return recordData; } /** * 将设备记录写入记录文件 * @param {String} clientRecordPath - 记录文件的地址 * @param {Object} recordData - 要写入的全部设备记录 */ function writeRecords(clientRecordPath, recordData) { try { _fs["default"].writeFileSync(clientRecordPath, (0, _stringify["default"])(recordData, null, 2)); } catch (err) { (0, _cliSharedUtils.error)("### App Server ### writeRecords\u51FA\u9519: ".concat(err.message)); } } /** * 读取一个项目的设备记录 * @param {Object} recordData - 全部设备信息 * @returns {Array} - 设备记录 */ function getProjectClients(recordData) { var records = recordData.records; records = records instanceof Object ? records : {}; var clients = records[_cliSharedUtils.projectPath]; if (clients && clients instanceof Array) { return clients; } return []; } /** * 保存设备记录 * @param {String} clientRecordPath - 记录文件的地址 * @param {String} newClient - 新的设备记录 * @param {callback} logHook - log信息的钩子,参数为要打印的信息 */ function recordClient(clientRecordPath, newClient, logHook) { try { var pathParentDir = _path["default"].dirname(clientRecordPath); var recordData; mkdirsSync(pathParentDir); if (_fs["default"].existsSync(clientRecordPath)) { recordData = getRecords(clientRecordPath); var clients = getProjectClients(recordData); logHook && logHook("writeClientLogFile(): before: ".concat((0, _stringify["default"])(recordData.records))); clients = clients.filter(function (client) { return client.ip !== newClient.ip || client.port !== newClient.port; }); // 保留最后的4条记录,最多记录5条 while (clients.length > 4) { clients.shift(); } recordData.records[_cliSharedUtils.projectPath] = clients; } else { recordData = { records: {} }; recordData.records[_cliSharedUtils.projectPath] = []; } // 写入文件 recordData.records[_cliSharedUtils.projectPath].push(newClient); writeRecords(clientRecordPath, recordData); logHook && logHook("writeClientLogFile(): after: ".concat((0, _stringify["default"])(recordData.records))); } catch (err) { (0, _cliSharedUtils.error)("### App Server ### recordClient\u51FA\u9519: ".concat(err.message)); } } /** * 根据设备sn和ip获取设备的记录 * @param {String} clientRecordPath - 记录文件地址 * @param {String} sn - 设备序列号 * @param {String} ip * @returns {Object} - 匹配的设备记录 */ function getRecordClient(clientRecordPath, sn, ip) { try { if (_fs["default"].existsSync(clientRecordPath)) { var recordData = getRecords(clientRecordPath); var clients = getProjectClients(recordData); return clients.find(function (client) { return client.sn === sn && client.ip === ip && client.port; }); } } catch (err) { (0, _cliSharedUtils.error)("### App Server ### getRecordClient\u51FA\u9519: ".concat(err.message)); } } /** * 清除一个项目的设备记录 * @param {String} clientRecordPath */ function clearProjectRecord(clientRecordPath) { if (_fs["default"].existsSync(clientRecordPath)) { var recordData = getRecords(clientRecordPath); recordData.records[_cliSharedUtils.projectPath] = []; writeRecords(clientRecordPath, recordData); console.info('### App Server ### 清空调试设备记录'); } else { console.info('### App Server ### 没有需要清空的调试设备记录'); } } /** * 从端口映射记录文件中移除项目的一个设备记录 * @param {String} clientRecordPath - 记录文件的地址 * @param {String} sn - 设备序列号 * @param {callback} logHook - log信息的钩子,参数为要打印的信息 */ function removeClientBySn(clientRecordPath, sn, logHook) { if (_fs["default"].existsSync(clientRecordPath)) { var recordData = getRecords(clientRecordPath); var records = recordData.records; var clients = getProjectClients(recordData); logHook && logHook("_removeItemFromClientLogFile(): before: ".concat((0, _stringify["default"])(records))); records[_cliSharedUtils.projectPath] = clients.filter(function (it) { return it.sn !== sn; }); writeRecords(clientRecordPath, recordData); logHook && logHook("_removeItemFromClientLogFile(): after: ".concat((0, _stringify["default"])(records))); } } /** * 创建任意深度的路径的文件夹 * @param dirname * @returns {boolean} */ function mkdirsSync(dirname) { if (_fs["default"].existsSync(dirname)) { return true; } else { if (mkdirsSync(_path["default"].dirname(dirname))) { _fs["default"].mkdirSync(dirname); return true; } } } /** * 获取服务器端的IP */ function getIPv4IPAddress() { var ifaces = _os["default"].networkInterfaces(); var result; for (var prop in ifaces) { if (Object.prototype.hasOwnProperty.call(ifaces, prop)) { var iface = ifaces[prop]; iface.every(function (eachAlias) { if ( eachAlias.family === 'IPv4' && !eachAlias.internal && eachAlias.address !== '127.0.0.1') { result = eachAlias; return false; } return true; }); if (result !== void 0) { break; } } } return result && result.address; } // ipv6地址转ipv4 function ipConvert(str) { if (_ipaddr["default"].IPv6.isValid(str)) { var addr = _ipaddr["default"].IPv6.parse(str); if (addr.isIPv4MappedAddress()) {return addr.toIPv4Address().toString();} } return str; } /** * 冗余方法,获取客户端的请求信息 * @param request * @returns {{clientIp: (any|*|string), sn, linkMode}} */ function getClientFromRequest(request) { var clientIp = ipConvert(request.ip); var serverIp = getIPv4IPAddress(); var sn = request.header['device-serial-number']; var linkMode = LINK_MODE.NULL; if (clientIp === '127.0.0.1' && sn) { linkMode = LINK_MODE.ADB; } else if (clientIp !== '127.0.0.1' && clientIp !== serverIp) { linkMode = LINK_MODE.WIFI; } return { clientIp: clientIp, sn: sn, linkMode: linkMode, serverIp: serverIp }; } /** * 获取请求信息 * @param request * @returns {{sn, linkMode, ws, application, devicePort}} */ function getDebugInfoFromRequest(request) { var _getClientFromRequest = getClientFromRequest(request),sn = _getClientFromRequest.sn,linkMode = _getClientFromRequest.linkMode,serverIp = _getClientFromRequest.serverIp; var _request$body = request.body,ws = _request$body.ws,application = _request$body.application; var devicePort = ws.split(':')[1].split('/')[0]; return { sn: sn, linkMode: linkMode, ws: ws, application: application, devicePort: devicePort, serverIp: serverIp }; }