Skip to content

Commit beb7826

Browse files
committed
chore: ask the questions imperatively
1 parent 98a048b commit beb7826

File tree

1 file changed

+39
-48
lines changed
  • packages/create-react-native-library/src

1 file changed

+39
-48
lines changed

packages/create-react-native-library/src/index.ts

Lines changed: 39 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -472,58 +472,49 @@ async function create(_argv: yargs.Arguments<any>) {
472472

473473
assertOptions(questions, argv);
474474

475-
const singleChoiceAnswers: Partial<Answers> = {};
475+
const promptAnswers: Partial<Answers> = {};
476476

477-
const answers = {
478-
...argv,
479-
local,
480-
...(await prompts(
481-
questions
482-
.filter((question) => {
483-
// Skip questions which are passed as parameter and pass validation
484-
if (
485-
argv[question.name] != null &&
486-
question.validate?.(argv[question.name]) !== false
487-
) {
488-
return false;
489-
}
477+
for (const question of questions) {
478+
// Skip questions which are passed as parameter and pass validation
479+
if (
480+
argv[question.name] != null &&
481+
question.validate?.(argv[question.name]) !== false
482+
) {
483+
continue;
484+
}
490485

491-
// Skip questions with a single choice
492-
if (
493-
Array.isArray(question.choices) &&
494-
question.choices.length === 1
495-
) {
496-
const onlyChoice = question.choices[0]!;
497-
singleChoiceAnswers[question.name] = onlyChoice.value;
498-
return false;
499-
}
486+
// Don't prompt questions with a single choice
487+
if (Array.isArray(question.choices) && question.choices.length === 1) {
488+
const onlyChoice = question.choices[0]!;
489+
promptAnswers[question.name] = onlyChoice.value;
500490

501-
return true;
502-
})
503-
.map((question) => {
504-
const { type, choices } = question;
505-
506-
// Skip dynamic questions with a single choice
507-
if (type === 'select' && typeof choices === 'function') {
508-
return {
509-
...question,
510-
type: (prev, values, prompt) => {
511-
const result = choices(prev, { ...argv, ...values }, prompt);
512-
if (result && result.length === 1) {
513-
const onlyChoice = result[0]!;
514-
singleChoiceAnswers[question.name] = onlyChoice.value;
515-
return null;
516-
}
517-
518-
return type;
519-
},
520-
};
521-
}
491+
continue;
492+
}
522493

523-
return question;
524-
})
525-
)),
526-
...singleChoiceAnswers,
494+
// Don't prompt dynamic questions with a single choice
495+
if (question.type === 'select' && typeof question.choices === 'function') {
496+
const dynamicChoices = question.choices(
497+
null,
498+
{ ...argv, ...promptAnswers },
499+
question
500+
);
501+
502+
if (dynamicChoices && dynamicChoices.length === 1) {
503+
const onlyChoice = dynamicChoices[0]!;
504+
promptAnswers[question.name] = onlyChoice.value;
505+
506+
continue;
507+
}
508+
}
509+
510+
const answer = await prompts(question);
511+
promptAnswers[question.name] = answer[question.name];
512+
}
513+
514+
const answers = {
515+
...argv,
516+
local,
517+
...promptAnswers,
527518
} as Answers;
528519

529520
assertOptions(questions, answers);

0 commit comments

Comments
 (0)