Created
June 5, 2016 22:16
-
-
Save rom1504/d7bc33522c071a943fad4833fd1e5dd8 to your computer and use it in GitHub Desktop.
script to make a diff between 2 protodef protocols
This file contains hidden or 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
const equal = require('deep-equal'); | |
if(process.argv.length != 4) { | |
console.log("Usage : node protocol-diff.js <protocol1.json> <protocol2.json>"); | |
process.exit(1); | |
} | |
const p1File=process.argv[2]; | |
const p2File=process.argv[3]; | |
const p1=require(p1File); | |
const p2=require(p2File); | |
function produceDiff(p1,p2) | |
{ | |
let o=Object | |
.keys(p2) | |
.filter(n => n!='types') | |
.reduce((acc,name) => { | |
const diff=produceDiff(p1[name],p2[name]); | |
if(Object.keys(diff).length!=0) acc[name]=diff; | |
return acc; | |
},{}); | |
const diff1=produceTypesDiff(p1['types'],p2['types']); | |
const diff2=produceTypesDiff(p2['types'],p1['types']); | |
if(diff2 && Object.keys(diff2).length!=0) | |
o['oldTypes']=diff2; | |
if(diff1 && Object.keys(diff1).length!=0) | |
o['newTypes']=diff1; | |
return o; | |
} | |
function produceTypesDiff(types1,types2) | |
{ | |
if(types2 == undefined) | |
return undefined; | |
return Object | |
.keys(types2) | |
.reduce((acc,name)=> { | |
if(!equal(types1[name],types2[name])) acc[name]=types2[name]; | |
return acc; | |
},{}) | |
} | |
console.log(JSON.stringify(produceDiff(p1,p2),null,2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment