-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplopfile.js
171 lines (151 loc) · 4.3 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
const path = require('path');
module.exports = function (plop) {
const testPath = '__tests__';
// adding a custom inquirer prompt type
plop.addPrompt('directory', require('inquirer-directory'));
// load some additional helpers from a module installed using npm
plop.load('plop-pack-fancy-comments', {
prefix: '',
upperCaseHeaders: true,
commentStart: '',
commentEnd: ''
});
// Utiles
const delayLog = msg => answers => new Promise((resolve) => {
setTimeout(() => resolve(msg), 1000);
});
const validateFileName = value => {
if(!value) {
return 'File name is required';
}
if (!(/^[a-zA-Z_.-]*$/).test(value)) {
return 'File name must only contain alphanumeric characters';
}
return true;
}
const promptsComponent = () => {
const list = {
type: 'list',
name: 'componentType',
message: 'Which the component you want to create ?',
choices: ['single', 'many']
};
const input = {
type: 'input',
name: 'fileName',
message: 'Enter a component name:',
validate: validateFileName,
};
const directory = {
type: 'directory',
name: 'path',
message: 'Where would you like to put this component?',
basePath: plop.getPlopfilePath()
};
return [list, directory, input];
};
const promptsRedux = () => {
const input = {
type: 'input',
name: 'fileName',
message: 'Enter a redux name:',
validate: validateFileName,
};
return [input];
};
// SET GENERATOR FOR COMPONENT
plop.setGenerator('component', {
description: 'Create awesome component folder with tsx, specs, index...',
prompts: promptsComponent(),
actions: ({ path, fileName, componentType }) => {
let actions = ['.... Please wait a second ....'];
const addDefault = {
type: 'add',
abortOnFail: true,
};
const defaultPath = `${path}/{{properCase fileName}}`;
const defaulTesttPath = `${testPath}/${path}/{{properCase fileName}}`;
const templatePath = 'templates/component';
const templateTestPath = 'templates/component/test';
if(componentType === 'single') {
const singleAction = [
{
...addDefault,
path: `${path}/{{properCase fileName}}.tsx`,
templateFile: `${templatePath}/single.txt`,
},
{
...addDefault,
path: `${testPath}/${path}/{{properCase fileName}}.test.tsx`,
templateFile: `${templatePath}/test.txt`,
},
]
actions = [...actions, ...singleAction];
}
if(componentType === 'many') {
const manyActions = [
{
...addDefault,
path: `${defaultPath}/{{properCase fileName}}Container.tsx`,
templateFile: `${templatePath}/container.txt`,
},
{
...addDefault,
path: `${defaultPath}/{{properCase fileName}}.tsx`,
templateFile: `${templatePath}/single.txt`,
},
{
...addDefault,
path: `${defaultPath}/index.ts`,
templateFile: `${templatePath}/index.txt`,
},
{
...addDefault,
path: `${defaulTesttPath}/{{properCase fileName}}.test.tsx`,
templateFile: `${templateTestPath}/single.test.txt`,
},
{
...addDefault,
path: `${defaulTesttPath}/{{properCase fileName}}Container.test.tsx`,
templateFile: `${templateTestPath}/singleContainer.test.txt`,
},
];
actions = [...actions, ...manyActions];
}
return actions;
},
});
// SET GENER/ATOR FOR REDUX
plop.setGenerator('redux', {
description: 'Create awesome redux folder with action, type, reducer, selector...',
prompts: promptsRedux(),
actions: ({ fileName }) => {
let actions = ['.... Please wait a second ....'];
const addDefault = {
type: 'add',
abortOnFail: true,
};
const destination = 'redux/{{camelCase fileName}}';
const base = `templates/redux`;
const destinationTest = `${testPath}/redux/{{camelCase fileName}}`;
const baseTest = 'templates/redux/test';
const reduxMap = ['actions', 'reducer', 'index', 'selectors', 'types'];
const reduxAction = [
...reduxMap.map(re => ({
...addDefault,
path: `${destination}/${re}.ts`,
templateFile: `${base}/${re}.txt`,
})),
...reduxMap
.filter(fi => !(/^(index|types)$/).test(fi))
.map(re => ({
...addDefault,
path: `${destinationTest}/${re}.test.ts`,
templateFile: `${baseTest}/${re}.test.txt`,
})),
];
actions = [...actions, ...reduxAction];
return actions;
},
});
};