Skip to content

Commit

Permalink
Merging upstream & inPlus and InLess & cli-tables
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas authored and Nicolas committed Apr 1, 2015
1 parent d5d1266 commit 6d3ca77
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 22 deletions.
35 changes: 27 additions & 8 deletions bin/jsobjectdiff
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ var _ = require('lodash');
var fs = require('fs');
var path = require('path');
var when = require('when');

var program = require('commander');
var Table = require('cli-table');

var opts = require('nomnom')
.option('config', {
Expand All @@ -22,8 +21,8 @@ var opts = require('nomnom')
var FULL_CONFIG_PATH = path.resolve(process.cwd(), opts.config);

when
.promise(function (resolve, reject) {
fs.readFile(FULL_CONFIG_PATH, 'utf8', function (err, data) {
.promise(function(resolve, reject) {
fs.readFile(FULL_CONFIG_PATH, 'utf8', function(err, data) {
if (err) {
reject(err);
return;
Expand All @@ -35,9 +34,10 @@ when
.then(function onSuccess(data) {
when.try(JSON.parse, data).then(function onSuccess(json) {

var configuration = json.files.map(function (file) {
var configuration = json.files.map(function(file) {
var absolutePath = path.resolve(path.dirname(FULL_CONFIG_PATH), file.filePath);
return {
filePath: file.filePath,
objectPath: file.objectPath,
content: fs.readFileSync(absolutePath).toString('utf8')
};
Expand All @@ -48,8 +48,27 @@ when
if (diff.length === 0) {
process.exit(0);
}

console.info('Diff:', JSON.stringify(diff, null, 2));
var table = new Table();
diff.map(function(objectFiles) {
table.push(['filePath', 'objectPath']);
table.push([objectFiles.filePath, objectFiles.objectPath]);
objectFiles.comparedTo.map(function(comparaison) {
table.push(['Compared with : ' + comparaison.filePath + ' ' + comparaison.objectPath]);
table.push(['comparaison', 'key', 'index']);
comparaison.inPlus.map(function(keyAndIndex) {
table.push({
'inPlus': [keyAndIndex.key, keyAndIndex.index]
})
});
comparaison.inLess.map(function(keyAndIndex) {
table.push({
'inLess': [keyAndIndex.key, keyAndIndex.index]
})
});
})
table.push()
})
console.log(table.toString());
process.exit(diff.length);

}, function onError(err) {
Expand All @@ -59,4 +78,4 @@ when
}, function onError(err) {
console.error('Could not open %s', FULL_CONFIG_PATH);
console.error(err);
});
});
46 changes: 43 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,50 @@ module.exports = {
return objectPath.get(eval(fileContent), file.objectPath);
});

var filesKeys = filesObjects.map(function (obj) {
return Object.keys(obj);
var filesKeys = filesObjects.map(function(obj, index) {
return {
filePath: files[index].filePath,
objectPath: files[index].objectPath,
fileKeys: Object.keys(obj)
}
});

return _.union(_.difference.apply(_, filesKeys), _.difference.apply(_, filesKeys.reverse()));
return filesKeys.map(function(obj) {
return {
filePath: obj.filePath,
objectPath: obj.objectPath,
comparedTo: _.flatten(filesKeys.map(function(otherObj){
if(obj.filePath == otherObj.filePath && obj.objectPath == otherObj.objectPath){
return []
}
return {
filePath: otherObj.filePath,
objectPath: otherObj.objectPath,
inPlus: _.flatten(_.difference(obj.fileKeys, otherObj.fileKeys).map(function(differenceKeys){
return _.flatten(obj.fileKeys.map(function(objKey, index){
if(objKey == differenceKeys){
return {
key: objKey,
index: index
}
}
return []
}))
})),
inLess: _.flatten(_.difference(otherObj.fileKeys, obj.fileKeys).map(function(differenceKeys){
return _.flatten(otherObj.fileKeys.map(function(objKey, index){
if(objKey == differenceKeys){
return {
key: objKey,
index: index
}
}
return []
}))
}))
}
}))
}
})
}
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"author": "Francois-Guillaume Ribreau <npm@fgribreau.com> (http://fgribreau.com/)",
"license": "MIT",
"dependencies": {
"cli-table": "^0.3.1",
"lodash": "^3.5.0",
"nomnom": "^1.8.1",
"object-path": "^0.9.0",
Expand Down
19 changes: 8 additions & 11 deletions tests/test.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
{
"files": [
[
"./fixtures/en_US.js",
"i18n.en_US"
],
[
"./fixtures/fr_FR.js",
"i18n.fr_FR"
]
],
"pre": "(function (Bringr) {",
"post": "return Bringr;})({});"
"post": "return Bringr;})({});",
"files": [{
"filePath": "./fixtures/en_US.js",
"objectPath": "i18n.en_US"
}, {
"filePath": "./fixtures/fr_FR.js",
"objectPath": "i18n.fr_FR"
}]
}

0 comments on commit 6d3ca77

Please sign in to comment.