-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate.js
183 lines (168 loc) · 5.66 KB
/
generate.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
var express = require('express');
var bodyParser = require('body-parser');
var async = require('async');
var fs = require('fs');
var path = require('path');
var helps = require('./generaters/tools/helps.js');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(__dirname + '/generaters/view'));
//创建页面,并创建相关的js控制器,并添加控制器引用到main.js入口文件
app.post('/createPage', function(req, res) {
var body = req.body;
var page = body.page;
body.module = body.controller = body.page;
async.waterfall([
function(callback) {
fs.readFile('./generaters/model/index.html', 'utf-8', function(err, data) {
callback(null, data);
});
},
function(data, callback) {
var data = data,
fixedData;
fixedData = helps.replacePlaceholder(helps.pageRegExp, data, body);
callback(null, fixedData);
},
function(data, callback) {
fs.writeFile('./src/modules/' + page + '.html', data, {
encoding: 'utf-8',
mode: 438,
flag: 'w'
}, function(err) {
if (err) {
return console.log(err);
}
callback(null, { status: true });
});
},
function(data, callback) {
if (data.status) {
fs.readFile('./generaters/model/controller.js', 'utf-8', function(err, data) {
callback(null, data);
});
}
},
function(data, callback) {
var data = data,
fixedData;
fixedData = helps.replacePlaceholder(helps.jsRegExp, data, { name: page });
callback(null, fixedData);
},
function(data, callback) {
var jsDirPath = __dirname + '\\src\\js\\pages';
if (!fs.existsSync(jsDirPath)) {
helps.mkdirsSync(jsDirPath, true);
console.log('Common目录创建成功');
}
fs.writeFile(jsDirPath + '\\' + page + 'Ctrl.js', data, {
encoding: 'utf-8',
mode: 438,
flag: 'w'
}, function(err) {
if (err) {
return console.log(err);
}
callback(null, { status: true });
});
},
function(data, callback) {
if (data.status) {
fs.readFile('./src/js/main.js', 'utf-8', function(err, data) {
callback(null, data);
});
}
},
function(data, callback) {
var data = data,
fixedData;
fixedData = helps.replacePlaceholder(helps.mainJsRegExp, data, {
import: "import " + page + "Ctrl from './pages/" + page + "Ctrl';\n//#import",
insert: page + "Ctrl();\n//#insert"
});
callback(null, fixedData);
},
function(data, callback) {
fs.writeFile('./src/js/main.js', data, {
encoding: 'utf-8',
mode: 438,
flag: 'w'
}, function(err) {
if (err) {
return console.log(err);
}
callback(null, { status: true , mes:"html和js文件创建成功"});
});
}
], function(err, result) {
res.send(result);
});
});
//显示menuJson.js文件内容到textara文本域
app.get('/menu', function(req, res) {
async.waterfall([
function(callback) {
fs.readFile('./src/js/modules/frame/menuJson.js', 'utf-8', function(err, data) {
callback(null, data);
});
}
], function(err, result) {
res.send(result);
});
});
//修该文本域的menuJson.js内容
app.post('/addMenu', function(req, res) {
var json = req.body.json;
async.waterfall([function(callback) {
fs.writeFile('./src/js/modules/frame/menuJson.js', json, {
encoding: 'utf-8',
mode: 438,
flag: 'w'
}, function(err) {
if (err) {
return console.log(err);
}
callback(null, { status: true, mes: "menu变更成功" });
});
}], function(err, result) {
res.send(result)
});
});
//单独创建控制器
app.post('/createCtrl', function(req, res) {
var body = req.body;
async.waterfall([
function(callback) {
fs.readFile('./generaters/model/controller.js', 'utf-8', function(err, data) {
callback(null, data);
});
},
function(data, callback) {
var data = data,
fixedData;
fixedData = helps.replacePlaceholder(helps.jsRegExp, data, body);
callback(null, fixedData);
},
function(data, callback) {
var jsDirPath = __dirname + '\\src\\js\\pages';
if (!fs.existsSync(jsDirPath)) {
helps.mkdirsSync(jsDirPath,true);
console.log('Common目录创建成功');
}
fs.writeFile(jsDirPath +"\\"+ body.name + 'Ctrl.js', data, {
encoding: 'utf-8',
mode: 438,
flag: 'w'
}, function(err) {
if (err) {
return console.log(err);
}
callback(null, { status: 1, mes: "js文件创建成功" });
});
}
], function(err, result) {
res.send(result);
});
});
app.listen(2468);