UNPKG

@ohos/hpm-cli

Version:

CLI for HarmonyOS package manager

154 lines (153 loc) 6.95 kB
"use strict"; 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); } Object.defineProperty(exports, "__esModule", { value: true }); exports.BundleCache = void 0; exports.cachePath = cachePath; var _path = _interopRequireDefault(require("path")); var _fs = _interopRequireDefault(require("fs")); var _helpers = require("../utils/helpers"); var _constant = require("../constant/constant"); var _config = require("../utils/config"); var _i18n = require("../i18n"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _classPrivateFieldInitSpec(e, t, a) { _checkPrivateRedeclaration(e, t), t.set(e, a); } function _checkPrivateRedeclaration(e, t) { if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object"); } function _classPrivateFieldGet(s, a) { return s.get(_assertClassBrand(s, a)); } function _classPrivateFieldSet(s, a, r) { return s.set(_assertClassBrand(s, a), r), r; } function _assertClassBrand(e, t, n) { if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n; throw new TypeError("Private element is not present on this object"); } /* * 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 _cacheMap = /*#__PURE__*/new WeakMap(); var _cacheBundle = /*#__PURE__*/new WeakMap(); var BundleCache = exports.BundleCache = /*#__PURE__*/function () { function BundleCache(cacheMap, cacheBundle) { _classCallCheck(this, BundleCache); _classPrivateFieldInitSpec(this, _cacheMap, void 0); _classPrivateFieldInitSpec(this, _cacheBundle, void 0); _classPrivateFieldSet(_cacheMap, this, cacheMap); _classPrivateFieldSet(_cacheBundle, this, cacheBundle); } return _createClass(BundleCache, [{ key: "saveToFile", value: function saveToFile(dir) { var cacheDir = _path["default"].join(dir, _constant.DEP_FOLDER_NAME, _constant.DOWNLOAD_TEMP_FOLDER); if (_fs["default"].existsSync(cacheDir)) { var json = this.toJson(); var cacheJsonDir = _path["default"].join(cacheDir, 'bundle-cache.json'); if (_fs["default"].existsSync(cacheJsonDir)) { (0, _helpers.runShellCmd)(function (shell) { return shell.rm('-rf', cacheJsonDir); }); } _fs["default"].writeFileSync(cacheJsonDir, JSON.stringify(json, null, 4)); } } }, { key: "setCacheMap", value: function setCacheMap(manifests) { var _this = this; manifests.forEach(function (manifest) { _classPrivateFieldGet(_cacheMap, _this)[manifest.name] = new Date().getTime(); if (!_classPrivateFieldGet(_cacheBundle, _this).includes(manifest.name)) { _classPrivateFieldGet(_cacheBundle, _this).push(manifest.name); } }); } }, { key: "addOrUpdate", value: function addOrUpdate(manifests) { this.setCacheMap(manifests); } }, { key: "removeCache", value: function removeCache(manifestNameList) { var _this2 = this; manifestNameList.forEach(function (name) { var index = _classPrivateFieldGet(_cacheBundle, _this2).findIndex(function (key) { return key === name; }); if (index >= 0) { _classPrivateFieldGet(_cacheBundle, _this2).splice(index, 1); delete _classPrivateFieldGet(_cacheMap, _this2)[name]; } }); } }, { key: "isManifestTimeout", value: function isManifestTimeout(manifest) { if (_classPrivateFieldGet(_cacheBundle, this).includes(manifest.name)) { return Date.now() - _classPrivateFieldGet(_cacheMap, this)[manifest.name] >= (0, _config.cacheExpiration)(); } return true; } }, { key: "toJson", value: function toJson() { return _classPrivateFieldGet(_cacheMap, this); } }, { key: "cacheBundle", get: function get() { return _classPrivateFieldGet(_cacheBundle, this); } }]); }(); BundleCache.fromManifests = function (manifests) { var bundleCache = new BundleCache({}, []); bundleCache.setCacheMap(manifests); return bundleCache; }; BundleCache.isExisted = function (dir) { return _fs["default"].existsSync(_path["default"].join(cachePath(dir), 'bundle-cache.json')); }; BundleCache.fromPath = function (dir) { function __fp(func, err) { try { return func(); } catch (e) { if (err) { var error = err(e); if (error) throw error; } } } var cacheJsonDir = _path["default"].join(cachePath(dir), 'bundle-cache.json'); var content = __fp(function () { return _fs["default"].readFileSync(cacheJsonDir); }, function (e) { return new Error((0, _i18n.getI18nMessage)('bundleCache.readJsonError', { url: cacheJsonDir, message: e.message })); }); var json = __fp(function () { return JSON.parse(content); }, function (e) { return new Error((0, _i18n.getI18nMessage)('bundleCache.jsonParseError', { message: e.message })); }); var keys = Object.keys(json); return new BundleCache(json, keys); }; function cachePath(dir) { return _path["default"].join(dir, _constant.DEP_FOLDER_NAME, _constant.DOWNLOAD_TEMP_FOLDER); }