create-react-native-library
Version:
CLI to scaffold React Native libraries
78 lines (75 loc) • 2.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.prompt = prompt;
var _prompts = _interopRequireDefault(require("prompts"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/**
* Wrapper around `prompts` with additional features:
*
* - Improved type-safety
* - Read answers from passed arguments
* - Skip questions with a single choice
* - Exit on canceling the prompt
*/
async function prompt(questions, argv, options) {
const singleChoiceAnswers = {};
const promptQuestions = [];
if (Array.isArray(questions)) {
for (const question of questions) {
// Skip questions which are passed as parameter and pass validation
const argValue = argv?.[question.name];
if (argValue && question.validate?.(argValue) !== false) {
continue;
}
const {
type,
choices
} = question;
// Don't prompt questions with a single choice
if (type === 'select' && Array.isArray(question.choices) && question.choices.length === 1) {
const onlyChoice = question.choices[0];
if (onlyChoice?.value) {
// @ts-expect-error assume the passed value is correct
singleChoiceAnswers[question.name] = onlyChoice.value;
}
continue;
}
// Don't prompt dynamic questions with a single choice
if (type === 'select' && typeof choices === 'function') {
question.type = (prev, values) => {
const dynamicChoices = choices(prev, {
...argv,
...values
});
if (dynamicChoices && dynamicChoices.length === 1) {
const onlyChoice = dynamicChoices[0];
if (onlyChoice?.value) {
// @ts-expect-error assume the passed value is correct
singleChoiceAnswers[question.name] = onlyChoice.value;
}
return null;
}
return type;
};
}
promptQuestions.push(question);
}
} else {
promptQuestions.push(questions);
}
const promptAnswers = await (0, _prompts.default)(promptQuestions, {
onCancel() {
// Exit the CLI on cancel
process.exit(1);
},
...options
});
return {
...argv,
...singleChoiceAnswers,
...promptAnswers
};
}
//# sourceMappingURL=prompt.js.map