<commit_before>'use strict';

module.exports = function(option, log, path, urls, template) {

    var //---------------
        //    Imports
        //---------------

        fs = require('fs'),
        q = require('q'),
        lang = require('mercenary').lang,
        undef = lang.undef,
        apply = lang.apply,
        destructured = lang.destructured,
        each = lang.each,
        concat = lang.concat,
        isArray = lang.is.array,
        isString = lang.is.string,

        //----------------------
        //    Implementation
        //----------------------

        // Raise an error using the fail log.
        raise = function(error) {
            log.fail(error.stack || 'An unexpected error occurred');
        },

        // Fill in missing directories for the given file path.
        mkdirs = function(filepath) {
            var mkdir = function(dirname) {
                    if (fs.existsSync(dirname)) {
                        if (!fs.statSync(dirname).isDirectory()) {
                            throw new Error(
                                'The path "' +
                                dirname +
                                '" is not a directory');
                        }
                    } else {
                        mkdir(path.path.join(dirname, '..'));
                        fs.mkdirSync(dirname);
                    }
                };

            mkdir(path.path.dirname(filepath));
        };

    //------------------
    //    Public API
    //------------------

    return function() {
        try {
            (q.all(
                apply(
                    concat, undef,
                    require(path.js(option('urls') || 'urls'))))
            .then(function(patterns) {
                try {
                    each(patterns, function(pattern) {
                        (q.when(
                            isString(pattern.view) ?
                                require(path.js(pattern.view)) :
                                pattern.view)
                        .then(function(view) {
                            try {
                                each(isArray(view) ? view : [view], function(view) {
                                    var urlpath = (
                                            path.build(
                                                urls.resolve(
                                                    pattern.name, view.params))),
                                        filepath = (
                                            path.path.extname(urlpath) ?
                                                urlpath :
                                                path.path.join(urlpath, 'index.html')),
                                        content = (
                                            template.render(
                                                view.template,
                                                view.context || {}));

                                    mkdirs(filepath);
                                    fs.writeFileSync(filepath, content);
                                });
                            } catch (error) {
                                raise(error);
                            }
                        }, raise));
                    });
                } catch (error) {
                    raise(error);
                }
            }, raise));
        } catch (error) {
            raise(error);
        }
    };
};
<commit_msg>Added more logging and verbosity switch.<commit_after>'use strict';

module.exports = function(option, log, path, urls, template) {

    var //---------------
        //    Imports
        //---------------

        fs = require('fs'),
        q = require('q'),
        lang = require('mercenary').lang,
        undef = lang.undef,
        apply = lang.apply,
        once = lang.once,
        destructured = lang.destructured,
        each = lang.each,
        concat = lang.concat,
        isArray = lang.is.array,
        isString = lang.is.string,

        //---------------
        //    Options
        //---------------

        overwrite = !(option('overwrite') === false),
        verbose = option('verbose') || false,

        //----------------------
        //    Implementation
        //----------------------

        // Raise an error using the fail log.
        raise = function(error) {
            log.fail(error.stack || 'An unexpected error occurred');
        },

        // Fill in missing directories for the given file path.
        mkdirs = function(filepath) {
            var mkdir = function(dirname) {
                    if (fs.existsSync(dirname)) {
                        if (!fs.statSync(dirname).isDirectory()) {
                            throw new Error(
                                'The path "' +
                                dirname +
                                '" is not a directory');
                        }
                    } else {
                        mkdir(path.path.join(dirname, '..'));
                        if (verbose) {
                            log.info('Creating directory "' + dirname + '"');
                        }
                        fs.mkdirSync(dirname);
                    }
                };

            mkdir(path.path.dirname(filepath));
        },

        // Write a file to disk.
        write = function(filepath, content) {
            var exists = once(function() {
                    return fs.existsSync(filepath);
                });

            if (!overwrite && exists()) {
                throw new Error(
                    'The overwrite setting is false and the file "' +
                    filepath +
                    '" already exists');
            }
            mkdirs(filepath);
            if (verbose) {
                if (exists()) {
                    log.warn('Rendering (overwrite) "' + filepath + '"');
                } else {
                    log.info('Rendering "' + filepath + '"');
                }
            }
            fs.writeFileSync(filepath, content);
        };

    //------------------
    //    Public API
    //------------------

    return function() {
        try {
            (q.all(
                apply(
                    concat, undef,
                    require(path.js(option('urls') || 'urls'))))
            .then(function(patterns) {
                try {
                    each(patterns, function(pattern) {
                        (q.when(
                            isString(pattern.view) ?
                                require(path.js(pattern.view)) :
                                pattern.view)
                        .then(function(view) {
                            try {
                                if (verbose) {
                                    log.info('Processing URL pattern "' + pattern.name + '"');
                                }
                                each(isArray(view) ? view : [view], function(view) {
                                    var urlpath = (
                                            path.build(
                                                urls.resolve(
                                                    pattern.name, view.params))),
                                        filepath = (
                                            path.path.extname(urlpath) ?
                                                urlpath :
                                                path.path.join(urlpath, 'index.html')),
                                        content = (
                                            template.render(
                                                view.template,
                                                view.context || {}));

                                    write(filepath, content);
                                });
                            } catch (error) {
                                raise(error);
                            }
                        }, raise));
                    });
                } catch (error) {
                    raise(error);
                }
            }, raise));
        } catch (error) {
            raise(error);
        }
    };
};

{'fraction_non_alphanumeric': 0.12738930558916042, 'fraction_numerical': 0.0, 'mean_word_length': 0.6802845528455285, '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': 51, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2299060', 'n_tokens_mistral': 1757, 'n_tokens_neox': 1641, 'n_words': 329}