UNPKG

@commercelayer/cli

Version:
93 lines (92 loc) 4.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const cli_core_1 = require("@commercelayer/cli-core"); const errors_1 = require("@oclif/core/lib/errors"); const inquirer_1 = tslib_1.__importDefault(require("inquirer")); const available_1 = require("../../commands/plugins/available"); const hook = async function (opts) { // Only for test purpouses to avoid an error of undefined object if (!opts.Command || !opts.argv) return; // Only plugins commands are affected by this hook if (!opts.Command.id.startsWith('plugins')) return; if (['plugins:install', 'plugins:uninstall'].includes(opts.Command.id)) { const command = opts.Command.id.replace('plugins:', ''); // Check tag flag const tgIndex = opts.argv.indexOf('--tag'); let tag; if (tgIndex > -1) { tag = opts.argv[tgIndex + 1]; opts.argv.splice(tgIndex, 2); } if (opts.argv.length === 0) { const arg = await promptPlugin(this.config, command) .catch((error) => { this.error(error); }); if (arg) opts.argv[0] = arg; else { this.log(`\nAll Commerce Layer CLI plugins have already been ${command}ed\n`); throw new errors_1.CLIError('HOOK_EXIT'); } } let index = -1; let plugin = ''; let pluginArg = ''; const found = opts.argv.some(a => { index++; if (a.startsWith('-')) return false; // ignore flags if (opts.argv[index - 1] === '--tag') return false; // ignore --tag value pluginArg = a; const p = (0, available_1.getPluginInfo)(pluginArg); if (p === undefined) this.error(`Unknown Commerce Layer CLI plugin: ${cli_core_1.clColor.msg.error(a)}. Run '${cli_core_1.clColor.italic(`${this.config.bin} plugins:available`)}' to get a list of all available plugins`); else plugin = p.plugin; return true; }); if (found && plugin) { let errMsg = ''; if ((command === 'install') && (0, available_1.isPluginInstalled)(plugin, this.config)) errMsg = 'Commerce Layer CLI plugin already installed'; else if ((command === 'uninstall') && !(0, available_1.isPluginInstalled)(plugin, this.config)) errMsg = 'Commerce Layer CLI plugin not installed'; if (errMsg) { this.log(`\n${errMsg}: ${cli_core_1.clColor.cli.plugin(pluginArg)}\n`); throw new errors_1.CLIError('HOOK_EXIT'); } else this.log(''); // Set version if (tgIndex > -1) plugin = `${plugin}@${tag}`; // Overwrite plugin short name whith its full name opts.argv[index] = plugin; } else this.error('No Commerce Layer CLI plugin to ' + command); } }; const promptPlugin = async (config, command) => { const installed = (0, available_1.getInstalledPlugins)(config); const plugins = (command === 'install') ? (0, available_1.getAvailablePlugins)().filter(p => !installed.includes(p) && !p.hidden) : installed; if (plugins.length === 0) return ''; const plgMaxLength = cli_core_1.clOutput.maxLength(plugins, 'name') + 4; plugins.sort((a, b) => a.name.localeCompare(b.name)); const answers = await inquirer_1.default.prompt([{ type: 'list', name: 'plugin', message: `Select a plugin to ${command}:`, choices: plugins.map(p => { return { name: `${p.name.padEnd(plgMaxLength, ' ')} ${cli_core_1.clColor.italic(p.description)}`, value: p.plugin }; }), loop: false, pageSize: 10, }]); return answers.plugin; }; exports.default = hook;