-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
120 lines (107 loc) · 4.44 KB
/
main.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
const colors = require('colors')
const promisify = require("util").promisify;
const fs = require("fs");
const writeFile = promisify(fs.writeFile);
const readFile = promisify(fs.readFile);
const deleteFile = promisify(fs.stat);
const mkdirp = promisify(require("mkdirp"));
const tableSchema = require('./tables')()
const relations = require("./relations")()
const routeTemplates = require("./Templates/route.template")
const controllerTemplates = require("./Templates/controller.template")
const allRoutesTemplates = require("./Templates/allRoutes.template")
const BASEPATH = "../autogenerated-server"
const MODULEPATH = "../autogenerated-server/modules"
let apiCount = 0
//TODO: remove those routesById that don't have primary key ex- /business/:businessid/stockcurrent/:stockcurrentid
mainMagicFunction()
async function mainMagicFunction(){
let startingTime = Date.now()
await initialize()
console.log(colors.yellow("Directories created."))
writeRouteJsFile()
console.log(colors.blue("All route files created."))
writeControllerJsFile()
console.log(colors.green("All controller files created."))
writeRouterJsFile()
console.log(colors.magenta("Module router created"))
let endingTime = Date.now()
console.log(colors.bgBlack(`Wrote ${apiCount+102} APIs in ${endingTime-startingTime} millisecons`))
console.log(colors.rainbow("Finished."))
}
// writeRouteJsFile()
async function writeRouteJsFile() {
// var tableObject = relations.find(o => o["parent"] === tableName)
relations.map(async tableObject => {
let parentTable = tableObject["parent"]
let routeWritingData = ""
routeWritingData+=routeTemplates.routeTemplate(parentTable)
Object.keys(tableObject).map(tName => {
apiCount++
if(tName!="parent" && tName!=parentTable){
routeWritingData+=routeTemplates.singleRoute(parentTable, tName)
}
})
routeWritingData+=routeTemplates.routeEndingExport(parentTable)
await writeFile(`${MODULEPATH}/${parentTable}/${parentTable}.route.js`, routeWritingData)
})
}
// writeControllerJsFile()
async function writeControllerJsFile() {
// var tableObject = relations.find(o => o["parent"] === tableName)
relations.map(async tableObject => {
let parentTable = tableObject["parent"]
let controllerWritingData = ""
controllerWritingData+=controllerTemplates.controllerTemplate(parentTable)
Object.keys(tableObject).map(tName => {
apiCount++
if(tName!="parent" && tName!=parentTable){
controllerWritingData+=controllerTemplates.relationGetController(parentTable, tName)
}
})
await writeFile(`${MODULEPATH}/${parentTable}/${parentTable}.controller.js`, controllerWritingData)
})
}
// writeRouterJsFile()
async function writeRouterJsFile() {
var tableNamesArray = Object.keys(tableSchema)
let importTableModules = ""
let exportTableModules = ""
let tableModuleRouter = ""
tableNamesArray.map(tName => {
importTableModules+=allRoutesTemplates.importDataTemplate(tName)
exportTableModules+=allRoutesTemplates.exportDataTemplate(tName)
})
tableModuleRouter = allRoutesTemplates.routerTemplate(importTableModules, exportTableModules)
await writeFile(`${BASEPATH}/routes/router.js`, tableModuleRouter)
}
async function initialize(){
return new Promise(async (resolve, reject) => {
let allAsyncMkdir = []
await mkdirp(`${BASEPATH}/modules`)
await mkdirp(`${BASEPATH}/routes`)
Object.keys(tableSchema).map(async tName => {
allAsyncMkdir.push(mkdirp(`${MODULEPATH}/${tName}`))
})
Promise.all(allAsyncMkdir)
.then(_ => {
resolve()
})
.catch(err =>{
reject(err)
})
})
}
async function readAndWrite(tableName, path, data) {
await writeFile(`${MODULEPATH}/${tableName}/${tableName}.${path}.js`, data)
console.log(colors.green("writing complete"))
}
// deleteFiles()
function deleteFiles(){
let modulePath = BASEPATH + "/module"
Object.keys(tableSchema).map(async tName => {
fs.unlinkSync(`${modulePath}/${tName}/${tName}.controller.js`)
fs.unlinkSync(`${modulePath}/${tName}/${tName}.route.js`)
console.log(colors.red("all files deleted"))
})
}