<commit_before>var Istanbul = require('istanbul');
var through = require('through2');
var minimatch = require("minimatch");
var _ = require('lodash');

function instrument(options) {
  var excludePattern = options.exclude ? [].concat(options.exclude) : [''];
  var instrumenter = new Istanbul.Instrumenter();

  function transform(file) {
    // If if doesnt match the pattern dont instrument it
    var matchResult = _.compact(_.map(excludePattern, function (pattern) {
      return minimatch(file, pattern);
    }));

    if (matchResult.length)
      return through();

    var data = '';
    return through(function(buf, enc, next) {
      data += buf;
      next();
    }, function(next) {
      var self = this;
      instrumenter.instrument(data, file, function(err, code) {
        if (!err) {
          // Inject __converage__ var
          self.push(code);
          self.push('after(function(){console.log("__coverage__=\'" + JSON.stringify(__coverage__) + "\';");});');
        } else {
          self.emit('error', err);
        }
        next();
      });
    });
  }

  return transform;
}

function writeReports(options) {
  var collector = new Istanbul.Collector();
  var report = options.report || [];
  delete(options.report);

  var data = '';
  return through(function(buf, enc, next) {
    data += buf;
    next();
  }, function(next) {
    var re = /__coverage__='([^;]*)';\n/gi,
        match = re.exec(data),
        coverage;

    // Clean up the stream
    this.push(data.replace(re,''));

    // match[1] contains JSON.stringify(__coverage__)
    coverage = match ? JSON.parse(match[1]) || {} : {};

    collector.add(coverage);

    // Add report
    [].concat(report).forEach(function (reportType) {
      Istanbul.Report
        .create(reportType, _.clone(options))
        .writeReport(collector, true);
    });

    next();
  });
}

module.exports = function (b, opts) {
  var reporterOptions = _.omit(opts, 'exclude');

  b.transform(instrument(opts));
  b.pipeline.get('wrap').push(writeReports(reporterOptions));
};<commit_msg>Apply reporter on any reset. Retain reports array<commit_after>var Istanbul = require('istanbul');
var through = require('through2');
var minimatch = require("minimatch");
var _ = require('lodash');

function instrument(options) {
  var excludePattern = options.exclude ? [].concat(options.exclude) : [''];
  var instrumenter = new Istanbul.Instrumenter();

  function transform(file) {
    // If if doesnt match the pattern dont instrument it
    var matchResult = _.compact(_.map(excludePattern, function (pattern) {
      return minimatch(file, pattern);
    }));

    if (matchResult.length)
      return through();

    var data = '';
    return through(function(buf, enc, next) {
      data += buf;
      next();
    }, function(next) {
      var self = this;
      instrumenter.instrument(data, file, function(err, code) {
        if (!err) {
          // Inject __converage__ var
          self.push(code);
          self.push('after(function(){console.log("__coverage__=\'" + JSON.stringify(__coverage__) + "\';");});');
        } else {
          self.emit('error', err);
        }
        next();
      });
    });
  }

  return transform;
}

var report = [];

function writeReports(options) {
  var collector = new Istanbul.Collector();

  if (options.report) {
    report = options.report;
    delete(options.report);
  }

  var data = '';
  return through(function(buf, enc, next) {
    data += buf;
    next();
  }, function(next) {
    var re = /__coverage__='([^;]*)';\n/gi,
        match = re.exec(data),
        coverage;

    // Clean up the stream
    this.push(data.replace(re,''));

    // match[1] contains JSON.stringify(__coverage__)
    coverage = match ? JSON.parse(match[1]) || {} : {};

    collector.add(coverage);

    // Add report
    [].concat(report).forEach(function (reportType) {
      Istanbul.Report
        .create(reportType, _.clone(options))
        .writeReport(collector, true);
    });

    next();
  });
}

module.exports = function (b, opts) {
  var reporterOptions = _.omit(opts, 'exclude');

  function apply() {
    b.pipeline.get('wrap').push(writeReports(reporterOptions));
  }

  b.transform(instrument(opts));
  apply();
  b.on('reset', apply);
};
{'fraction_non_alphanumeric': 0.16494845360824742, 'fraction_numerical': 0.0014058106841611997, 'mean_word_length': 2.9165137614678898, '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': '992445', 'n_tokens_mistral': 1333, 'n_tokens_neox': 1290, 'n_words': 281}