Skip to content

Commit 6aff2c6

Browse files
committed
refactor: make questions an array
1 parent 63e90ef commit 6aff2c6

File tree

1 file changed

+28
-24
lines changed
  • packages/create-react-native-library/src

1 file changed

+28
-24
lines changed

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

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -361,13 +361,11 @@ async function create(_argv: yargs.Arguments<any>) {
361361

362362
const basename = path.basename(folder);
363363

364-
const questions: Record<
365-
keyof Answers,
366-
Omit<PromptObject<keyof Answers>, 'validate'> & {
367-
validate?: (value: string) => boolean | string;
368-
}
369-
> = {
370-
slug: {
364+
const questions: (Omit<PromptObject<keyof Answers>, 'validate' | 'name'> & {
365+
validate?: (value: string) => boolean | string;
366+
name: string;
367+
})[] = [
368+
{
371369
type: 'text',
372370
name: 'slug',
373371
message: 'What is the name of the npm package?',
@@ -380,28 +378,28 @@ async function create(_argv: yargs.Arguments<any>) {
380378
validateNpmPackage(input).validForNewPackages ||
381379
'Must be a valid npm package name',
382380
},
383-
description: {
381+
{
384382
type: 'text',
385383
name: 'description',
386384
message: 'What is the description for the package?',
387385
validate: (input) => Boolean(input) || 'Cannot be empty',
388386
},
389-
authorName: {
387+
{
390388
type: local ? null : 'text',
391389
name: 'authorName',
392390
message: 'What is the name of package author?',
393391
initial: name,
394392
validate: (input) => Boolean(input) || 'Cannot be empty',
395393
},
396-
authorEmail: {
394+
{
397395
type: local ? null : 'text',
398396
name: 'authorEmail',
399397
message: 'What is the email address for the package author?',
400398
initial: email,
401399
validate: (input) =>
402400
/^\S+@\S+$/.test(input) || 'Must be a valid email address',
403401
},
404-
authorUrl: {
402+
{
405403
type: local ? null : 'text',
406404
name: 'authorUrl',
407405
message: 'What is the URL for the package author?',
@@ -419,7 +417,7 @@ async function create(_argv: yargs.Arguments<any>) {
419417
},
420418
validate: (input) => /^https?:\/\//.test(input) || 'Must be a valid URL',
421419
},
422-
repoUrl: {
420+
{
423421
type: local ? null : 'text',
424422
name: 'repoUrl',
425423
message: 'What is the URL for the repository?',
@@ -434,13 +432,13 @@ async function create(_argv: yargs.Arguments<any>) {
434432
},
435433
validate: (input) => /^https?:\/\//.test(input) || 'Must be a valid URL',
436434
},
437-
type: {
435+
{
438436
type: 'select',
439437
name: 'type',
440438
message: 'What type of library do you want to develop?',
441439
choices: TYPE_CHOICES,
442440
},
443-
languages: {
441+
{
444442
type: 'select',
445443
name: 'languages',
446444
message: 'Which languages do you want to use?',
@@ -454,15 +452,15 @@ async function create(_argv: yargs.Arguments<any>) {
454452
});
455453
},
456454
},
457-
};
455+
];
458456

459457
// Validate arguments passed to the CLI
460458
for (const [key, value] of Object.entries(argv)) {
461459
if (value == null) {
462460
continue;
463461
}
464462

465-
const question = questions[key as keyof Answers];
463+
const question = questions.find((q) => q.name === key);
466464

467465
if (question == null) {
468466
continue;
@@ -504,27 +502,33 @@ async function create(_argv: yargs.Arguments<any>) {
504502
...argv,
505503
local,
506504
...(await prompts(
507-
Object.entries(questions)
508-
.filter(([k, v]) => {
505+
questions
506+
.filter((question) => {
509507
// Skip questions which are passed as parameter and pass validation
510-
if (argv[k] != null && v.validate?.(argv[k]) !== false) {
508+
if (
509+
argv[question.name] != null &&
510+
question.validate?.(argv[question.name]) !== false
511+
) {
511512
return false;
512513
}
513514

514515
// Skip questions with a single choice
515-
if (Array.isArray(v.choices) && v.choices.length === 1) {
516+
if (
517+
Array.isArray(question.choices) &&
518+
question.choices.length === 1
519+
) {
516520
return false;
517521
}
518522

519523
return true;
520524
})
521-
.map(([, v]) => {
522-
const { type, choices } = v;
525+
.map((question) => {
526+
const { type, choices } = question;
523527

524528
// Skip dynamic questions with a single choice
525529
if (type === 'select' && typeof choices === 'function') {
526530
return {
527-
...v,
531+
...question,
528532
type: (prev, values, prompt) => {
529533
const result = choices(prev, { ...argv, ...values }, prompt);
530534

@@ -537,7 +541,7 @@ async function create(_argv: yargs.Arguments<any>) {
537541
};
538542
}
539543

540-
return v;
544+
return question;
541545
})
542546
)),
543547
} as Answers;

0 commit comments

Comments
 (0)