diff --git a/Gruntfile.js b/Gruntfile.js index 893c5be..4db1449 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -27,6 +27,7 @@ module.exports = function(grunt) { compare: { src: ['test/**/*.json'], filter: 'isFile', + byPath:true, fatal: true } }, diff --git a/README.md b/README.md index b1867da..45d0cf7 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,8 @@ grunt.initConfig({ comparejson: { compare: { src: [''], - fatal: true | false + fatal: true | false, + byPath: true | false } } }) @@ -48,6 +49,18 @@ Default value: `true` Flag to indicate wheter or not the task failed if some differences are found. +#### byPath +Type: `Boolean`
+Mandatory: `false`
+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 @@ -56,6 +69,7 @@ grunt.initConfig({ compare: { src: ['dir/**/*.json'], fatal: true + byPath: false } } }) diff --git a/tasks/compare-json.js b/tasks/compare-json.js index b3e2e06..cfdf1b1 100644 --- a/tasks/compare-json.js +++ b/tasks/compare-json.js @@ -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; + } }); }; diff --git a/test/data/fileD.json b/test/data/fileD.json new file mode 100644 index 0000000..c7db8e1 --- /dev/null +++ b/test/data/fileD.json @@ -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" + } +} \ No newline at end of file diff --git a/test/data/fileB.json b/test/data/sub/fileB.json similarity index 100% rename from test/data/fileB.json rename to test/data/sub/fileB.json diff --git a/test/data/fileC.json b/test/data/sub/fileC.json similarity index 100% rename from test/data/fileC.json rename to test/data/sub/fileC.json