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

Commit

Permalink
DRY up domains
Browse files Browse the repository at this point in the history
  • Loading branch information
robinheinze committed Sep 18, 2018
1 parent 3a02d7d commit 1a74ab3
Show file tree
Hide file tree
Showing 5 changed files with 1,916 additions and 59 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 = domains.getDomainPath('views', context)

const name = parameters.first
const pascalName = pascalCase(name)
Expand Down
25 changes: 5 additions & 20 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 } = 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,22 +19,7 @@ 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 = domains.getDomainPath('models', context)

const props = { name, pascalName }
const jobs = [
Expand Down Expand Up @@ -68,4 +53,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 = 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 1a74ab3

Please sign in to comment.