<commit_before>var fs = require('fs'),
    Path = require('path');

var parent = module.parent;
var parentFile = parent.filename;
var parentDir = Path.dirname(parentFile);

module.exports = {
  mirror: function(dir, gulp, opts){

    dir = dir || '.';
    dir = Path.resolve(parentDir, dir);
    var folders = getFolders(dir);

    if (folders.length === 0)
      return mapFiles(dir, gulp, opts);
    else
      return mapFolders(dir, folders, gulp, opts);
  }
};

function mapFiles(dir, gulp, opts){
  var tasks = getFiles(dir);
  var map = [];
  var taskName;
  var task;


  var args = [];
  args.push(gulp);
  if (opts !== undefined) {
    Object.keys(opts).forEach(function(key){
      args.push(opts[key]);
    });
  }

  tasks.forEach(function(file){
    task = file.replace('.js', '');
    map.push(task);
    taskName = dir + '/' + task;
    gulp.task(task, require(taskName).apply(null, args));
  });
  return map;
};

function mapFolders(dir, folders, gulp, opts){
  var map = {};
  var tasks = getFiles(dir);
  var taskName;
  var task;

  var args = [];
  args.push(gulp);
  if (opts !== undefined) {
    Object.keys(opts).forEach(function(key){
      args.push(opts[key]);
    });
  }

  if (tasks.length > 0){
    map['root'] = [];
    tasks.forEach(function(file){
      task = file.replace('.js', '');
      map['root'].push(task);
      taskName = dir + '/' + task;
      gulp.task(task, require(taskName).apply(null, args));
    });
  }
  folders.forEach(function(folder){
    map[folder] = [];
    tasks = getFiles(dir + '/' + folder);
    tasks.forEach(function(file){
      task = file.replace('.js', '');
      map[folder].push(task);
      taskName = dir + '/' + folder + '/' + task;
      gulp.task(task, require(taskName).apply(null, args));
    });
  });
  return map;
};

function getFolders(dir) {
  return fs.readdirSync(dir)
    .filter(function(file) {
      return fs.statSync(Path.join(dir, file)).isDirectory();
    });
};

function getFiles(dir) {
  return fs.readdirSync(dir)
    .filter(function(file) {
      return fs.statSync(Path.join(dir, file)).isFile();
    });
};<commit_msg>refactor index; add reflect function<commit_after>var fs = require('fs'),
    Path = require('path'),
    io = require('./lib/io'),
    mirror = require('./src/mirror'),
    reflect = require('./src/reflect');

var parent = module.parent;
var parentFile = parent.filename;
var parentDir = Path.dirname(parentFile);

module.exports = {
  mirror: function(dir, gulp, opts){
    dir = dir || '.';
    dir = Path.resolve(parentDir, dir);
    var folders = io.getFolders(dir);

    if (folders.length === 0)
      return mirror.mapFiles(dir, gulp, opts);
    else
      return mirror.mapFolders(dir, folders, gulp, opts);
  },

  reflect: function(dir, opts){
    dir = dir || '.';
    dir = Path.resolve(parentDir, dir);
    var folders = io.getFolders(dir);

    if (folders.length === 0) {
      return reflect.mapFiles(dir, opts);
    }
    else {
      return reflect.mapFolders(dir, folders, opts);
    }
  }
};
{'fraction_non_alphanumeric': 0.18029004614370467, 'fraction_numerical': 0.0013183915622940012, 'mean_word_length': 3.046666666666667, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '5224904', 'n_tokens_mistral': 1050, 'n_tokens_neox': 1034, 'n_words': 215}