-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplopfile.js
More file actions
215 lines (201 loc) · 8.38 KB
/
plopfile.js
File metadata and controls
215 lines (201 loc) · 8.38 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
module.exports = (plop) => {
let componentTypes = [
{ name: "Common component", value: "CC" },
{ name: "Higher-Order Component", value: "HOC" },
{ name: "Redux", value: "REDUX" },
];
const printMessage = msg => answers => new Promise((resolve) => resolve(msg));
// controller generator
plop.setGenerator('controller', {
description: 'application controller logic',
prompts: [{
type: 'list',
name: 'type',
message: "Chose what you want to generate",
choices: componentTypes
},{
type: 'input',
name: 'name',
validate: (value) => {
if((/^\S+$/).test(value)) return true;
return 'Name cannot be empty or contain space';
},
message: ({type}) => componentTypes.find(c => c.value === type).name + ' name:'
},{
// Only if it's common component
when: (response) => response.type === "CC",
type: "confirm",
name: "createMd",
// Prompt to display on command line
message: "Do you want to create styleguide example?",
},{
// Only if is common component
when: (response) => response.type === "CC" || response.type === "HOC",
type: "confirm",
name: "createIndex",
message: "Do you want to create index.js file? (Recommended)",
},{
// Only if is common component
when: (response) => response.createIndex === true && response.type === "CC" || response.type === "HOC",
type: "confirm",
name: "injectIndexExport",
message: "Do you want to add this component to ./src/components/index.js file? (Recommended)",
},{
// Only if it's common component
when: (response) => response.type === "CC",
type: "confirm",
name: "createUseEffect",
message: "Add useEffects to component?",
},{
// Only if it's common component
when: (response) => response.type === "CC",
type: "confirm",
name: "createUseState",
message: "Add useState to component?",
},{
// Only if it's common component
when: (response) => response.type === "CC",
type: "confirm",
name: "createPropTypes",
message: "Add PropTypes to component?",
}],
actions: (data) => {
let actions = [];
// createUseEffect, createUseState
console.log(data, componentTypes);
let { type, name, createMd, createIndex, injectIndexExport } = data;
actions.push(printMessage('\nStarted generating: ' + componentTypes.find(c => c.value === type).name));
actions.push(printMessage('\nName: ' + name));
actions.push(printMessage('\n-------------------------------------\n'));
// If type is custom component
if(type === 'CC') {
actions.push({
type: 'add',
skipIfExists: true,
abortOnFail: true,
path: 'src/components/{{name}}/{{name}}.js',
templateFile: 'plop-templates/CommonComponent/Cc.hbs'
});
// Create index.js
if(createIndex) {
actions.push({
type: 'add',
skipIfExists: true,
abortOnFail: true,
path: 'src/components/{{name}}/index.js',
// One line template for default export from component file
template: "export {default} from './{{name}}';"
});
if(injectIndexExport) {
actions.push({
type: 'append',
path: 'src/components/index.js',
pattern: /(\/\/ GENERATOR EXPORT)/gi,
template: "export {default as " + name + "} from './" + name + "';"
});
}
}
if(createMd) {
actions.push({
type: 'add',
skipIfExists: true,
abortOnFail: true,
path: 'src/components/{{name}}/{{name}}.md',
templateFile: 'plop-templates/CommonComponent/CcMd.hbs'
});
}
}
// If type is HoC
if(type === 'HOC') {
// HoC example
actions.push({
type: 'add',
skipIfExists: true,
abortOnFail: true,
path: 'src/components/HOC/{{name}}/{{name}}.js',
templateFile: 'plop-templates/HOC/Hoc.hbs'
});
// Create index.js
if(createIndex) {
actions.push({
type: 'add',
skipIfExists: true,
abortOnFail: true,
path: 'src/components/HOC/{{name}}/index.js',
// One line template for default export from component file
template: "export {default} from './{{name}}';"
});
if(injectIndexExport) {
actions.push({
type: 'append',
path: 'src/components/index.js',
pattern: /(\/\/ GENERATOR EXPORT)/gi,
template: "export {default as " + name + "} from './HOC/" + name + "';"
});
}
}
}
// If type is redux
if(type === 'REDUX') {
// Create [REDUX-NAME]/index.js file
actions.push({
type: 'add',
skipIfExists: true,
abortOnFail: true,
path: 'src/redux/{{name}}/index.js',
templateFile: 'plop-templates/redux/ReduxIndex.hbs'
});
// Actions file
actions.push({
type: 'add',
skipIfExists: true,
abortOnFail: true,
path: 'src/redux/{{name}}/actions.js',
templateFile: 'plop-templates/redux/ReduxActions.hbs'
});
// Reducers file
actions.push({
type: 'add',
skipIfExists: true,
abortOnFail: true,
path: 'src/redux/{{name}}/reducers.js',
templateFile: 'plop-templates/redux/ReduxReducers.hbs'
});
// Selectors file
actions.push({
type: 'add',
skipIfExists: true,
abortOnFail: true,
path: 'src/redux/{{name}}/selectors.js',
templateFile: 'plop-templates/redux/ReduxSelectors.hbs'
});
// Types file
actions.push({
type: 'add',
skipIfExists: true,
abortOnFail: true,
path: 'src/redux/{{name}}/types.js',
templateFile: 'plop-templates/redux/ReduxTypes.hbs'
});
// Print message about injection
actions.push(printMessage('\n\nInjecting import in ./src/redux/index.js'));
// Append import in index of redux
actions.push({
type: 'append',
path: 'src/redux/index.js',
pattern: /(\/\/ IMPORTS)/gi,
template: "import {{name}} from './{{name}}'"
});
// Append export in index of redux
actions.push({
type: 'append',
path: 'src/redux/index.js',
pattern: /(export default {)/gi,
template: ' {{name}},'
});
}
actions.push(printMessage('\nCode generation completed!'));
return actions;
}
});
};