UNPKG

cli-engine

Version:
97 lines (73 loc) 2.38 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _cliEngineCommand = require('cli-engine-command'); var _cliEngineCommand2 = _interopRequireDefault(_cliEngineCommand); var _manager = require('./manager'); var _path = require('path'); var _path2 = _interopRequireDefault(_path); var _cliUx = require('cli-ux'); require('./cache'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const debug = require('debug')('cli:plugins'); class Plugin { constructor(config, pluginPath, cachedPlugin) { this.config = config; this.cli = new _cliUx.CLI({ mock: config.mock }); this.pluginPath = pluginPath; this.cachedPlugin = cachedPlugin; } get tag() { return this.pluginPath.tag; } get type() { return this.pluginPath.type; } get path() { return this.pluginPath.path; } get name() { return this.cachedPlugin.name; } get version() { return this.cachedPlugin.version; } get commands() { return this.cachedPlugin.commands; } get topics() { return this.cachedPlugin.topics; } async findCachedCommand(id) { if (!id) return; return this.commands.find(c => c.id === id || (c.aliases || []).includes(id)); } async findCommand(id) { const cachedCommand = await this.findCachedCommand(id); if (!cachedCommand) return; let { topic, command } = cachedCommand; let p = await this.pluginPath.require(); let Command = (p.commands || []).find(d => topic === d.topic && command === d.command); if (!Command) return; return typeof Command === 'function' ? Command : this.convertFromV5(Command); } async findTopic(id) { let t = this.topics.find(t => t.id === id); if (!t) return; return this.buildTopic(t); } buildTopic(t) { var _class, _temp; return _temp = _class = class extends _cliEngineCommand.Topic {}, _class.topic = t.id, _class.description = t.description, _class.hidden = t.hidden, _temp; } convertFromV5(command) { if (!this.config.legacyConverter) { debug(command); throw new Error('received v5 command but no legacyConverter was specified'); } const converter = require(_path2.default.join(this.config.root, this.config.legacyConverter)); return converter.convertFromV5(command); } } exports.default = Plugin;