<commit_before>var glob = require('glob');
var extend = require('extend');
var fs = require('fs');
var Hjson = require('hjson');

function parseJSON(data) {
	let rtnJSON;

	try {
		rtnJSON = Hjson.parse(data);
	} catch (e) {
		rtnJSON = null;
	}

	return rtnJSON;
}

/**
 * [set local data from glob of json files]
 * @param {String}   directory [directory to grab json from]
 * @param {Object}   locals    [current locals passed in to extend with site data]
 * @param {Function} callback  [next function for middleware]
 */
exports = module.exports = function (directory, locals, callback) {
	glob(directory, function (err, files) {
		if(err) return callback(err);
		if(!files.length) return callback();

		var remaining = files.length;

		for (var file of files) {
			fs.readFile(file, 'utf8', (err, data) => {
				if (err) throw err;
				remaining -= 1;

				let parsedData = parseJSON(data);
<commit_msg>Passes in filename for better error reporting<commit_after>var glob = require('glob');
var extend = require('extend');
var fs = require('fs');
var Hjson = require('hjson');

function parseJSON(data, file_path = '') {
	let rtnJSON;

	try {
		rtnJSON = Hjson.parse(data);
	} catch (e) {
		console.log(`UNABLE TO PARSE JSON for 'set-locals' in file:\n\t"${file_path}"\n`);
		console.log(e);
		rtnJSON = null;
	}

	return rtnJSON;
}

/**
 * [set local data from glob of json files]
 * @param {String}   directory [directory to grab json from]
 * @param {Object}   locals    [current locals passed in to extend with site data]
 * @param {Function} callback  [next function for middleware]
 */
exports = module.exports = function (directory, locals, callback) {
	glob(directory, function (err, files) {
		if(err) return callback(err);
		if(!files.length) return callback();

		var remaining = files.length;

		for (var file of files) {
			fs.readFile(file, 'utf8', (err, data) => {
				if (err) throw err;
				remaining -= 1;

				let parsedData = parseJSON(data, file);

{'fraction_non_alphanumeric': 0.141409021794222, 'fraction_numerical': 0.0020273694880892043, 'mean_word_length': 3.91044776119403, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7495498', 'n_tokens_mistral': 685, 'n_tokens_neox': 640, 'n_words': 211}