11import path from 'path' ;
22import fs from 'fs-extra' ;
3- import ejs from 'ejs' ;
43import dedent from 'dedent' ;
54import kleur from 'kleur' ;
65import yargs from 'yargs' ;
@@ -16,17 +15,13 @@ import { spawn } from './utils/spawn';
1615import { version } from '../package.json' ;
1716import { addCodegenBuildScript } from './exampleApp/addCodegenBuildScript' ;
1817import { createInitialGitCommit } from './utils/initialCommit' ;
19- import { assertNpx , assertOptions } from './utils/assert' ;
18+ import { assertAnswers , assertNpx } from './utils/assert' ;
2019import { resolveBobVersionWithFallback } from './utils/promiseWithFallback' ;
2120import { generateTemplateConfiguration } from './template/config' ;
21+ import { applyTemplate } from './template/applyTemplate' ;
2222
2323const FALLBACK_BOB_VERSION = '0.32.0' ;
2424
25- const BINARIES = [
26- / ( g r a d l e w | \. ( j a r | k e y s t o r e | p n g | j p g | g i f ) ) $ / ,
27- / \$ \. y a r n (? ! [ a - z ] ) / ,
28- ] ;
29-
3025const COMMON_FILES = path . resolve ( __dirname , '../templates/common' ) ;
3126const COMMON_EXAMPLE_FILES = path . resolve (
3227 __dirname ,
@@ -465,7 +460,7 @@ async function create(_argv: yargs.Arguments<any>) {
465460 } ) ;
466461 }
467462
468- assertOptions ( questions , argv ) ;
463+ assertAnswers ( questions , argv ) ;
469464
470465 const singleChoiceAnswers : Partial < Answers > = { } ;
471466 const finalQuestions : Question [ ] = [ ] ;
@@ -516,7 +511,7 @@ async function create(_argv: yargs.Arguments<any>) {
516511 ...promptAnswers ,
517512 } as Required < Answers > ;
518513
519- assertOptions ( questions , answers ) ;
514+ assertAnswers ( questions , answers ) ;
520515
521516 const bobVersion = await resolveBobVersion ( ) ;
522517
@@ -526,35 +521,6 @@ async function create(_argv: yargs.Arguments<any>) {
526521 answers,
527522 } ) ;
528523
529- async function applyTemplate ( source : string , destination : string ) {
530- await fs . mkdirp ( destination ) ;
531-
532- const files = await fs . readdir ( source ) ;
533-
534- for ( const f of files ) {
535- const target = path . join (
536- destination ,
537- ejs . render ( f . replace ( / ^ \$ / , '' ) , config , {
538- openDelimiter : '{' ,
539- closeDelimiter : '}' ,
540- } )
541- ) ;
542-
543- const file = path . join ( source , f ) ;
544- const stats = await fs . stat ( file ) ;
545-
546- if ( stats . isDirectory ( ) ) {
547- await applyTemplate ( file , target ) ;
548- } else if ( ! BINARIES . some ( ( r ) => r . test ( file ) ) ) {
549- const content = await fs . readFile ( file , 'utf8' ) ;
550-
551- await fs . writeFile ( target , ejs . render ( content , config ) ) ;
552- } else {
553- await fs . copyFile ( file , target ) ;
554- }
555- }
556- }
557-
558524 await fs . mkdirp ( folder ) ;
559525
560526 if ( answers . reactNativeVersion != null ) {
@@ -593,52 +559,61 @@ async function create(_argv: yargs.Arguments<any>) {
593559 spinner . text = 'Copying files' ;
594560
595561 if ( local ) {
596- await applyTemplate ( COMMON_LOCAL_FILES , folder ) ;
562+ await applyTemplate ( config , COMMON_LOCAL_FILES , folder ) ;
597563 } else {
598- await applyTemplate ( COMMON_FILES , folder ) ;
564+ await applyTemplate ( config , COMMON_FILES , folder ) ;
599565
600566 if ( config . example !== 'none' ) {
601- await applyTemplate ( COMMON_EXAMPLE_FILES , folder ) ;
567+ await applyTemplate ( config , COMMON_EXAMPLE_FILES , folder ) ;
602568 }
603569 }
604570
605571 if ( answers . languages === 'js' ) {
606- await applyTemplate ( JS_FILES , folder ) ;
607- await applyTemplate ( EXPO_FILES , folder ) ;
572+ await applyTemplate ( config , JS_FILES , folder ) ;
573+ await applyTemplate ( config , EXPO_FILES , folder ) ;
608574 } else {
609- await applyTemplate ( NATIVE_COMMON_FILES , folder ) ;
575+ await applyTemplate ( config , NATIVE_COMMON_FILES , folder ) ;
610576
611577 if ( config . example !== 'none' ) {
612- await applyTemplate ( NATIVE_COMMON_EXAMPLE_FILES , folder ) ;
578+ await applyTemplate ( config , NATIVE_COMMON_EXAMPLE_FILES , folder ) ;
613579 }
614580
615581 if ( config . project . module ) {
616582 await applyTemplate (
583+ config ,
617584 NATIVE_FILES [ `module_${ config . project . arch } ` ] ,
618585 folder
619586 ) ;
620587 } else {
621- await applyTemplate ( NATIVE_FILES [ `view_${ config . project . arch } ` ] , folder ) ;
588+ await applyTemplate (
589+ config ,
590+ NATIVE_FILES [ `view_${ config . project . arch } ` ] ,
591+ folder
592+ ) ;
622593 }
623594
624595 if ( config . project . swift ) {
625- await applyTemplate ( SWIFT_FILES [ `module_legacy` ] , folder ) ;
596+ await applyTemplate ( config , SWIFT_FILES [ `module_legacy` ] , folder ) ;
626597 } else {
627598 if ( config . project . module ) {
628- await applyTemplate ( OBJC_FILES [ `module_common` ] , folder ) ;
599+ await applyTemplate ( config , OBJC_FILES [ `module_common` ] , folder ) ;
629600 } else {
630- await applyTemplate ( OBJC_FILES [ `view_${ config . project . arch } ` ] , folder ) ;
601+ await applyTemplate (
602+ config ,
603+ OBJC_FILES [ `view_${ config . project . arch } ` ] ,
604+ folder
605+ ) ;
631606 }
632607 }
633608
634609 const templateType = `${ config . project . module ? 'module' : 'view' } _${
635610 config . project . arch
636611 } ` as const ;
637612
638- await applyTemplate ( KOTLIN_FILES [ templateType ] , folder ) ;
613+ await applyTemplate ( config , KOTLIN_FILES [ templateType ] , folder ) ;
639614
640615 if ( config . project . cpp ) {
641- await applyTemplate ( CPP_FILES , folder ) ;
616+ await applyTemplate ( config , CPP_FILES , folder ) ;
642617 await fs . remove ( path . join ( folder , 'ios' , `${ config . project . name } .m` ) ) ;
643618 }
644619 }
0 commit comments