<commit_before>/**
 * grunt-angular-configurator
 * @author vitre
 * @contributor <NAME>
 * @licence MIT
 * @version 1.1.22
 * @url https://github.com/TimmersThomas/grunt-angular-configurator
 */

'use strict';

var _ = require('lodash'),
    path = require('path'),
    fs = require('fs');

function generateTemplateSimple() {
    var content = "'use strict';\n" +
        "angular.module('<%= module %>')\n" +
        "    .constant('<%= variable %>', <%= config %>);";

    return content;
};

function generateTemplateBrowserified() {
    var content = "'use strict';\n\n" +
        "exports.inject = function (appModule) {\n" +
        "   appModule.constant('<%= variable %>', exports.constant);\n" +
        "   return exports.constant;\n" +
        "};\n\n" +
        "exports.constant = <%= config %>";

    return content;
};


function generateConfiguration(grunt, options) {
    var files = grunt.file.expand({filter: 'isDirectory'}, options.search_base + '*');

    var config = {},
        states = {};

    grunt.log.debug("Reading files: " + JSON.stringify(files));
    _.forEach(files, function (dir) {
        var module = path.basename(dir),
            configFile = path.resolve(dir, options.config_folder, options.config_file_config),
            stateFile = path.resolve(dir, options.config_folder, options.config_file_state);

        if (fs.existsSync(configFile)) {
            grunt.log.debug("File exists: " + configFile);
            _.merge(config, grunt.file.readJSON(configFile));
        }

        if (fs.existsSync(stateFile)) {
            grunt.log.debug("File exists: " + stateFile);

            _.merge(states, {
                states: _.mapValues(grunt.file.readJSON(stateFile), function (state) {
                    return _.extend({
                        'module': module
                    }, state);
                })
            });
        }
    });

    config = _.merge(
        {},
        states,
        config,
        grunt.file.readJSON(path.resolve(options.general_config_folder, 'config.json')),
        grunt.file.readJSON(path.resolve(options.general_config_folder, 'config_' + options.env + '.json'))
    );

    grunt.log.debug("Generated Config: " + JSON.stringify(config));

    return config;
}

module.exports = function (grunt) {

    grunt.registerTask('ng_config_configurator', 'Generate a configuration based on the current ENVIRONMENT', function (envTarget) {
        var environment = envTarget || grunt.config('env');

        // Merge task-specific and/or target-specific options with these defaults.
        var options = this.options({
            "env": environment,
            "general_config_folder": "app/scripts/_config/",
            "search_base": "app/scripts/",
            "config_folder": "config",
            "config_file_config": "config.json",
            "config_file_state": "states.json",
            "export_module": "app.common",
            "export_variable": "config",
            "export_dest": "app/scripts/common/config/config.js",
            "browserify": true
        });

        grunt.log.debug("Using options: " + JSON.stringify(options));

        var template = generateTemplateSimple(),
            configuraton = generateConfiguration(grunt, options);

        if (options.browserify) {
            template = generateTemplateBrowserified();
        }

        grunt.log.debug("Used template: " + template);


        var generatedContent = grunt.template.process(template, {
                data: {
                    "module": options.export_module,
                    "variable": options.export_variable,
                    "config": generateConfiguration()
                }
            }
        );

        fs.writeFile(options.export_dest, generatedContent);
        grunt.log.ok('Generated ' + options.export_dest);
    });
};<commit_msg>Small fix<commit_after>/**
 * grunt-angular-configurator
 * @author vitre
 * @contributor <NAME>
 * @licence MIT
 * @version 1.1.22
 * @url https://github.com/TimmersThomas/grunt-angular-configurator
 */

'use strict';

var _ = require('lodash'),
    path = require('path'),
    fs = require('fs');

function generateTemplateSimple() {
    var content = "'use strict';\n" +
        "angular.module('<%= module %>')\n" +
        "    .constant('<%= variable %>', <%= config %>);";

    return content;
};

function generateTemplateBrowserified() {
    var content = ""+
        "'use strict';\n\n" +
        "exports.inject = function (appModule) {\n" +
        "   appModule.constant('<%= variable %>', exports.constant);\n" +
        "   return exports.constant;\n" +
        "};\n\n" +
        "exports.constant = <%= config %>;";

    return content;
};


function generateConfiguration(grunt, options) {
    var files = grunt.file.expand({filter: 'isDirectory'}, options.search_base + '*');

    var config = {},
        states = {};

    grunt.log.debug("Reading files: " + JSON.stringify(files));
    _.forEach(files, function (dir) {
        var module = path.basename(dir),
            configFile = path.resolve(dir, options.config_folder, options.config_file_config),
            stateFile = path.resolve(dir, options.config_folder, options.config_file_state);

        if (fs.existsSync(configFile)) {
            grunt.log.debug("File exists: " + configFile);
            _.merge(config, grunt.file.readJSON(configFile));
        }

        if (fs.existsSync(stateFile)) {
            grunt.log.debug("File exists: " + stateFile);

            _.merge(states, {
                states: _.mapValues(grunt.file.readJSON(stateFile), function (state) {
                    return _.extend({
                        'module': module
                    }, state);
                })
            });
        }
    });

    config = _.merge(
        {},
        states,
        config,
        grunt.file.readJSON(path.resolve(options.general_config_folder, 'config.json')),
        grunt.file.readJSON(path.resolve(options.general_config_folder, 'config_' + options.env + '.json'))
    );

    grunt.log.debug("Generated Config: " + JSON.stringify(config));

    return config;
}

module.exports = function (grunt) {

    grunt.registerTask('ng_config_configurator', 'Generate a configuration based on the current ENVIRONMENT', function (envTarget) {
        var environment = envTarget || grunt.config('env');

        // Merge task-specific and/or target-specific options with these defaults.
        var options = this.options({
            "env": environment,
            "general_config_folder": "app/scripts/_config/",
            "search_base": "app/scripts/",
            "config_folder": "config",
            "config_file_config": "config.json",
            "config_file_state": "states.json",
            "export_module": "app.common",
            "export_variable": "config",
            "export_dest": "app/scripts/common/config/config.js",
            "browserify": true
        });

        grunt.log.debug("Using options: " + JSON.stringify(options));

        var template = generateTemplateSimple(),
            configuraton = generateConfiguration(grunt, options);

        if (options.browserify) {
            template = generateTemplateBrowserified();
        }

        grunt.log.debug("Used template: " + template);


        var generatedContent = grunt.template.process(template, {
                data: {
                    "module": options.export_module,
                    "variable": options.export_variable,
                    "config": JSON.stringify(generateConfiguration(), null, 4)
                }
            }
        );

        grunt.file.write(options.export_dest, generatedContent);
        grunt.log.ok('Generated ' + options.export_dest);
    });
};
{'fraction_non_alphanumeric': 0.14443872296601443, 'fraction_numerical': 0.0011585993820803296, 'mean_word_length': 2.2465524446301712, 'pattern_counts': {'":': 26, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 2, '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': '882045', 'n_tokens_mistral': 2226, 'n_tokens_neox': 2128, 'n_words': 399}