<commit_before><commit_msg>[init] add simple dispatch logic<commit_after>
var path = require('path');
var localizer = require('localizer');
var detective = require('detective');
var async = require('async');
var Bundle = require('./bundle.js');

function ModuleBox(settings) {
  if (!(this instanceof ModuleBox)) return new ModuleBox(settings);

  this._root = settings.root;
  this._cache = {};

  this._localizer = localizer(settings);
}
module.exports = ModuleBox;

ModuleBox.prototype._realpath = function (filepath) {
  return path.resolve(this._root, '.' + filepath);
};

ModuleBox.prototype._getDependencies = function (filepath) {
  var exists = this._cache.hasOwnProperty(filepath);
  if (!exists) return null;

  return this._cache[filepath];
};

ModuleBox.prototype._setDependencies = function (filepath, dependencies) {
  this._cache[filepath] = dependencies;
};

ModuleBox.prototype._scanFile = function (filepath, content, callback) {
  var self = this;

  // fetch from and input data, from filepath and file content
  var from = path.dirname(filepath);
  var calls = detective(content);

  // The wanted result is a input: require.resolve(input) map
  var resolveObject = {};
  for (var i = 0; i < calls.length; i++) {
    resolveObject[calls[i]] = resolveMap.bind(this, calls[i]);
  }

  function resolveMap(input, callback) {
    self._localizer(from, input, function (err, result) {
      if (err && err.code === 'MODULE_NOT_FOUND') return callback(null, null);

      callback(err, result);
    });
  }

  // resolve all dependencies to fullpaths
  async.parallel(resolveObject, callback);
};

ModuleBox.prototype.dispatch = function (info) {
  // FIXME: blocking
  var startpath = this._localizer(path.dirname(info.source), info.request);

  return new Bundle(this, startpath, info.acquired);
};

{'fraction_non_alphanumeric': 0.1375894331315355, 'fraction_numerical': 0.000550357732526142, 'mean_word_length': 4.559633027522936, 'pattern_counts': {'":': 0, '<': 4, '<?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': '2305745', 'n_tokens_mistral': 546, 'n_tokens_neox': 532, 'n_words': 142}