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
·91 lines (81 loc) · 2.21 KB
/
Copy pathcli.js
File metadata and controls
executable file
·91 lines (81 loc) · 2.21 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
#!/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");
var unknown = false;
var argv = minimist(process.argv.slice(2), {
string: ["t", "target", "scheme", "host", "basePath"],
boolean: [
"d",
"diagram",
"h",
"help",
"p",
"pretty",
"u",
"used-schemas-only",
],
alias: {
d: "diagram",
h: "help",
p: "pretty",
t: "target",
u: "used-schemas-only",
},
default: {
pretty: 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)
-d, --diagram include YUML diagram
-h, --help show this info
--host host (default: localhost)
-p, --pretty pretty-print JSON result
--scheme scheme (default: http)
-t, --target target file (default: source file basename + .openapi3.json)`);
} 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);
if (json.$Version < "3.0") {
console.error("Only OData Version 3.0 or greater is supported");
return;
}
const target =
argv.target ||
(source.lastIndexOf(".") <= 0
? source
: source.substring(0, source.lastIndexOf("."))) + ".openapi3.json";
console.log(target);
const openapi = lib.csdl2openapi(json, {
scheme: argv.scheme,
host: argv.host,
basePath: argv.basePath,
diagram: argv.diagram,
});
fs.writeFileSync(target, JSON.stringify(openapi, null, argv.pretty ? 4 : 0));
}