Created
March 29, 2021 22:00
-
-
Save Souvikns/a17f2eb761949974e7adb37999236bfb to your computer and use it in GitHub Desktop.
AsyncApiDiff_v2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* Having two asynapi doc we will be trying to find changes with respect to the first | |
* basically we will be checking how much of the first document has changed | |
* | |
* We can try to change categorise the changes as | |
* - additions | |
* - deletions | |
* - changes | |
* | |
*/ | |
const parser = require('@asyncapi/parser'); | |
const fs = require('fs'); | |
const path = require('path'); | |
const _ = require('lodash'); | |
class Diff { | |
additions = {}; | |
deletions = {}; | |
changes = {}; | |
} | |
const diff = new Diff(); | |
const main = async () => { | |
try { | |
// fetching and parsing the AsyncApi documents | |
const doc1 = await parser.parse(fs.readFileSync(path.resolve(__dirname, 'apiv1.yml'), 'utf-8')); | |
const doc2 = await parser.parse(fs.readFileSync(path.resolve(__dirname, 'apiv2.yml'), 'utf-8')); | |
/*We will loop through all the components in the document and try and only registor all the changes*/ | |
/**Checking info Component */ | |
if(doc1.info() !== doc2.info()){ | |
// first we will check for additions or deletions | |
diff.additions.info = {}; | |
diff.deletions.info = {}; | |
diff.changes.info = {}; | |
_.union( | |
Object.keys(doc1.info().json()), | |
Object.keys(doc2.info().json()) | |
).forEach(key => { | |
/** | |
* If we find a key that is present in doc1 and not in doc2 | |
* then that is a deletion | |
* | |
* If we find a key that is present in doc2 and not in doc1 | |
* then that is a addition | |
*/ | |
if(!Object.keys(doc1.info().json()).includes(key)){ | |
diff.additions.info[key] = doc2.info().json()[key]; | |
} | |
if(!Object.keys(doc2.info().json()).includes(key)){ | |
diff.deletions.info[key] = doc1.info().json()[key]; | |
} | |
// Checking for changes in info | |
if(Object.keys(doc1.info().json()).includes(key)&& Object.keys(doc2.info().json()).includes(key)){ | |
if(doc1.info().json()[key] !== doc2.info().json()[key]){ | |
diff.changes.info[key] = { | |
"-": doc1.info().json()[key], | |
"+": doc2.info().json()[key] | |
} | |
} | |
} | |
}) | |
} | |
console.log(diff.changes); | |
} catch (error) { | |
console.log(error); | |
} | |
} | |
main(); | |
// The final changelog Object tree | |
/** | |
Diff { | |
additions: { info: { description: 'Testing AsyncApi' } }, | |
deletions: { info: { termsOfService: 'http://asyncapi.org/terms/' } }, | |
changes: { info: { { version: { '-': '0.1.0', '+': '0.2.0' } } } } | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment