UNPKG

@ohos/hpm-cli

Version:

CLI for HarmonyOS package manager

135 lines (134 loc) 6.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getNpmManifests = getNpmManifests; exports.registerPluginHooks = registerPluginHooks; exports.resolveCmdPlugins = resolveCmdPlugins; exports.resolveNpmPlugins = resolveNpmPlugins; var _path = _interopRequireDefault(require("path")); var _repository = require("../core/repository"); var _constant = require("../constant/constant"); var _config = require("../utils/config"); var _pluginAPI = require("./pluginAPI"); var _fs = _interopRequireDefault(require("fs")); var _events = _interopRequireDefault(require("../utils/events")); var _i18n = _interopRequireDefault(require("../i18n")); var _log = _interopRequireDefault(require("../core/log")); var _helpers = require("../utils/helpers"); var _package = _interopRequireDefault(require("../../package.json")); var _manifest = require("../core/manifest"); var _excluded = ["type"]; /* * 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 _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 _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var s = Object.getOwnPropertySymbols(e); for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; } function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; } function resolveCmdPlugins(pluginAPI) { try { var cwd = process.cwd(); var repo = new _repository.Repository(_path["default"].join(cwd, _constant.DEP_FOLDER_NAME), (0, _config.getGlobalRepo)()); var manifests = repo.manifests(filterPluginFolder).concat(repo.globalManifests(filterPluginFolder)); manifests.filter(_pluginAPI.isPlugin).filter(function (manifest) { return !pluginAPI.getDisabledPlugins().includes(manifest.name); }).forEach(function (manifest) { requireEntry(manifest, manifest.path, pluginAPI); }); } catch (error) { _i18n["default"].log('plugin.resolveError', { error: (0, _helpers.getErrorMessage)(error) }); _log["default"].debug(error); } } function resolveNpmPlugins(pluginAPI) { var manifests = getNpmManifests(); manifests.forEach(function (m) { requireEntry(m, m.path, pluginAPI); }); } function registerPluginHooks(pluginAPI) { _events["default"].on('hooks', function (_ref) { var type = _ref.type, args = _objectWithoutProperties(_ref, _excluded); var hooks = pluginAPI.getHooks(); if (hooks[type]) { try { var _iterator = _createForOfIteratorHelper(hooks[type]), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var hook = _step.value; hook(args); } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } } catch (error) { _i18n["default"].log('plugin.hooksError', { error: (0, _helpers.getErrorMessage)(error), type: type }); } } }); } function getNpmManifests() { var deps = _package["default"].dependencies; var manifests = []; if (deps) { var names = Object.keys(deps); var npmPlugins = []; names.forEach(function (n) { var _parseScopeName = (0, _helpers.parseScopeName)(n), scope = _parseScopeName.scope, name = _parseScopeName.name; if (scope && name.startsWith(_constant.NPM_PLUGIN_PREFIX)) { npmPlugins.push({ scope: scope, name: name }); } }); npmPlugins.forEach(function (_ref2) { var scope = _ref2.scope, name = _ref2.name; var pluginPath = _path["default"].join(__dirname, '..', '..', 'node_modules', scope, name); if (_fs["default"].existsSync(_path["default"].join(pluginPath, 'bundle.json'))) { var manifest = _manifest.Manifest.fromPath(pluginPath); manifest.path = pluginPath; manifests.push(manifest); } }); } return manifests; } function requireEntry(manifest, projectPath, pluginAPI) { var entryFileName = manifest.plugin && manifest.plugin.entry; if (entryFileName) { var entryFilePath = _path["default"].resolve(_path["default"].join(projectPath, entryFileName)); if (_fs["default"].existsSync(entryFilePath)) { require(entryFilePath)(pluginAPI); } } } function filterPluginFolder(folderName) { return !!(folderName && (folderName.startsWith(_constant.PLUGIN_PREFIX) || folderName.startsWith(_constant.UI_PLUGIN_PREFIX))); }