-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·350 lines (322 loc) · 11 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
340
341
342
343
344
345
346
347
348
349
350
#!/usr/bin/env node
const fs = require('fs'),
path = require('path'),
tpl = require(path.join(__dirname, 'template', 'tpl.js')),
pwd = process.cwd(),
operation = process.argv[2],
option = process.argv[3],
db_type = process.argv[4],
project_name = path.basename(pwd);
let pkg = require(path.join(__dirname, 'package.example.json'));
if (!operation || (operation !== 'init' && !option)) {
usage_info();
process.exit(0);
}
switch (operation) {
case 'init':
init();
break;
case 'new':
new_module(option);
break;
case 'delete':
delete_module(option);
break;
default:
usage_info();
break;
}
function usage_info() {
const usage = `Version:${require(path.join(__dirname, 'package.json')).version}\nUsage: web-zero operation [init | new | delete] option [module_name] [databse_type]\n\nExample:\n\t web-zero init \t\t\t Create a api project named current dir.\n\t web-zero new users \t\t Create routes/users.js and dao/users.js files.\n\t web-zero new users mysql \t Create users module with DB base on mysql.\n\t web-zero delete users \t\t Delete routes/users.js and dao/users.js files.`;
console.log(usage);
}
/**
* init project.
*/
async function init() {
await init_dir();
await init_file();
await init_dependencies();
}
/**
* create route and dao files.
*/
async function new_module(option) {
await new_route(option);
await new_dao(option);
}
/**
* delete route and dao files.
*/
async function delete_module(option) {
await del_route(option);
await del_dao(option);
}
async function del_route(option) {
await new Promise((resolve, reject) => {
fs.unlink(path.join(pwd, 'routes', option + '.js'), (err) => { // asynchronous delete
console.log(` ---> Delete File\troutes/${option}.js \tsuccess...`);
resolve();
});
});
}
async function del_dao(option) {
await new Promise((resolve, reject) => {
fs.unlink(path.join(pwd, 'dao', option + '.js'), (err) => { // asynchronous delete
console.log(` ---> Delete File\tdao/${option}.js\t\tsuccess...`);
resolve();
});
});
}
async function new_route(option) {
await new Promise((resolve, reject) => {
let daoPath = null;
if (option.indexOf('/') > -1) {
let dir = option.split('/')[0];
let p = path.join(pwd, 'routes', dir);
if (!fs.existsSync(p)) {
fs.mkdirSync(p);
}
daoPath = '../../dao/' + option;
} else {
daoPath = '../dao/' + option;
}
if (db_type && db_type == 'mysql') {
fs.writeFile(path.join(pwd, 'routes', option + '.js'), tpl.router_mysql.replace(/\$daoPath/g, daoPath).replace(/\$option/g, option), (err) => {
if (err)
throw err;
console.log(` ---> Create File\troutes/${option}.js \tsuccess...`);
resolve();
});
} else {
fs.writeFile(path.join(pwd, 'routes', option + '.js'), tpl.base_router.replace(/\$daoPath/g, daoPath).replace(/\$option/g, option), (err) => {
if (err)
throw err;
console.log(` ---> Create File\troutes/${option}.js \tsuccess...`);
resolve();
});
}
});
}
async function new_dao(option) {
await new Promise((resolve, reject) => {
let daoPath = null;
let table = null;
if (option.indexOf('/') > -1) {
let dir = option.split('/')[0];
let p = path.join(pwd, 'dao', dir);
if (!fs.existsSync(p)) {
fs.mkdirSync(p);
}
daoPath = '../../tools/';
table = option.split('/')[0] + '_' + option.split('/')[1];
} else {
daoPath = '../tools/';
table = option;
}
if (db_type && db_type == 'mysql') {
fs.writeFile(path.join(pwd, 'dao', option + '.js'), tpl.mysql_dao.replace(/\$daoPath/g, daoPath + 'mysql').replace(/\$option/g, table), (err) => {
if (err)
throw err;
console.log(` ---> Create File\tdao/${option}.js\t\tsuccess...`);
resolve();
});
} else {
fs.writeFile(path.join(pwd, 'dao', option + '.js'), tpl.base_dao.replace(/\$daoPath/g, daoPath + 'mongo').replace(/\$option/g, table), (err) => {
if (err)
throw err;
console.log(` ---> Create File\tdao/${option}.js\t\tsuccess...`);
resolve();
});
}
});
}
/**
* create route,dao,middleware and conf dir.
*/
async function init_dir() {
await new Promise((resolve, reject) => {
fs.mkdir(path.join(pwd, 'routes'), (err) => {
if (err && err.code !== 'EEXIST')
throw err;
console.log(' ---> Create Directory\troutes\t\t\tsuccess...');
resolve();
});
});
await new Promise((resolve, reject) => {
fs.mkdir(path.join(pwd, 'dao'), (err) => {
if (err && err.code !== 'EEXIST')
throw err;
console.log(' ---> Create Directory\tdao\t\t\tsuccess...');
resolve();
});
});
await new Promise((resolve, reject) => {
fs.mkdir(path.join(pwd, 'middleware'), (err) => {
if (err && err.code !== 'EEXIST')
throw err;
console.log(' ---> Create Directory\tmiddleware\t\tsuccess...');
resolve();
});
});
await new Promise((resolve, reject) => {
fs.mkdir(path.join(pwd, 'conf'), (err) => {
if (err && err.code !== 'EEXIST')
throw err;
console.log(' ---> Create Directory\tconf\t\t\tsuccess...');
resolve();
});
});
await new Promise((resolve, reject) => {
fs.mkdir(path.join(pwd, 'tools'), (err) => {
if (err && err.code !== 'EEXIST')
throw err;
console.log(' ---> Create Directory\ttools\t\t\tsuccess...');
resolve();
});
});
}
async function init_file() {
/**
* create app.js and write tpl code into it.
*/
await new Promise((resolve, reject) => {
fs.writeFile(path.join(pwd, 'app.js'), tpl.app, (err) => {
if (err)
throw err;
console.log(' ---> Create File\tapp.js\t\t\tsuccess...');
resolve();
});
});
await new Promise((resolve, reject) => {
fs.writeFile(path.join(pwd, 'routes', 'index.js'), tpl.index, (err) => {
if (err)
throw err;
console.log(' ---> Create File\troutes/index.js \tsuccess...');
resolve();
});
});
/**
* create middleware/log.js
*/
await new Promise((resolve, reject) => {
fs.writeFile(path.join(pwd, 'middleware', 'log.js'), tpl.log, (err) => {
if (err)
throw err;
console.log(' ---> Create File\tmiddleware/log.js \tsuccess...');
resolve();
});
});
await new Promise((resolve, reject) => {
fs.writeFile(path.join(pwd, 'middleware', 'koa-router-ext.js'), tpl.koa_router_ext, (err) => {
if (err)
throw err;
console.log(' ---> Create File\tmiddleware/koa-router-ext.js \tsuccess...');
resolve();
});
});
/**
* create config.js | db_development.js | db_production.js | db_staging.js
*/
await new Promise((resolve, reject) => {
fs.writeFile(path.join(pwd, 'conf', 'config.js'), tpl.config, (err) => {
if (err)
throw err;
console.log(' ---> Create File\tconf/config.js\t\tsuccess...');
resolve();
});
});
await new Promise((resolve, reject) => {
fs.writeFile(path.join(pwd, 'conf', 'db_development.js'), tpl.db_development, (err) => {
if (err)
throw err;
console.log(' ---> Create File\tconf/db_development.js \tsuccess...');
resolve();
});
});
await new Promise((resolve, reject) => {
fs.writeFile(path.join(pwd, 'conf', 'db_staging.js'), tpl.db_staging, (err) => {
if (err)
throw err;
console.log(' ---> Create File\tconf/db_staging.js \tsuccess...');
resolve();
});
});
await new Promise((resolve, reject) => {
fs.writeFile(path.join(pwd, 'conf', 'db_production.js'), tpl.db_production, (err) => {
if (err)
throw err;
console.log(' ---> Create File\tconf/db_production.js \tsuccess...');
resolve();
});
});
/**
* create mongo.js | redis.js | qiniu.js | mysql.js
*/
await new Promise((resolve, reject) => {
fs.writeFile(path.join(pwd, 'tools', 'mongo.js'), tpl.mongo, (err) => {
if (err)
throw err;
console.log(' ---> Create File\ttools/mongo.js\t\tsuccess...');
resolve();
});
});
await new Promise((resolve, reject) => {
fs.writeFile(path.join(pwd, 'tools', 'redis.js'), tpl.redis, (err) => {
if (err)
throw err;
console.log(' ---> Create File\ttools/redis.js\t\tsuccess...');
resolve();
});
});
await new Promise((resolve, reject) => {
fs.writeFile(path.join(pwd, 'tools', 'qiniu.js'), tpl.qiniu, (err) => {
if (err)
throw err;
console.log(' ---> Create File\ttools/qiniu.js\t\tsuccess...');
resolve();
});
});
await new Promise((resolve, reject) => {
fs.writeFile(path.join(pwd, 'tools', 'mysql.js'), tpl.mysql, (err) => {
if (err)
throw err;
console.log(' ---> Create File\ttools/mysql.js\t\tsuccess...');
resolve();
});
});
await new Promise((resolve, reject) => {
fs.writeFile(path.join(pwd, 'tools', 'security.js'), tpl.tools, (err) => {
if (err)
throw err;
console.log(' ---> Create File\ttools/security.js \tsuccess...');
resolve();
});
});
}
/**
* add dependencies to package.json
*/
async function init_dependencies() {
pkg.dependencies = {
"ioredis": "^2.3.0",
"koa": "^2.4.1",
"koa-bodyparser": "^4.2.0",
"koa-exception": "^2.0.4",
"koa-router": "^7.3.0",
"koa-router-form-parser": "0.0.1",
"mongodb": "^2.2.8",
"mysql": "^2.11.1",
"qiniu": "^6.1.11",
"superagent": "^2.1.0"
}
pkg.name = project_name;
await new Promise((resolve, reject) => {
fs.writeFile(path.join(pwd, 'package.json'), JSON.stringify(pkg, null, 4), (err) => {
if (err)
throw err;
console.log(' ---> Add dependencies \tsuccess...');
resolve();
});
});
}