Created
December 8, 2014 14:53
-
-
Save lennym/46835013d0f7a8af7454 to your computer and use it in GitHub Desktop.
json-linter.js
This file contains hidden or 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
var fs = require('fs'), | |
path =require('path'); | |
var dir = process.argv[2]; | |
if (!dir) { | |
console.error('Folder path must be defined.'); | |
console.log('Usage: node json-linter.js /path/to/test'); | |
process.exit(1); | |
} | |
var files = fs.readdirSync(dir), | |
success = 0, | |
errors = 0; | |
files.forEach(function (file) { | |
if (file.indexOf('.json') > -1) { | |
var filepath = path.resolve(__dirname, dir, file); | |
try { | |
var data = require(filepath); | |
success++; | |
} catch (e) { | |
errors++; | |
console.error(filepath + ' contains invalid json'); | |
} | |
} | |
}); | |
console.log((success + errors) + ' files tested.'); | |
console.log((success) + ' files passed.'); | |
console.log((errors) + ' files failed.'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment