Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module.exports = function(grunt) {
compare: {
src: ['test/**/*.json'],
filter: 'isFile',
byPath:true,
fatal: true
}
},
Expand Down
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ grunt.initConfig({
comparejson: {
compare: {
src: ['<dir>'],
fatal: true | false
fatal: true | false,
byPath: true | false
}
}
})
Expand All @@ -48,6 +49,18 @@ Default value: `true`

Flag to indicate wheter or not the task failed if some differences are found.

#### byPath
Type: `Boolean`<br />
Mandatory: `false`<br />
Default value: `false`

Flag to indicate that files will be compared by path. For example :
/path1/file1.json
/path1/file1.json
/path2/file3.json

The file1.json will only be compared with the file2.json.

### Usage Examples

```js
Expand All @@ -56,6 +69,7 @@ grunt.initConfig({
compare: {
src: ['dir/**/*.json'],
fatal: true
byPath: false
}
}
})
Expand Down
64 changes: 50 additions & 14 deletions tasks/compare-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,71 @@
*/

'use strict';
var utils = require('../lib/utils');
var utils = require('../lib/utils'),
path = require('path');

module.exports = function(grunt) {
module.exports = function (grunt) {

grunt.registerMultiTask('comparejson', 'Compare some JSON files', function() {
grunt.registerMultiTask('comparejson', 'Compare some JSON files', function () {

// Fail task if no src directory property is set.
grunt.config.requires('comparejson.compare.src');

var allParsedFiles = [];
var allParsedFiles = [],
fatal = grunt.config.get('comparejson').compare.fatal,
isByPath = grunt.config.get('comparejson').compare.byPath,
byPath = {},
currentPath,
result = true,
reports;

// First, parse all the json file into javascript map
this.files.forEach(function(file) {
this.files.forEach(function (file) {
allParsedFiles = utils.JSONFilesToMap(grunt, file.src);
});

// Then, compare each
var reports = utils.compareMaps(allParsedFiles);
if (isByPath) {
allParsedFiles.forEach(function (item) {
currentPath = path.dirname(item.fileName);

// If we get some differences, the task is failed and the reports is shown
if (reports.length) {
reports.forEach(function(report) {
grunt.log.error(report);
if (!byPath[currentPath]) {
byPath[currentPath] = [];
}

byPath[currentPath].push(item);
});

return !grunt.config.get('comparejson').compare.fatal;
Object.keys(byPath).forEach(function (bucket) {
reports = utils.compareMaps(byPath[bucket]);

if (reports.length) {
grunt.log.error('path name ' + bucket);
reports.forEach(function (report) {
grunt.log.error(report);
});

result = false;
}
});
} else {
// Then, compare each
reports = utils.compareMaps(allParsedFiles);

// If we get some differences, the task is failed and the reports is shown
if (reports.length) {
reports.forEach(function (report) {
grunt.log.error(report);
});

result = false;
}
}

grunt.log.writeln('SUCCESS : all files have the same keys.');
return true;
if (result) {
grunt.log.writeln('SUCCESS : all files have the same keys.');
return true;
} else {
return !fatal;
}
});
};
13 changes: 13 additions & 0 deletions test/data/fileD.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"JSON.string.1": "string1",
"JSON.string.2": "string2",
"JSON.string.3": "string3",

"JSON.int.1": "int1",
"JSON.int.2": "int2",
"JSON.int.3": "int3",

"JSON.subs": {
"sub.5": "sub1"
}
}
File renamed without changes.
File renamed without changes.