Skip to content

Commit

Permalink
#2 cleanup and refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
Raisel Melian committed Jan 24, 2019
1 parent 5e2685b commit c123c10
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
11 changes: 3 additions & 8 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<command>');
// .option('-c, --config [configFile]', 'config file (containing JSON/YAML). See README for potential values.');

program
.command('validate <file-or-url>')
.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 <file-or-url>')
.description('convert AsyncAPI file/url from YAML to JSON and vice-versa. No compression is performed')
.option('-f, --format <format>', 'specify desired target format: json|yaml', /^(json|yaml)$/i)
.option('-f, --format <format>', 'specify desired target format: json|yaml')
.action((specFile, cmd) => {
console.log(cmd.format);
convert.command(specFile, cmd.format)
});

Expand Down
8 changes: 6 additions & 2 deletions convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
28 changes: 18 additions & 10 deletions lib/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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}`;
Expand Down

0 comments on commit c123c10

Please sign in to comment.