<commit_before>
/*

  Compile.js

*/

var Bundler     = require("./bundler"),
    express     = require("express"),
    Metadata    = require("./metadata"),
    Motors      = global.engines,
    Promise     = require("bluebird"),
    Utils       = require("./utils");
    makeQuery   = Promise.promisify(global.db.queries);

function createDataQuery(queries) {
  return function(context) {
    return makeQuery(queries, context).then(function(data) {
      return Promise.resolve(Object.assign({}, global.vars, context, data));
    });
  }
}


/**

  @returns <String> Directory to ignore (if exists)

**/

function addFile(router, route, file, type, basePath) {

    var tasks = [],
        metadata = {};

    // Add to list of extensions if not present
    if (!Motors.hasEngine(file.ext)) Motors.addEngine(file.ext);

    // Extract metadata
    if (/^html?$/.test(type)) {
      metadata = Metadata.extract(Utils.readFileHead(file.path, 500));
      tasks.push(createDataQuery(metadata.data));
    }

    // File compilation function
    if (file.bundle)
      tasks.push(Bundler.fromFile(file, type).get);
    else
      tasks.push(Promise.promisify(Motors.compileFile.bind(Motors, file.path)));

    // Route handling function
    function handler(req, res) {

      // Set cookies
      for (var key in metadata.cookies || {})
        res.cookie(key, metadata.cookies[key]);

      // Set session
      for (var key in metadata.session || {})
        req.session[key] = metadata.session[key];

      Utils.sequence(tasks, req.params)
      .then(res.type(type).send.bind(res))
      .catch(function(err) { console.log(err); res.sendStatus(500); });

    }

    // Add routes to router
    Utils.makeRoutes(route, file, { type : type, query : metadata.query }).forEach(function(r) {
      router.get(r, handler);
    });

    // If we have a bundle, return the path (we won't "watch" as this happens
    // already in Bundler.
    if (file.ignored) return file.ignored;

}


module.exports = function(options) {

  var router  = express(),
      ignore,
      ignored = [],
      files   = Utils.mapFiles(options.path, options),
      exts    = {};

  files.forEach(function(file) {

    ignore = addFile(router, options.url, file, options.ext);
    if (ignore) ignored = ignored.concat(ignore);

    exts[file.ext] = 1;

  });

  for (var key in exts)
    Utils.watch(options.path, key, options.ext, { ignoreInitial : true, ignored : ignored });<commit_msg>Compiler: uses chokidar-style callbacks<commit_after>
/*

  Compile.js

*/

var Bundler     = require("./bundler"),
    express     = require("express"),
    Metadata    = require("./metadata"),
    Motors      = global.engines,
    Promise     = require("bluebird"),
    Utils       = require("./utils");
    makeQuery   = Promise.promisify(global.db.queries);

function createDataQuery(queries) {
  return function(context) {
    return makeQuery(queries, context).then(function(data) {
      return Promise.resolve(Object.assign({}, global.vars, context, data));
    });
  }
}


/**

  @returns <String> Directory to ignore (if exists)

**/

function addFile(router, route, file, type, basePath) {

    var tasks = [],
        metadata = {};

    // Add to list of extensions if not present
    if (!Motors.hasEngine(file.ext)) Motors.addEngine(file.ext);

    // Extract metadata
    if (/^html?$/.test(type)) {
      metadata = Metadata.extract(Utils.readFileHead(file.path, 500));
      tasks.push(createDataQuery(metadata.data));
    }

    // File compilation function
    if (file.bundle)
      tasks.push(Bundler.fromFile(file, type).get);
    else
      tasks.push(Promise.promisify(Motors.compileFile.bind(Motors, file.path)));

    // Route handling function
    function handler(req, res) {

      // Set cookies
      for (var key in metadata.cookies || {})
        res.cookie(key, metadata.cookies[key]);

      // Set session
      for (var key in metadata.session || {})
        req.session[key] = metadata.session[key];

      Utils.sequence(tasks, req.params)
      .then(res.type(type).send.bind(res))
      .catch(function(err) { console.log(err); res.sendStatus(500); });

    }

    // Add routes to router
    Utils.makeRoutes(route, file, { type : type, query : metadata.query }).forEach(function(r) {
      router.get(r, handler);
    });

    // If we have a bundle, return the path (we won't "watch" as this happens
    // already in Bundler.
    if (file.ignored) return file.ignored;

}


module.exports = function(options) {

  var router  = express(),
      ignore,
      ignored = [],
      files   = Utils.mapFiles(options.path, options),
      exts    = {};

  files.forEach(function(file) {

    ignore = addFile(router, options.url, file, options.ext);
    if (ignore) ignored = ignored.concat(ignore);

    exts[file.ext] = 1;

  });

  for (var key in exts)
    Utils.watch(options.path, key, { ignoreInitial : true, ignored : ignored });
{'fraction_non_alphanumeric': 0.1488974307100951, 'fraction_numerical': 0.0028322880841594174, 'mean_word_length': 2.7711670480549198, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '444256', 'n_tokens_mistral': 1589, 'n_tokens_neox': 1514, 'n_words': 348}