UNPKG

zcatalyst-cli

Version:

Command Line Tool for CATALYST

80 lines (79 loc) 3.72 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const ansi_colors_1 = require("ansi-colors"); const path_1 = require("path"); const error_1 = __importDefault(require("../../error")); const prompt_1 = __importDefault(require("../../prompt")); const constants_1 = require("../../util_modules/constants"); const fs_1 = require("../../util_modules/fs"); const { handlers_event_template } = constants_1.CLIQ; exports.default = (inputs) => __awaiter(void 0, void 0, void 0, function* () { const input = inputs.split(','); let component; let handler; if (input.length === 1 && input[0] !== '') { throw new error_1.default('incorrect input for event cliq', { exit: 1, errorId: 'CLIQ-1', arg: [(0, ansi_colors_1.bold)('[component-type,handler]')] }); } else if (input[0] === '') { const componentChoices = Object.keys(handlers_event_template).map((component) => { return prompt_1.default.choice(component, { value: component, short: component }); }); const componenetsQuestion = yield prompt_1.default.ask(prompt_1.default.question('component', 'Please select any one of the available components', { type: 'list', choices: componentChoices })); component = componenetsQuestion.component; const handlerChoices = []; handlers_event_template[component].forEach((handler) => { handlerChoices.push(prompt_1.default.choice(handler, { value: handler, short: handler })); }); const handlerQuestion = yield prompt_1.default.ask(prompt_1.default.question('handler', 'Please select any one of the following handlers to continue', { type: 'list', choices: handlerChoices })); handler = handlerQuestion.handler; } else { const inputComponent = input[0]; const inputHandler = input[1]; if (handlers_event_template[inputComponent] !== undefined && handlers_event_template[inputComponent].includes(inputHandler)) { component = inputComponent; handler = inputHandler; } else { throw new error_1.default('invalid input combination', { exit: 1, errorId: 'CLIQ-2', arg: [(0, ansi_colors_1.bold)(input[0]), ansi_colors_1.bold.red('X'), (0, ansi_colors_1.bold)(input[1]), (0, ansi_colors_1.bold)('--inputs')] }); } } const filePath = (0, path_1.join)(constants_1.CLIQ.event_template_dir, component, handler + '.json'); if (!fs_1.ASYNC.fileExists(filePath)) { throw new error_1.default("Couldn't read request template", { exit: 2 }); } return yield fs_1.ASYNC.readFile(filePath); });