forked from oasis-tcs/odata-openapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·120 lines (108 loc) · 2.95 KB
/
cli.js
File metadata and controls
executable file
·120 lines (108 loc) · 2.95 KB
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
#!/usr/bin/env node
"use strict";
//console.dir(argv);
//TODO: what to require?
const csdl = require("odata-csdl");
const lib = require("./csdl2openapi");
const minimist = require("minimist");
const fs = require("fs");
const { stringifyStream } = require("@discoveryjs/json-ext");
var unknown = false;
var argv = minimist(process.argv.slice(2), {
string: [
"o",
"openapi-version",
"t",
"target",
"basePath",
"description",
"host",
"levels",
"scheme",
"title",
],
boolean: [
"d",
"diagram",
"h",
"help",
"p",
"pretty",
"u",
"used-schemas-only",
"skipBatchPath",
],
alias: {
d: "diagram",
h: "help",
o: "openapi-version",
p: "pretty",
t: "target",
u: "used-schemas-only",
},
default: {
levels: 4,
pretty: false,
skipBatchPath: false,
},
unknown: (arg) => {
if (arg.substring(0, 1) == "-") {
console.error("Unknown option: " + arg);
unknown = true;
return false;
}
},
});
if (unknown || argv._.length == 0 || argv.h) {
console.log(`Usage: odata-openapi3 <options> <source file>
Options:
--basePath base path (default: /service-root)
--description default description if none is annotated
-d, --diagram include YUML diagram
-h, --help show this info
--host host (default: localhost)
--levels maximum number of path segments
-o, --openapi-version 3.0.0 to 3.0.3 or 3.1.0 (default: 3.0.2)
-p, --pretty pretty-print JSON result
--scheme scheme (default: http)
--skipBatchPath skips the generation of the $batch path, (default: false)
-t, --target target file (default: source file basename + .openapi3.json)
--title default title if none is annotated`);
} else {
//TODO: further input parameters reserved for e.g. referenced CSDL documents
// for (var i = 0; i < argv._.length; i++) {
// convert(argv._[i]);
// }
convert(argv._[0]);
}
function convert(source) {
if (!fs.existsSync(source)) {
console.error("Source file not found: " + source);
return;
}
const text = fs.readFileSync(source, "utf8");
const json = text.startsWith("<") ? csdl.xml2json(text) : JSON.parse(text);
const target =
argv.target ||
(source.lastIndexOf(".") <= 0
? source
: source.substring(0, source.lastIndexOf("."))) + ".openapi3.json";
console.log(target);
const messages = [];
const openapi = lib.csdl2openapi(json, {
scheme: argv.scheme,
host: argv.host,
basePath: argv.basePath,
diagram: argv.diagram,
maxLevels: Number(argv.levels),
openapiVersion: argv.o,
messages,
skipBatchPath: argv.skipBatchPath,
defaultTitle: argv.title,
defaultDescription: argv.description,
});
stringifyStream(openapi, null, argv.pretty ? 4 : 0).pipe(
fs.createWriteStream(target),
);
if (messages.length > 0) console.dir(messages);
}