-
Notifications
You must be signed in to change notification settings - Fork 2
/
SwaggerServiceGenerator.js
101 lines (93 loc) · 3.69 KB
/
SwaggerServiceGenerator.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
let fs = require("fs");
let Handlebars = require("handlebars");
const abi2oas = require('abi2oas');
/**
* @class
* @classdesc Parser class that actually parses and generates OpenAPI config
* */
class SwaggerServiceGenerator {
/**
* @constructor
* @description
* Read's the contract schema which is built using truffle migrate and stores the schema.
* */
constructor(contract_path, config={}) {
this.openAPIGenerator = new abi2oas(contract_path);
this.config = typeof config === 'string' ?
JSON.parse(fs.readFileSync(config)) :
config;
}
/**
* @function
* @instance
* @description Initializes the service generator by initializing the OpenApiGenerator
* */
init() {
this.openAPIGenerator.init();
}
/**
* @function
* @instance
* @param {String} relative_path_to_eth_connector -
* relative path to eth connector from output folder to folder in which Connector.js is present
* @description process generation by processing openAPI generation
* */
process(relative_path_to_eth_connector) {
this.openAPIGenerator.process();
let controller_template = String(fs.readFileSync("./SwaggerService_template.hbs"));
Handlebars.registerHelper({
ifEquals(param1, param2, options){
return param1 === param2 ? options.fn(this) : options.inverse(this);
},
logconsole(){
let args = Array.prototype.slice.call(arguments);
console.log(args.slice(0, args.length - 1));
},
append(){
let args = Array.prototype.slice.call(arguments);
return args.slice(0, args.length - 1).join("");
},
ifHas(list, item, options){
return (list.indexOf(item) !== -1) ? options.fn(this) : "";
}
});
let abi = this.openAPIGenerator.contract.abi;
this.service_files_code = {};
for(let tag of this.openAPIGenerator.openAPI.tags){
let methods = this.openAPIGenerator.openAPI.getMethodsForTag(tag);
let allowed_methods = methods.map(method => {
return `${method.method}_${method.path.path}`;
});
this.service_files_code[tag.name] = Handlebars.compile(controller_template)({
path_to_eth_connector: relative_path_to_eth_connector,
methods: abi,
allowed_methods: allowed_methods
});
console.log(`Generated ${methods.length} method(s) in "${tag.name}" scope`);
}
}
/**
* @function
* @instance
* @param {String} swagger_service_folder - path to swagger service folder
* @description Writes the serialized json data to provided file path. Else writes the output to console...
* */
build(swagger_service_folder) {
console.log(`Writing to file...`);
for(let service_file_tag in this.service_files_code){
if(this.service_files_code.hasOwnProperty(service_file_tag)){
let service_file_name = service_file_tag.replace(service_file_tag[0], service_file_tag[0].toUpperCase());
fs.writeFileSync(swagger_service_folder+"/"+service_file_name+"Service.js", this.service_files_code[service_file_tag]);
}
}
console.log(`Written! please check ${swagger_service_folder} for generated service files.`);
}
}
if (require.main === module) {
let parser = new SwaggerServiceGenerator(process.argv[2]);
parser.init();
parser.process(process.argv[3]);
parser.build(process.argv[4]);
}else{
module.exports = SwaggerServiceGenerator;
}