UNPKG

@ohos/hpm-cli

Version:

CLI for HarmonyOS package manager

475 lines (474 loc) 22 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.publish = publish; exports.publishBundle = publishBundle; var _fs = _interopRequireDefault(require("fs")); var _path = _interopRequireDefault(require("path")); var _formData = _interopRequireDefault(require("form-data")); var _build = require("./build"); var _pack = require("./pack"); var _bundle = require("../core/bundle"); var _axios = require("../utils/axios"); var _helpers = require("../utils/helpers"); var _repository = require("../core/repository"); var _constant = require("../constant/constant"); var _config = require("../utils/config"); var _config2 = _interopRequireDefault(require("../config.json")); var _crypto = _interopRequireDefault(require("../core/crypto")); var _i18n = _interopRequireWildcard(require("../i18n")); var _events = _interopRequireWildcard(require("../utils/events")); var _preference = _interopRequireDefault(require("../core/preference")); var _login = require("../core/login"); var _common = require("../utils/common"); 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 _t2 in e) "default" !== _t2 && {}.hasOwnProperty.call(e, _t2) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t2)) && (i.get || i.set) ? o(f, _t2, i) : f[_t2] = e[_t2]); return f; })(e, t); } function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, 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 _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 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 publish(_x) { return _publish.apply(this, arguments); } function _publish() { _publish = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(_ref) { var old, target, bundle, repo, tg, prePublishCmd, afterPublishCmd, _t; return _regenerator().w(function (_context) { while (1) switch (_context.n) { case 0: old = _ref.old, target = _ref.target; bundle = null; repo = null; _context.p = 1; if (!target) { _context.n = 4; break; } // target 指定发布部件 // 递归查找binarys目录下的target tg = (0, _common.getDirByFileName)(_constant.DEFAULT_PROJECT_BINARY_DIR, target); if (!(tg && tg.length > 1)) { _context.n = 2; break; } _i18n["default"].log('publish.error', { error: "Has the same target '".concat(target, "' in ").concat(_constant.DEFAULT_PROJECT_BINARY_DIR, ".") }); return _context.a(2, false); case 2: if (!(tg && tg.length == 0)) { _context.n = 3; break; } _i18n["default"].log('publish.error', { error: "No found the target '".concat(target, "' in ").concat(_constant.DEFAULT_PROJECT_BINARY_DIR, ".") }); return _context.a(2, false); case 3: bundle = _bundle.Bundle.from(_path["default"].join(tg.pop())); repo = new _repository.Repository(_path["default"].join(_constant.DEFAULT_PROJECT_PACKS_DIR, _constant.DEP_FOLDER_NAME), (0, _config.getGlobalRepo)()); _context.n = 5; break; case 4: bundle = _bundle.Bundle.from(process.cwd()); repo = new _repository.Repository(_path["default"].join(process.cwd(), _constant.DEP_FOLDER_NAME), (0, _config.getGlobalRepo)()); case 5: if (!bundle.isPublishAs('chip-definition')) { _context.n = 6; break; } _i18n["default"].log('publish.supportError'); return _context.a(2); case 6: prePublishCmd = bundle.scripts(_constant.Hooks.prePublish); if (prePublishCmd) { (0, _helpers.runCmdWithSpawnSync)(function (spawnSync) { return spawnSync(prePublishCmd, { shell: process.env.shellPath, env: bundle.vars.env, cwd: bundle.path, stdio: 'inherit' }); }); } _events["default"].emit('hooks', { type: _events.EventTypes.beforePublish, name: bundle.name, path: bundle.path }); _context.n = 7; return loginAndPublish(bundle, repo, { old: old }); case 7: afterPublishCmd = bundle.scripts(_constant.Hooks.publish) || bundle.scripts(_constant.Hooks.afterPublish); if (afterPublishCmd) { (0, _helpers.runCmdWithSpawnSync)(function (spawnSync) { return spawnSync(afterPublishCmd, { shell: process.env.shellPath, env: bundle.vars.env, cwd: bundle.path, stdio: 'inherit' }); }); } _events["default"].emit('hooks', { type: _events.EventTypes.afterPublish, name: bundle.name, path: bundle.path }); _context.n = 9; break; case 8: _context.p = 8; _t = _context.v; throw _t; case 9: _context.p = 9; removePackedBundle(bundle); return _context.f(9); case 10: return _context.a(2); } }, _callee, null, [[1, 8, 9, 10]]); })); return _publish.apply(this, arguments); } function loginAndPublish(_x2, _x3) { return _loginAndPublish.apply(this, arguments); } function _loginAndPublish() { _loginAndPublish = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(bundle, repo) { var _ref2, old, inputJson, _args2 = arguments; return _regenerator().w(function (_context2) { while (1) switch (_context2.n) { case 0: _ref2 = _args2.length > 2 && _args2[2] !== undefined ? _args2[2] : {}, old = _ref2.old; inputJson = { old: old }; _context2.n = 1; return publishBundle(bundle, repo, inputJson); case 1: return _context2.a(2); } }, _callee2); })); return _loginAndPublish.apply(this, arguments); } function publishBundle(_x4, _x5) { return _publishBundle.apply(this, arguments); } function _publishBundle() { _publishBundle = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(bundle, repo) { var inputJson, _args3 = arguments; return _regenerator().w(function (_context3) { while (1) switch (_context3.n) { case 0: inputJson = _args3.length > 2 && _args3[2] !== undefined ? _args3[2] : {}; if (!bundle.isPublishAs('binary')) { _context3.n = 1; break; } _context3.n = 1; return buildBeforePublish(bundle, repo); case 1: _context3.n = 2; return packAndUpload(bundle, repo, inputJson); case 2: return _context3.a(2); } }, _callee3); })); return _publishBundle.apply(this, arguments); } function buildBeforePublish(_x6, _x7) { return _buildBeforePublish.apply(this, arguments); } function _buildBeforePublish() { _buildBeforePublish = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(bundle, repo) { var buildCmd; return _regenerator().w(function (_context4) { while (1) switch (_context4.n) { case 0: buildCmd = bundle.scripts('build'); if (!buildCmd) { _context4.n = 1; break; } _context4.n = 1; return (0, _build.startBuild)(bundle, repo); case 1: return _context4.a(2); } }, _callee4); })); return _buildBeforePublish.apply(this, arguments); } function packAndUpload(_x8, _x9) { return _packAndUpload.apply(this, arguments); } function _packAndUpload() { _packAndUpload = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(bundle, repo) { var inputJson, _args5 = arguments; return _regenerator().w(function (_context5) { while (1) switch (_context5.n) { case 0: inputJson = _args5.length > 2 && _args5[2] !== undefined ? _args5[2] : {}; (0, _pack.packBundle)(bundle, { silent: true }); if ((0, _config.publishGate)()) { _context5.n = 2; break; } _context5.n = 1; return uploadByContentCenter(bundle, repo, inputJson); case 1: _context5.n = 3; break; case 2: _context5.n = 3; return uploadByBundles(bundle, repo, inputJson); case 3: return _context5.a(2); } }, _callee5); })); return _packAndUpload.apply(this, arguments); } function uploadByBundles(_x0, _x1) { return _uploadByBundles.apply(this, arguments); } function _uploadByBundles() { _uploadByBundles = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(bundle, repo) { var inputJson, filePath, stat, formData, stream, headers, res, code, _args6 = arguments; return _regenerator().w(function (_context6) { while (1) switch (_context6.n) { case 0: inputJson = _args6.length > 2 && _args6[2] !== undefined ? _args6[2] : {}; filePath = "".concat(bundle.id, ".tgz"); stat = _fs["default"].statSync(filePath); if (!(stat.size > _config2["default"].maxUploadSize)) { _context6.n = 1; break; } throw (0, _i18n.getI18nMessage)('publish.overlimit', { size: Math.floor(_config2["default"].maxUploadSize / 1024 / 1024) }); case 1: _context6.n = 2; return (0, _login.getTokenLoginCookie)(_helpers.URL.login(), { ignoreCache: true }); case 2: formData = new _formData["default"]({ maxDataSize: _config2["default"].maxUploadSize }); stream = _fs["default"].createReadStream(filePath); formData.append('file', stream); if (inputJson.user || inputJson.pwd) { formData.append('user', inputJson.user); formData.append('pwd', inputJson.pwd); } headers = formData.getHeaders(); // 上传组件 _context6.n = 3; return _axios.axios.post(_helpers.URL.bundles(bundle.manifest.name), formData, { headers: _objectSpread(_objectSpread({}, headers), {}, { lang: _preference["default"].get('lang') }), maxContentLength: _config2["default"].maxUploadSize, maxBodyLength: _config2["default"].maxUploadSize, needAuth: true }); case 3: res = _context6.v; code = res && res.data && res.data.code; if (!(code === 200)) { _context6.n = 4; break; } _i18n["default"].log('publish.success'); _context6.n = 5; break; case 4: throw res; case 5: return _context6.a(2); } }, _callee6); })); return _uploadByBundles.apply(this, arguments); } function uploadByContentCenter(_x10, _x11) { return _uploadByContentCenter.apply(this, arguments); } function _uploadByContentCenter() { _uploadByContentCenter = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7(bundle, repo) { var inputJson, filePath, stat, fileName, uploadRes, data, _data$data, url, storageKey, header, file, cRes, statusRes, statusCode, _args7 = arguments; return _regenerator().w(function (_context7) { while (1) switch (_context7.n) { case 0: inputJson = _args7.length > 2 && _args7[2] !== undefined ? _args7[2] : {}; filePath = "".concat(bundle.id, ".tgz"); stat = _fs["default"].statSync(filePath); if (!(stat.size > _config2["default"].maxUploadSize)) { _context7.n = 1; break; } throw (0, _i18n.getI18nMessage)('publish.overlimit', { size: Math.floor(_config2["default"].maxUploadSize / 1024 / 1024) }); case 1: fileName = bundle.manifest.name; // 获取内容中心地址 _context7.n = 2; return getUpload(fileName, filePath, stat); case 2: uploadRes = _context7.v; data = uploadRes && uploadRes.data; if (!(data && data.code)) { _context7.n = 10; break; } if (!(data.code === 200)) { _context7.n = 9; break; } _data$data = data.data, url = _data$data.url, storageKey = _data$data.storageKey, header = _data$data.header; file = _fs["default"].readFileSync(filePath); // 上传至内容中心 _context7.n = 3; return _axios.axios.put(url, file, { headers: header, maxContentLength: Math.pow(1024, 3), maxBodyLength: Math.pow(1024, 3) }); case 3: cRes = _context7.v; if (!(cRes && cRes.status === 200)) { _context7.n = 7; break; } _context7.n = 4; return _axios.axios.post(_helpers.URL.uploadStatus(fileName, storageKey), {}, { maxContentLength: Math.pow(1024, 3), maxBodyLength: Math.pow(1024, 3), needAuth: true, headers: { lang: _preference["default"].get('lang') } }); case 4: statusRes = _context7.v; statusCode = statusRes && statusRes.data && statusRes.data.code; if (!(statusCode === 200)) { _context7.n = 5; break; } _i18n["default"].log('publish.success'); _context7.n = 6; break; case 5: throw statusRes; case 6: _context7.n = 8; break; case 7: throw cRes; case 8: _context7.n = 10; break; case 9: throw uploadRes; case 10: return _context7.a(2); } }, _callee7); })); return _uploadByContentCenter.apply(this, arguments); } function getUpload(_x12, _x13, _x14) { return _getUpload.apply(this, arguments); } function _getUpload() { _getUpload = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8(fileName, filePath, stat) { var checksum, res; return _regenerator().w(function (_context8) { while (1) switch (_context8.n) { case 0: checksum = _crypto["default"].digest(filePath); _context8.n = 1; return _axios.axios.post(_helpers.URL.upload(fileName), { fileName: filePath, checksum: checksum, fileSize: stat.size }, { headers: { 'Content-Type': 'application/json', lang: _preference["default"].get('lang') }, maxContentLength: Math.pow(1024, 3), maxBodyLength: Math.pow(1024, 3), needAuth: true }); case 1: res = _context8.v; return _context8.a(2, res); } }, _callee8); })); return _getUpload.apply(this, arguments); } function removePackedBundle(bundle) { if (bundle) { (0, _helpers.runShellCmd)(function (shell) { return shell.rm('-rf', "".concat(bundle.id, ".tgz")); }); } }