index.js
function _requireDirAll(originalModule, absDir, options) {
  var modules = {};

  var files = [];
  try {
    files = fs.readdirSync(absDir);
  } catch (e) {
    if (options.throwNoDir) {
      throw e;
    }
  }

  for (var length=files.length, i=0; i<length; ++i) {

    var reqModule = {};
    reqModule.filename = files[i];                                         // full filename without path
    reqModule.ext      = path.extname(reqModule.filename);                 // file extension
    reqModule.base     = path.basename(reqModule.filename, reqModule.ext); // filename without extension
    reqModule.filepath = path.join(absDir, reqModule.filename);            // full filename with absolute path

    //console.log('reqModule:', reqModule);

    // If this is subdirectory, then descend recursively into it (excluding matching patter excludeDirs)
    if (fs.statSync(reqModule.filepath).isDirectory() &&
      options.recursive &&
      ! isExcludedDir(reqModule, options.excludeDirs) ) {

      // use filename (with extension) instead of base name (without extension)
      // to keep complete directory name for directories with '.', like 'dir.1.2.3'
      reqModule.name = reqModule.filename;

      // go recursively into subdirectory
      //if (typeof modules === 'undefined') {
      //  modules = {};
      //}
      modules[reqModule.name] = _requireDirAll(originalModule, reqModule.filepath, options);

    } else if ( ! isExcludedFileRe(reqModule, options.includeFiles) && !isExcludedFileParent(reqModule, originalModule)) {
      reqModule.name    = reqModule.base;
      reqModule.exports = require(reqModule.filepath);
      if (options.map) {
        options.map(reqModule);
      }

      var source = reqModule.exports;
      var target = (reqModule.name === 'index' && options.indexAsParent) ? modules : modules &&  modules[ reqModule.name ];

      var sourceIsObject = (typeof source === 'object');
      var targetIsObject = (typeof target === 'object');
      //var targetUnassigned = (typeof target === 'undefined');

      if (sourceIsObject && targetIsObject) {
        //if (Object.assign) {
        //  Object.assign(target, source);
        //} else {
          deepAssign(target, source);
        //}
      } else //if (
      //(!sourceIsObject && !targetIsObject) || // if source and target both are not objects  or...
      //(targetUnassigned)   // if target is not yet assigned, we may assign any type to it
      //)
      {
        target = source;
        //} else {
        //  console.log('!!!! ' +
        //    '  source:', source,
        //    '  target:', target,
        //    '; sourceIsObject:', sourceIsObject,
        //    '; targetIsObject:', targetIsObject,
        //    '; targetUnassigned:', targetUnassigned,
        //    '');
        //  throw 'Not possible to mix objects with scalar or array values: ' +
        //  'filepath: '+ reqModule.filepath + '; ' +
        //  'modules: '+ JSON.stringify(modules) + '; ' +
        //  'exports: '+ JSON.stringify(reqModule.exports)
        //    ;
      }

      if (reqModule.name === 'index' && options.indexAsParent) {
        modules = target;
      } else {
        //if (typeof modules === 'undefined') {
        //  modules = {};
        //}
        modules[ reqModule.name ] = target;
      }
    }

  }

  return modules;
}
function _requireDirAll(originalModule, absDir, options) {
  var modules = {};

  var files = [];
  try {
    files = fs.readdirSync(absDir);
  } catch (e) {
    if (options.throwNoDir) {
      throw e;
    }
  }

  for (var length=files.length, i=0; i<length; ++i) {

    var reqModule = {};
    reqModule.filename = files[i];                                         // full filename without path
    reqModule.ext      = path.extname(reqModule.filename);                 // file extension
    reqModule.base     = path.basename(reqModule.filename, reqModule.ext); // filename without extension
    reqModule.filepath = path.join(absDir, reqModule.filename);            // full filename with absolute path

    //console.log('reqModule:', reqModule);

    // If this is subdirectory, then descend recursively into it (excluding matching patter excludeDirs)
    if (fs.statSync(reqModule.filepath).isDirectory() &&
      options.recursive &&
      ! isExcludedDir(reqModule, options.excludeDirs) ) {

      // use filename (with extension) instead of base name (without extension)
      // to keep complete directory name for directories with '.', like 'dir.1.2.3'
      reqModule.name = reqModule.filename;

      // go recursively into subdirectory
      //if (typeof modules === 'undefined') {
      //  modules = {};
      //}
      modules[reqModule.name] = _requireDirAll(originalModule, reqModule.filepath, options);

    } else if ( ! isExcludedFileRe(reqModule, options.includeFiles) && !isExcludedFileParent(reqModule, originalModule)) {
      reqModule.name    = reqModule.base;
      reqModule.exports = require(reqModule.filepath);
      if (options.map) {
        options.map(reqModule);
      }

      var source = reqModule.exports;
      var target = (reqModule.name === 'index' && options.indexAsParent) ? modules : modules &&  modules[ reqModule.name ];

      var sourceIsObject = (typeof source === 'object');
      var targetIsObject = (typeof target === 'object');
      //var targetUnassigned = (typeof target === 'undefined');

      if (sourceIsObject && targetIsObject) {
        //if (Object.assign) {
        //  Object.assign(target, source);
        //} else {
          deepAssign(target, source);
        //}
      } else //if (
      //(!sourceIsObject && !targetIsObject) || // if source and target both are not objects  or...
      //(targetUnassigned)   // if target is not yet assigned, we may assign any type to it
      //)
      {
        target = source;
        //} else {
        //  console.log('!!!! ' +
        //    '  source:', source,
        //    '  target:', target,
        //    '; sourceIsObject:', sourceIsObject,
        //    '; targetIsObject:', targetIsObject,
        //    '; targetUnassigned:', targetUnassigned,
        //    '');
        //  throw 'Not possible to mix objects with scalar or array values: ' +
        //  'filepath: '+ reqModule.filepath + '; ' +
        //  'modules: '+ JSON.stringify(modules) + '; ' +
        //  'exports: '+ JSON.stringify(reqModule.exports)
        //    ;
      }

      if (reqModule.name === 'index' && options.indexAsParent) {
        modules = target;
      } else {
        //if (typeof modules === 'undefined') {
        //  modules = {};
        //}
        modules[ reqModule.name ] = target;
      }
    }

  }

  return modules;
}
function _requireDirAll ( originalModule , absDir , options ) { var modules = { } ; var files = [ ] ; try { files = fs . readdirSync ( absDir ) ; } catch ( e ) { if ( options . throwNoDir ) { throw e ; } } for ( var length = files . length , i = 0 ; i < length ; ++ i ) { var reqModule = { } ; reqModule . filename = files [ i ] ; // full filename without path reqModule . ext = path . extname ( reqModule . filename ) ; // file extension reqModule . base = path . basename ( reqModule . filename , reqModule . ext ) ; // filename without extension reqModule . filepath = path . join ( absDir , reqModule . filename ) ; // full filename with absolute path //console.log('reqModule:', reqModule); // If this is subdirectory, then descend recursively into it (excluding matching patter excludeDirs) if ( fs . statSync ( reqModule . filepath ) . isDirectory ( ) && options . recursive && ! isExcludedDir ( reqModule , options . excludeDirs ) ) { // use filename (with extension) instead of base name (without extension) // to keep complete directory name for directories with '.', like 'dir.1.2.3' reqModule . name = reqModule . filename ; // go recursively into subdirectory //if (typeof modules === 'undefined') { //  modules = {}; //} modules [ reqModule . name ] = _requireDirAll ( originalModule , reqModule . filepath , options ) ; } else if ( ! isExcludedFileRe ( reqModule , options . includeFiles ) && ! isExcludedFileParent ( reqModule , originalModule ) ) { reqModule . name = reqModule . base ; reqModule . exports = require ( reqModule . filepath ) ; if ( options . map ) { options . map ( reqModule ) ; } var source = reqModule . exports ; var target = ( reqModule . name === 'index' && options . indexAsParent ) ? modules : modules && modules [ reqModule . name ] ; var sourceIsObject = ( typeof source === 'object' ) ; var targetIsObject = ( typeof target === 'object' ) ; //var targetUnassigned = (typeof target === 'undefined'); if ( sourceIsObject && targetIsObject ) { //if (Object.assign) { //  Object.assign(target, source); //} else { deepAssign ( target , source ) ; //} } else //if ( //(!sourceIsObject && !targetIsObject) || // if source and target both are not objects  or... //(targetUnassigned)   // if target is not yet assigned, we may assign any type to it //) { target = source ; //} else { //  console.log('!!!! ' + //    '  source:', source, //    '  target:', target, //    '; sourceIsObject:', sourceIsObject, //    '; targetIsObject:', targetIsObject, //    '; targetUnassigned:', targetUnassigned, //    ''); //  throw 'Not possible to mix objects with scalar or array values: ' + //  'filepath: '+ reqModule.filepath + '; ' + //  'modules: '+ JSON.stringify(modules) + '; ' + //  'exports: '+ JSON.stringify(reqModule.exports) //    ; } if ( reqModule . name === 'index' && options . indexAsParent ) { modules = target ; } else { //if (typeof modules === 'undefined') { //  modules = {}; //} modules [ reqModule . name ] = target ; } } } return modules ; }
Main function. Recursively go through directories and require modules according to options

@param   {object}  originalModule
@param   {string}  absDir
@param   {RequireOptions} options
@returns {object}
@private
Main function . Recursively go through directories and require modules according to options