UNPKG

@ohos/hpm-cli

Version:

CLI for HarmonyOS package manager

407 lines (406 loc) 21.5 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.showUpdateRes = showUpdateRes; exports.update = update; exports.updateAfterCheck = updateAfterCheck; exports.updateBundle = updateBundle; var _path = _interopRequireWildcard(require("path")); var _bundle = require("../core/bundle"); var _repository = require("../core/repository"); var _install = require("./install"); var _helpers = require("../utils/helpers"); var _constant = require("../constant/constant"); var _common = require("../utils/common"); var _config = require("../utils/config"); var pkg = _interopRequireWildcard(require("../../package.json")); var _i18n = _interopRequireDefault(require("../i18n")); var _tar = _interopRequireDefault(require("tar")); var _dependency = require("../core/dependency"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t5 in e) "default" !== _t5 && {}.hasOwnProperty.call(e, _t5) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t5)) && (i.get || i.set) ? o(f, _t5, i) : f[_t5] = e[_t5]); return f; })(e, t); } function _regeneratorValues(e) { if (null != e) { var t = e["function" == typeof Symbol && Symbol.iterator || "@@iterator"], r = 0; if (t) return t.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) return { next: function next() { return e && r >= e.length && (e = void 0), { value: e && e[r++], done: !e }; } }; } throw new TypeError(_typeof(e) + " is not iterable"); } function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i["return"]) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); } function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } _regeneratorDefine2 = function _regeneratorDefine(e, r, n, t) { if (r) i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n;else { var o = function o(r, n) { _regeneratorDefine2(e, r, function (e) { return this._invoke(r, n, e); }); }; o("next", 0), o("throw", 1), o("return", 2); } }, _regeneratorDefine2(e, r, n, t); } 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 asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); } function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; } /* * 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 update(_x) { return _update.apply(this, arguments); } function _update() { _update = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(descriptor) { var options, commandStr, bundleManifest, _bundleManifest, component, publishAs, os, buildArch, destPath, _parseScopeName, pathName, targetPath, _parseScopeName2, _pathName, _targetPath, bundleDescriptorArr, updateCount, bundleInfo, globalManifest, _iterator, _step, desc, count, _args = arguments, _t; return _regenerator().w(function (_context) { while (1) switch (_context.n) { case 0: options = _args.length > 1 && _args[1] !== undefined ? _args[1] : {}; if (!(options.self || 'hpm' === descriptor)) { _context.n = 1; break; } commandStr = "echo ".concat(pkg.version, " && npm update -g ").concat(pkg.name, " && hpm -V"); (0, _helpers.runCmdWithSpawnSync)(function (spawnSync) { return spawnSync(commandStr, { shell: process.env.shellPath, stdio: 'inherit' }); }); return _context.a(2); case 1: bundleManifest = (0, _common.getCurrentBundleManifest)(); if ((0, _common.checkGlobalBundlePath)(bundleManifest)) { _context.n = 2; break; } _i18n["default"].log('install.error', { error: 'Please npm install' }); return _context.a(2); case 2: _bundleManifest = bundleManifest, component = _bundleManifest.component, publishAs = _bundleManifest.publishAs, os = _bundleManifest.os, buildArch = _bundleManifest.buildArch; if (component && component.subsystem) { destPath = null; // 不是hpm生成的bundleJson,并且是新二进制包 if (publishAs === 'binary' && os && buildArch) { destPath = component ? "".concat(component.subsystem, "/").concat(component.name) : ''; _parseScopeName = (0, _helpers.parseScopeName)(bundleManifest.name), pathName = _parseScopeName.name; targetPath = _path["default"].join(_constant.DEFAULT_PROJECT_BINARY_DIR, destPath || pathName); bundleManifest = (0, _common.getLocalManiFest)(targetPath); } } else { if (publishAs === 'binary' && os && buildArch) { _parseScopeName2 = (0, _helpers.parseScopeName)(bundleManifest.name), _pathName = _parseScopeName2.name; _targetPath = _path["default"].join(_constant.DEFAULT_PROJECT_BINARY_DIR, _pathName); bundleManifest = (0, _common.getLocalManiFest)(_targetPath); } } bundleDescriptorArr = []; updateCount = 0; bundleInfo = (0, _common.getBundleJsonAndDeps)({ bundleManifest: bundleManifest }); globalManifest = bundleInfo.manifest; if (!descriptor) { bundleDescriptorArr = bundleInfo.bundleDescriptorArr; } if (!(bundleDescriptorArr.length > 0)) { _context.n = 11; break; } _iterator = _createForOfIteratorHelper(bundleDescriptorArr); _context.p = 3; _iterator.s(); case 4: if ((_step = _iterator.n()).done) { _context.n = 7; break; } desc = _step.value; _context.n = 5; return updateAfterCheck(desc, options, globalManifest); case 5: count = _context.v; updateCount += count; case 6: _context.n = 4; break; case 7: _context.n = 9; break; case 8: _context.p = 8; _t = _context.v; _iterator.e(_t); case 9: _context.p = 9; _iterator.f(); return _context.f(9); case 10: _context.n = 13; break; case 11: _context.n = 12; return updateAfterCheck(descriptor, options, globalManifest); case 12: updateCount = _context.v; case 13: showUpdateRes(updateCount); case 14: return _context.a(2); } }, _callee, null, [[3, 8, 9, 10]]); })); return _update.apply(this, arguments); } function showUpdateRes(updateCount) { if (updateCount > 0) { _i18n["default"].log('update.success'); } else { _i18n["default"].log('update.noNeedUpdate'); } } function updateAfterCheck(_x2) { return _updateAfterCheck.apply(this, arguments); } /** * 根据bundleJson更新组件 * @param {Bundle} bundle * @param {Repository} repo * @param {Object} update 需要更新的内容 * @param {String} descriptor 组件描述 * @param {Boolean} isGlobal * @param {Boolean} isBinary 是否为新二进制包 * @returns {Number} updateCount 更新包的数量 */ function _updateAfterCheck() { _updateAfterCheck = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(descriptor) { var options, globalManifest, _args3 = arguments; return _regenerator().w(function (_context3) { while (1) switch (_context3.n) { case 0: options = _args3.length > 1 && _args3[1] !== undefined ? _args3[1] : {}; globalManifest = _args3.length > 2 ? _args3[2] : undefined; return _context3.a(2, new Promise(/*#__PURE__*/function () { var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(resolve, reject) { var repo, isGlobal, pathInfo, installPath, fromPath, bundlePath, osInfo, isBinary, bundleInfo, bundle, needUpdate, updateCount, _t2; return _regenerator().w(function (_context2) { while (1) switch (_context2.n) { case 0: repo = null; _context2.p = 1; if (options.global) { repo = new _repository.Repository('', (0, _config.getGlobalRepo)()); } else { repo = new _repository.Repository(_path["default"].join(process.cwd(), _constant.DEP_FOLDER_NAME)); } isGlobal = options.global; _context2.n = 2; return (0, _common.getNeccesaryInstallInfo)(descriptor, globalManifest, isGlobal, true); case 2: pathInfo = _context2.v; installPath = pathInfo.updatePath, fromPath = pathInfo.fromPath, bundlePath = pathInfo.bundlePath, osInfo = pathInfo.osInfo; isBinary = osInfo.isBinary; if (pathInfo.repo) repo = pathInfo.repo; bundleInfo = { bundleDescriptor: descriptor, isBinary: isBinary, bundlePath: bundlePath }; bundle = _bundle.Bundle.from(fromPath, bundleInfo); _context2.n = 3; return bundle.update(repo, osInfo); case 3: needUpdate = _context2.v; needUpdate = bundle.resolveSnapshotManifest(needUpdate, repo, options.global, options.force).filter(function (m) { return !!m.url; }); _context2.n = 4; return updateBundle(bundle, repo, needUpdate, descriptor, options.global, isBinary); case 4: updateCount = _context2.v; resolve(updateCount); _context2.n = 6; break; case 5: _context2.p = 5; _t2 = _context2.v; repo.restoreSuspend(options.global); repo.deleteSuspend(options.global); reject(_t2); case 6: return _context2.a(2); } }, _callee2, null, [[1, 5]]); })); return function (_x5, _x6) { return _ref.apply(this, arguments); }; }())); } }, _callee3); })); return _updateAfterCheck.apply(this, arguments); } function updateBundle(_x3, _x4) { return _updateBundle.apply(this, arguments); } function _updateBundle() { _updateBundle = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(bundle, repo) { var update, descriptor, isGlobal, isBinary, deps, directDeps, updatePath, dirTemp, _iterator2, _step2, _loop, _args5 = arguments, _t3, _t4; return _regenerator().w(function (_context5) { while (1) switch (_context5.n) { case 0: update = _args5.length > 2 && _args5[2] !== undefined ? _args5[2] : {}; descriptor = _args5.length > 3 ? _args5[3] : undefined; isGlobal = _args5.length > 4 ? _args5[4] : undefined; isBinary = _args5.length > 5 ? _args5[5] : undefined; if (!descriptor) { _context5.n = 2; break; } _context5.n = 1; return (0, _install.parseInstallParam)(descriptor); case 1: _t3 = _context5.v; _context5.n = 3; break; case 2: _t3 = null; case 3: deps = _t3; if (deps && deps.length) { // update命令更新指定的依赖 update = update.filter(function (manifest) { return !!deps.find(function (dep) { return dep.name === manifest.name; }); }); } if (!(update.length > 0)) { _context5.n = 11; break; } directDeps = getDirectDeps(bundle, update); repo.suspend(update.map(function (m) { return m.name; }), isGlobal); // 判断是不是二进制包,是二进制包安装到全局临时目录下 updatePath = isBinary ? _constant.DEFAULT_PROJECT_PACKS_DIR : bundle.path; dirTemp = _path["default"].resolve(_path["default"].join(updatePath, _constant.DEP_FOLDER_NAME, _constant.DOWNLOAD_TEMP_FOLDER)); (0, _repository.createDirIfNotExists)(dirTemp); _iterator2 = _createForOfIteratorHelper(update); _context5.p = 4; _loop = /*#__PURE__*/_regenerator().m(function _loop() { var manifest, tarfile, bundlePath; return _regenerator().w(function (_context4) { while (1) switch (_context4.n) { case 0: manifest = _step2.value; if (!manifest.url) { _context4.n = 2; break; } tarfile = _path["default"].resolve(dirTemp, "".concat(manifest.id, ".tgz")); _context4.n = 1; return (0, _repository.bundleDownload)(manifest, isGlobal, bundle, isBinary); case 1: bundlePath = _context4.v; manifest.path = bundlePath; _tar["default"].extract({ file: tarfile, cwd: bundlePath, sync: true }); (0, _helpers.runShellCmd)(function (shell) { return shell.rm(tarfile); }); (0, _bundle.execInstallHook)(manifest, isGlobal, bundle); case 2: return _context4.a(2); } }, _loop); }); _iterator2.s(); case 5: if ((_step2 = _iterator2.n()).done) { _context5.n = 7; break; } return _context5.d(_regeneratorValues(_loop()), 6); case 6: _context5.n = 5; break; case 7: _context5.n = 9; break; case 8: _context5.p = 8; _t4 = _context5.v; _iterator2.e(_t4); case 9: _context5.p = 9; _iterator2.f(); return _context5.f(9); case 10: repo.deleteSuspend(isGlobal); if (directDeps && directDeps.length > 0) { bundle.addDependencies(directDeps, ''); bundle.updateBundleJson(); } bundle.updateCacheJson(update); case 11: return _context5.a(2, update.length); } }, _callee4, null, [[4, 8, 9, 10]]); })); return _updateBundle.apply(this, arguments); } function getDirectDeps(bundle, updateManifests) { var directDeps = []; updateManifests.forEach(function (manifest) { if (bundle.isDirectDep(manifest.name)) { directDeps.push(new _dependency.Dependency(manifest.name, resolveVersion(manifest.version.version))); } }); return directDeps; } function resolveVersion(version) { var connector = (0, _helpers.isSnapshot)(version) ? '' : '~'; return "".concat(connector).concat(version); }