UNPKG

@mail-core/cli

Version:

Инструментарий для написания cli-скриптов

168 lines 5.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.select = exports.input = exports.confirm = exports.interactive = exports.createInteractiveTools = exports.interactiveToolNewLine = void 0; const inquirer_1 = require("inquirer"); const color_1 = require("../color"); const dottedNewLine = '·'.repeat(10); function getQuestionType(q) { if (q.type) { return q.type; } if (Array.isArray(q.choices)) { return 'list'; } if (typeof q.value === 'boolean') { return 'confirm'; } return 'input'; } function isQuestion(val) { return val && typeof val === 'object' && 'message' in val && 'value' in val; } function isQuestionGroup(val) { return val && typeof val === 'object' && isQuestion(Object.values(val)[0]); } function questionGroupWalker(questions, iterator) { (function next(xpath, questions) { Object.entries(questions).forEach(([name, rest]) => { if (isQuestionGroup(rest)) { next(xpath.concat(name), rest); } else { iterator(xpath.concat(name), rest); } }); })([], questions); } exports.interactiveToolNewLine = { before: false, after: false, touch(label) { exports.interactiveToolNewLine.before = true; if (exports.interactiveToolNewLine.after) { exports.interactiveToolNewLine.after = false; console.log(color_1.gray(label || '', dottedNewLine)); } }, }; function castOption(option, val) { if (option.type === 'boolean') { return Boolean(val); } else if (option.type === 'number') { return Number(val); } return String(val); } function createInteractiveTools(init = {}) { const baseProps = { prefix: init.prefix ? `${init.prefix} ${color_1.bold(color_1.green('?'))}` : undefined, }; async function interactive(questions) { const list = []; questionGroupWalker(questions, (xpath, rest) => { list.push(Object.assign(Object.assign(Object.assign({}, baseProps), { name: xpath, type: getQuestionType(rest), default: rest.value === '' ? undefined : rest.value }), rest)); }); exports.interactiveToolNewLine.after = true; if (exports.interactiveToolNewLine.before) { exports.interactiveToolNewLine.before = false; console.log(color_1.gray(init.prefix || '', dottedNewLine)); } const answers = await inquirer_1.prompt(list); questionGroupWalker(questions, (xpath, rest) => { let i = 0; let cursor = answers; for (; i < xpath.length - 1; i++) { const k = xpath[i]; if (cursor[k] === undefined) { cursor[k] = {}; } cursor = cursor[k]; } if (cursor[xpath[i]] === undefined) { cursor[xpath[i]] = rest.value; } }); return answers; } async function confirm(msg, value) { const { input } = await interactive({ input: { message: msg, value, type: 'confirm', }, }); return input; } async function input(msg, value) { const { input } = await interactive({ input: { message: msg, value, type: 'input', }, }); return input; } async function select(msg, value, choices, type = 'list') { const { input } = await interactive({ input: { message: msg, value, type, choices, }, }); return input; } async function require(option, configOrDemandOption) { const config = typeof configOrDemandOption === 'object' ? configOrDemandOption : { allowEmpty: false, }; const desc = option.desc || option.describe || option.description; const msg = `${desc}:`; const defaultValue = option.default; const hasDefault = defaultValue != null; const isDefault = hasDefault && defaultValue === option.value; let value = option.value; // Если не `--yes`, то спрашиваем юзера if (!init.yes && (value == null || isDefault && !config.allowDefault)) { if (option.type === 'boolean') { value = await confirm(msg, defaultValue); } else if (option.choices != null) { const type = option.type === 'array' || option.array ? 'checkbox' : 'list'; const items = option.choices.map((value) => ({ value: value + '', name: value + '', })); value = await select(msg, defaultValue, items, type); } else { value = await input(msg, defaultValue); } } // Проверяем значение на «пустату» if ((init.yes || config.demandOption) && ((value == '' && !config.allowEmpty) || (value == null))) { throw new Error(`"${option.name}" — "${desc}" required`); } return castOption(option, value); } return { interactive, confirm, input, select, require, }; } exports.createInteractiveTools = createInteractiveTools; const tools = createInteractiveTools(); exports.interactive = tools.interactive; exports.confirm = tools.confirm; exports.input = tools.input; exports.select = tools.select; //# sourceMappingURL=interactive.js.map