<commit_before>var fs = require('fs');
var async = require('async');
var path = require('path');
var merge = require('./merge');

function readAllFiles(filename, callback) {
  fs.stat(filename, function (err, stat) {
    if (err) {
      return callback(err);
    }
    if (stat.isFile()) {
      return fs.readFile(filename, function (err, data) {
        if (err) {
          return callback(err);
        }
        callback(null, [data]);
      });
    }

    fs.readdir(filename, function (err, filenames) {
      if (err) {
        return callback(err);
      }
      filenames = filenames.filter(function (fname) { return !(/^\..*$/.test(fname)); });
      filenames = filenames.map(function (fname) { return path.join(filename, fname); });
      var results = [];
      async.forEach(
        filenames,
        function (filename, callback) {
          readAllFiles(filename, function (err, datas) {
            if (err) {
              return callback(err);
            }
            results = results.concat(datas);
            callback();
          });
        },
        function (err) {
          callback(err, results);<commit_msg>fixed error with too many open files on OSX<commit_after>var fs = require('fs');
var async = require('async');
var path = require('path');
var merge = require('./merge');

function readAllFiles(filename, callback) {
  fs.stat(filename, function (err, stat) {
    if (err) {
      return callback(err);
    }
    if (stat.isFile()) {
      return fs.readFile(filename, function (err, data) {
        if (err) {
          return callback(err);
        }
        callback(null, [data]);
      });
    }

    fs.readdir(filename, function (err, filenames) {
      if (err) {
        return callback(err);
      }
      filenames = filenames.filter(function (fname) { return !(/^\..*$/.test(fname)); });
      filenames = filenames.map(function (fname) { return path.join(filename, fname); });
      var results = [];
      async.forEachSeries(
        filenames,
        function (filename, callback) {
          readAllFiles(filename, function (err, datas) {
            if (err) {
              return callback(err);
            }
            results = results.concat(datas);
            callback();
          });
        },
        function (err) {
          callback(err, results);
{'fraction_non_alphanumeric': 0.14875322441960448, 'fraction_numerical': 0.0, 'mean_word_length': 2.1068090787716955, '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': '851548', 'n_tokens_mistral': 681, 'n_tokens_neox': 674, 'n_words': 161}