@@ -361,13 +361,11 @@ async function create(_argv: yargs.Arguments<any>) {
361
361
362
362
const basename = path . basename ( folder ) ;
363
363
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
+ {
371
369
type : 'text' ,
372
370
name : 'slug' ,
373
371
message : 'What is the name of the npm package?' ,
@@ -380,28 +378,28 @@ async function create(_argv: yargs.Arguments<any>) {
380
378
validateNpmPackage ( input ) . validForNewPackages ||
381
379
'Must be a valid npm package name' ,
382
380
} ,
383
- description : {
381
+ {
384
382
type : 'text' ,
385
383
name : 'description' ,
386
384
message : 'What is the description for the package?' ,
387
385
validate : ( input ) => Boolean ( input ) || 'Cannot be empty' ,
388
386
} ,
389
- authorName : {
387
+ {
390
388
type : local ? null : 'text' ,
391
389
name : 'authorName' ,
392
390
message : 'What is the name of package author?' ,
393
391
initial : name ,
394
392
validate : ( input ) => Boolean ( input ) || 'Cannot be empty' ,
395
393
} ,
396
- authorEmail : {
394
+ {
397
395
type : local ? null : 'text' ,
398
396
name : 'authorEmail' ,
399
397
message : 'What is the email address for the package author?' ,
400
398
initial : email ,
401
399
validate : ( input ) =>
402
400
/ ^ \S + @ \S + $ / . test ( input ) || 'Must be a valid email address' ,
403
401
} ,
404
- authorUrl : {
402
+ {
405
403
type : local ? null : 'text' ,
406
404
name : 'authorUrl' ,
407
405
message : 'What is the URL for the package author?' ,
@@ -419,7 +417,7 @@ async function create(_argv: yargs.Arguments<any>) {
419
417
} ,
420
418
validate : ( input ) => / ^ h t t p s ? : \/ \/ / . test ( input ) || 'Must be a valid URL' ,
421
419
} ,
422
- repoUrl : {
420
+ {
423
421
type : local ? null : 'text' ,
424
422
name : 'repoUrl' ,
425
423
message : 'What is the URL for the repository?' ,
@@ -434,13 +432,13 @@ async function create(_argv: yargs.Arguments<any>) {
434
432
} ,
435
433
validate : ( input ) => / ^ h t t p s ? : \/ \/ / . test ( input ) || 'Must be a valid URL' ,
436
434
} ,
437
- type : {
435
+ {
438
436
type : 'select' ,
439
437
name : 'type' ,
440
438
message : 'What type of library do you want to develop?' ,
441
439
choices : TYPE_CHOICES ,
442
440
} ,
443
- languages : {
441
+ {
444
442
type : 'select' ,
445
443
name : 'languages' ,
446
444
message : 'Which languages do you want to use?' ,
@@ -454,15 +452,15 @@ async function create(_argv: yargs.Arguments<any>) {
454
452
} ) ;
455
453
} ,
456
454
} ,
457
- } ;
455
+ ] ;
458
456
459
457
// Validate arguments passed to the CLI
460
458
for ( const [ key , value ] of Object . entries ( argv ) ) {
461
459
if ( value == null ) {
462
460
continue ;
463
461
}
464
462
465
- const question = questions [ key as keyof Answers ] ;
463
+ const question = questions . find ( ( q ) => q . name === key ) ;
466
464
467
465
if ( question == null ) {
468
466
continue ;
@@ -504,27 +502,33 @@ async function create(_argv: yargs.Arguments<any>) {
504
502
...argv ,
505
503
local,
506
504
...( await prompts (
507
- Object . entries ( questions )
508
- . filter ( ( [ k , v ] ) => {
505
+ questions
506
+ . filter ( ( question ) => {
509
507
// 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
+ ) {
511
512
return false ;
512
513
}
513
514
514
515
// 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
+ ) {
516
520
return false ;
517
521
}
518
522
519
523
return true ;
520
524
} )
521
- . map ( ( [ , v ] ) => {
522
- const { type, choices } = v ;
525
+ . map ( ( question ) => {
526
+ const { type, choices } = question ;
523
527
524
528
// Skip dynamic questions with a single choice
525
529
if ( type === 'select' && typeof choices === 'function' ) {
526
530
return {
527
- ...v ,
531
+ ...question ,
528
532
type : ( prev , values , prompt ) => {
529
533
const result = choices ( prev , { ...argv , ...values } , prompt ) ;
530
534
@@ -537,7 +541,7 @@ async function create(_argv: yargs.Arguments<any>) {
537
541
} ;
538
542
}
539
543
540
- return v ;
544
+ return question ;
541
545
} )
542
546
) ) ,
543
547
} as Answers ;
0 commit comments