-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
156 lines (149 loc) · 4.94 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
const inquirer = require('inquirer');
const fs = require('fs');
const path = require('path');
const write = require('write');
const exec = require('child_process').exec;
const component = (name, state = null, props = null) => {
let JStemplatePath = path.join(__dirname, '../../frontend-react/dev/templates/component/index.js');
let CSStemplatePath = path.join(__dirname, '../../frontend-react/dev/templates/component/index.css');
fs.readFile(JStemplatePath, {encoding: 'utf-8'}, (err, data) => {
if (!err && !data.replace(/\s/g, '').length == 0) {
write.sync(`frontend-react/src/components/${name}/index.js`, data.replace(/comp_name/g, name), { overwrite: false })
console.log('componenet created with template')
} else {
write.sync(`frontend-react/src/components/${name}/index.js`, '', { overwrite: false })
console.log('created without componenet template')
}
})
fs.readFile(CSStemplatePath, {encoding: 'utf-8'}, (err, data) => {
if (!err && !data.replace(/\s/g, '').length == 0) {
write.sync(`frontend-react/src/components/${name}/index.css`, data.replace(/comp_name/g, name), { overwrite: false })
console.log('componenet created with template')
} else {
write.sync(`frontend-react/src/components/${name}/index.css`, '', { overwrite: false })
console.log('created without componenet template')
}
})
}
const scaffold = (model, attributes, file) => {
const templatePath = (template, file) => {
return path.join(__dirname, `../../frontend-react/dev/templates/scaffold/${template}/index.${file}`);
}
const readWrite = (jsPath, cssPath, template) => {
console.log(jsPath)
fs.readFile(jsPath, {encoding: 'utf-8'}, (err, data) => {
if (!err && !data.replace(/\s/g, '').length == 0) {
write.sync(`frontend-react/src/views/${model}/${template}/index.js`, data.replace(/comp_name/g, model), { overwrite: false })
console.log('componenet created with js template')
} else {
write.sync(`frontend-react/src/views/${model}/${template}/index.js`, '', { overwrite: false })
console.log('created without js template')
}
})
fs.readFile(cssPath, {encoding: 'utf-8'}, (err, data) => {
if (!err && !data.replace(/\s/g, '').length == 0) {
write.sync(`frontend-react/src/views/${model}/${template}/index.css`, data.replace(/comp_name/g, model), { overwrite: false })
console.log('componenet created with css template')
} else {
write.sync(`frontend-react/src/views/${model}/${template}/index.css`, '', { overwrite: false })
console.log('created without css template')
}
})
}
readWrite(templatePath('edit', 'js'), templatePath('edit', 'css'), 'edit');
readWrite(templatePath('index', 'js'), templatePath('index', 'css'), 'index');
readWrite(templatePath('new', 'js'), templatePath('new', 'css'), 'new');
readWrite(templatePath('show', 'js'), templatePath('show', 'css'), 'show');
api(model, attributes)
}
const api = (model, attributes) => {
exec(`amber g api ${model} ${attributes}`, () => {
console.log('\nAPI Generated')
exec(`amber db migrate`, () => {
console.log('\nDB Migration Complete')
});
});
};
module.exports.react_tools = () => {
inquirer.prompt([
{
type: 'list',
message: 'Select Generator',
name: 'generator',
choices: [
{
name: 'component'
},
{
name: 'scaffold'
},
{
name: 'api'
}
]
}
])
.then(answers => {
switch(answers.generator) {
case 'component':
inquirer.prompt([
{
type: 'input',
name: 'component_name',
message: "Component Name: "
},
{
type: 'input',
name: 'component_state',
message: "Component State: "
},
{
type: 'input',
name: 'component_props',
message: "Component Props: "
}
])
.then(answers => {
component(answers.component_name, answers.component_state, answers.component_props)
})
break;
case 'scaffold':
inquirer.prompt([
{
type: 'input',
name: 'model',
message: "model: "
},
{
type: 'input',
name: 'attributes',
message: "attributes: "
}
])
.then(answers => {
scaffold(answers.model, answers.attributes)
})
break;
case 'api':
inquirer.prompt([
{
type: 'input',
name: 'model',
message: "model: "
},
{
type: 'input',
name: 'attributes',
message: "attributes: "
}
])
.then(answers => {
api(answers.model, answers.attributes)
})
break;
default:
generator()
break;
}
});
}