UNPKG

@ohos/hpm-cli

Version:

CLI for HarmonyOS package manager

218 lines (217 loc) 9.94 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.modifyVersion = modifyVersion; exports.pack = pack; exports.packBundle = packBundle; var _tar = _interopRequireDefault(require("tar")); var _fs = _interopRequireDefault(require("fs")); var _path = _interopRequireDefault(require("path")); var _bundle = require("../core/bundle"); var _helpers = require("../utils/helpers"); var _i18n = _interopRequireWildcard(require("../i18n")); var _constant = require("../constant/constant"); var _events = _interopRequireWildcard(require("../utils/events")); var _common = require("../utils/common"); 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 _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; } /* * 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 pack() { var option = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var bundle = null; var bundleTarget = null; var isSnapShot = false; if (option.snapshot) { isSnapShot = true; } if (option.target) { // 补充动态[subsystem] var tg = (0, _common.getDirByFileName)(_constant.DEFAULT_PROJECT_BINARY_DIR, option.target); if (tg && tg.length > 1) { _i18n["default"].log('pack.error', { error: "Has the same target '".concat(option.target, "' in ").concat(_constant.DEFAULT_PROJECT_BINARY_DIR, ".") }); return false; } if (tg && tg.length == 0) { _i18n["default"].log('pack.error', { error: "No found the target '".concat(option.target, "' in ").concat(_constant.DEFAULT_PROJECT_BINARY_DIR, ".") }); return false; } bundleTarget = tg.pop(); } else { bundleTarget = process.cwd(); } modifyVersion(bundleTarget, isSnapShot); bundle = _bundle.Bundle.from(bundleTarget); packBundle(bundle); } function modifyVersion(bundleTarget, isSnapShot) { if (!isSnapShot) return false; // Read Target BundleJson var targetPath = _path["default"].resolve(bundleTarget, 'bundle.json'); if (!_fs["default"].existsSync(targetPath)) { _i18n["default"].log('init.error', { error: "Can't resolve the source bundle.json in current dir." }); return false; } var targetFileContents = _fs["default"].readFileSync(targetPath, 'utf-8').toString(); var targetRes = JSON.parse(targetFileContents); targetRes.version = "".concat(targetRes.version.replace(/-snapshot/gi, ''), "-snapshot"); var newRes = JSON.stringify(targetRes, null, ' '); // Write Target BundleJson _fs["default"].writeFileSync(targetPath, newRes, 'utf-8'); } function packBundle(bundle) { var tgz = "".concat(bundle.id, ".tgz"); if (_fs["default"].existsSync(tgz)) { (0, _helpers.runShellCmd)(function (shell) { return shell.rm('-rf', tgz); }); } var prePackCmd = bundle.scripts(_constant.Hooks.prePack); if (prePackCmd) { (0, _helpers.runCmdWithSpawnSync)(function (spawnSync) { return spawnSync(prePackCmd, { shell: process.env.shellPath, env: bundle.vars.env, cwd: bundle.path, stdio: 'inherit' }); }); } _events["default"].emit('hooks', { type: _events.EventTypes.beforePack, name: bundle.name, path: bundle.path }); bundle.copyReadme(); _i18n["default"].log('pack.step1', { tgz: tgz, bundlePath: bundle.path }); var base = _path["default"].join((0, _helpers.runShellCmd)(function (shell) { return shell.tempdir(); }, false), "".concat(bundle.id, "-").concat(Date.now())); (0, _helpers.runShellCmd)(function (shell) { return shell.mkdir('-p', base); }); var files = bundle.pack(); var dirname = _path["default"].parse(bundle.path).base; makeFiles(files, base, dirname, bundle); var temSourcePath = ['code-segment', 'template'].some(bundle.isPublishAs.bind(bundle)) ? _path["default"].join(base, dirname) : base; var tarFiles = _fs["default"].readdirSync(temSourcePath); if (bundle.isPublishAs('chip-definition') && !isXmlExisted(tarFiles, temSourcePath)) { throw (0, _i18n.getI18nMessage)('bundle.fileRequired', { name: '*.xml' }); } tarFiles = tarFiles.map(function (f) { return f.startsWith('@') ? "./".concat(f) : f; }); // tar-node _tar["default"].create({ gzip: true, file: tgz, cwd: temSourcePath, sync: true }, tarFiles); _i18n["default"].log('pack.step4', { tgz: tgz }); var afterPackCmd = bundle.scripts(_constant.Hooks.pack) || bundle.scripts(_constant.Hooks.afterPack); if (afterPackCmd) { (0, _helpers.runCmdWithSpawnSync)(function (spawnSync) { return spawnSync(afterPackCmd, { shell: process.env.shellPath, env: bundle.vars.env, cwd: bundle.path, stdio: 'inherit' }); }); } _events["default"].emit('hooks', { type: _events.EventTypes.afterPack, name: bundle.name, path: bundle.path }); if (_fs["default"].existsSync(base)) { (0, _helpers.runShellCmd)(function (shell) { return shell.rm('-rf', base); }); } } function isXmlExisted(files, sourcePath) { return files.find(function (file) { return file.endsWith('.xml') && _fs["default"].statSync(_path["default"].resolve(sourcePath, file)).isFile(); }); } function makeFiles(files, base, dirname, bundle) { if (!files) { var baseDir = _path["default"].resolve(base, dirname); (0, _helpers.runShellCmd)(function (shell) { return shell.mkdir('-p', baseDir); }); // cp的时候过滤掉ohos_bundles var excludeDepFolder = _fs["default"].readdirSync(_path["default"].resolve(bundle.path)).filter(function (item) { return item !== _constant.DEP_FOLDER_NAME; }).map(function (item) { return _path["default"].resolve(bundle.path, item); }); (0, _helpers.runShellCmd)(function (shell) { return shell.cp('-R', excludeDepFolder, baseDir); }); } else { var _loop = function _loop(dir) { _i18n["default"].log('pack.step2', { dir: dir }); (0, _helpers.runShellCmd)(function (shell) { return shell.mkdir('-p', _path["default"].join(base, dir)); }); var _iterator = _createForOfIteratorHelper(files[dir]), _step; try { var _loop2 = function _loop2() { var file = _step.value; var fileRelativePath = _path["default"].relative(bundle.path, file); _i18n["default"].log('pack.step3', { fileRelativePath: fileRelativePath }); (0, _helpers.runShellCmd)(function (shell) { return shell.cp('-R', file, _path["default"].join(base, dir)); }); }; for (_iterator.s(); !(_step = _iterator.n()).done;) { _loop2(); } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } }; for (var dir in files) { _loop(dir); } } }