1+ const fs = require ( 'fs' ) ;
2+ const goLivePath = './log/live'
3+ const goLocalPath = './log/local'
4+ const diffFilePath = './log/diff'
5+ const decorators = fs . readdirSync ( goLivePath )
6+ 7+ const dataProvider = ( decorator , fileName ) => {
8+ const localResponsePath = `${ goLocalPath } /${ decorator } /${ fileName } `
9+ const liveResponsePath = `${ goLivePath } /${ decorator } /${ fileName } `
10+ const localData = parse ( read ( localResponsePath ) )
11+ const liveData = parse ( read ( liveResponsePath ) )
12+ return [ localData , liveData ]
13+ }
14+ 15+ const adm = ( ) => ( { added : [ ] , modified : [ ] , deleted : [ ] } )
16+ const isPrimitive = v => v !== Object ( v )
17+ const intersection = ( xs , ys ) => xs . filter ( x => ys . includes ( x ) )
18+ const setDifference = ( xs , ys ) => {
19+ const common = intersection ( xs , ys )
20+ return xs . filter ( x => ! common . includes ( x ) )
21+ }
22+ const parse = JSON . parse
23+ const keys = Object . keys
24+ const read = path => fs . readFileSync ( path , 'utf8' )
25+ const uniq = xs => keys ( xs . reduce ( ( acc , x ) => acc [ x ] = true && acc , { } ) )
26+ const isEmpty = xs => xs . length === 0
27+ 28+ const diff = ( o1 , o2 , path , diffAdm ) => {
29+ if ( ! o1 || ! o2 ) return diffAdm
30+ const [ keys1 , keys2 ] = [ keys ( o1 ) , keys ( o2 ) ]
31+ const [ addedKeys , deletedKeys ] = [ setDifference ( keys2 , keys1 ) , setDifference ( keys1 , keys2 ) ]
32+ diffAdm . added = [ ...diffAdm . added , ...addedKeys . map ( k => `${ path } .${ k } ` ) ]
33+ diffAdm . deleted = [ ...diffAdm . deleted , ...deletedKeys . map ( k => `${ path } .${ k } ` ) ]
34+ const modifiedKeys = intersection ( keys1 , keys2 )
35+ diffAdm = modifiedKeys . reduce ( ( acc , key ) => {
36+ const fullKey = `${ path } .${ key } `
37+ const [ v1 , v2 ] = [ o1 [ key ] , o2 [ key ] ]
38+ if ( isPrimitive ( v1 ) && isPrimitive ( v2 ) ) {
39+ if ( v1 !== v2 ) {
40+ const diffKey = fullKey . split ( '.' ) . slice ( 3 ) . join ( '.' )
41+ acc . modified = [ ...acc . modified , diffKey ]
42+ }
43+ return acc
44+ } else {
45+ return diff ( v1 , v2 , fullKey , diffAdm )
46+ }
47+ } , diffAdm )
48+ return diffAdm
49+ }
50+ 51+ const writeChangesToFile = ( fileName , added , modified , deleted ) => {
52+ const data = `
53+ Added Keys: \n${ added . reduce ( ( acc , x ) => `${ acc } \n\t\t${ x } ` , '' ) } \n
54+ Modified Keys: \n${ modified . reduce ( ( acc , x ) => `${ acc } \n\t\t${ x } ` , '' ) } \n
55+ Deleted Keys: \n${ deleted . reduce ( ( acc , x ) => `${ acc } \n\t\t${ x } ` , '' ) } \n
56+ ` ;
57+ fs . writeFileSync ( `${ diffFilePath } /${ fileName } .txt` , data )
58+ } ;
59+ 60+ decorators . forEach ( decorator => {
61+ const files = fs . readdirSync ( `${ goLivePath } /${ decorator } /` )
62+ files . forEach ( file => {
63+ const [ localData , liveData ] = dataProvider ( decorator , file )
64+ let changes = diff ( localData , liveData , '' , adm ( ) )
65+ let { added, modified, deleted } = changes
66+ 67+ if ( ! isEmpty ( added ) || ! isEmpty ( modified ) || ! isEmpty ( deleted ) ) {
68+ writeChangesToFile ( `${ decorator } /${ file } ` , added , modified , deleted )
69+ }
70+ } )
71+ } ) ;
0 commit comments