const patterns = require ( '../lib/patterns' ) ; module . exports = async function ( context ) { const { print , parameters , strings , ignite , filesystem } = context ; const { pascalCase , isBlank } = strings ; const config = ignite . loadIgniteConfig ( ) ; if ( isBlank ( parameters . first ) ) { print . info ( `${ context . runtime . brand } generate list <name>\n` ) ; print . info ( 'A name is required.' ) ; return ; } const name = pascalCase ( parameters . first ) ; const props = { name } ; const typeMessage = 'What kind of List would you like to generate?' ; const typeChoices = [ 'Row' , 'Grid' ] ; const typeDataMessage = 'How will your data be presented on this list?' ; const typeDataChoices = [ 'Single' , 'Sectioned' ] ; let typeCode = parameters . options . codeType ; let type = parameters . options . type ; let dataType = parameters . options . dataType ; if ( ! typeCode ) { typeCode = 'flatlist' ; } if ( ! type ) { const answers = await context . prompt . ask ( { name : 'type' , type : 'list' , message : typeMessage , choices : typeChoices } ) ; type = answers . type ; } if ( ! dataType ) { const dataAnswers = await context . prompt . ask ( { name : 'type' , type : 'list' , message : typeDataMessage , choices : typeDataChoices } ) ; dataType = dataAnswers . type ; } let componentTemplate = dataType . toLowerCase ( ) === 'sectioned' ? typeCode + '-sections' : typeCode ; let styleTemplate = '' ; if ( typeCode === 'flatlist' ) { if ( type . toLowerCase ( ) === 'grid' && dataType . toLowerCase ( ) === 'sectioned' ) { styleTemplate = 'listview-grid-style' ; } else if ( type . toLowerCase ( ) === 'grid' ) { componentTemplate = componentTemplate + '-grid' ; styleTemplate = 'flatlist-grid-style' ; } else { styleTemplate = 'listview-style' ; } } else { styleTemplate = type . toLowerCase ( ) === 'grid' ? 'listview-grid-style' : 'listview-style' ; } const jobs = [ { template : `${ componentTemplate }.ejs` , target : `App/Containers/${ name }/${ name }.tsx` } , { template : `${ styleTemplate }.ejs` , target : `App/Containers/${ name }/${ name }Style.ts` } ] ; await ignite . copyBatch ( context , jobs , props ) ; if ( config . navigation === 'react-navigation' ) { const screenName = `${ name }` ; const appNavFilePath = `${ process . cwd ( ) }/App/Navigation/AppNavigation.tsx` ; const importToAdd = `import { ${ screenName } } from "../Containers/${ screenName }";` ; const routeToAdd = `  ${ screenName }: { screen: ${ screenName } },` ; if ( ! filesystem . exists ( appNavFilePath ) ) { const msg = `No '${ appNavFilePath }' file found.  Can't insert list screen.` ; print . error ( msg ) ; process . exit ( 1 ) ; } ignite . patchInFile ( appNavFilePath , { after : patterns [ patterns . constants . PATTERN_IMPORTS ] , insert : importToAdd } ) ; ignite . patchInFile ( appNavFilePath , { after : patterns [ patterns . constants . PATTERN_ROUTES ] , insert : routeToAdd } ) ; } else { print . info ( 'List screen created, manually add it to your navigation' ) ; } } ;