-
Notifications
You must be signed in to change notification settings - Fork 3
/
service.js
132 lines (108 loc) · 3.57 KB
/
service.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
#!/usr/bin/env node
const program = require('commander');
const shell = require('shelljs');
const fs = require('fs');
const lineByLineReader = require('line-by-line');
program
.version('0.0.1')
.description('Tool to make new service and its respective controller and an entry in routes');
program
.command('new <servicename> <apiv>')
.alias('a')
.description('Create a service')
.action((serviceName, apiVersion) => {
createServiceFile(serviceName);
createControllerFile(serviceName);
addEntryToRoutes(apiVersion, serviceName);
});
const createServiceFolder = (foldername) => {
return new Promise((resolve, reject) => {
const folderPath = process.cwd() + '/server/services/' + foldername;
if (!fs.existsSync(folderPath)){
fs.mkdirSync(folderPath);
resolve(folderPath);
} else {
reject('Service with ' + foldername + ' already exist');
}
});
}
const createServiceFile = (filename) => {
createServiceFolder(filename).then((folderPath, error) => {
const filePath = folderPath + '/index.js';
shell.touch(filePath);
return filePath;
}).catch(error => {
console.log(error);
});
};
const createControllerFolder = (controllerFolder) => {
return new Promise((resolve, reject) => {
const folderPath = process.cwd() + '/server/controllers/apis/' + controllerFolder;
if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath);
resolve(folderPath);
} else {
reject('Service with ' + controllerFolder + ' already exist');
}
});
}
const createControllerFile = (controllerFile) => {
createControllerFolder(controllerFile).then((folderPath, error) => {
const filePath = folderPath + '/index.js';
shell.touch(filePath);
let content = '';
content += "'use strict'\n\n";
content += "const express = require('express');\n";
content += "const " + controllerFile + "Service = require('../../../services/" + controllerFile + "');\n\n";
content += "let router = express.Router();\n\n";
content += "router.post('/', " + controllerFile + "Service.AddYourMethod);\n\n";
content += "module.exports = router;\n";
fs.appendFile(filePath, content, (err) => {
if (err) {
console.error(err)
return
}
});
return filePath;
}).catch(error => {
console.log(error);
});
};
const addEntryToRoutes = (apiVersion, controllerName) => {
let apiRoutePath = './server/routes/apis/' + apiVersion + '/index.js';
readApiRouteFile(controllerName, apiVersion).then(response => {
fs.writeFile(apiRoutePath, response.toString().replace(/,/g, '\n'), function () {
console.log('Service is created');
});
});
};
const readApiRouteFile = (controllerName, apiVersion) => {
let lr = new lineByLineReader('./server/routes/apis/' + apiVersion + '/index.js');
let lines = [];
let row = 0;
return new Promise((resolve, reject) => {
lr.on('line', function (line) {
lines.push(line);
if (line.length == 0) {
if (row === 1) {
lines.push("const " + controllerName + "Controller = require('../../../controllers/apis/" + controllerName + "');");
}
}
if (line.includes('let')) {
let nextRow = row + 1;
nextRow++;
lines.splice(nextRow, 0, "\nrouter.use('/" + controllerName + "', " + controllerName + "Controller);");
}
row++;
});
lr.on('end', function () {
resolve(lines);
lines = [];
});
});
};
if (!process.argv.slice(2).length || !/[arudl]/.test(process.argv.slice(2))) {
program.outputHelp();
process.exit();
}
program.parse(process.argv);