Skip to content

feat: add command line to convert files #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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);
}
Loading