@asyncapi/converter
Advanced tools
Comparing version 0.7.0 to 0.7.1
11
cli.js
@@ -40,5 +40,12 @@ #!/usr/bin/env node | ||
const asyncapi = fs.readFileSync(asyncapiFile, 'utf-8'); | ||
console.log(converter.convert(asyncapi, version, { | ||
let converted = converter.convert(asyncapi, version, { | ||
id: program.id, | ||
})); | ||
}); | ||
// JSON case | ||
if (typeof converted === 'object') { | ||
converted = JSON.stringify(converted, undefined, 2); | ||
} | ||
console.log(converted); | ||
} catch (e) { | ||
@@ -45,0 +52,0 @@ showErrorAndExit(e); |
@@ -6,3 +6,20 @@ const _ = require('lodash'); | ||
helpers.serialize = (text) => { | ||
if (typeof text === 'object') { | ||
return { | ||
isYAML: false, | ||
parsed: text, | ||
}; | ||
} | ||
try { | ||
const maybeJSON = JSON.parse(text); | ||
if (typeof maybeJSON === 'object') { | ||
return { | ||
isYAML: false, | ||
parsed: maybeJSON, | ||
}; | ||
} | ||
// if `maybeJSON` is object, then we have 100% sure that we operate on JSON, | ||
// but if it's `string` then we have option that it can be YAML but it doesn't have to be | ||
return { | ||
@@ -14,5 +31,6 @@ isYAML: true, | ||
try { | ||
// try to parse again YAML, because the text itself may not have a JSON representation and cannot be represented as a JSON object/string | ||
return { | ||
isYAML: false, | ||
parsed: JSON.parse(text) | ||
isYAML: true, | ||
parsed: yaml.safeLoad(text) | ||
}; | ||
@@ -19,0 +37,0 @@ } catch (err) { |
{ | ||
"name": "@asyncapi/converter", | ||
"version": "0.7.0", | ||
"version": "0.7.1", | ||
"description": "Convert AsyncAPI documents from older to newer versions.", | ||
@@ -5,0 +5,0 @@ "bin": { |
@@ -5,2 +5,3 @@ const assert = require('assert'); | ||
const { convert } = require('../lib'); | ||
const { serialize } = require('../lib/helpers'); | ||
@@ -197,4 +198,46 @@ describe('#convert', () => { | ||
}); | ||
it('should convert from 2.0.0 to 2.1.0 (JSON case)', () => { | ||
const input = fs.readFileSync(path.resolve(__dirname, 'input', '2.0.0', 'streetlights.json'), 'utf8'); | ||
let output = fs.readFileSync(path.resolve(__dirname, 'output', '2.1.0', 'streetlights.json'), 'utf8'); | ||
let result = convert(input, '2.1.0'); | ||
output = JSON.stringify(JSON.parse(output)); | ||
result = JSON.stringify(JSON.parse(JSON.stringify(result))); | ||
assert.strictEqual(output, result); | ||
}); | ||
}); | ||
describe('#serialize', () => { | ||
it('should serialize JSON', () => { | ||
const input = '{"foo": "bar"}'; | ||
const output = serialize(input); | ||
assert.strictEqual(output.isYAML, false); | ||
assert.deepEqual(output.parsed, {foo: "bar"}); | ||
}); | ||
it('should serialize YAML', () => { | ||
const input = 'foo: bar'; | ||
const output = serialize(input); | ||
assert.strictEqual(output.isYAML, true); | ||
assert.deepEqual(output.parsed, {foo: "bar"}); | ||
}); | ||
it('should serialize YAML (with JSON syntax)', () => { | ||
const input = '{foo: bar}'; | ||
const output = serialize(input); | ||
assert.strictEqual(output.isYAML, true); | ||
assert.deepEqual(output.parsed, {foo: "bar"}); | ||
}); | ||
it('should throw error', () => { | ||
const input = '%{foo: bar}'; | ||
try { | ||
serialize(input); | ||
} catch(e) { | ||
assert.strictEqual(e.message, 'AsyncAPI document must be a valid JSON or YAML document.'); | ||
} | ||
}); | ||
}); | ||
/* | ||
@@ -210,2 +253,2 @@ It is a helper required for testing on windows. It can't be solved by editor configuration and the end line setting because expected result is converted during tests. | ||
assert.strictEqual(removeLineBreaks(output), removeLineBreaks(result)); | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
235309
51
875