-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.js
76 lines (58 loc) · 2.72 KB
/
generator.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
// eslint-disable-next-line
let readline = require('readline');
// eslint-disable-next-line
let fs = require('fs');
const ReducerTemplate = require('./generator-templates/reducer-template');
const ComponentTemplate = require('./generator-templates/component-template');
const defaultFolder = `${__dirname}/src/app`;
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const createFolder = (path) =>
new Promise((resolve) => {
if (!fs.existsSync(path)) {
fs.mkdirSync(path);
resolve();
}
});
const createFile = (path, content) =>
new Promise((resolve) => {
fs.writeFile(path, content, (err) => {
if (err) {
throw err;
}
return resolve();
});
});
const questions = (question, defaultFolderPath, path, featureName, content, extension, hidePathInFileName) =>
new Promise((resolve) => {
rl.question(question, async (res) => {
const answer = res.toLowerCase();
const getFileName = () => (hidePathInFileName ? '' : `-${path}`);
if ((answer === 'y' || answer === 'yes') && path && defaultFolderPath && featureName) {
await createFolder(`${defaultFolderPath}/${path}`);
await createFile(`${defaultFolderPath}/${path}/${featureName}${getFileName()}.${extension || 'js'}`, content);
}
return resolve(answer);
});
});
const start = () => {
rl.question('Hi. My name is NiGi and I am a very "smart" bot. I am here to help you create some cool stuff. So let us start. \n\nWhat is the name of the feature FOLDER? ', async (folder) => {
const folderPath = `${defaultFolder}/${folder}`;
await createFolder(folderPath);
const buildActionTypes = await questions('\nWill there be any ACTION TYPES in this feature? [y/N] \n', folderPath, 'constants', 'action-types', 'export default {\n};', null, true);
await questions('\nWill there be any React.JS ACTIONS in this feature? [y/N] \n', folderPath, 'actions', folder, '');
const reducer = ReducerTemplate();
await questions('\nWill there be any Redux REDUCERS in this feature? [y/N] \n', folderPath, 'reducers', folder, reducer.create(folder, buildActionTypes === 'y' || buildActionTypes === 'yes'));
const buildComponents = await questions('\nWill there be any React.js COMPONENTS in this feature? [y/N] \n');
if (buildComponents === 'y' || buildComponents === 'yes') {
createFolder(`${defaultFolder}/${folder}/components`);
const component = ComponentTemplate();
await createFile(`${defaultFolder}/${folder}/components/${folder}.js`, component.create(folder));
}
await questions('\nWill there be any styles in this feature? [y/N] \n', folderPath, 'styles', folder, '', 'scss', true);
rl.close();
});
};
start();