|
| 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