forked from Mermade/openapi-codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
164 lines (148 loc) · 7.55 KB
/
index.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// @ts-check
'use strict';
const fs = require('fs');
const path = require('path');
const util = require('util');
const mkdirp = require('mkdirp');
const rimraf = require('rimraf');
const Hogan = require('hogan.js');
const clone = require('reftools/lib/clone.js').circularClone; // must preserve functions
const adaptor = require('./adaptor.js');
// allows other backends, such as a stream writer for .tar.gz files
let ff = {
readFileSync: fs.readFileSync,
createFile: fs.writeFileSync,
rimraf: rimraf,
mkdirp: mkdirp
};
function tpl(config, ...segments) {
if (config.templateDir) {
segments.splice(0,1);
return path.join(config.templateDir, ...segments);
}
return path.join(__dirname, 'templates', ...segments)
}
function main(o, config, configName, callback) {
let outputDir = config.outputDir || './out/';
let verbose = config.defaults.verbose;
config.defaults.configName = configName;
adaptor.transform(o, config.defaults, function(err, model) {
for (let p in config.partials) {
let partial = config.partials[p];
if (verbose) console.log('Processing partial '+partial);
config.partials[p] = ff.readFileSync(tpl(config, configName, partial),'utf8');
}
let actions = [];
for (let t in config.transformations) {
let tx = config.transformations[t];
if (tx.input) {
if (verbose) console.log('Processing template ' + tx.input);
tx.template = ff.readFileSync(tpl(config, configName, tx.input),'utf8');
}
actions.push(tx);
}
const subDir = (config.defaults.flat ? '' : configName);
if (verbose) console.log('Making/cleaning output directories');
ff.mkdirp(path.join(outputDir,subDir),function(){
ff.rimraf(path.join(outputDir,subDir)+'/*',function(){
if (config.directories) {
for (let directory of config.directories) {
ff.mkdirp.sync(path.join(outputDir,subDir,directory));
}
}
for (let action of actions) {
if (verbose) console.log('Rendering '+action.output);
let template = Hogan.compile(action.template);
let content = template.render(model,config.partials);
ff.createFile(path.join(outputDir,subDir,action.output),content,'utf8');
}
if (config.touch) { // may not now be necessary
let touchTmp = Hogan.compile(config.touch);
let touchList = touchTmp.render(model,config.partials);
let files = touchList.split('\r').join('').split('\n');
for (let file of files) {
file = file.trim();
if (file) {
if (!fs.existsSync(path.join(outputDir,subDir,file))) {
ff.createFile(path.join(outputDir,subDir,file),'','utf8');
}
}
}
}
if (config.apache) {
ff.createFile(path.join(outputDir,subDir,'LICENSE'),ff.readFileSync(tpl({}, '_common', 'LICENSE'),'utf8'),'utf8');
}
else {
ff.createFile(path.join(outputDir,subDir,'LICENSE'),ff.readFileSync(tpl({}, '_common', 'UNLICENSE'),'utf8'),'utf8');
}
let outer = model;
if (config.perApi) {
let toplevel = clone(model);
delete toplevel.apiInfo;
for (let pa of config.perApi) {
let fnTemplate = Hogan.compile(pa.output);
let template = Hogan.compile(ff.readFileSync(tpl(config, configName, pa.input), 'utf8'));
for (let api of model.apiInfo.apis) {
let cApi = Object.assign({},config.defaults,pa.defaults||{},toplevel,api);
let filename = fnTemplate.render(cApi,config.partials);
if (verbose) console.log('Rendering '+filename+' (dynamic:'+pa.input+')');
ff.createFile(path.join(outputDir,subDir,filename),template.render(cApi,config.partials),'utf8');
}
}
}
if (config.perPath) {
let toplevel = clone(model);
delete toplevel.apiInfo;
for (let pa of config.perPath) {
let fnTemplate = Hogan.compile(pa.output);
let template = Hogan.compile(ff.readFileSync(tpl(config, configName, pa.input), 'utf8'));
for (let pat of model.apiInfo.paths) {
let cPath = Object.assign({},config.defaults,pa.defaults||{},toplevel,pat);
let filename = fnTemplate.render(cPath,config.partials);
let dirname = path.dirname(filename);
if (verbose) console.log('Rendering '+filename+' (dynamic:'+pa.input+')');
ff.mkdirp.sync(path.join(outputDir,subDir,dirname));
ff.createFile(path.join(outputDir,subDir,filename),template.render(cPath,config.partials),'utf8');
}
}
}
if (config.perModel) {
let cModels = clone(model.models);
for (let pm of config.perModel) {
let fnTemplate = Hogan.compile(pm.output);
let template = Hogan.compile(ff.readFileSync(tpl(config, configName, pm.input), 'utf8'));
for (let model of cModels) {
outer.models = [];
let effModel = Object.assign({},model,pm.defaults||{});
outer.models.push(effModel);
let filename = fnTemplate.render(outer,config.partials);
if (verbose) console.log('Rendering '+filename+' (dynamic:'+pm.input+')');
ff.createFile(path.join(outputDir,subDir,filename),template.render(outer,config.partials),'utf8');
}
}
}
if (config.perOperation) { // now may not be necessary
for (let po of config.perOperation) {
for (let api of outer.apiInfo.apis) {
let cOperations = clone(api.operations);
let fnTemplate = Hogan.compile(po.output);
let template = Hogan.compile(ff.readFileSync(tpl(config, configName, po.input), 'utf8'));
for (let operation of cOperations.operation) {
model.operations = [];
model.operations.push(operation);
let filename = fnTemplate.render(outer,config.partials);
if (verbose) console.log('Rendering '+filename+' (dynamic:'+po.input+')');
ff.createFile(path.join(outputDir,subDir,filename),template.render(outer,config.partials),'utf8');
}
}
}
}
if (callback) callback(null,true);
});
});
});
}
module.exports = {
fileFunctions: ff,
main : main
};