-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder.js
More file actions
48 lines (40 loc) · 1.94 KB
/
builder.js
File metadata and controls
48 lines (40 loc) · 1.94 KB
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
import fs from "fs";
import path from "path";
import { Builder } from "coalcodes-route-builder";
import { essentials } from "pulseflow";
const structureJSON = await essentials.readStructure();
const modules = await structureJSON.modules;
for (let module in modules) {
if (modules[module].controllers) {
const controllersPath = `./modules/${module}/controllers`;
let builder = new Builder(controllersPath, controllersPath);
builder.options.wrappers.routes = (routes) =>
`import {requestHandler} from "./handler.js";\n\nexport default (router) => {\n${routes}\n\treturn router\n}`;
builder.options.wrappers.auth = (route) => {
let roles = []
if (Array.isArray(route.role)) {
roles = route.role
}
roles = roles.map(role => role.trim()).filter(role => role)
return ` requestHandler(${route.className}, ${route.className}.${route.classMethod}, ${JSON.stringify(roles)})`;
}
builder.build()
}
}
console.log('concatenating all in ./project/endpoints.js ...');
var allRoutes = '';
for (let module in modules) {
if (modules[module].controllers) {
const controllersPath = `./modules/${module}/controllers`;
const indexPath = path.join(controllersPath, 'index.js');
const routesStr = await essentials.trimFile(indexPath, 'export default (router) => {', ' return router\n}');
if (routesStr != '') {
allRoutes = allRoutes + routesStr;
}
}
}
const myimports = await essentials.importControllers();
const imports = await myimports.join('\n');
const endpointsFileContent = imports + `\n\nimport { requestHandler } from '../handler.js'\n` + '\nexport default (router) => {' + allRoutes + ' return router\n}';
fs.writeFileSync('./project/endpoints.js', endpointsFileContent);
console.log(essentials.colors.green + 'imported all endpoints in ./project/endpoints.js' + essentials.colors.reset);