<commit_before>var exec = require('child_process').exec;

var minimatch = require('minimatch');
var _ = require("lodash");

var log = require("./log");
var utils = require("./utils");


var Hooks = function (hook_path) {
  var self = this,
    callable,
    floo = utils.load_floo(hook_path),
    hooks = floo.hooks;

  self.hooks = [];

  if (!hooks) {
    return log.log("Didn't find any hooks in .floo.");
  }

  _.each(hooks, function (hook, regex) {
    if (_.isString(regex)) {
      regex = minimatch.makeRe(regex, {
        dot: true,
        matchBase: true
      });
    }
    if (!_.isRegExp(regex)) {
      throw new Error('regex must be a string or regex' + JSON.stringify(regex));
    }

    if (_.isString(hook)) {
      callable = function (_path, cb) {
        var calling = hook.replace(/#FLOO_HOOK_FILE/g, _path);
        console.log('Calling', calling);
        exec(calling, function (error, stdout, stderr) {
          if (error) {
            log.error(error);
          }
          if (stderr) {
            log.error(stderr);
          }
          if (stdout) {
            log.log(stdout);
          }
          return cb();
        });
      };
    } else {
      callable = hook;
    }

    if (!_.isFunction(callable)) {
      throw new Error('Hooks must be functions or strings: ' + JSON.stringify(callable));
    }
    log.log('Installing hook:', regex);
    self.hooks.push({re: regex, hook: callable});
  });
};

Hooks.prototype.on_saved = function (_path) {
  var self = this;

  _.each(self.hooks, function (hook) {<commit_msg> Hooks now reload when the .floo file is changed.<commit_after>var exec = require('child_process').exec;
var fs = require('fs');

var minimatch = require('minimatch');
var _ = require("lodash");

var log = require("./log");
var utils = require("./utils");


var Hooks = function (hook_path) {
  var self = this;

  self.hook_path = hook_path;
  self.hooks = [];
  self.load_hooks(hook_path);
  self.watcher = fs.watchFile(hook_path, self.on_hooks_change.bind(self));
};

Hooks.prototype.load_hooks = function () {
  var self = this,
    floo = utils.load_floo(self.hook_path),
    hooks = floo.hooks;

  if (!hooks) {
    return log.log("Didn't find any hooks in .floo.");
  }

  _.each(hooks, function (hook, regex) {
    var callable;

    if (!_.isString(regex)) {
      throw new Error('Hooks must be strings (for globbing)' + JSON.stringify(regex));
    }

    regex = minimatch.makeRe(regex, {
      dot: true,
      matchBase: true
    });

    callable = function (_path, cb) {
      var calling = hook.replace(/#FLOO_HOOK_FILE/g, _path);
      console.log('Calling', calling);
      exec(calling, function (error, stdout, stderr) {
        if (error) {
          log.error(error);
        }
        if (stderr) {
          log.error(stderr);
        }
        if (stdout) {
          log.log(stdout);
        }
        return cb();
      });
    };

    log.log('Installing hook:', regex, hook);
    self.hooks.push({re: regex, hook: callable});
  });
};

Hooks.prototype.on_hooks_change = function(prev, current) {
  var self = this;

  log.log('Reloading hooks.');
  self.load_hooks();
};

Hooks.prototype.on_saved = function (_path) {
  var self = this;

  _.each(self.hooks, function (hook) {
{'fraction_non_alphanumeric': 0.15549433731251913, 'fraction_numerical': 0.0, 'mean_word_length': 2.4914529914529915, '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': '1913818', 'n_tokens_mistral': 1100, 'n_tokens_neox': 1094, 'n_words': 251}