Skip to content

Commit

Permalink
feat: plop new generate store (PanJiaChen#2805)
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeycf authored and Trong. Pham Van - CMC Global DU1.19 committed Jul 22, 2022
1 parent ff8b35d commit abef2e1
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
16 changes: 16 additions & 0 deletions plop-templates/store/index.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{{#if state}}
const state = {}
{{/if}}

{{#if mutations}}
const mutations = {}
{{/if}}

{{#if actions}}
const actions = {}
{{/if}}

export default {
namespaced: true,
{{options}}
}
62 changes: 62 additions & 0 deletions plop-templates/store/prompt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const { notEmpty } = require('../utils.js')

module.exports = {
description: 'generate store',
prompts: [{
type: 'input',
name: 'name',
message: 'store name please',
validate: notEmpty('name')
},
{
type: 'checkbox',
name: 'blocks',
message: 'Blocks:',
choices: [{
name: 'state',
value: 'state',
checked: true
},
{
name: 'mutations',
value: 'mutations',
checked: true
},
{
name: 'actions',
value: 'actions',
checked: true
}
],
validate(value) {
if (!value.includes('state') || !value.includes('mutations')) {
return 'store require at least state and mutations'
}
return true
}
}
],
actions(data) {
const name = '{{name}}'
const { blocks } = data
const options = ['state', 'mutations']
const joinFlag = `,
`
if (blocks.length === 3) {
options.push('actions')
}

const actions = [{
type: 'add',
path: `src/store/modules/${name}.js`,
templateFile: 'plop-templates/store/index.hbs',
data: {
options: options.join(joinFlag),
state: blocks.includes('state'),
mutations: blocks.includes('mutations'),
actions: blocks.includes('actions')
}
}]
return actions
}
}
2 changes: 2 additions & 0 deletions plopfile.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const viewGenerator = require('./plop-templates/view/prompt')
const componentGenerator = require('./plop-templates/component/prompt')
const storeGenerator = require('./plop-templates/store/prompt.js')

module.exports = function(plop) {
plop.setGenerator('view', viewGenerator)
plop.setGenerator('component', componentGenerator)
plop.setGenerator('store', storeGenerator)
}

0 comments on commit abef2e1

Please sign in to comment.