<commit_before>var q = require('q');
var fs = require('fs');
var _ = require('lodash');

module.exports = {
    createJSON: createJSON
};


function createJSON(options) {
    var defaults = {
            moduleName: 'angularPoint',
            constantName: 'apCachedXML',
            fileName: 'offlineXML.js',
            //dest: '.',
            src: []
        },
        deferred = q.defer(),
        opts = _.extend({}, defaults, options),
        offlineXML = {};

    opts.dest = opts.dest || opts.src[0];

    opts.src.forEach(function (fileDirectory) {
        var promises = [];
        fs.readdir(fileDirectory, function (err, files) {
            if (err) throw err;

            files.forEach(function (fileName) {
                if (fileName.indexOf('.xml') > -1) {
                    promises.push(parseFile(fileName, fileDirectory)
                        .then(function (file) {
                            offlineXML[fileName.split('.xml')[0]] = file;
                        }));
                }
            });

            q.all(promises).then(function () {
                var fileContents = 'angular.module(\'' + opts.moduleName + '\').constant(\'' + opts.constantName + '\', ';
                fileContents += JSON.stringify(offlineXML) + ');';

                fs.writeFile(opts.dest + '/' + opts.fileName, fileContents, {encoding: 'utf8'}, function (err) {
                    if (err) throw err;
                    deferred.resolve(offlineXML);
                    console.log("Parsed XML Constant File Created");
                });
            });
        });
    });

    return deferred.promise;
}

function parseFile(fileName, filePath) {
    var deferred = q.defer();

    fs.readFile(filePath + '/' + fileName, {encoding: 'utf8'}, function (err, data) {<commit_msg>Moved completion logic outsize src loop.<commit_after>var q = require('q');
var fs = require('fs');
var _ = require('lodash');

module.exports = {
    createJSON: createJSON
};


function createJSON(options) {
    var defaults = {
            moduleName: 'angularPoint',
            constantName: 'apCachedXML',
            fileName: 'offlineXML.js',
            //dest: '.',
            src: []
        },
        deferred = q.defer(),
        opts = _.extend({}, defaults, options),
        offlineXML = {};

    opts.dest = opts.dest || opts.src[0];
    var promises = [];

    opts.src.forEach(function (fileDirectory) {
        fs.readdir(fileDirectory, function (err, files) {
            if (err) throw err;

            files.forEach(function (fileName) {
                if (fileName.indexOf('.xml') > -1) {
                    promises.push(parseFile(fileName, fileDirectory)
                        .then(function (file) {
                            offlineXML[fileName.split('.xml')[0]] = file;
                        }));
                }
            });
        });
    });

    q.all(promises).then(function () {
        var fileContents = 'angular.module(\'' + opts.moduleName + '\').constant(\'' + opts.constantName + '\', ';
        fileContents += JSON.stringify(offlineXML) + ');';

        fs.writeFile(opts.dest + '/' + opts.fileName, fileContents, {encoding: 'utf8'}, function (err) {
            if (err) throw err;
            deferred.resolve(offlineXML);
            console.log("Parsed XML Constant File Created");
        });
    });

    return deferred.promise;
}

function parseFile(fileName, filePath) {
    var deferred = q.defer();

    fs.readFile(filePath + '/' + fileName, {encoding: 'utf8'}, function (err, data) {
{'fraction_non_alphanumeric': 0.15304689693906207, 'fraction_numerical': 0.002808199943836001, 'mean_word_length': 1.8158102766798419, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '6373961', 'n_tokens_mistral': 1005, 'n_tokens_neox': 987, 'n_words': 199}