@asyncapi/converter
Advanced tools
Comparing version 0.4.3 to 0.5.0
@@ -35,3 +35,3 @@ #!/usr/bin/env node | ||
if (!version) { | ||
version = '2.0.0'; | ||
version = '2.1.0'; | ||
} | ||
@@ -38,0 +38,0 @@ |
@@ -13,18 +13,16 @@ const yaml = require('js-yaml'); | ||
/** | ||
* Value for key (version) represents the function which converts specification from previous version to the given as key. | ||
*/ | ||
const conversions = { | ||
'1.0.0-to-2.0.0-rc1': from1to2rc1, | ||
'1.1.0-to-2.0.0-rc1': from1to2rc1, | ||
'1.2.0-to-2.0.0-rc1': from1to2rc1, | ||
'2.0.0-rc1-to-2.0.0-rc2': from2rc1to2rc2, | ||
'1.0.0-to-2.0.0-rc2': from1to2rc2, | ||
'1.1.0-to-2.0.0-rc2': from1to2rc2, | ||
'1.2.0-to-2.0.0-rc2': from1to2rc2, | ||
'1.0.0-to-2.0.0': from1to2, | ||
'1.1.0-to-2.0.0': from1to2, | ||
'1.2.0-to-2.0.0': from1to2, | ||
'2.0.0-rc1-to-2.0.0': from2rc1to2, | ||
'2.0.0-rc2-to-2.0.0': from2rc2to2, | ||
'1.0.0': undefined, | ||
'1.1.0': from__1_0_0__to__1_1_0, | ||
'1.2.0': from__1_1_0__to__1_2_0, | ||
'2.0.0-rc1': from__1_2_0__to__2_0_0_rc1, | ||
'2.0.0-rc2': from__2_0_0_rc1__to__2_0_0_rc2, | ||
'2.0.0': from__2_0_0_rc2__to__2_0_0, | ||
'2.1.0': from__2_0_0__to__2_1_0, | ||
} | ||
const conversionVersions = Object.keys(conversions); | ||
}; | ||
lib.convert = (asyncapi, version, options = {}) => { | ||
@@ -34,15 +32,41 @@ const { isYAML, parsed } = serialize(asyncapi); | ||
const conversion = `${parsed.asyncapi}-to-${version}`; | ||
let fromVersion = conversionVersions.indexOf(parsed.asyncapi); | ||
const toVersion = conversionVersions.indexOf(version); | ||
if (conversions[conversion]) { | ||
const result = conversions[conversion](parsed, options); | ||
let converted = result; | ||
if (isYAML) converted = yaml.safeDump(result, { skipInvalid: true }); | ||
return converted; | ||
} else { | ||
console.error(`Can't convert from ${parsed.asyncapi} to ${version}.`); | ||
if (fromVersion === -1 || toVersion === -1) { | ||
console.error(`Cannot convert from ${parsed.asyncapi} to ${version}.`); | ||
return; | ||
} | ||
if (fromVersion > toVersion) { | ||
console.error(`Cannot downgrade from ${parsed.asyncapi} to ${version}.`); | ||
return; | ||
} | ||
if (fromVersion === toVersion) { | ||
console.error(`Cannot convert to the same version.`); | ||
return; | ||
} | ||
// add 1 to `fromVersion` because we convert from previous to next | ||
fromVersion++; | ||
let converted = parsed; | ||
for (let i = fromVersion; i <= toVersion; i++) { | ||
const fn = conversions[conversionVersions[i]]; | ||
converted = fn(converted, options); | ||
} | ||
if (isYAML) { | ||
converted = yaml.safeDump(converted, { skipInvalid: true }); | ||
} | ||
return converted; | ||
}; | ||
function from1to2rc1 (asyncapi1, options) { | ||
function from__1_0_0__to__1_1_0(asyncapi) { | ||
return asyncapi; | ||
} | ||
function from__1_1_0__to__1_2_0(asyncapi) { | ||
return asyncapi; | ||
} | ||
function from__1_2_0__to__2_0_0_rc1(asyncapi1, options) { // NOSONAR | ||
const result = asyncapi1; | ||
@@ -104,3 +128,3 @@ | ||
function from2rc1to2rc2 (asyncapi2rc1, options) { | ||
function from__2_0_0_rc1__to__2_0_0_rc2(asyncapi2rc1, options) { // NOSONAR | ||
const result = asyncapi2rc1; | ||
@@ -161,31 +185,17 @@ | ||
if (!options.id) delete result.id; | ||
return result; | ||
} | ||
function from1to2rc2 (asyncapi1, options) { | ||
let res = from1to2rc1(asyncapi1, options); | ||
if (!options.id) delete res.id; | ||
return from2rc1to2rc2(res, options); | ||
} | ||
// Release 2.0.0 mappings | ||
function from1to2 (asyncapi1, options) { | ||
return from2rc2to2(from1to2rc2(asyncapi1, options), options); | ||
} | ||
function from2rc1to2 (asyncapi2rc1, options) { | ||
let result = from2rc1to2rc2(asyncapi2rc1, options); | ||
function from__2_0_0_rc2__to__2_0_0(asyncapi2rc2, options) { | ||
const result = asyncapi2rc2; | ||
if (!options.id) delete result.id; | ||
result.asyncapi = '2.0.0'; | ||
return result; | ||
} | ||
// version 2.0.0-rc2 has the same schema as 2.0.0 release | ||
function from2rc2to2 (asyncapi2rc2, options) { | ||
const result = asyncapi2rc2; | ||
result.asyncapi = '2.0.0'; | ||
function from__2_0_0__to__2_1_0(asyncapi2) { | ||
const result = asyncapi2; | ||
result.asyncapi = '2.1.0'; | ||
return result; | ||
} |
{ | ||
"name": "@asyncapi/converter", | ||
"version": "0.4.3", | ||
"version": "0.5.0", | ||
"description": "Convert AsyncAPI documents from older to newer versions.", | ||
@@ -13,4 +13,6 @@ "bin": { | ||
"release": "semantic-release", | ||
"get-version": "echo $npm_package_version", | ||
"get-name": "echo $npm_package_name" | ||
"generate:assets": "echo 'No additional assets need to be generated at the moment'", | ||
"bump:version": "npm --no-git-tag-version --allow-same-version version $VERSION", | ||
"get:version": "echo $npm_package_version", | ||
"get:name": "echo $npm_package_name" | ||
}, | ||
@@ -17,0 +19,0 @@ "keywords": [ |
@@ -8,3 +8,3 @@ # AsyncAPI Converter | ||
```sh | ||
npm i -g asyncapi-converter | ||
npm i -g @asyncapi/converter | ||
``` | ||
@@ -47,3 +47,3 @@ | ||
```js | ||
const { convert } = require('asyncapi-converter') | ||
const { convert } = require('@asyncapi/converter') | ||
@@ -50,0 +50,0 @@ try { |
@@ -7,2 +7,17 @@ const assert = require('assert'); | ||
describe('#convert', () => { | ||
it('should not convert to lowest version', () => { | ||
const result = convert(`asyncapi: '2.1.0'`, '2.0.0'); | ||
assert.strictEqual(result, undefined); | ||
}); | ||
it('should not convert from non existing version', () => { | ||
const result = convert(`asyncapi: '2.0.0-rc3'`, '2.1.0'); | ||
assert.strictEqual(result, undefined); | ||
}); | ||
it('should not convert to this same version', () => { | ||
const result = convert(`asyncapi: '2.1.0'`, '2.1.0'); | ||
assert.strictEqual(result, undefined); | ||
}); | ||
it('should convert from 1.0.0 to 2.0.0-rc1', () => { | ||
@@ -12,3 +27,3 @@ const input = fs.readFileSync(path.resolve(__dirname, 'input', '1.0.0', 'streetlights.yml'), 'utf8'); | ||
const result = convert(input, '2.0.0-rc1'); | ||
assert.strictEqual(output, result); | ||
assertResults(output, result); | ||
}); | ||
@@ -20,3 +35,3 @@ | ||
const result = convert(input, '2.0.0-rc1'); | ||
assert.strictEqual(output, result); | ||
assertResults(output, result); | ||
}); | ||
@@ -28,3 +43,3 @@ | ||
const result = convert(input, '2.0.0-rc1'); | ||
assert.strictEqual(output, result); | ||
assertResults(output, result); | ||
}); | ||
@@ -36,3 +51,3 @@ | ||
const result = convert(input, '2.0.0-rc1'); | ||
assert.strictEqual(output, result); | ||
assertResults(output, result); | ||
}); | ||
@@ -44,3 +59,3 @@ | ||
const result = convert(input, '2.0.0-rc1'); | ||
assert.strictEqual(output, result); | ||
assertResults(output, result); | ||
}); | ||
@@ -52,3 +67,3 @@ | ||
const result = convert(input, '2.0.0-rc2'); | ||
assert.strictEqual(output, result); | ||
assertResults(output, result); | ||
}); | ||
@@ -60,3 +75,3 @@ | ||
const result = convert(input, '2.0.0-rc2'); | ||
assert.strictEqual(output, result); | ||
assertResults(output, result); | ||
}); | ||
@@ -68,3 +83,3 @@ | ||
const result = convert(input, '2.0.0-rc2'); | ||
assert.strictEqual(output, result); | ||
assertResults(output, result); | ||
}); | ||
@@ -76,3 +91,3 @@ | ||
const result = convert(input, '2.0.0-rc2'); | ||
assert.strictEqual(output, result); | ||
assertResults(output, result); | ||
}); | ||
@@ -84,3 +99,3 @@ | ||
const result = convert(input, '2.0.0-rc2'); | ||
assert.strictEqual(output, result); | ||
assertResults(output, result); | ||
}); | ||
@@ -92,3 +107,3 @@ | ||
const result = convert(input, '2.0.0'); | ||
assert.strictEqual(output, result); | ||
assertResults(output, result); | ||
}); | ||
@@ -100,3 +115,3 @@ | ||
const result = convert(input, '2.0.0'); | ||
assert.strictEqual(output, result); | ||
assertResults(output, result); | ||
}); | ||
@@ -108,3 +123,3 @@ | ||
const result = convert(input, '2.0.0'); | ||
assert.strictEqual(output, result); | ||
assertResults(output, result); | ||
}); | ||
@@ -116,3 +131,3 @@ | ||
const result = convert(input, '2.0.0'); | ||
assert.strictEqual(output, result); | ||
assertResults(output, result); | ||
}); | ||
@@ -124,4 +139,58 @@ | ||
const result = convert(input, '2.0.0'); | ||
assert.strictEqual(output, result); | ||
assertResults(output, result); | ||
}); | ||
}); | ||
it('should convert from 1.0.0 to 2.1.0', () => { | ||
const input = fs.readFileSync(path.resolve(__dirname, 'input', '1.0.0', 'streetlights.yml'), 'utf8'); | ||
const output = fs.readFileSync(path.resolve(__dirname, 'output', '2.1.0', 'streetlights.yml'), 'utf8'); | ||
const result = convert(input, '2.1.0'); | ||
assertResults(output, result); | ||
}); | ||
it('should convert from 1.1.0 to 2.1.0', () => { | ||
const input = fs.readFileSync(path.resolve(__dirname, 'input', '1.1.0', 'streetlights.yml'), 'utf8'); | ||
const output = fs.readFileSync(path.resolve(__dirname, 'output', '2.1.0', 'streetlights.yml'), 'utf8'); | ||
const result = convert(input, '2.1.0'); | ||
assertResults(output, result); | ||
}); | ||
it('should convert from 1.2.0 to 2.1.0', () => { | ||
const input = fs.readFileSync(path.resolve(__dirname, 'input', '1.2.0', 'streetlights.yml'), 'utf8'); | ||
const output = fs.readFileSync(path.resolve(__dirname, 'output', '2.1.0', 'streetlights.yml'), 'utf8'); | ||
const result = convert(input, '2.1.0'); | ||
assertResults(output, result); | ||
}); | ||
it('should convert from 2.0.0-rc1 to 2.1.0', () => { | ||
const input = fs.readFileSync(path.resolve(__dirname, 'input', '2.0.0-rc1', 'streetlights.yml'), 'utf8'); | ||
const output = fs.readFileSync(path.resolve(__dirname, 'output', '2.1.0', 'streetlights.yml'), 'utf8'); | ||
const result = convert(input, '2.1.0'); | ||
assertResults(output, result); | ||
}); | ||
it('should convert from 2.0.0-rc2 to 2.1.0', () => { | ||
const input = fs.readFileSync(path.resolve(__dirname, 'input', '2.0.0-rc2', 'streetlights.yml'), 'utf8'); | ||
const output = fs.readFileSync(path.resolve(__dirname, 'output', '2.1.0', 'streetlights.yml'), 'utf8'); | ||
const result = convert(input, '2.1.0'); | ||
assertResults(output, result); | ||
}); | ||
it('should convert from 2.0.0 to 2.1.0', () => { | ||
const input = fs.readFileSync(path.resolve(__dirname, 'input', '2.0.0', 'streetlights.yml'), 'utf8'); | ||
const output = fs.readFileSync(path.resolve(__dirname, 'output', '2.1.0', 'streetlights.yml'), 'utf8'); | ||
const result = convert(input, '2.1.0'); | ||
assertResults(output, result); | ||
}); | ||
}); | ||
/* | ||
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. | ||
We need to remove all line breaks from the string | ||
*/ | ||
function removeLineBreaks(str) { | ||
return str.replace(/\r?\n|\r/g, '') | ||
} | ||
function assertResults(output, result){ | ||
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
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
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
182568
37
439