Skip to content
This repository has been archived by the owner on Mar 9, 2021. It is now read-only.

Commit

Permalink
move command from listview to list
Browse files Browse the repository at this point in the history
  • Loading branch information
GantMan committed Aug 15, 2017
1 parent 876e99e commit 4d75704
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 110 deletions.
118 changes: 118 additions & 0 deletions 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 <name>\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')
}
}
116 changes: 6 additions & 110 deletions 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 <name>\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 <name>\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
}
1 change: 1 addition & 0 deletions ignite.json
Expand Up @@ -3,6 +3,7 @@
"component",
"container",
"listview",
"list",
"redux",
"saga",
"screen"
Expand Down

0 comments on commit 4d75704

Please sign in to comment.