Skip to content

Commit 65d8c45

Browse files
monkeycfPanJiaChen
authored andcommitted
feat: plop new generate store (PanJiaChen#2805)
1 parent e1554fd commit 65d8c45

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

plop-templates/store/index.hbs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{{#if state}}
2+
const state = {}
3+
{{/if}}
4+
5+
{{#if mutations}}
6+
const mutations = {}
7+
{{/if}}
8+
9+
{{#if actions}}
10+
const actions = {}
11+
{{/if}}
12+
13+
export default {
14+
namespaced: true,
15+
{{options}}
16+
}

plop-templates/store/prompt.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const { notEmpty } = require('../utils.js')
2+
3+
module.exports = {
4+
description: 'generate store',
5+
prompts: [{
6+
type: 'input',
7+
name: 'name',
8+
message: 'store name please',
9+
validate: notEmpty('name')
10+
},
11+
{
12+
type: 'checkbox',
13+
name: 'blocks',
14+
message: 'Blocks:',
15+
choices: [{
16+
name: 'state',
17+
value: 'state',
18+
checked: true
19+
},
20+
{
21+
name: 'mutations',
22+
value: 'mutations',
23+
checked: true
24+
},
25+
{
26+
name: 'actions',
27+
value: 'actions',
28+
checked: true
29+
}
30+
],
31+
validate(value) {
32+
if (!value.includes('state') || !value.includes('mutations')) {
33+
return 'store require at least state and mutations'
34+
}
35+
return true
36+
}
37+
}
38+
],
39+
actions(data) {
40+
const name = '{{name}}'
41+
const { blocks } = data
42+
const options = ['state', 'mutations']
43+
const joinFlag = `,
44+
`
45+
if (blocks.length === 3) {
46+
options.push('actions')
47+
}
48+
49+
const actions = [{
50+
type: 'add',
51+
path: `src/store/modules/${name}.js`,
52+
templateFile: 'plop-templates/store/index.hbs',
53+
data: {
54+
options: options.join(joinFlag),
55+
state: blocks.includes('state'),
56+
mutations: blocks.includes('mutations'),
57+
actions: blocks.includes('actions')
58+
}
59+
}]
60+
return actions
61+
}
62+
}

plopfile.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const viewGenerator = require('./plop-templates/view/prompt')
22
const componentGenerator = require('./plop-templates/component/prompt')
3+
const storeGenerator = require('./plop-templates/store/prompt.js')
34

45
module.exports = function(plop) {
56
plop.setGenerator('view', viewGenerator)
67
plop.setGenerator('component', componentGenerator)
8+
plop.setGenerator('store', storeGenerator)
79
}

0 commit comments

Comments
 (0)