From c123c10e1bbd6a53cdfbdf6f5d96c3bd0a2654b7 Mon Sep 17 00:00:00 2001 From: Raisel Melian Date: Thu, 24 Jan 2019 17:43:18 -0500 Subject: [PATCH] #2 cleanup and refactorings --- cli.js | 11 +++-------- convert.js | 8 ++++++-- lib/converter.js | 28 ++++++++++++++++++---------- 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/cli.js b/cli.js index 273aff8..5b45454 100755 --- a/cli.js +++ b/cli.js @@ -9,26 +9,21 @@ const { version } = require('./package.json'); const validate = require('./validate'); const convert = require('./convert'); -function collect(val, item) { - item.push(val); - return item; -} - program .version(version) .usage(''); - // .option('-c, --config [configFile]', 'config file (containing JSON/YAML). See README for potential values.'); program .command('validate ') - .description('validate AsyncAPI file/url') + .description('validate AsyncAPI file/url. If valid, exit code is 0, otherwise exit code is 1.') .action(validate.command); program .command('convert ') .description('convert AsyncAPI file/url from YAML to JSON and vice-versa. No compression is performed') - .option('-f, --format ', 'specify desired target format: json|yaml', /^(json|yaml)$/i) + .option('-f, --format ', 'specify desired target format: json|yaml') .action((specFile, cmd) => { + console.log(cmd.format); convert.command(specFile, cmd.format) }); diff --git a/convert.js b/convert.js index 46ba2d6..e2c2b0d 100644 --- a/convert.js +++ b/convert.js @@ -7,8 +7,12 @@ const formats = converter.formats; const command = async (specFilePath, targetFormat) => { - if (targetFormat !== formats.YAML || targetFormat !== formats.JSON) { - console.log("throw `Invalid target format: ${targetFormat}`;"); + if (targetFormat === true ) { + targetFormat = undefined; + } + + if (targetFormat !== formats.YAML && targetFormat !== formats.JSON) { + console.error(`Invalid target format: ${targetFormat}`); process.exit(1); } diff --git a/lib/converter.js b/lib/converter.js index ba1c75c..ff5f55b 100644 --- a/lib/converter.js +++ b/lib/converter.js @@ -8,6 +8,22 @@ const formats = Object.freeze({ YAML: "yaml" }); +function convertToJson(content) { + let jsonObject = JSON.parse(content); + + return YAML.safeDump(jsonObject); +} + +async function convertToYaml(content) { + const parsedContent = YAML.safeLoad(content); + + const bundledContent = await parser.bundle(parsedContent); + + const dereferencedContent = await parser.dereference(bundledContent); + + return JSON.stringify(dereferencedContent); +} + const convert = async (specFilePath, targetFormat) => { let content = await parser.getFileContent(specFilePath); @@ -16,19 +32,11 @@ const convert = async (specFilePath, targetFormat) => { if (targetFormat === formats.YAML) { - let jsonObject = JSON.parse(content); - - return YAML.safeDump(jsonObject); + return convertToJson(content); } else if (targetFormat === formats.JSON) { - const parsedContent = YAML.safeLoad(content); - - const bundledContent = await parser.bundle(parsedContent); - - const dereferencedContent = await parser.dereference(bundledContent); - - return JSON.stringify(dereferencedContent); + return await convertToYaml(content); } throw `Invalid target format: ${targetFormat}`;