-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
executable file
·339 lines (327 loc) · 9.59 KB
/
index.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/env node
// Project Dependencies
var program = require('commander');
var colors = require('colors');
var inquirer = require('inquirer');
var _ = require('lodash');
// Mikey Files
var generatePath = require('./generators/pathGen.js');
var generateProject = require('./generators/projectGen.js');
var generateFile = require('./generators/fileGen.js');
var generateCustomFile = require('./generators/customGen.js');
var eraseDefaults = require('./defaults/eraseDefaults.js');
var listDefaults = require('./defaults/listDefaults.js');
var saveTemplate = require('./custom_templates/saveTemplate.js');
var deleteTemplate = require('./custom_templates/deleteTemplate.js');
var listTemplates = require('./custom_templates/listTemplates.js');
var mikeyJsonGenerator = require('./generators/mikeyGen.js');
function collect(val, memo) {
memo.push(val);
return memo;
}
program
.version('3.10.3')
.option(
'new <projectName>',
'Generate New Mikey Project: react/redux/universal',
/^(react|redux|universal)$/i
)
.option(
'g <fileType>',
'Generate New React File: container/component/action/reducer/helper/custom',
/^(container|component|action|reducer|helper|custom)$/i
)
.option(
'-i, import [importName]',
'(Optional) Add imports on file generation.',
collect,
[]
)
.option(
'-d, defaults [Y/n]',
'(Optional) Import default dependencies (n, Y).',
/^(Y|n)$/i,
'n'
)
.option(
'-l, list [choice]',
'List defaults(plural): all/actions/containers/components/reducers/helpers',
/^(actions|components|containers|reducers|helpers|all)$/i,
'undefined'
)
.option(
'-e, erase [choice]',
'Erase defaults(plural): all/actions/containers/components/reducers/helpers',
/^(actions|components|containers|reducers|helpers|all)$/i,
'undefined'
)
.option(
'save_template [fileType]',
'Save file in current project as custom template.(singular)',
/^(action|component|container|reducer|helper)$/i
)
.option(
'delete_template [fileType]',
'Delete saved custom template.(singular)',
/^(action|component|container|reducer|helper)$/i
)
.option(
'list_templates [fileType]',
'List saved custom templates for file type.(singular)',
/^(action|component|container|reducer|helper)$/i
)
.parse(process.argv);
// Directory for Mikey
var directory = __dirname;
// Imports
var inpm = program.import;
// Defaults
var defaults = program.defaults;
// Current Working Directory
var currentWDir = process.cwd();
// Automated Help
if (!process.argv.slice(2).length) {
program.outputHelp();
}
// Project Generation
if (typeof program.new !== 'undefined') {
if (
program.new === 'react' ||
program.new === 'redux' ||
program.new === 'universal'
) {
setTimeout(function() {
var projectQuestion = '';
if (program.new === 'universal') {
projectQuestion = [
{
type: 'input',
name: 'api',
message: 'Universal App with API? (Y/n):'
},
{
type: 'input',
name: 'name',
message: 'Enter Project Name:'
}
];
} else {
projectQuestion = [
{
type: 'input',
name: 'name',
message: 'Enter Project Name:'
}
];
}
inquirer.prompt(projectQuestion).then(function(answer) {
var api = answer.api;
if (api === 'Y' || api === 'y') {
api = true;
} else {
api = false;
}
var projectType = program.new;
var projectName = answer.name;
if (projectName.length >= 1) {
if (api === true || api === 'Y' || api === 'y') {
console.log(
colors.bold(
`Generating New ${_.upperFirst(
projectType
)} Mikey Project with API: `
) +
colors.yellow(projectName.toString()) +
colors.bold(' in ') +
colors.yellow(currentWDir.toString())
);
} else {
console.log(
colors.bold(
`Generating New ${_.upperFirst(projectType)} Mikey Project: `
) +
colors.yellow(projectName.toString()) +
colors.bold(' in ') +
colors.yellow(currentWDir.toString())
);
}
generateProject(
projectName,
currentWDir,
directory,
projectType,
api
);
} else {
console.log(colors.red('Project name cannot be blank!'));
}
});
}, 100);
} else {
console.log(
colors.red('Project type did not match and/or cannot be blank!')
);
}
}
// File Generation
if (typeof program.g !== 'undefined') {
if (
program.g === 'container' ||
program.g === 'component' ||
program.g === 'action' ||
program.g === 'reducer' ||
program.g === 'helper' ||
program.g === 'custom'
) {
mikeyJsonGenerator(currentWDir);
var fileType = _.lowerFirst(program.g);
var filePathToFileType = '';
if (fileType === 'custom') {
filePathToFileType = directory + '/custom_templates/' + fileType;
listTemplates(fileType, filePathToFileType);
setTimeout(function() {
var questions = [
{
type: 'input',
name: 'templateName',
message: 'Enter Saved Template Name(no extension):'
},
{
type: 'input',
name: 'fileNameAnswer',
message:
'Enter Desired File Name(no extension / default: Template Name):'
}
];
inquirer.prompt(questions).then(function(answer) {
var templateName = answer.templateName;
var fileName = answer.fileNameAnswer;
if (fileName.length <= 1 || templateName.length <= 1) {
console.log(
colors.red('File name or Template name cannot be blank!')
);
} else {
var folderPath = generatePath(fileType + 's', currentWDir);
console.log(
colors.green('Generating ') +
fileName +
' as ' +
_.capitalize(fileType)
);
generateCustomFile(
folderPath,
fileType,
templateName,
fileName,
inpm,
directory
);
}
});
}, 100);
} else {
filePathToFileType = directory + '/custom_templates/' + fileType;
setTimeout(function() {
var questions = [
{
type: 'input',
name: 'fileNameAnswer',
message: 'Enter Desired File Name(no extension):'
}
];
inquirer.prompt(questions).then(function(answer) {
var fileName = '';
if (fileType === 'component' || fileType === 'container') {
fileName = _.upperFirst(answer.fileNameAnswer);
} else {
fileName = _.camelCase(answer.fileNameAnswer);
}
if (fileName.length <= 1) {
console.log(colors.red('Filename cannot be blank!'));
} else {
var folderPath = generatePath(fileType + 's', currentWDir);
console.log(
colors.green('Generating ') +
fileName +
' as ' +
_.capitalize(fileType)
);
generateFile(
folderPath,
fileType,
fileName,
inpm,
directory,
defaults,
currentWDir
);
}
});
}, 100);
}
} else {
console.log(colors.red('File type did not match and/or cannot be blank!'));
}
}
// Erase Defaults
if (typeof program.erase !== 'undefined') {
eraseDefaults(program.erase, directory);
}
// List Defaults
if (typeof program.list !== 'undefined') {
listDefaults(program.list, directory);
}
// Save Custom Template
if (typeof program.save_template !== 'undefined') {
// Prompt Questions
var question = [
{
type: 'input',
name: 'saveFile',
message:
'Enter Project ' +
_.capitalize(program.save_template) +
' File Name To Save(no extension):'
}
];
var fileType = _.lowerFirst(program.save_template);
var filePath = generatePath(fileType + 's', currentWDir);
listTemplates(fileType, filePath);
setTimeout(function() {
inquirer.prompt(question).then(function(answer) {
var templateName = answer.saveFile;
// Save File
saveTemplate(filePath, fileType, templateName, directory);
});
}, 100);
}
// Delete Custom Template
if (typeof program.delete_template !== 'undefined') {
var fileType = _.lowerFirst(program.delete_template);
var filePathToFileType = directory + '/custom_templates/' + fileType;
listTemplates(fileType, filePathToFileType);
setTimeout(function() {
// Prompt Questions
var question = [
{
type: 'input',
name: 'deleteFile',
message:
'Enter ' +
_.capitalize(program.delete_template) +
' Template Name To Delete(no extension):'
}
];
inquirer.prompt(question).then(function(answer) {
var templateName = answer.deleteFile;
// Delete File
deleteTemplate(fileType, templateName, directory);
});
}, 100);
}
// List Files
if (typeof program.list_templates !== 'undefined') {
var fileType = _.lowerFirst(program.list_templates);
var filePathToFileType = directory + '/custom_templates/' + fileType;
listTemplates(fileType, filePathToFileType);
}