mihawk
Version:
A tiny & simple mock server tool, support json,js,cjs,ts(typescript).
69 lines (68 loc) • 2.12 kB
JavaScript
;
import prompts from 'prompts';
import minimist from 'minimist';
import { Printer } from './print';
export function getCliArgs(processArgv = process.argv, options) {
const { logDetail = false } = options || {};
if (!processArgv?.length) {
processArgv = process.argv || [];
}
const res = minimist(processArgv.slice(2));
if (logDetail) {
Printer.log('[getCliArgs] processArgv:', processArgv);
Printer.log('[getCliArgs] return:', res);
}
return res;
}
export async function confirmInCLI(question, defaultYes) {
const { sure = false } = await prompts({
type: 'toggle',
name: 'sure',
initial: defaultYes,
active: 'yes',
inactive: 'no',
message: question || 'Are you sure?',
});
return sure;
}
export async function inputTxtInCLI(promptTxt, initial) {
const { txt } = await prompts({
type: 'text',
name: 'txt',
message: promptTxt || 'Input your text:',
initial,
});
return txt;
}
export async function inputNumInCLI(promptTxt, options) {
const { num } = await prompts({
type: 'number',
name: 'num',
message: promptTxt || 'Input your number:',
...options,
});
return num;
}
export async function selectInCli(promptTxt, choices, options) {
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 } = await prompts(config);
return arr;
}
export async function singleSelectInCli(promptTxt, choices, initial) {
const res = await selectInCli(promptTxt, choices, { multiple: false, initial });
return res;
}
export async function multipleSelectInCli(promptTxt, choices, initial) {
const res = await selectInCli(promptTxt, choices, { multiple: true, initial });
return res;
}