@@ -217,6 +217,24 @@ const TYPE_CHOICES: {
217
217
} ,
218
218
] ;
219
219
220
+ const EXAMPLE_CHOICES = [
221
+ {
222
+ title : 'Test app' ,
223
+ value : 'test-app' ,
224
+ description : "app's native code is abstracted away" ,
225
+ } ,
226
+ {
227
+ title : 'Vanilla' ,
228
+ value : 'vanilla' ,
229
+ description : "provides access to app's native code" ,
230
+ } ,
231
+ {
232
+ title : 'Expo' ,
233
+ value : 'expo' ,
234
+ description : 'managed expo project with web support' ,
235
+ } ,
236
+ ] as const ;
237
+
220
238
const args : Record < ArgName , yargs . Options > = {
221
239
'slug' : {
222
240
description : 'Name of the npm package' ,
@@ -261,8 +279,7 @@ const args: Record<ArgName, yargs.Options> = {
261
279
'example' : {
262
280
description : 'Type of the app to create' ,
263
281
type : 'string' ,
264
- choices : [ 'vanilla' , 'test-app' ] ,
265
- default : 'vanilla' ,
282
+ choices : EXAMPLE_CHOICES . map ( ( { value } ) => value ) ,
266
283
} ,
267
284
} ;
268
285
@@ -451,64 +468,78 @@ async function create(argv: yargs.Arguments<any>) {
451
468
} ) ;
452
469
} ,
453
470
} ,
454
- } ;
455
-
456
- // Validate arguments passed to the CLI
457
- for ( const [ key , value ] of Object . entries ( argv ) ) {
458
- if ( value == null ) {
459
- continue ;
460
- }
471
+ 'example' : {
472
+ type : 'select' ,
473
+ name : 'example' ,
474
+ message : 'What type of example app do you want to create?' ,
475
+ choices : ( _ , values ) => {
476
+ return EXAMPLE_CHOICES . filter ( ( choice ) => {
477
+ if ( values . type ) {
478
+ return values . type === 'library'
479
+ ? choice . value === 'expo'
480
+ : choice . value !== 'expo' ;
481
+ }
461
482
462
- const question = questions [ key as ArgName ] ;
483
+ return true ;
484
+ } ) ;
485
+ } ,
486
+ } ,
487
+ } ;
463
488
464
- if ( question == null ) {
465
- continue ;
466
- }
489
+ const validate = ( answers : Answers ) => {
490
+ for ( const [ key , value ] of Object . entries ( answers ) ) {
491
+ if ( value == null ) {
492
+ continue ;
493
+ }
467
494
468
- let valid = question . validate ? question . validate ( String ( value ) ) : true ;
495
+ const question = questions [ key as ArgName ] ;
469
496
470
- // We also need to guard against invalid choices
471
- // If we don't already have a validation message to provide a better error
472
- if ( typeof valid !== 'string' && 'choices' in question ) {
473
- const choices =
474
- typeof question . choices === 'function'
475
- ? question . choices ( undefined , argv , question )
476
- : question . choices ;
497
+ if ( question == null ) {
498
+ continue ;
499
+ }
477
500
478
- if ( choices && ! choices . some ( ( choice ) => choice . value === value ) ) {
479
- valid = `Supported values are - ${ choices . map ( ( c ) =>
480
- kleur . green ( c . value )
481
- ) } `;
501
+ let valid = question . validate ? question . validate ( String ( value ) ) : true ;
502
+
503
+ // We also need to guard against invalid choices
504
+ // If we don't already have a validation message to provide a better error
505
+ if ( typeof valid !== 'string' && 'choices' in question ) {
506
+ const choices =
507
+ typeof question . choices === 'function'
508
+ ? question . choices (
509
+ undefined ,
510
+ // @ts -expect-error: it complains about optional values, but it should be fine
511
+ answers ,
512
+ question
513
+ )
514
+ : question . choices ;
515
+
516
+ if ( choices && ! choices . some ( ( choice ) => choice . value === value ) ) {
517
+ valid = `Supported values are - ${ choices
518
+ . map ( ( c ) => kleur . green ( c . value ) )
519
+ . join ( ', ' ) } `;
520
+ }
482
521
}
483
- }
484
522
485
- if ( valid !== true ) {
486
- let message = `Invalid value ${ kleur . red (
487
- String ( value )
488
- ) } passed for ${ kleur . blue ( key ) } `;
523
+ if ( valid !== true ) {
524
+ let message = `Invalid value ${ kleur . red (
525
+ String ( value )
526
+ ) } passed for ${ kleur . blue ( key ) } `;
489
527
490
- if ( typeof valid === 'string' ) {
491
- message += `: ${ valid } ` ;
492
- }
528
+ if ( typeof valid === 'string' ) {
529
+ message += `: ${ valid } ` ;
530
+ }
493
531
494
- console . log ( message ) ;
532
+ console . log ( message ) ;
495
533
496
- process . exit ( 1 ) ;
534
+ process . exit ( 1 ) ;
535
+ }
497
536
}
498
- }
537
+ } ;
499
538
500
- const {
501
- slug,
502
- description,
503
- authorName,
504
- authorEmail,
505
- authorUrl,
506
- repoUrl,
507
- type = 'module-mixed' ,
508
- languages = type === 'library' ? 'js' : 'java-objc' ,
509
- example : hasExample ,
510
- reactNativeVersion,
511
- } = {
539
+ // Validate arguments passed to the CLI
540
+ validate ( argv ) ;
541
+
542
+ const answers = {
512
543
...argv ,
513
544
...( await prompts (
514
545
Object . entries ( questions )
@@ -557,6 +588,21 @@ async function create(argv: yargs.Arguments<any>) {
557
588
) ) ,
558
589
} as Answers ;
559
590
591
+ validate ( answers ) ;
592
+
593
+ const {
594
+ slug,
595
+ description,
596
+ authorName,
597
+ authorEmail,
598
+ authorUrl,
599
+ repoUrl,
600
+ type = 'module-mixed' ,
601
+ languages = type === 'library' ? 'js' : 'java-objc' ,
602
+ example = local ? null : type === 'library' ? 'expo' : 'test-app' ,
603
+ reactNativeVersion,
604
+ } = answers ;
605
+
560
606
// Get latest version of Bob from NPM
561
607
let version : string ;
562
608
@@ -580,9 +626,6 @@ async function create(argv: yargs.Arguments<any>) {
580
626
? 'mixed'
581
627
: 'legacy' ;
582
628
583
- const example =
584
- hasExample && ! local ? ( type === 'library' ? 'expo' : hasExample ) : null ;
585
-
586
629
const project = slug . replace ( / ^ ( r e a c t - n a t i v e - | @ [ ^ / ] + \/ ) / , '' ) ;
587
630
588
631
let namespace : string | undefined ;
0 commit comments