Skip to content

Instantly share code, notes, and snippets.

@Mansh05
Created June 9, 2018 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mansh05/54ea3ca5d051dec8eafbc0582459a594 to your computer and use it in GitHub Desktop.
Save Mansh05/54ea3ca5d051dec8eafbc0582459a594 to your computer and use it in GitHub Desktop.
JSON parser which helps us check whether it has been formatted correctly
let fs = require('fs');
let checkJson = (path) => {
fs.readdir(path, (err, files) => {
files.forEach(file => {
if (fs.lstatSync(`${path}/${file}`).isDirectory()) {
checkJson(`${path}/${file}`)
} else {
readFile(`${path}/${file}`);
}
});
});
}
let readFile = (path) => {
let aa = fs.createReadStream(path, 'utf8');
aa.on('data', (chunk) => {
try {
JSON.parse(chunk);
// console.log(JSON.parse(chunk));
} catch (e) {
console.log('There is an error in' + path);
process.exit(1);
}
});
}
checkJson('./demo');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment