Created
May 2, 2023 04:42
-
-
Save gilberto-009199/13b117c0d85ea127286c8724e849f08f to your computer and use it in GitHub Desktop.
Compare Properties files
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
let fs = require('fs'); | |
let path = require('path'); | |
// npm install readline-sync event-stream | |
const es = require('event-stream'); | |
const os = require('os'); | |
const readlineSync = require('readline-sync'); | |
let dirs = []; | |
fs.readdir( __dirname, { withFileTypes: true }, async (err, files) => { | |
if (err) { | |
console.error(err); | |
return; | |
} | |
dirs = files.filter((file) => file.isDirectory()); | |
let dir_compare0 = ""; | |
let dir_compare1 = ""; | |
console.log(" Define 1st source directory: "); | |
for(let dir of dirs){ | |
console.log("\t "+ dir.name); | |
let res = (readlineSync.question(" (y)es or (n)ot }> ")+"").toLocaleLowerCase(); | |
if( res == "y" || res == "ye" || res == "yes" || res == "s"){ | |
dir_compare0 = path.join( __dirname, dir.name ); | |
break; | |
} | |
} | |
console.log(" Define 2st source directory: "); | |
for(let dir of dirs){ | |
console.log("\t "+ dir.name); | |
let res = (readlineSync.question(" (y)es or (n)ot }> ")+"").toLocaleLowerCase(); | |
if( res == "y" || res == "ye" || res == "yes" || res == "s"){ | |
dir_compare1 = path.join( __dirname, dir.name ); | |
break; | |
} | |
} | |
// console.log(" Load keys for first diretory"); | |
// console.log("\t",dir_compare0) | |
let map0 = {}; | |
let streams0 = []; | |
for(let file of fs.readdirSync(dir_compare0) ){ | |
map0[file] = {}; | |
streams0.push( new Promise((resolve, reject)=>{ | |
fs.createReadStream( path.join( dir_compare0 , file ) ).pipe(es.split()) | |
.pipe(es.mapSync(function(line) { | |
let key_value = line.split('='); | |
if( key_value.length == 2 ){ | |
map0[ file ][ key_value[0] ] = digest( key_value[1] ) | |
} | |
})).on('error', function(err) { | |
console.log('Error:', err); | |
}).on('end', function() { | |
// console.log("\t [ OK ] "+file) | |
resolve() | |
}) | |
}) | |
) | |
} | |
// console.log(" Load keys for second diretory"); | |
// console.log("\t",dir_compare1); | |
let map1 = {}; | |
let streams1 = []; | |
for(let file of fs.readdirSync(dir_compare1) ){ | |
map1[file] = {}; | |
streams1.push( new Promise((resolve, reject)=>{ | |
fs.createReadStream( path.join( dir_compare1 , file ) ).pipe(es.split()) | |
.pipe(es.mapSync(function(line) { | |
let key_value = line.split('='); | |
if( key_value.length == 2 ){ | |
map1[ file ][ key_value[0] ] = digest( key_value[1] ); | |
} | |
})).on('error', function(err) { | |
console.log('Error:', err); | |
}).on('end', function() { | |
// console.log("\t [ OK ] "+file) | |
resolve() | |
}) | |
}) | |
) | |
} | |
Promise.all([ Promise.all(streams0) , Promise.all(streams1) ]).then(()=>{ | |
let diff = {} | |
let files0 = Object.keys( map0 ); | |
let files1 = Object.keys( map1 ); | |
let file_adding_remove = []; | |
if( files0.length != files1.length ){ | |
// let files0_maior = files0.length > files1.length; | |
// let files1_maior = files0.length < files1.length; | |
for( let file0 of files0 ){ | |
if( ! ~files1.indexOf( file0 ) ){ | |
file_adding_remove.push( path.join( dir_compare0 , file0 ) ) | |
} | |
} | |
for( let file1 of files1 ){ | |
if( ! ~files0.indexOf( file1 ) ){ | |
file_adding_remove.push( path.join( dir_compare1 , file1 ) ) | |
} | |
} | |
} else if( files0.toString() != files1.toString() ){ | |
for( let file0 of files0 ){ | |
if( ! ~files1.indexOf( file0 ) ){ | |
file_adding_remove.push( path.join( dir_compare0 , file0 ) ) | |
} | |
} | |
for( let file1 of files1 ){ | |
if( ! ~files0.indexOf( file1 ) ){ | |
file_adding_remove.push( path.join( dir_compare1 , file1 ) ) | |
} | |
} | |
} | |
console.log(" Summary: "); | |
for( let file0 of files0 ){ | |
if( ~files1.indexOf( file0 ) ){ | |
console.log("\t * "+file0); | |
let keys0 = Object.keys( map0[file0] ); | |
let keys1 = Object.keys( map1[file0] ); | |
let desc = ""; | |
if( keys0.length != keys1.length ){ | |
desc += "[ new/remove keys ]"; | |
} | |
console.log("\t keys: "+ keys0.length + "\t "+ desc); | |
for( let key0 of keys0 ){ | |
if( ~keys1.indexOf( key0 ) ){ | |
if( map0[file0][ key0 ] != map1[file0][ key0 ] ){ | |
console.log("\t [ DIFF ] "+ key0 ) | |
} | |
} else console.log("\t [ Adding/Remove ] "+ key0 ) | |
} | |
for( let key1 of keys1 ){ | |
if( ! ~keys0.indexOf( key1 ) ){ | |
console.log("\t [ Adding/Remove ] "+ key1 ) | |
} | |
} | |
} | |
} | |
if( file_adding_remove.length > 0)console.log(" Files adding/remove:") | |
for( let file of file_adding_remove ){ | |
console.log("\t * "+ file ); | |
} | |
}) | |
}); | |
function digest(text){ return (text.split("a").length + text.split("b").length + text.split("c").length + text.split("d").length + text.split("e").length + text.split("f").length)+ "" + text.length; } |
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
{ | |
"dependencies": { | |
"event-stream": "^4.0.1", | |
"readline-sync": "^1.4.10" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment