UNPKG

@ohos/hpm-cli

Version:

CLI for HarmonyOS package manager

125 lines (124 loc) 4.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.cleanData = cleanData; exports.cleanFiles = cleanFiles; exports.getFormattedDate = getFormattedDate; exports.getLogEnvironment = getLogEnvironment; var _fs = _interopRequireDefault(require("fs")); var _path = _interopRequireDefault(require("path")); var _package = _interopRequireDefault(require("../../package.json")); var _helpers = require("./helpers"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } /* * 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. */ function getLogEnvironment() { var envs = process.env; var envKeys = Object.keys(envs); var cleanedEnvs = {}; envKeys.forEach(function (key) { if (key.startsWith('DEP_')) { cleanedEnvs[key] = envKeys[key]; } }); return { platform: process.platform, node: process.version, hpm: _package["default"].version, argv: process.argv, env: cleanedEnvs }; } function cleanData(data) { if (data && _typeof(data) === 'object') { var clone = data; var keywords = ['cookie', 'proxy', 'password', 'headers', '_header']; var stack = [clone]; var cache = [clone]; var _loop = function _loop() { var current = stack.pop(); var keys = Object.keys(current); var _loop2 = function _loop2() { var key = keys[i]; var lowercaseKey = key.toLowerCase(); keywords.forEach(function (word) { if (lowercaseKey.indexOf(word) >= 0) { delete current[key]; } }); var child = current[key]; if (child && (0, _helpers.isObject)(child) && !cache.find(function (c) { return c === child; })) { stack.push(current[key]); cache.push(child); } }; for (var i = 0; i < keys.length; i += 1) { _loop2(); } }; while (stack.length > 0) { _loop(); } return clone; } return data; } function cleanFiles(folder, maxFileNum) { if (_fs["default"].existsSync(folder)) { var dirs = _fs["default"].readdirSync(folder); if (dirs.length >= maxFileNum) { var oldFile = ''; var oldTime = Infinity; dirs.forEach(function (fileName) { var filePath = _path["default"].join(folder, fileName); var stats = _fs["default"].statSync(filePath); if (oldTime > stats.mtimeMs) { oldTime = stats.mtimeMs; oldFile = filePath; } }); if (oldFile) { (0, _helpers.runShellCmd)(function (shell) { return shell.rm('-rf', oldFile); }); } } } } function getFormattedDate(d, format) { var year = d.getFullYear(); var month = d.getMonth() + 1; var date = d.getDate(); var hour = d.getHours(); var minute = d.getMinutes(); var second = d.getSeconds(); if (format === 'YYYY-MM-DD-hh-mm-ss') { return [year, month, date, hour, minute, second].map(function (m) { return String(m).padStart(2, '0'); }).join('-'); } if (format === 'YYYY-MM') { return "".concat(String(year), "-").concat(String(month).padStart(2, '0')); } if (format === 'YYYY-MM-DD') { return [year, month, date].map(function (m) { return String(m).padStart(2, '0'); }).join('-'); } return d.getTime(); }