@ohos/hpm-cli
Version:
CLI for HarmonyOS package manager
115 lines (114 loc) • 5.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.traceLogger = void 0;
var _fs = _interopRequireDefault(require("fs"));
var _path = _interopRequireDefault(require("path"));
var _constant = require("../constant/constant");
var _helpers = require("./helpers");
var _logHelpers = require("./logHelpers");
var _child_process = require("child_process");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; } /*
* Copyright (c) 2020-2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var traceLogger = exports.traceLogger = {
historyLogPath: _path["default"].resolve(_constant.DEFAULT_HISTORY_LOG_DIR, "history-".concat((0, _logHelpers.getFormattedDate)(new Date(), 'YYYY-MM-DD'), ".log")),
writeToLog: function writeToLog(str) {
if (!_fs["default"].existsSync(_constant.DEFAULT_HISTORY_LOG_DIR)) {
(0, _helpers.runShellCmd)(function (shell) {
return shell.mkdir('-p', _constant.DEFAULT_HISTORY_LOG_DIR);
});
}
var writeStream = _fs["default"].createWriteStream(this.historyLogPath, {
flags: 'a'
});
writeStream.write(str, 'utf-8');
},
write: function write(_ref) {
var action = _ref.action,
msg = _ref.msg;
var data = {
action: action,
msg: msg,
type: 'cmd'
};
var str = "[HPMCLI] : \n".concat(JSON.stringify(data), "\n");
this.writeToLog(str);
},
unlinkLog: function unlinkLog(filePath) {
_fs["default"].unlinkSync(filePath);
},
getHistoryLogger: function getHistoryLogger(filePath) {
return new Promise(function (resolve, reject) {
var readStream = _fs["default"].createReadStream(filePath);
var data = '';
readStream.on('data', function (chunk) {
data += chunk;
}).on('error', function (error) {
reject(error);
}).on('close', function () {
resolve(data);
});
});
},
shouldSendMessage: function shouldSendMessage() {
try {
var logFiles = _fs["default"].readdirSync(_constant.DEFAULT_HISTORY_LOG_DIR);
return logFiles.some(function (logFile) {
var _logFile$match = logFile.match(/history-(\S*)\.log/),
_logFile$match2 = _slicedToArray(_logFile$match, 2),
filename = _logFile$match2[0],
date = _logFile$match2[1];
var diffDays = (Date.parse(new Date()) - Date.parse(date)) / (1000 * 60 * 60 * 24);
if (diffDays < 1) {
return false;
} else {
return true;
}
});
} catch (err) {
process.exit(-1);
}
},
isTraceInstalled: function isTraceInstalled(pluginAPI) {
var cmdPlugins = pluginAPI.getCmdPlugins();
for (var cmd in cmdPlugins) {
if (cmd === 'trace-server') {
return true;
}
}
return false;
},
sendToTrace: function sendToTrace(pluginAPI) {
try {
if (this.shouldSendMessage() && this.isTraceInstalled(pluginAPI)) {
var child = (0, _child_process.spawn)('hpm', ['trace-server'], {
shell: true,
detached: false,
windowsHide: false,
stdio: 'inherit'
});
}
} catch (err) {
process.exit(-1);
}
}
};