UNPKG

@ohos/hpm-cli

Version:

CLI for HarmonyOS package manager

377 lines (376 loc) 25.3 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.renderInfo = exports.renderData = exports.querySolutions = exports.query = exports.pageSearch = void 0; exports.search = search; var _inquirer = require("inquirer"); var _fs = _interopRequireDefault(require("fs")); var _path = _interopRequireDefault(require("path")); var _semver = _interopRequireDefault(require("semver")); var _axios = require("../utils/axios"); var _helpers = require("../utils/helpers"); var _i18n = _interopRequireWildcard(require("../i18n")); var _preference = _interopRequireDefault(require("../core/preference")); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; } function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator["return"] && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, "catch": function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; } 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. */ // 兼容历史数据 var boardMap = { 'hispark_pegasus': 'hispark_pegasus,hi3861', 'hispark_taurus': 'hispark_taurus,hi3516', 'hispark_aries': 'hispark_aries,hi3518', 'gr5515_sk': 'gr5515_sk,goodix', 'v200zr': 'v200zr,fnlink' }; function search(_x, _x2) { return _search.apply(this, arguments); } function _search() { _search = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5(searchKey, _ref) { var _ref$pageSize, pageSize, withJson, type, _ref$currentPage, currentPage, ver, kernel, board, osVersion, matchBy, currentPageNum, pageSizeNum, params, queryAction; return _regeneratorRuntime().wrap(function _callee5$(_context5) { while (1) switch (_context5.prev = _context5.next) { case 0: _ref$pageSize = _ref.pageSize, pageSize = _ref$pageSize === void 0 ? 10 : _ref$pageSize, withJson = _ref.json, type = _ref.type, _ref$currentPage = _ref.currentPage, currentPage = _ref$currentPage === void 0 ? 1 : _ref$currentPage, ver = _ref.ver, kernel = _ref.kernel, board = _ref.board, osVersion = _ref.osVersion; matchBy = []; if (searchKey) { matchBy.push({ field: 'name', opt: 'CONTAIN', value: searchKey }); } if (type && type != 'solution') { matchBy.push({ field: 'publishAs', opt: 'EQUAL', value: type }); } if (type != 'solution' && ver && (ver === 'latest' || ver === 'all' || _semver["default"].validRange(ver))) { matchBy.push({ field: 'version', opt: 'EQUAL', value: ver }); } if (kernel) { matchBy.push({ field: 'ohos.kernel', opt: 'IN', value: kernel }); } if (board) { matchBy.push({ field: 'ohos.board', opt: 'IN', value: boardMap[board] || board }); } if (osVersion) { matchBy.push({ field: 'ohos.os', opt: 'IN', value: osVersion }); } currentPageNum = Number(currentPage); pageSizeNum = Number(pageSize); params = { pageSize: pageSizeNum, condition: JSON.stringify({ matchBy: matchBy }), currentPage: currentPageNum }; queryAction = type === 'solution' ? querySolutions : query; _context5.next = 14; return _pageSearch({ query: queryAction, searchKey: searchKey, condition: params.condition, currentPage: currentPageNum, totalPage: Infinity, pageSize: pageSizeNum, withJson: withJson, type: type }); case 14: case "end": return _context5.stop(); } }, _callee5); })); return _search.apply(this, arguments); } var query = exports.query = /*#__PURE__*/function () { var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(searchKey, params) { var _yield$axios$get, data; return _regeneratorRuntime().wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: _context.next = 2; return _axios.axios.get(_helpers.URL.search(searchKey), { params: params }); case 2: _yield$axios$get = _context.sent; data = _yield$axios$get.data.data; return _context.abrupt("return", { data: data.datas, totalSize: data.total }); case 5: case "end": return _context.stop(); } }, _callee); })); return function query(_x3, _x4) { return _ref2.apply(this, arguments); }; }(); var querySolutions = exports.querySolutions = /*#__PURE__*/function () { var _ref3 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(searchKey, params) { var _yield$axios$get2, data; return _regeneratorRuntime().wrap(function _callee2$(_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: _context2.next = 2; return _axios.axios.get(_helpers.URL.searchSolution(searchKey), { params: params }); case 2: _yield$axios$get2 = _context2.sent; data = _yield$axios$get2.data.data; return _context2.abrupt("return", { data: data.datas, totalSize: data.total }); case 5: case "end": return _context2.stop(); } }, _callee2); })); return function querySolutions(_x5, _x6) { return _ref3.apply(this, arguments); }; }(); var _pageSearch = exports.pageSearch = /*#__PURE__*/function () { var _ref5 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4(_ref4) { var query, searchKey, condition, currentPage, totalPage, pageSize, withJson, type, nextTotalPage, _yield$query, data, totalSize, mapAction, renderKey, _doUserAction; return _regeneratorRuntime().wrap(function _callee4$(_context4) { while (1) switch (_context4.prev = _context4.next) { case 0: query = _ref4.query, searchKey = _ref4.searchKey, condition = _ref4.condition, currentPage = _ref4.currentPage, totalPage = _ref4.totalPage, pageSize = _ref4.pageSize, withJson = _ref4.withJson, type = _ref4.type; nextTotalPage = totalPage; if (!(currentPage <= nextTotalPage)) { _context4.next = 13; break; } _context4.next = 5; return query(searchKey, { pageSize: pageSize, condition: condition, curPage: currentPage }); case 5: _yield$query = _context4.sent; data = _yield$query.data; totalSize = _yield$query.totalSize; addDefaultTemplateIntoData(data, currentPage, type); mapAction = type === 'solution' ? mapRenderSolution : mapRender; renderKey = type === 'solution' ? ['name', 'bundleName', 'remark'] : ['name', 'version', 'description']; renderData(data, searchKey, withJson, mapAction, renderKey); nextTotalPage = Math.ceil(totalSize / pageSize); case 13: _doUserAction = /*#__PURE__*/function () { var _ref6 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3() { var _yield$prompt, userAction; return _regeneratorRuntime().wrap(function _callee3$(_context3) { while (1) switch (_context3.prev = _context3.next) { case 0: _context3.next = 2; return (0, _inquirer.prompt)([{ type: 'input', message: "".concat(renderInfo(currentPage, nextTotalPage)), name: 'userAction' }]); case 2: _yield$prompt = _context3.sent; userAction = _yield$prompt.userAction; _context3.t0 = userAction; _context3.next = _context3.t0 === 'n' ? 7 : _context3.t0 === 'p' ? 16 : _context3.t0 === 'q' ? 25 : 27; break; case 7: if (!(currentPage > nextTotalPage)) { _context3.next = 13; break; } _i18n["default"].log('search.noMoreResult'); _context3.next = 11; return _doUserAction(); case 11: _context3.next = 15; break; case 13: _context3.next = 15; return _pageSearch({ query: query, searchKey: searchKey, condition: condition, currentPage: Math.max(1, Math.min(currentPage + 1, nextTotalPage + 1)), totalPage: nextTotalPage, pageSize: pageSize, type: type }); case 15: return _context3.abrupt("break", 30); case 16: if (!(currentPage === 1)) { _context3.next = 22; break; } _i18n["default"].log('search.onFirstPage'); _context3.next = 20; return _doUserAction(); case 20: _context3.next = 24; break; case 22: _context3.next = 24; return _pageSearch({ query: query, searchKey: searchKey, condition: condition, currentPage: Math.max(1, Math.min(currentPage - 1, nextTotalPage)), totalPage: nextTotalPage, pageSize: pageSize, type: type }); case 24: return _context3.abrupt("break", 30); case 25: _i18n["default"].log('search.quit'); return _context3.abrupt("return"); case 27: _context3.next = 29; return _doUserAction(); case 29: return _context3.abrupt("break", 30); case 30: case "end": return _context3.stop(); } }, _callee3); })); return function doUserAction() { return _ref6.apply(this, arguments); }; }(); if (withJson) { _context4.next = 17; break; } _context4.next = 17; return _doUserAction(); case 17: case "end": return _context4.stop(); } }, _callee4); })); return function pageSearch(_x7) { return _ref5.apply(this, arguments); }; }(); var renderInfo = exports.renderInfo = function renderInfo(currentPage, totalPage) { if (currentPage > totalPage) { return (0, _i18n.getI18nMessage)('search.lastPage'); } if (currentPage === 1) { return (0, _i18n.getI18nMessage)('search.firstPage'); } return (0, _i18n.getI18nMessage)('search.middlePage'); }; var renderData = exports.renderData = function renderData(currentResultList, searchKey, withJson) { var mapAction = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : mapRender; var renderKey = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : ['name', 'version', 'description']; if (currentResultList instanceof Array && currentResultList.length > 0) { withJson ? _i18n["default"].log('common.message', { message: "".concat(JSON.stringify(currentResultList, null, 4)) }) : console.table(currentResultList.map(mapAction), renderKey); } else { _i18n["default"].log('search.noResult', { searchKey: searchKey ? "\"".concat(searchKey, "\"") : '' }); } withJson ? _i18n["default"].log('common.message', { message: '' }) : _i18n["default"].log('search.moreInfo'); }; function mapRender(_ref7) { var name = _ref7.name, version = _ref7.version, description = _ref7.manifest.description; return { name: (0, _helpers.tableColumnRender)(name, 30), version: (0, _helpers.tableColumnRender)(version, 20), description: (0, _helpers.tableColumnRender)(description || (0, _i18n.getI18nMessage)('common.noDescription'), 50) }; } function mapRenderSolution(_ref8) { var name = _ref8.name, enName = _ref8.enName, remark = _ref8.remark, enRemark = _ref8.enRemark, bundleName = _ref8.bundleName; var lang = _preference["default"].get('lang'); var realName = lang === 'en' ? enName || name : name || enName; var realRemark = lang === 'en' ? enRemark || remark : remark || enRemark; return { name: (0, _helpers.tableColumnRender)(realName, 20), bundleName: (0, _helpers.tableColumnRender)(bundleName, 30), remark: (0, _helpers.tableColumnRender)(realRemark, 22) }; } function addDefaultTemplateIntoData(data, currentPage, type) { if (type === 'template' && currentPage === 1 && data instanceof Array) { var templatePath = _path["default"].join(__dirname, '..', 'asset', 'template', 'default'); var dirs = _fs["default"].readdirSync(templatePath); dirs.forEach(function (d) { var jsonPath = _path["default"].join(templatePath, d); var parsed = _path["default"].parse(d); if (parsed.name && parsed.ext === '.json' && _fs["default"].existsSync(jsonPath)) { var json = JSON.parse(_fs["default"].readFileSync(jsonPath)); data.unshift({ name: json.name, version: 'latest', manifest: { description: json.description } }); } }); } }