From 4d757046ad8543a35a57410e16439dbf937c3255 Mon Sep 17 00:00:00 2001 From: Gant Laborde Date: Mon, 14 Aug 2017 12:34:12 -0500 Subject: [PATCH] move command from listview to list --- commands/list.js | 118 +++++++++++++++++++++++++++++++++++++++++++ commands/listview.js | 116 +++--------------------------------------- ignite.json | 1 + 3 files changed, 125 insertions(+), 110 deletions(-) create mode 100644 commands/list.js diff --git a/commands/list.js b/commands/list.js new file mode 100644 index 0000000..d0cc262 --- /dev/null +++ b/commands/list.js @@ -0,0 +1,118 @@ +// @cliDescription Generates a screen with a ListView/Flatlist/SectionList + walkthrough. + +const patterns = require('../lib/patterns') + +module.exports = async function (context) { + // grab some features + const { print, parameters, strings, ignite, filesystem } = context + const { pascalCase, isBlank } = strings + const config = ignite.loadIgniteConfig() + + // validation + if (isBlank(parameters.first)) { + print.info(`${context.runtime.brand} generate listview \n`) + print.info('A name is required.') + return + } + + const name = pascalCase(parameters.first) + const props = { name } + + // which type of list in code + const typeCodeMessage = 'What coding style do you want for your list?' + const typeCodeChoices = ['Flatlist (new)', 'Listview (classic)'] + + // which type of layout? + const typeMessage = 'What kind of ListView would you like to generate?' + const typeChoices = ['Row', 'Grid'] + + // Sections or no? + const typeDataMessage = 'How will your data be presented on this listview?' + const typeDataChoices = ['Single', 'Sectioned'] + + // Check for parameters to bypass questions + let typeCode = parameters.options.type + let type = parameters.options.type + let dataType = parameters.options.dataType + + // only prompt if type is not defined + if (!type) { + // as question 1 + const codeAnswers = await context.prompt.ask({ + name: 'type', + type: 'list', + message: typeCodeMessage, + choices: typeCodeChoices + }) + typeCode = codeAnswers.type + // ask question 2 + const answers = await context.prompt.ask({ + name: 'type', + type: 'list', + message: typeMessage, + choices: typeChoices + }) + type = answers.type + // ask question 3 + const dataAnswers = await context.prompt.ask({ + name: 'type', + type: 'list', + message: typeDataMessage, + choices: typeDataChoices + }) + dataType = dataAnswers.type + } + + // set appropriate templates to generate + let componentTemplate = typeCode === typeCodeChoices[0] + ? 'flatlist' + : 'listview' + componentTemplate = dataType === 'Sectioned' + ? componentTemplate + '-sections' + : componentTemplate + const styleTemplate = type === 'Grid' + ? 'listview-grid-style' + : 'listview-style' + + const jobs = [ + { + template: `${componentTemplate}.ejs`, + target: `App/Containers/${name}.js` + }, + { + template: `${styleTemplate}.ejs`, + target: `App/Containers/Styles/${name}Style.js` + } + ] + + await ignite.copyBatch(context, jobs, props) + + // if using `react-navigation` go the extra step + // and insert the screen into the nav router + if (config.navigation === 'react-navigation') { + const screenName = `${name}` + const appNavFilePath = `${process.cwd()}/App/Navigation/AppNavigation.js` + 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 listview screen.` + print.error(msg) + process.exit(1) + } + + // insert listview screen import + ignite.patchInFile(appNavFilePath, { + after: patterns[patterns.constants.PATTERN_IMPORTS], + insert: importToAdd + }) + + // insert listview screen route + ignite.patchInFile(appNavFilePath, { + after: patterns[patterns.constants.PATTERN_ROUTES], + insert: routeToAdd + }) + } else { + print.info('Listview screen created, manually add it to your navigation') + } +} diff --git a/commands/listview.js b/commands/listview.js index db0c584..85382e1 100644 --- a/commands/listview.js +++ b/commands/listview.js @@ -1,118 +1,14 @@ -// @cliDescription Generates a screen with a ListView + walkthrough. +// @cliDescription Deprecated - Use `list` const patterns = require('../lib/patterns') module.exports = async function (context) { - // grab some features - const { print, parameters, strings, ignite, filesystem } = context - const { pascalCase, isBlank } = strings + const { print } = context const config = ignite.loadIgniteConfig() - // validation - if (isBlank(parameters.first)) { - print.info(`${context.runtime.brand} generate listview \n`) - print.info('A name is required.') - return - } + print.info(`Listview is now simply 'list' and supports multiple types of views.\n`) + print.info(`Instead run: ${context.runtime.brand} generate list \n`) + print.info('This warning will be removed in a future release.') - const name = pascalCase(parameters.first) - const props = { name } - - // which type of list in code - const typeCodeMessage = 'What coding style do you want for your list?' - const typeCodeChoices = ['Flatlist (new)', 'Listview (classic)'] - - // which type of layout? - const typeMessage = 'What kind of ListView would you like to generate?' - const typeChoices = ['Row', 'Grid'] - - // Sections or no? - const typeDataMessage = 'How will your data be presented on this listview?' - const typeDataChoices = ['Single', 'Sectioned'] - - // Check for parameters to bypass questions - let typeCode = parameters.options.type - let type = parameters.options.type - let dataType = parameters.options.dataType - - // only prompt if type is not defined - if (!type) { - // as question 1 - const codeAnswers = await context.prompt.ask({ - name: 'type', - type: 'list', - message: typeCodeMessage, - choices: typeCodeChoices - }) - typeCode = codeAnswers.type - // ask question 2 - const answers = await context.prompt.ask({ - name: 'type', - type: 'list', - message: typeMessage, - choices: typeChoices - }) - type = answers.type - // ask question 3 - const dataAnswers = await context.prompt.ask({ - name: 'type', - type: 'list', - message: typeDataMessage, - choices: typeDataChoices - }) - dataType = dataAnswers.type - } - - // set appropriate templates to generate - let componentTemplate = typeCode === typeCodeChoices[0] - ? 'flatlist' - : 'listview' - componentTemplate = dataType === 'Sectioned' - ? componentTemplate + '-sections' - : componentTemplate - const styleTemplate = type === 'Grid' - ? 'listview-grid-style' - : 'listview-style' - - const jobs = [ - { - template: `${componentTemplate}.ejs`, - target: `App/Containers/${name}.js` - }, - { - template: `${styleTemplate}.ejs`, - target: `App/Containers/Styles/${name}Style.js` - } - ] - - await ignite.copyBatch(context, jobs, props) - - // if using `react-navigation` go the extra step - // and insert the screen into the nav router - if (config.navigation === 'react-navigation') { - const screenName = `${name}` - const appNavFilePath = `${process.cwd()}/App/Navigation/AppNavigation.js` - 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 listview screen.` - print.error(msg) - process.exit(1) - } - - // insert listview screen import - ignite.patchInFile(appNavFilePath, { - after: patterns[patterns.constants.PATTERN_IMPORTS], - insert: importToAdd - }) - - // insert listview screen route - ignite.patchInFile(appNavFilePath, { - after: patterns[patterns.constants.PATTERN_ROUTES], - insert: routeToAdd - }) - } else { - print.info('Listview screen created, manually add it to your navigation') - } + return } diff --git a/ignite.json b/ignite.json index fd2711e..941f608 100644 --- a/ignite.json +++ b/ignite.json @@ -3,6 +3,7 @@ "component", "container", "listview", + "list", "redux", "saga", "screen"