UNPKG

@ohos/hpm-cli

Version:

CLI for HarmonyOS package manager

243 lines (242 loc) 11.8 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.LocalRepository = void 0; var _fs = _interopRequireDefault(require("fs")); var _path2 = _interopRequireDefault(require("path")); var _semver = _interopRequireDefault(require("semver")); var _manifest = require("./manifest"); var _helpers = require("../utils/helpers"); var _dependency = require("./dependency"); var _constant = require("../constant/constant"); var _i18n = require("../i18n"); var _log = _interopRequireDefault(require("./log")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; } 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 _readOnlyError(r) { throw new TypeError('"' + r + '" is read-only'); } 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 _path = /*#__PURE__*/new WeakMap(); var _suspendPath = /*#__PURE__*/new WeakMap(); var _localmanifests = /*#__PURE__*/new WeakMap(); var LocalRepository = exports.LocalRepository = /*#__PURE__*/function () { function LocalRepository(repoPath) { _classCallCheck(this, LocalRepository); _classPrivateFieldInitSpec(this, _path, void 0); _classPrivateFieldInitSpec(this, _suspendPath, void 0); _classPrivateFieldInitSpec(this, _localmanifests, null); _classPrivateFieldSet(_path, this, repoPath); _classPrivateFieldSet(_suspendPath, this, _path2["default"].join(_classPrivateFieldGet(_path, this), _constant.SUSPEND_FOLDER)); } return _createClass(LocalRepository, [{ key: "path", get: function get() { return _classPrivateFieldGet(_path, this); } }, { key: "manifests", value: function manifests(filter) { var _this = this; var manifests = []; if (_fs["default"].existsSync(_classPrivateFieldGet(_path, this))) { var files = []; var dirs = _fs["default"].readdirSync(_classPrivateFieldGet(_path, this)); dirs.forEach(function (dir) { if (dir.startsWith('@')) { var subdirs = _fs["default"].readdirSync(_path2["default"].join(_classPrivateFieldGet(_path, _this), dir)); if (filter) { subdirs = subdirs.filter(filter); } subdirs = subdirs.map(function (subdir) { return _path2["default"].join(dir, subdir); }); files = files.concat(subdirs); } else { if (!_constant.BUILT_IN_OHOS_FOLDER.includes(dir)) { files.push(dir); } } }); var _iterator = _createForOfIteratorHelper(files), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var file = _step.value; try { var jsonPath = _path2["default"].resolve(_path2["default"].join(_classPrivateFieldGet(_path, this), file, 'bundle.json')); if (_fs["default"].existsSync(jsonPath)) { var manifestJSON = JSON.parse(_fs["default"].readFileSync(jsonPath)); var manifest = _manifest.Manifest.json(manifestJSON); manifest.path = _path2["default"].join(_classPrivateFieldGet(_path, this), manifest.name); manifests.push(manifest); } } catch (e) { _log["default"].debug(e); } } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } } _classPrivateFieldSet(_localmanifests, this, manifests); return manifests; } }, { key: "resolve", value: function resolve(manifest) { var result = []; var _iterator2 = _createForOfIteratorHelper(manifest.dependencies), _step2; try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { var dependency = _step2.value; var depManifest = this.manifest(dependency.name, dependency.versionRanges); if (!depManifest) { throw (0, _i18n.getI18nMessage)('dependencies.unResolved', { name: dependency.name, folderPath: _constant.DEP_FOLDER_NAME }); } result.push(depManifest); } } catch (err) { _iterator2.e(err); } finally { _iterator2.f(); } return result; } }, { key: "manifest", value: function manifest(name, range) { var manifests = this.manifests(); var dependency = new _dependency.Dependency(name, range); var found = manifests.filter(function (b) { return dependency.match(b); }); if (found.length === 0) { return null; } return found.reduce(function (m, c) { return _semver["default"].lt(m.version, c.version) ? c : m; }, found[0]); } }, { key: "suspend", value: function suspend() { var _this2 = this; var manifestNames = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; var localManifests = this.manifests(); manifestNames.forEach(function (name) { var found = localManifests.find(function (lm) { return lm.name === name; }); if (found) { var _parseScopeName = (0, _helpers.parseScopeName)(found.name), scope = _parseScopeName.scope, _name = _parseScopeName.name; var oldPath = _path2["default"].join(_classPrivateFieldGet(_path, _this2), scope, _name); var newPath = _path2["default"].join(_classPrivateFieldGet(_suspendPath, _this2), scope); if (!_fs["default"].existsSync(newPath)) { (0, _helpers.runShellCmd)(function (shell) { return shell.mkdir('-p', newPath); }); } (0, _helpers.runShellCmd)(function (shell) { return shell.mv(oldPath, newPath); }); } }); } }, { key: "deleteSuspend", value: function deleteSuspend() { var _this3 = this; if (_fs["default"].existsSync(_classPrivateFieldGet(_suspendPath, this))) { (0, _helpers.runShellCmd)(function (shell) { return shell.rm('-rf', _classPrivateFieldGet(_suspendPath, _this3)); }); } } }, { key: "restoreSuspend", value: function restoreSuspend() { var _this4 = this; if (_fs["default"].existsSync(_classPrivateFieldGet(_suspendPath, this))) { var dirs = _fs["default"].readdirSync(_classPrivateFieldGet(_suspendPath, this)); var _iterator3 = _createForOfIteratorHelper(dirs), _step3; try { var _loop = function _loop() { var dir = _step3.value; (0, _helpers.runShellCmd)(function (shell) { return shell.cp('-R', _path2["default"].join(_classPrivateFieldGet(_suspendPath, _this4), dir), _this4.path); }); }; for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) { _loop(); } } catch (err) { _iterator3.e(err); } finally { _iterator3.f(); } } } }, { key: "remove", value: function remove(nameList) { var _this5 = this; if (_fs["default"].existsSync(this.path)) { var _iterator4 = _createForOfIteratorHelper(nameList), _step4; try { var _loop2 = function _loop2() { var name = _step4.value; var info = (0, _helpers.parseScopeName)(name); var folder = _path2["default"].join(_this5.path, info.scope, info.name); if (_fs["default"].existsSync(folder)) { (0, _helpers.runShellCmd)(function (shell) { return shell.rm('-rf', _path2["default"].join(_this5.path, name)); }); } }; for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) { _loop2(); } } catch (err) { _iterator4.e(err); } finally { _iterator4.f(); } } } }]); }();