Skip to content

Instantly share code, notes, and snippets.

@Souvikns
Created Mar 28, 2021
Embed
What would you like to do?
asyncApiDiff
const parser = require('@asyncapi/parser');
const fs = require('fs');
const path = require('path');
const main = async () => {
const diff = {
additions: [],
deletions: [],
changes: {},
generate: doc => {
}
};
try {
const apiv1 = fs.readFileSync(path.resolve(__dirname, 'apiv1.yml'), 'utf-8');
const apiv2 = fs.readFileSync(path.resolve(__dirname, 'apiv2.yml'), 'utf-8');
let doc1 = await parser.parse(apiv1);
let doc2 = await parser.parse(apiv2);
// TODO: Check info between both the docs
if (doc1.info() !== doc2.info()) {
diff.changes.info = {}
// TODO: check all the info objects and register the changes
Object.keys(doc1.info()._json).forEach(key => {
switch (key) {
case "title":
if (doc1.info().title() !== doc2.info().title()) {
diff.changes.info.title = {
"+": doc2.info().title(),
"-": doc1.info().title()
}
}
case "version":
if (doc1.info().version() !== doc2.info().version()) {
diff.changes.info.version = {
"+": doc2.info().version(),
"-": doc1.info().version()
}
}
}
})
}
// TODO: Check all the changes in the channels and register them
if (doc1.json().channels !== doc2.json().channels) {
diff.changes.channels = {}
Object.keys({ ...doc1.channels(), ...doc2.channels() }).forEach(channelName => {
if (doc1.channel(channelName) !== null) {
if (doc1.channel(channelName)._json !== doc2.channel(channelName)._json) {
console.log(channelName, doc2.channel(channelName));
diff.changes.channels[channelName] = doc2.channel(channelName);
}
}
})
}
console.log(diff.changes);
diff.generate();
} catch (error) {
console.log(error);
}
}
main();
/*
diff generates all the changes that have been made to the document
{
info: { version: { '+': '0.2.0', '-': '0.1.0' } },
channels: { 'example-channel': Channel { _json: [Object] } }
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment