-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
98 lines (83 loc) · 3.68 KB
/
index.ts
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
import * as fs from "fs";
import * as JSONStream from "JSONStream";
import * as es from "event-stream";
import { Service } from "./class/Service";
import { UtilService } from "./UtilService";
import { SwaggerMethodDefinition } from "./class/swagger/SwaggerMethodDefinition";
import { SwaggerEntidade } from "./class/swagger/SwaggerEntidade";
import { APIDefinition } from "./class/ApiDefinition";
import { Entidade } from "./class/Entidade";
import { Readable, Writable } from "stream";
import { WriterService } from "./WriterService";
// var swaggerJson = require('./swagger.json')
// var agrupamentosServicos: any = {}
var servicos: Service[] = []
var entidades: Entidade[] = [];
class ServiceStream extends Writable {
_write(chunk: any, encoding: string, done: any) {
console.log(chunk);
done()
}
}
var requestTemplateString = fs.readFileSync('./request-template.template').toString();
var stream = fs.createReadStream('swagger-teste.json', { encoding: 'utf8' })
.pipe(JSONStream.parse(null))
.pipe(es.map(function (swaggerJson: any, cb: any) {
// console.log(data)
swaggerJson.tags.forEach((tag: any) => {
var servico = new Service();
servico.tag = tag.name;
servico.nome = UtilService.tagNameToServiceName(tag.name);
servico.apis = [];
servicos.push(servico)
})
var definitions: any = swaggerJson.definitions
for (const entidadeName in definitions) {
if (definitions.hasOwnProperty(entidadeName)) {
const swaggerEntidade: SwaggerEntidade = definitions[entidadeName];
entidades.push(UtilService.converterEntidadeFromSwaggerEntidade(entidadeName, swaggerEntidade));
}
}
swaggerJson.definitions
var paths: any = swaggerJson.paths;
for (const pathString in paths) {
if (paths.hasOwnProperty(pathString)) {
const swaggerPath = paths[pathString];
for (const methodString in swaggerPath) {
if (swaggerPath.hasOwnProperty(methodString)) {
const methodApi: SwaggerMethodDefinition = swaggerPath[methodString];
const service = UtilService.getServiceByTag(servicos, methodApi.tags[0]);
if (service) {
service.apis.push(UtilService.getApiDefinitionFromSwaggerMethodDefinition(methodString, pathString, methodApi));
} else {
console.warn(`Service ${methodApi.tags[0]} não encontrada`)
}
}
}
}
}
// console.log(JSON.stringify(servicos));
servicos.forEach(servico => {
if (servico.apis.length) {
// var servico = servicos[0];
var writerStream = fs.createWriteStream(`./server/api/${servico.nome}.js`, { flags: 'w' })
.on('finish', function () {
console.log("Write Finish.");
})
.on('error', function (err) {
console.log(err.stack);
});
writerStream.write(`module.exports = function ${servico.nome.replace(" ", "")}(app) {\n\n`)
servico.apis.forEach(api => {
writerStream.write(WriterService.getTemplateForApi(requestTemplateString, api, entidades));
})
writerStream.write('}')
writerStream.end();
}
})
// Mark the end of file
// });
cb(null)
}))
.pipe(new ServiceStream())
setInterval(() => { }, 1 << 30);