alykoshin / require-dir-all

Yet another Node.js helper to require all files in directory
MIT License
9 stars 1 forks source link

indexAsParent should ignore all other files in directory #3

Open Redsandro opened 7 years ago

Redsandro commented 7 years ago

When I want to load a tree of files and directories, and want directories to handle their own index:

index.js

module.exports = require('require-dir-all')('.', {
    recursive   : true,
    indexAsParent   : true

If subdirectories contain index files, all other files should be ignored because recursive: true but you can still see them being processed. E.g. using:

require-dir-all map function:

map: reqModule => {
    console.log(reqModule);
    return reqModule;
}

You see all irrelevant files being processed.

alykoshin commented 7 years ago

Looks like you have a use case different from what indexAsParent doing. That's how indexAsParent behaves:

// indexAsParent_false
// In this case export from index.js was taken as property named 'index' of root object:

indexAsParent_false: {
  "index": {
    "index_property": "value exported from index.js"
  },
  "module1": {
    "module1_property": "value exported from module1.js"
  },
  "module2": {
    "module2_property": "string exported from module2.js"
  }
}

// indexAsParent_true
// In this case export from index.js was taken as a root object

indexAsParent_true: {
  "index_property": "value exported from index.js",
  "module1": {
    "module1_property": "value exported from module1.js"
  },
  "module2": {
    "module2_property": "string exported from module2.js"
  }
}

I.e. it always takes all the files, but places the properties of the object exported by index.js as a properties of root or 'parent' object (true) or inside a single property named index of that root or parent object (false).

alykoshin commented 7 years ago

As far as I understood, in your case you need to include only index.js files. There are following properties to control which files are processed and which one are not:

    includeFiles:  /^.*\.(js|json|coffee)$/,       // RegExp to select files; default value shown
    excludeDirs:   /^(\.git|\.svn|node_modules)$/  // RegExp to ignore subdirectories; default value shown
alykoshin commented 7 years ago

I need more details on your case.

If all files in subdirectories except index.js to be ignored, includeFiles is what you need.

But if you need at same time to ignore other files if index.js exists and include them if index.js does not exists, this is completely different task.

Usually I have one of following situations: 1) all the files are require-d by require-dir-all. 2) all the files are require-d by listing them index.js. 3) some parts are loaded by required-dir-all (for example, top-level index.js files or instead files at lowest level) and some part of files by manual list in index.js. I avoid situations when same files are simultaneously require-d globally by require-dir-all and also require-d by index.js files. You can always access the files require-d by require-dir-all by using properties of returned object.

Redsandro commented 7 years ago

I will get back to this after I set up a testcase for you to run.