Skip to content

Commit b0df26e

Browse files
diegotremperTremper, Diego (ESI)
andauthored
feat: add command line to convert files (#24)
Co-authored-by: Tremper, Diego (ESI) <[email protected]>
1 parent f6cf737 commit b0df26e

File tree

5 files changed

+1183
-3831
lines changed

5 files changed

+1183
-3831
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,21 @@ If set to `false`, converts the provided schema in place. If `true`, clones the
6262

6363
If set to `true`, all local and remote references (http/https and file) $refs will be dereferenced. Defaults to `false`.
6464

65+
## Command Line
66+
67+
```sh
68+
Usage:
69+
json-schema-to-openapi-schema <command> [options] <file>
70+
71+
Commands:
72+
convert Converts JSON Schema Draft 04 to OpenAPI 3.0 Schema Object
73+
74+
Options:
75+
-h, --help Show help for any command
76+
-v, --version Output the CLI version number
77+
-d, --dereference If set all local and remote references (http/https and file) $refs will be dereferenced
78+
```
79+
6580
## Why?
6681

6782
OpenAPI is often described as an extension of JSON Schema, but both specs have changed over time and grown independently. OpenAPI v2 was based on JSON Schema draft v4 with a long list of deviations, but OpenAPI v3 shrank that list, upping their support to draft v4 and making the list of discrepancies shorter. This has been solved for OpenAPI v3.1, but for those using OpenAPI v3.0, you can use this tool to solve [the divergence](https://apisyouwonthate.com/blog/openapi-and-json-schema-divergence).

bin/help-text.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"default": [
3+
"Usage:",
4+
" json-schema-to-openapi-schema <command> [options] <file>",
5+
"",
6+
"Commands:",
7+
" convert Converts JSON Schema Draft 04 to OpenAPI 3.0 Schema Object",
8+
"",
9+
"Options:",
10+
" -h, --help Show help for any command",
11+
" -v, --version Output the CLI version number",
12+
" -d, --dereference If set all local and remote references (http/https and file) $refs will be dereferenced",
13+
""
14+
],
15+
"convert": [
16+
"Converts JSON Schema Draft 04 to OpenAPI 3.0 Schema Object.",
17+
"Returns a non-zero exit code if conversion fails.",
18+
"",
19+
"Usage:",
20+
" json-schema-to-openapi-schema convert [options] <file>",
21+
"",
22+
"Options:",
23+
" -d, --dereference If set all local and remote references (http/https and file) $refs will be dereferenced",
24+
""
25+
]
26+
}

bin/json-schema-to-openapi-schema.js

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#!/usr/bin/env node
2+
"use strict";
3+
4+
const yargs = require("yargs");
5+
const chalk = require("chalk");
6+
const converter = require("..");
7+
const helpText = require("./help-text.json");
8+
const fs = require('fs');
9+
const readFileAsync = require('util').promisify(fs.readFile);
10+
11+
(function main () {
12+
let args = parseArgs();
13+
let command = args.command;
14+
let file = args.file;
15+
let options = args.options;
16+
17+
if (options.help) {
18+
// Show help text and exit
19+
console.log(getHelpText(command));
20+
process.exit(0);
21+
}
22+
else if (command === "convert" && file) {
23+
// Convert the JSON Schema file
24+
convert(file, options);
25+
}
26+
else {
27+
// Invalid args. Show help text and exit with non-zero
28+
console.error("Error: Invalid arguments\n");
29+
console.error(getHelpText(command));
30+
process.exit(1);
31+
}
32+
}());
33+
34+
35+
/**
36+
* Parses the command-line arguments
37+
*
38+
* @returns {object} - The parsed arguments
39+
*/
40+
function parseArgs () {
41+
// Configure the argument parser
42+
yargs
43+
.option("d", {
44+
alias: "dereference",
45+
type: "boolean",
46+
default: false,
47+
})
48+
.option("h", {
49+
alias: "help",
50+
type: "boolean",
51+
});
52+
53+
// Show the version number on "--version" or "-v"
54+
yargs
55+
.version()
56+
.alias("v", "version");
57+
58+
// Disable the default "--help" behavior
59+
yargs.help(false);
60+
61+
// Parse the command-line arguments
62+
let args = yargs.argv;
63+
64+
// Normalize the parsed arguments
65+
let parsed = {
66+
command: args._[0],
67+
file: args._[1],
68+
options: {
69+
dereference: args.dereference,
70+
help: args.help,
71+
}
72+
};
73+
74+
return parsed;
75+
}
76+
77+
78+
/**
79+
* Convert an JSON Schema to OpenAPI schema
80+
*
81+
* @param {string} file - The path of the file to convert
82+
* @param {object} options - Conversion options
83+
*/
84+
async function convert (file, options) {
85+
try {
86+
const schema = await readFileAsync(file, 'utf8');
87+
const converted = await converter(JSON.parse(schema), options)
88+
console.log(JSON.stringify(converted));
89+
}
90+
catch (error) {
91+
errorHandler(error);
92+
}
93+
}
94+
95+
96+
/**
97+
* Returns the help text for the specified command
98+
*
99+
* @param {string} [commandName] - The command to show help text for
100+
* @returns {string} - the help text
101+
*/
102+
function getHelpText (commandName) {
103+
let lines = helpText[commandName] || helpText.default;
104+
return lines.join("\n");
105+
}
106+
107+
108+
/**
109+
* Writes error information to stderr and exits with a non-zero code
110+
*
111+
* @param {Error} err
112+
*/
113+
function errorHandler (err) {
114+
let errorMessage = process.env.DEBUG ? err.stack : err.message;
115+
console.error(chalk.red(errorMessage));
116+
process.exit(1);
117+
}

0 commit comments

Comments
 (0)