-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
plopfile.js
199 lines (187 loc) · 5.36 KB
/
plopfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
var mkdirp = require('mkdirp')
var fs = require('fs')
function addDirectory (path, fs) {
mkdirp.sync(path)
return true
}
function getSubPath (path) {
path = path.trim()
if (path === '') return ''
return path + '/'
}
function required (varName) {
return function (value) {
if ((/.+/).test(value)) { return true }
return varName + ' is required'
}
}
module.exports = function (plop) {
plop.addHelper('subPath', function (text) {
return getSubPath(text)
})
plop.addHelper('ifEqual', function (v1, v2, options) {
if (v1 === v2) {
return options.fn(this)
}
return options.inverse(this)
})
plop.setGenerator('Action', {
description: 'add a new action (in src/actions)',
prompts: [
{
type: 'input',
name: 'group',
message: 'What is this action part of? (user, group, etc)',
validate: required('group')
},
{
type: 'input',
name: 'name',
message: 'What is the name of the action?',
validate: required('name')
}
],
actions: function (answers) {
var actions = []
var filePath = plop.renderString('src/actions/{{ camelCase group}}.js', answers)
if (!fs.existsSync(filePath)) {
actions.push({
type: 'add',
path: filePath,
templateFile: '.plop/blankAction.hbs'
})
}
actions.push({
type: 'modify',
path: filePath,
pattern: /(\/\/ Add Action String Constant Here)/gi,
template: 'export const {{constantCase name}} = \'{{constantCase name}}\'\n$1'
})
actions.push({
type: 'modify',
path: filePath,
pattern: /(\/\/ Add Action Creator Here)/gi,
templateFile: '.plop/actionCreator.hbs'
})
return actions
}
})
plop.setGenerator('Reducer', {
description: 'add a new reducer (in src/reducers)',
prompts: [
{
type: 'input',
name: 'name',
message: 'What is the name of the property in the state tree?',
validate: required('name')
},
{
type: 'list',
name: 'type',
message: 'What is the type of this property in the state?',
choices: [
'Object',
'Array',
'Primary'
]
}
],
actions: function (answers) {
var actions = []
actions.push({
type: 'add',
path: 'src/reducers/{{camelCase name}}.js',
templateFile: '.plop/reducer.hbs'
})
actions.push({
type: 'modify',
path: 'src/store/reducers.js',
pattern: /(\/\/ Import Reducers Here)/gi,
template: 'import {{name}}Reducer from \'reducers/{{camelCase name}}\'\n$1'
})
actions.push({
type: 'modify',
path: 'src/store/reducers.js',
pattern: /(\/\/ Add Reducers Here)/gi,
template: '{{name}}: {{name}}Reducer,\n $1'
})
return actions
}
})
plop.setGenerator('Component', {
description: 'add a component (in src/components)',
prompts: [
{
type: 'input',
name: 'subpath',
message: 'What is the sub path of the component? example: core, content, views, forms',
default: ''
},
{
type: 'confirm',
name: 'stateless',
message: 'Is this a state-less functional component?',
default: false
},
{
type: 'input',
name: 'name',
message: 'What is the name of the component?',
validate: required('name')
},
{
type: 'confirm',
name: 'container',
message: 'Do you want to create a container?',
default: true
}
],
actions: function (answers) {
var actions = []
actions.push(function (answers) {
process.chdir(plop.getPlopfilePath())
var fs = require('fs')
var path = plop.renderString('src/components/{{subPath subpath}}{{properCase name }}', answers)
addDirectory(path, fs)
return plop.renderString('{{name}} directory is created in src/component', answers)
})
if (answers.stateless) {
actions.push({
type: 'add',
path: 'src/components/{{subPath subpath}}{{ properCase name }}/{{ properCase name }}.js',
templateFile: '.plop/baseStatelessComponent.hbs'
})
} else {
actions.push({
type: 'add',
path: 'src/components/{{subPath subpath}}{{ properCase name }}/{{ properCase name }}.js',
templateFile: '.plop/baseComponent.hbs'
})
}
if (answers.container) {
actions.push({
type: 'add',
path: 'src/components/{{subPath subpath}}{{ properCase name }}/{{ properCase name }}Container.js',
templateFile: '.plop/baseContainer.hbs'
})
actions.push({
type: 'add',
path: 'src/components/{{subPath subpath}}{{ properCase name }}/index.js',
templateFile: '.plop/containerIndex.hbs'
})
} else {
actions.push({
type: 'add',
path: 'src/components/{{subPath subpath}}{{ properCase name }}/index.js',
templateFile: '.plop/componentIndex.hbs'
})
}
actions.push({
type: 'add',
path: 'src/components/{{subPath subpath}}{{ properCase name}}/{{properCase name}}.scss',
templateFile: '.plop/styles.txt'
})
return actions
}
})
}