<commit_before>/**
 * Created by pgotthardt on 07/12/15.
 */

var Promise = require('bluebird'),
    chalk = require('chalk');

/**
 * Choose the most correct version of webpack, prefer locally installed version,
 * fallback to the own dependency if there's none.
 * @returns {*}
 */
function getWebpack() {
    try {
        return require(process.cwd() + '/node_modules/webpack');
    } catch(e) {
        return require('webpack');
    }
}

function getAppName(webpackConfig) {
    return webpackConfig.name || webpackConfig.output.filename;
}

function getOutputOptions(webpackConfig, options) {
    var outputOptions = Object.create(webpackConfig.stats || {});
    if(typeof options.modulesSort !== 'undefined') {
        outputOptions.modulesSort = options.modulesSort;
    }
    if(typeof options.chunksSort !== 'undefined') {
        outputOptions.chunksSort = options.chunksSort;
    }
    if(typeof options.assetsSort !== 'undefined') {
        outputOptions.assetsSort = options.assetsSort;
    }
    if(typeof options.exclude !== 'undefined') {
        outputOptions.exclude = options.exclude;
    }
    if(typeof options.colors !== 'undefined') {
        outputOptions.colors = options.colors;
    }
    return outputOptions;
}

/**
 * Create a single webpack build using the specified configuration.
 * Calls the done callback once it has finished its work.
 *
 * @param {string} configuratorFileName The app configuration filename
 * @param {Object} options The build options
 * @param {boolean} options.watch If `true`, then the webpack watcher is being run; if `false`, runs only ones
 * @param {boolean} options.json If `true`, then the webpack watcher will only report the result as JSON but not produce any other output
 * @param {number} index The configuration index
 * @param {number} expectedConfigLength
 * @param {Function} done The callback that should be invoked once this worker has finished the build.
 */
module.exports = function(configuratorFileName, options, index, expectedConfigLength, done) {
    if(options.argv) {
        process.argv = options.argv;
    }
    chalk.enabled = options.colors;
    var config = require(configuratorFileName),
        watch = !!options.watch,
        silent = !!options.json;
    if(expectedConfigLength !== 1 && !Array.isArray(config)
            || Array.isArray(config) && config.length !== expectedConfigLength) {
        if(config.length !== expectedConfigLength) {
            var errorMessage =                 '[WEBPACK] There is a difference between the amount of the'
                + ' provided configs. Maybe you where expecting command line'
                + ' arguments to be passed to your webpack.config.js. If so,'
                + " you'll need to separate them with a -- from the parallel-webpack options.";
            console.error(errorMessage);
            return Promise.reject(errorMessage);
        }
    }
    if(Array.isArray(config)) {
        config = config[index];
    }
    Promise.resolve(config).then(function(webpackConfig) {
        var webpack = getWebpack(),
            outputOptions = getOutputOptions(webpackConfig, options),
            finishedCallback = function(err, stats) {
                if(err) {
                    console.error('%s fatal error occured', chalk.red('[WEBPACK]'));
                    console.error(err);
                    done(err);
                }
                if(stats.compilation.errors && stats.compilation.errors.length) {
                    var message = chalk.red('[WEBPACK]') + ' Errors building ' + chalk.yellow(getAppName(webpackConfig)) + "\n"
                        + stats.compilation.errors.map(function(error) {
                            return error.message;
                        }).join("\n");
                    if(watch) {
                        console.log(message);
                    } else {
                        return done({
                            message: message,
                            stats: JSON.stringify(stats.toJson(outputOptions), null, 2)
                        });
                    }
                }
                if(!silent) {
                    if(options.stats) {
                        console.log(stats.toString(outputOptions));
                    }
                    console.log('%s Finished building %s within %s seconds', chalk.blue('[WEBPACK]'), chalk.yellow(getAppName(webpackConfig)), chalk.blue((stats.endTime - stats.startTime) / 1000));
                }
                if(!watch) {
                    done(null, options.stats ? JSON.stringify(stats.toJson(outputOptions), null, 2) : '');
                }
            };
        if(!silent) {
            console.log('%s Started %s %s', chalk.blue('[WEBPACK]'), watch ? 'watching' : 'building', chalk.yellow(getAppName(webpackConfig)));
        }
        var compiler = webpack(webpackConfig),
            watcher;
        if(watch || webpack.watch) {<commit_msg>Add timestamp for watcher. Closes #12.<commit_after>/**
 * Created by pgotthardt on 07/12/15.
 */

var Promise = require('bluebird'),
    chalk = require('chalk');

/**
 * Choose the most correct version of webpack, prefer locally installed version,
 * fallback to the own dependency if there's none.
 * @returns {*}
 */
function getWebpack() {
    try {
        return require(process.cwd() + '/node_modules/webpack');
    } catch(e) {
        return require('webpack');
    }
}

function getAppName(webpackConfig) {
    return webpackConfig.name || webpackConfig.output.filename;
}

function getOutputOptions(webpackConfig, options) {
    var outputOptions = Object.create(webpackConfig.stats || {});
    if(typeof options.modulesSort !== 'undefined') {
        outputOptions.modulesSort = options.modulesSort;
    }
    if(typeof options.chunksSort !== 'undefined') {
        outputOptions.chunksSort = options.chunksSort;
    }
    if(typeof options.assetsSort !== 'undefined') {
        outputOptions.assetsSort = options.assetsSort;
    }
    if(typeof options.exclude !== 'undefined') {
        outputOptions.exclude = options.exclude;
    }
    if(typeof options.colors !== 'undefined') {
        outputOptions.colors = options.colors;
    }
    return outputOptions;
}

/**
 * Create a single webpack build using the specified configuration.
 * Calls the done callback once it has finished its work.
 *
 * @param {string} configuratorFileName The app configuration filename
 * @param {Object} options The build options
 * @param {boolean} options.watch If `true`, then the webpack watcher is being run; if `false`, runs only ones
 * @param {boolean} options.json If `true`, then the webpack watcher will only report the result as JSON but not produce any other output
 * @param {number} index The configuration index
 * @param {number} expectedConfigLength
 * @param {Function} done The callback that should be invoked once this worker has finished the build.
 */
module.exports = function(configuratorFileName, options, index, expectedConfigLength, done) {
    if(options.argv) {
        process.argv = options.argv;
    }
    chalk.enabled = options.colors;
    var config = require(configuratorFileName),
        watch = !!options.watch,
        silent = !!options.json;
    if(expectedConfigLength !== 1 && !Array.isArray(config)
            || Array.isArray(config) && config.length !== expectedConfigLength) {
        if(config.length !== expectedConfigLength) {
            var errorMessage = '[WEBPACK] There is a difference between the amount of the'
                + ' provided configs. Maybe you where expecting command line'
                + ' arguments to be passed to your webpack.config.js. If so,'
                + " you'll need to separate them with a -- from the parallel-webpack options.";
            console.error(errorMessage);
            return Promise.reject(errorMessage);
        }
    }
    if(Array.isArray(config)) {
        config = config[index];
    }
    Promise.resolve(config).then(function(webpackConfig) {
        var webpack = getWebpack(),
            outputOptions = getOutputOptions(webpackConfig, options),
            finishedCallback = function(err, stats) {
                if(err) {
                    console.error('%s fatal error occured', chalk.red('[WEBPACK]'));
                    console.error(err);
                    done(err);
                }
                if(stats.compilation.errors && stats.compilation.errors.length) {
                    var message = chalk.red('[WEBPACK]') + ' Errors building ' + chalk.yellow(getAppName(webpackConfig)) + "\n"
                        + stats.compilation.errors.map(function(error) {
                            return error.message;
                        }).join("\n");
                    if(watch) {
                        console.log(message);
                    } else {
                        return done({
                            message: message,
                            stats: JSON.stringify(stats.toJson(outputOptions), null, 2)
                        });
                    }
                }
                if(!silent) {
                    if(options.stats) {
                        console.log(stats.toString(outputOptions));
                    }
                    var timeStamp = watch
                        ? ' ' + chalk.yellow(new Date().toTimeString().split(/ +/)[0])
                        : '';
                    console.log('%s Finished building %s within %s seconds', chalk.blue('[WEBPACK' + timeStamp + ']'), chalk.yellow(getAppName(webpackConfig)), chalk.blue((stats.endTime - stats.startTime) / 1000));
                }
                if(!watch) {
                    done(null, options.stats ? JSON.stringify(stats.toJson(outputOptions), null, 2) : '');
                }
            };
        if(!silent) {
            console.log('%s Started %s %s', chalk.blue('[WEBPACK]'), watch ? 'watching' : 'building', chalk.yellow(getAppName(webpackConfig)));
        }
        var compiler = webpack(webpackConfig),
            watcher;
        if(watch || webpack.watch) {
{'fraction_non_alphanumeric': 0.11618915435709329, 'fraction_numerical': 0.0028749876078120354, 'mean_word_length': 2.1270923744575327, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '618737', 'n_tokens_mistral': 2642, 'n_tokens_neox': 2554, 'n_words': 662}