-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
339 lines (263 loc) · 11.3 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
(function () {
"use strict";
const fs = require('fs')
const SwaggerParser = require("@apidevtools/swagger-parser");
const swaggerParser = new SwaggerParser()
const plantuml = require('node-plantuml')
const urlLib = require('url')
const path = require('path')
const mkdirp = require('mkdirp');
const parseArgs = require('minimist')
// There seems to be no unique extension for plantuml file that the community agrees on
// Some use the short ".pu", other the descriptive ".plantuml", and I have seen a case of ".puml"
// If people request otherwise, a different default could be used, or it could be specified in the --uml flag
const umlExtension = ".plantuml"
var swaggerUrlStr = "" // will be filled up by code at the end of this function
var outfileFileName = "" // either swaggerFileName + .png, or specified by the --output arg
var formatAsPlantUml = false // output plantuml data or png diagram
// to get a printf like function in javascript you need to add it yourself!
const str = {}
str.format = function() {
var s = arguments[0];
for (var i = 0; i < arguments.length - 1; i += 1) {
var reg = new RegExp('\\{' + i + '\\}', 'gm');
s = s.replace(reg, arguments[i + 1]);
}
return s;
};
const internals = {}
internals.plant_writeStartUml = function () {
return '@startuml\n\n';
}
internals.plant_writeTitle = function (apiTree, pc) {
return pc + 'title ' + apiTree.title + ' - Version ' + apiTree.version + '\n\n';
}
internals.plant_writeEndUml = function (pc) {
return pc + '@enduml\n';
}
internals.plant_writeResourceClasses = function (apiTree, pc) {
var s = pc;
var resourceTree = apiTree;
for (var r in resourceTree.resources) {
if (resourceTree.resources.hasOwnProperty(r)) {
s = internals.plant_writeResourceClass(r, apiTree.title, resourceTree.resources[r], s);
}
}
return s;
}
internals.plant_writeResourceClass = function (resource, parent, subResources, pc) {
var s = pc;
s = s + 'class "' + resource + '" <<resource>> {\n';
// add http_verbs as methods
s = s + '__ http __\n';
for (var v in subResources.http_verbs) {
if (!subResources.http_verbs.hasOwnProperty(v)) continue;
s = s + v + '(';
for (var param in subResources.http_verbs[v].params) {
if (!subResources.http_verbs[v].params.hasOwnProperty(param)) continue;
s = s + param + ',';
}
if (s[s.length - 1] == ',') {
s = s.slice(0, -1);
}
s = s + ')\n';
}
// end of class
s = s + '}\n\n';
s = s + '"' + parent + '" --> "' + resource + '"\n\n';
for (var r in subResources) {
if (r == 'http_verbs') continue;
if (subResources.hasOwnProperty(r)) {
s = internals.plant_writeResourceClass(r, resource, subResources[r], s);
}
}
return s;
}
internals.plant_writeApiClass = function (apiTree, pc) {
var s = pc;
s = s + 'class "' + apiTree.title + '" <<api>>\n\n';
return s;
}
/*
Die relevanten Informationen werden aus der swagger API
Struktur extrahiert und für die Transformation in die Ausgabeformate
in eine Zwischenstruktur überführt.
*/
internals.extractApiData = function (api, cb) {
var valid_http_verbs = ['get', 'put', 'post', 'delete', 'head', 'options', 'patch'];
var resourceTree = {};
resourceTree.resources = {};
resourceTree.title = api.info.title;
resourceTree.version = api.info.version;
for (var p in api.paths) {
if (api.paths.hasOwnProperty(p)) {
// Pfadangabe muss mit Slash starten
if (p[0] == '/') {
// Pfad in URI-Teile zerlegen
var pathSegments = p.split('/');
// Leere Elemente entfernen
pathSegments = pathSegments.filter(function (n) { return n != '' });
var root = resourceTree.resources;
for (var r in pathSegments) {
if (!pathSegments.hasOwnProperty(r)) continue;
var prop = pathSegments[r];
if (root[prop] == undefined) {
root[prop] = {};
}
root = root[prop];
}
root.http_verbs = {};
for (var v in valid_http_verbs) {
if (!valid_http_verbs.hasOwnProperty(v)) continue;
if (api.paths[p][valid_http_verbs[v]] != undefined) {
root.http_verbs[valid_http_verbs[v]] = {};
root.http_verbs[valid_http_verbs[v]].params = {};
for (var param in api.paths[p][valid_http_verbs[v]].parameters) {
if (!api.paths[p][valid_http_verbs[v]].parameters.hasOwnProperty(param)) continue;
root.http_verbs[valid_http_verbs[v]].params[api.paths[p][valid_http_verbs[v]].parameters[param].name] = {};
}
}
}
}
}
}
resourceTree.definitions = {};
for (var d in api.definitions) {
if (!api.definitions.hasOwnProperty(d)) continue;
resourceTree.definitions[d] = {};
for (p in api.definitions[d].properties) {
if (!api.definitions[d].properties.hasOwnProperty(p)) continue;
resourceTree.definitions[d][p] = {};
}
}
cb(resourceTree);
}
internals.plant_writeRepresentationClasses = function (apiData, pc) {
var s = pc;
s = s + 'package Representations/Messages <<Folder>> {\n';
for (var d in apiData.definitions) {
s = s + 'class "' + d + '" <<representation>> { \n';
s = s + '__properties__\n';
for (var p in apiData.definitions[d]) {
if (!apiData.definitions[d].hasOwnProperty(p)) continue;
s = s + p + '\n';
}
s = s + '}\n';
}
s = s + "}\n\n";
return s;
}
internals.plant_writeSkinParams = function (pc) {
var s = pc;
s = s + 'skinparam stereotypeCBackgroundColor<<representation>> DimGray\n';
s = s + 'skinparam stereotypeCBackgroundColor<<api>> Red\n';
s = s + 'skinparam stereotypeCBackgroundColor<<resource>> SpringGreen\n';
s = s + 'skinparam class {\n';
s = s + 'BackgroundColor<<api>> Yellow\n';
s = s + 'BackgroundColor<<representation>> Silver\n';
s = s + 'BackgroundColor<<resource>> YellowGreen\n';
s = s + '}\n\n';
return s;
}
internals.plant_writeLegend = function (apiTree, pc) {
var s = pc;
var d = new Date();
d.setHours(d.getHours() + 2);
s += 'legend left\n';
s += 'created with pikturr (https://github.com/nrekretep/pikturr)\n';
s += d.toISOString() + '\n';
s += 'endlegend\n\n';
return s;
}
internals.convertToPlantUml = function (apiData) {
var s = internals.plant_writeStartUml();
s = internals.plant_writeTitle(apiData, s);
s = internals.plant_writeSkinParams(s);
s = internals.plant_writeApiClass(apiData, s);
s = internals.plant_writeResourceClasses(apiData, s);
s = internals.plant_writeRepresentationClasses(apiData, s);
s = internals.plant_writeLegend(apiData, s);
s = internals.plant_writeEndUml(s);
if (formatAsPlantUml) {
// write the plantuml syntax in the s string to the output file
fs.writeFile(outfileFileName, s, function(err) {
if(err) {
return console.error(err);
}
console.log(str.format("Saved uml specification into file: {0}.",outfileFileName));
});
return
}
// generate the png diagram, and store it in the output file
var gen = plantuml.generate(s, { format: 'png' });
gen.out.pipe(fs.createWriteStream(outfileFileName));
console.log(str.format("Saved png diagram into file: {0}.",outfileFileName));
}
// runs the equivalent to the "mkdir -p" command to create all directories in the output file path
// if any of them is missing
internals.ensureOutputPath = function (filePath) {
var outPath = path.parse(filePath).dir
try {
mkdirp.sync(outPath)
}
catch (err) {
if (err.code != 'EEXIST' || ! fs.lstatSync(outPath).isDirectory() ) {
console.error(str.format("Failed to create output path: {0}; {1}.", outPath, err));
process.exit(2)
}
}
}
var pikturr = {};
exports.generate = pikturr.generate = function (url) {
swaggerParser.parse(url).then(function (api) {
internals.extractApiData(api, internals.convertToPlantUml);
}).catch(function (err) {
console.error(err);
})
}
var options = {}
// Set variables based on options provided on the command line
// -o | --output filePath:
// path or file name of the output file
// default is leaf file name in the url argument
// -u | --uml: output plant uml script in output file
// default is to output png diagram
options.processOptions = function() {
var optionsConfig = {boolean: ["u", "uml"]}
var argv = parseArgs(process.argv.slice(2), optionsConfig)
if (argv._.length < 1) {
console.error("Last argument must be the path or url of the swagger file to read.")
process.exit(1)
}
// pick up last arg; the url/path of file to read
swaggerUrlStr = argv._[argv._.length - 1]
if (("o" in argv) && (argv.o != "")) {
outfileFileName = argv.o
} else if (("output" in argv) && (argv.output != "")) {
outfileFileName = argv.output
}
if ((("u" in argv) && argv.u) || (("uml" in argv) && argv.uml)) {
formatAsPlantUml = true
}
}
if (!module.parent) {
// sets: swaggerUrlStr, and optionally outfileFileName
options.processOptions()
if (outfileFileName == "") {
// the default output file is the url leaf name + ".png" or ".plantuml"
var swaggerURL = urlLib.parse(swaggerUrlStr)
var swaggerPath = swaggerURL.pathname
// leaf name without extension
var swaggerFileName = path.parse(swaggerPath).name
// outfileFileName to be used by internals.convertToPlantUml
if (formatAsPlantUml) {
outfileFileName = swaggerFileName + umlExtension
} else {
outfileFileName = swaggerFileName + '.png'
}
}
// ensure all dir in outfileFileName exists
internals.ensureOutputPath(outfileFileName)
pikturr.generate(swaggerUrlStr);
}
})();