/**
 * Compile jade files.
 * @function task.worker.compileJadeDirectory
 * @param {object} config - Work configuration.
 * @param {string} config.srcDir - Source jade directory.
 * @param {string} config.ignore - Ignore  jade pattern. Glob.
 * @param {string} config.pattern - Source jade file pattern. Glob.
 * @param {string} config.destDir - Destination html directory.
 * @param {function} callback - Callback when done.
 * @author Taka Okunishi
 *
 */

var apeman = require('apeman'),
    debug = require('./_debug'),
    compile = require('../../lib/compile'),
    file = require('../../lib/file'),
    forceUnlink = file.forceUnlink,
    _convertDirectory = require('./_convert_directory'),
    precompileJadeFile = compile.precompileJadeFile;

exports = module.exports = function (config, callback) {
    _convertDirectory({
        srcDir: config.srcDir,
        destDir: config.destDir,
        pattern: config.pattern || '*.jade',
        ignore: config.ignore,
        fileNameConverter: function (filename) {
            return filename.replace(/\.jade$/, '.js');
        },
        fileContentConverter: function (src, dest, callback) {
            precompileJadeFile(src, dest, function (err) {
                if (!err) {
                    debug.didCreateFile(dest);
                }
                callback(err);
            });
        }
    }, function (err) {
        callback(err);
    });
};
