forked from wework/json-schema-to-openapi-schema
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add command line to convert files (#24)
Co-authored-by: Tremper, Diego (ESI) <[email protected]>
- Loading branch information
1 parent
f6cf737
commit b0df26e
Showing
5 changed files
with
1,183 additions
and
3,831 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
"" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.