Skip to content
This repository has been archived by the owner on Jan 13, 2023. It is now read-only.

Commit

Permalink
Merge pull request #99 from infinitered/dry-up-domains
Browse files Browse the repository at this point in the history
Dry up domains
  • Loading branch information
robinheinze authored Sep 19, 2018
2 parents af09237 + 0c15e77 commit 7e7e8ea
Show file tree
Hide file tree
Showing 8 changed files with 2,046 additions and 68 deletions.
23 changes: 4 additions & 19 deletions commands/component.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// @cliDescription Generates a component, supporting files, and a storybook test.

const domains = require('../lib/domains')

module.exports = async function (context) {
// grab some features
const { parameters, strings, print, ignite, filesystem, patching, prompt } = context
const { parameters, strings, print, ignite, patching } = context
const { pascalCase, isBlank } = strings
const options = parameters.options || {}
const folder = options.folder || options.f

// validation
if (isBlank(parameters.first)) {
Expand All @@ -14,22 +14,7 @@ module.exports = async function (context) {
return
}

const domains = filesystem.list('./src/views/')
const domainChoices = ['(Create New)', ...domains]
let domainAddAnswer = {}
let domainPath = ''
if (!folder) {
const domainQuestion = 'Add component to which domain?'
domainAddAnswer = await prompt.ask({
name: 'domain',
type: 'list',
message: domainQuestion,
choices: domainChoices
})
domainPath = (domainAddAnswer.domain === domainChoices[0]) ? '' : domainAddAnswer.domain + '/'
} else {
domainPath = (folder === 'views') ? '' : folder + '/'
}
const domainPath = await domains.getDomainPath('views', context)

const name = parameters.first
const pascalName = pascalCase(name)
Expand Down
43 changes: 15 additions & 28 deletions commands/model.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// @cliDescription Generates a model and model test.

const domains = require('../lib/domains')

module.exports = async function (context) {
// grab some features
const { parameters, strings, print, ignite, filesystem, patching, prompt } = context
const { parameters, strings, print, ignite, patching, filesystem } = context
const { camelCase, kebabCase, pascalCase, isBlank } = strings
const options = parameters.options || {}
const folder = options.folder || options.f

// validation
if (isBlank(parameters.first)) {
Expand All @@ -19,41 +19,28 @@ module.exports = async function (context) {
const pascalName = pascalCase(givenName)
const camelName = camelCase(givenName)

const domains = filesystem.list('./src/models/') || []
const domainChoices = ['(Create New)', ...domains]
let domainAddAnswer = {}
let domainPath = ''
if (!folder) {
const domainQuestion = 'Add model to which domain?'
domainAddAnswer = await prompt.ask({
name: 'domain',
type: 'list',
message: domainQuestion,
choices: domainChoices
})
domainPath = (domainAddAnswer.domain === domainChoices[0]) ? name : domainAddAnswer.domain
} else {
domainPath = (folder === 'models') ? '' : folder
}
const domainPath = await domains.getDomainPath('models', context)

const newDomain = isBlank(domainPath)
const props = { name, pascalName, newDomain }

const props = { name, pascalName }
const jobs = [
{
template: 'model.ejs',
target: `src/models/${domainPath}/${name}.ts`
target: `src/models/${domainPath}${name}/${name}.ts`
}, {
template: 'model.test.ejs',
target: `src/models/${domainPath}/${name}.test.ts`
target: `src/models/${domainPath}${name}/${name}.test.ts`
}
]

const rollupPath = `src/models/${domainPath}/index.ts`
const newDomain = domainPath === name
const rollupPath = `src/models/${domainPath}${name}/index.ts`
const rollupExists = filesystem.exists(rollupPath)

if (newDomain) {
jobs.push({ template: 'rollup-index.ts.ejs', target: rollupPath })
} else {
if (rollupExists) {
patching.insertInFile(rollupPath, 'export', `export * from "./${name}"`, false)
} else {
jobs.push({ template: 'rollup-index.ts.ejs', target: rollupPath })
}

await ignite.copyBatch(context, jobs, props)
Expand All @@ -68,4 +55,4 @@ module.exports = async function (context) {
patching.insertInFile(rootStorePath, 'import', storeTypeImport)
patching.insertInFile(rootStorePath, rootStoreDef, storeType)
}
}
}
22 changes: 3 additions & 19 deletions commands/screen.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// @cliDescription Generates an opinionated container.

const patterns = require('../lib/patterns')
const domains = require('../lib/domains')

module.exports = async function (context) {
// grab some features
const { parameters, print, strings, ignite, filesystem, prompt } = context
const { parameters, print, strings, ignite, filesystem } = context
const { pascalCase, isBlank, camelCase } = strings
const config = ignite.loadIgniteConfig()
const options = parameters.options || {}
const folder = options.folder || options.f

// validation
if (isBlank(parameters.first)) {
Expand All @@ -17,22 +16,7 @@ module.exports = async function (context) {
return
}

const domains = filesystem.list('./src/views/')
const domainChoices = ['(Create New)', ...domains]
let domainAddAnswer = {}
let domainPath = ''
if (!folder) {
const domainQuestion = 'Add screen to which domain?'
domainAddAnswer = await prompt.ask({
name: 'domain',
type: 'list',
message: domainQuestion,
choices: domainChoices
})
domainPath = (domainAddAnswer.domain === domainChoices[0]) ? '' : domainAddAnswer.domain + '/'
} else {
domainPath = (folder === 'views') ? '' : folder + '/'
}
const domainPath = await domains.getDomainPath('views', context)

const name = parameters.first
const screenName = name.endsWith('-screen') ? name : `${name}-screen`
Expand Down
37 changes: 37 additions & 0 deletions lib/domains.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
*
* @param {*} baseDir - The directory under `/src` where we should get the domain list
* @param {*} context - The gluegun context
*/

const getDomainPath = async (baseDir, context) => {
const { parameters, filesystem, prompt } = context
const options = parameters.options || {}
const domains = filesystem.list(`./src/${baseDir}/`) || []
const domainChoices = ['(Create New)', ...domains]
const folder = options.folder || options.f

let domainAddAnswer = {}
let domainPath = ''
if (!folder) {
const domainQuestion = 'Add this to which domain?'
domainAddAnswer = await prompt.ask({
name: 'domain',
type: 'list',
message: domainQuestion,
choices: domainChoices
})
domainPath =
domainAddAnswer.domain === domainChoices[0]
? ''
: domainAddAnswer.domain + '/'
} else {
domainPath = folder === baseDir ? '' : folder + '/'
}

return domainPath
}

module.exports = {
getDomainPath
}
Loading

0 comments on commit 7e7e8ea

Please sign in to comment.