UNPKG

mihawk

Version:

A tiny & simple mock server tool, support json,js,cjs,ts(typescript).

97 lines (96 loc) 4.08 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 }); exports.multipleSelectInCli = exports.singleSelectInCli = exports.selectInCli = exports.inputNumInCLI = exports.inputTxtInCLI = exports.confirmInCLI = exports.getCliArgs = void 0; const prompts_1 = __importDefault(require("prompts")); const minimist_1 = __importDefault(require("minimist")); const print_1 = require("./print"); function getCliArgs(processArgv = process.argv, options) { const { logDetail = false } = options || {}; if (!(processArgv === null || processArgv === void 0 ? void 0 : processArgv.length)) { processArgv = process.argv || []; } const res = (0, minimist_1.default)(processArgv.slice(2)); if (logDetail) { print_1.Printer.log('[getCliArgs] processArgv:', processArgv); print_1.Printer.log('[getCliArgs] return:', res); } return res; } exports.getCliArgs = getCliArgs; function confirmInCLI(question, defaultYes) { return __awaiter(this, void 0, void 0, function* () { const { sure = false } = yield (0, prompts_1.default)({ type: 'toggle', name: 'sure', initial: defaultYes, active: 'yes', inactive: 'no', message: question || 'Are you sure?', }); return sure; }); } exports.confirmInCLI = confirmInCLI; function inputTxtInCLI(promptTxt, initial) { return __awaiter(this, void 0, void 0, function* () { const { txt } = yield (0, prompts_1.default)({ type: 'text', name: 'txt', message: promptTxt || 'Input your text:', initial, }); return txt; }); } exports.inputTxtInCLI = inputTxtInCLI; function inputNumInCLI(promptTxt, options) { return __awaiter(this, void 0, void 0, function* () { const { num } = yield (0, prompts_1.default)(Object.assign({ type: 'number', name: 'num', message: promptTxt || 'Input your number:' }, options)); return num; }); } exports.inputNumInCLI = inputNumInCLI; function selectInCli(promptTxt, choices, options) { return __awaiter(this, void 0, void 0, function* () { const { initial, multiple } = options || {}; const config = { type: multiple ? 'multiselect' : 'select', name: 'arr', message: promptTxt || `Select ${multiple ? 'some choices' : 'just one choice'}:`, choices, initial, }; if (multiple) { config.hint = '- Space to select. Return to submit'; } const { arr } = yield (0, prompts_1.default)(config); return arr; }); } exports.selectInCli = selectInCli; function singleSelectInCli(promptTxt, choices, initial) { return __awaiter(this, void 0, void 0, function* () { const res = yield selectInCli(promptTxt, choices, { multiple: false, initial }); return res; }); } exports.singleSelectInCli = singleSelectInCli; function multipleSelectInCli(promptTxt, choices, initial) { return __awaiter(this, void 0, void 0, function* () { const res = yield selectInCli(promptTxt, choices, { multiple: true, initial }); return res; }); } exports.multipleSelectInCli = multipleSelectInCli;