UNPKG

esa-cli

Version:

A CLI for operating Alibaba Cloud ESA Functions and Pages.

98 lines (97 loc) 3.93 kB
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()); }); }; import { confirm as clackConfirm, isCancel, multiselect as clackMultiselect, select as clackSelect, text as clackText, cancel as clackCancel } from '@clack/prompts'; import multiLevelSelect from '../components/mutiLevelSelect.js'; function normalizeChoices(choices) { if (!choices) return undefined; return choices.map((c) => { if (typeof c === 'string') return { label: c, value: c }; return { label: c.name, value: String(c.value), hint: c.hint }; }); } export function promptParameter(param) { return __awaiter(this, void 0, void 0, function* () { var _a; const { type, question, defaultValue, validate } = param; let value = ''; const msg = question; if (type === 'text') { const v = yield clackText({ message: msg, placeholder: typeof defaultValue === 'string' ? defaultValue : undefined, initialValue: typeof defaultValue === 'string' ? defaultValue : undefined, validate: validate ? (val) => { if (val === undefined) return 'Value is required'; const res = validate(val); return res === true ? undefined : res; } : undefined }); if (isCancel(v)) { clackCancel('Operation cancelled.'); process.exit(130); } value = v; } else if (type === 'confirm') { const v = yield clackConfirm({ message: msg, initialValue: (_a = defaultValue) !== null && _a !== void 0 ? _a : false }); if (isCancel(v)) { clackCancel('Operation cancelled.'); process.exit(130); } value = v; } else if (type === 'select') { const options = normalizeChoices(param.choices) || []; const v = yield clackSelect({ message: msg, options, initialValue: defaultValue || undefined }); if (isCancel(v)) { clackCancel('Operation cancelled.'); process.exit(130); } value = v; } else if (type === 'multiselect') { const options = normalizeChoices(param.choices) || []; const initialValues = (defaultValue || []).map((v) => String(v)); const v = yield clackMultiselect({ message: msg, options, initialValues }); if (isCancel(v)) { clackCancel('Operation cancelled.'); process.exit(130); } value = v; } else if (type === 'multiLevelSelect') { const items = (param.treeItems || []); const v = yield multiLevelSelect(items, msg); if (v === null) { clackCancel('Operation cancelled.'); process.exit(130); } value = v; } return value; }); } export default promptParameter;