Skip to content

Commit

Permalink
feat: add command line to convert files (#24)
Browse files Browse the repository at this point in the history
Co-authored-by: Tremper, Diego (ESI) <diego.tremper@adp.com>
  • Loading branch information
diegotremper and Tremper, Diego (ESI) committed Jul 29, 2021
1 parent f6cf737 commit b0df26e
Show file tree
Hide file tree
Showing 5 changed files with 1,183 additions and 3,831 deletions.
15 changes: 15 additions & 0 deletions README.md
Expand Up @@ -62,6 +62,21 @@ If set to `false`, converts the provided schema in place. If `true`, clones the

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

## Command Line

```sh
Usage:
json-schema-to-openapi-schema <command> [options] <file>

Commands:
convert Converts JSON Schema Draft 04 to OpenAPI 3.0 Schema Object

Options:
-h, --help Show help for any command
-v, --version Output the CLI version number
-d, --dereference If set all local and remote references (http/https and file) $refs will be dereferenced
```

## Why?

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).
Expand Down
26 changes: 26 additions & 0 deletions bin/help-text.json
@@ -0,0 +1,26 @@
{
"default": [
"Usage:",
" json-schema-to-openapi-schema <command> [options] <file>",
"",
"Commands:",
" convert Converts JSON Schema Draft 04 to OpenAPI 3.0 Schema Object",
"",
"Options:",
" -h, --help Show help for any command",
" -v, --version Output the CLI version number",
" -d, --dereference If set all local and remote references (http/https and file) $refs will be dereferenced",
""
],
"convert": [
"Converts JSON Schema Draft 04 to OpenAPI 3.0 Schema Object.",
"Returns a non-zero exit code if conversion fails.",
"",
"Usage:",
" json-schema-to-openapi-schema convert [options] <file>",
"",
"Options:",
" -d, --dereference If set all local and remote references (http/https and file) $refs will be dereferenced",
""
]
}
117 changes: 117 additions & 0 deletions bin/json-schema-to-openapi-schema.js
@@ -0,0 +1,117 @@
#!/usr/bin/env node
"use strict";

const yargs = require("yargs");
const chalk = require("chalk");
const converter = require("..");
const helpText = require("./help-text.json");
const fs = require('fs');
const readFileAsync = require('util').promisify(fs.readFile);

(function main () {
let args = parseArgs();
let command = args.command;
let file = args.file;
let options = args.options;

if (options.help) {
// Show help text and exit
console.log(getHelpText(command));
process.exit(0);
}
else if (command === "convert" && file) {
// Convert the JSON Schema file
convert(file, options);
}
else {
// Invalid args. Show help text and exit with non-zero
console.error("Error: Invalid arguments\n");
console.error(getHelpText(command));
process.exit(1);
}
}());


/**
* Parses the command-line arguments
*
* @returns {object} - The parsed arguments
*/
function parseArgs () {
// Configure the argument parser
yargs
.option("d", {
alias: "dereference",
type: "boolean",
default: false,
})
.option("h", {
alias: "help",
type: "boolean",
});

// Show the version number on "--version" or "-v"
yargs
.version()
.alias("v", "version");

// Disable the default "--help" behavior
yargs.help(false);

// Parse the command-line arguments
let args = yargs.argv;

// Normalize the parsed arguments
let parsed = {
command: args._[0],
file: args._[1],
options: {
dereference: args.dereference,
help: args.help,
}
};

return parsed;
}


/**
* Convert an JSON Schema to OpenAPI schema
*
* @param {string} file - The path of the file to convert
* @param {object} options - Conversion options
*/
async function convert (file, options) {
try {
const schema = await readFileAsync(file, 'utf8');
const converted = await converter(JSON.parse(schema), options)
console.log(JSON.stringify(converted));
}
catch (error) {
errorHandler(error);
}
}


/**
* Returns the help text for the specified command
*
* @param {string} [commandName] - The command to show help text for
* @returns {string} - the help text
*/
function getHelpText (commandName) {
let lines = helpText[commandName] || helpText.default;
return lines.join("\n");
}


/**
* Writes error information to stderr and exits with a non-zero code
*
* @param {Error} err
*/
function errorHandler (err) {
let errorMessage = process.env.DEBUG ? err.stack : err.message;
console.error(chalk.red(errorMessage));
process.exit(1);
}

0 comments on commit b0df26e

Please sign in to comment.