UNPKG

@feflow/cli

Version:
132 lines 5.34 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FefError = void 0; var fs_1 = require("fs"); var path_1 = require("path"); var chalk_1 = __importDefault(require("chalk")); var lodash_1 = require("lodash"); var file_1 = require("./file"); var command_picker_1 = require("../core/command-picker"); // 不用强制中断流程 var FefError = /** @class */ (function () { function FefError(context) { this.defaultDocs = 'https://github.com/Tencent/feflow/issues'; this.docsPathList = ['docs', 'doc', 'bugs.url', 'repository.url']; this.pluginFile = 'package.json'; this.unversalpluginFile = ['plugin.yaml', 'plugin.yml']; this.context = context; // 区分npm插件和多语言插件 // npm插件 => package.json // 多语言插件 => plugin.yaml if (this.context.commandPick) { this.picker = context.commandPick; } else { this.picker = null; context.logger.debug('command picker is not initialized'); } } FefError.prototype.checkPick = function () { if (this.picker) return true; if (this.context.commandPick) { this.picker = this.context.commandPick; } return !!this.picker; }; FefError.prototype.printError = function (obj) { var _a; var pluginPath = obj.pluginPath; if (!pluginPath) { if (!this.checkPick()) { this.context.logger.debug('无法找到命令路径'); } else { this.context.logger.debug('FefError command picker 初始化成功'); } } var docs = this.getDocPath(pluginPath); var cmdType = (((_a = this.picker) === null || _a === void 0 ? void 0 : _a.getCmdInfo()) || { type: command_picker_1.CommandType.PLUGIN_TYPE }).type; if (docs) { this.printErrorWithDocs(obj, docs); } else { var error = obj.error; var msg = obj.msg; if (!obj.hideError) { msg = "".concat(msg || error); this.context.logger[[command_picker_1.CommandType.UNIVERSAL_PLUGIN_TYPE, command_picker_1.CommandType.UNKNOWN_TYPE].includes(cmdType) ? 'debug' : 'error']({ err: error }, msg, chalk_1.default.magenta("".concat(error))); } else if (msg) { // 兼容多语言插件 this.context.logger.info("".concat(msg)); } } }; FefError.prototype.printErrorWithDocs = function (obj, docs) { var error = obj.error; var msg = obj.msg; if (!obj.hideError) { msg = "".concat(msg || error, "\n \u63D2\u4EF6\u6267\u884C\u53D1\u751F\u5F02\u5E38\uFF0C\u8BF7\u67E5\u770B\u6587\u6863\u83B7\u53D6\u66F4\u591A\u5185\u5BB9\uFF1A").concat(chalk_1.default.green(docs)); this.context.logger.error({ err: error }, msg, chalk_1.default.magenta("".concat(error))); } else { // 兼容多语言插件 msg = "".concat(msg, " \u8BF7\u67E5\u770B\u6587\u6863\u83B7\u53D6\u66F4\u591A\u5185\u5BB9\uFF1A").concat(chalk_1.default.green(docs)); this.context.logger.info(msg); } }; FefError.prototype.getDocPath = function (pluginPath) { var docs = ''; var configPath = ''; var type = command_picker_1.CommandType.PLUGIN_TYPE; var finalPluginPath = ''; if (!pluginPath) { if (this.picker !== null) { var _a = this.picker.getCmdInfo(), path = _a.path, cmdType = _a.type; finalPluginPath = path; type = cmdType; } else { return docs; } } if (!(0, fs_1.existsSync)(finalPluginPath)) { return docs; } if (type === command_picker_1.CommandType.PLUGIN_TYPE) { configPath = (0, path_1.join)(finalPluginPath, this.pluginFile); } else if (type === command_picker_1.CommandType.UNIVERSAL_PLUGIN_TYPE) { this.unversalpluginFile.forEach(function (ext) { var tmpPath = (0, path_1.join)(finalPluginPath, ext); if ((0, fs_1.existsSync)(tmpPath)) configPath = tmpPath; }); } else if (type === command_picker_1.CommandType.NATIVE_TYPE) { return this.defaultDocs; } if ((0, fs_1.existsSync)(configPath)) { var config_1 = this.parseConfig(configPath); this.docsPathList.forEach(function (docsPath) { if (docs) return; docs = (0, lodash_1.get)(config_1, docsPath); }); } else { this.context.logger.debug("\u672A\u627E\u5230\u63D2\u4EF6\u914D\u7F6E\u6587\u4EF6: ".concat(finalPluginPath)); } return docs; }; FefError.prototype.parseConfig = function (filePath) { return file_1.Config.loadConfigFile(filePath); }; return FefError; }()); exports.FefError = FefError; //# sourceMappingURL=fef-error.js.map