support fs.readdir and fs.readdirSync
- Dominant language
- JavaScript
- Stars
- 556
- Forks
- 58
- PR merge metrics
- No merged PRs in 30d
Description
I think this is feasible, and I personally think this is really useful.
In node.js it's reasonably common for me to do things like:
``` javascript
// controllers/index.js
'use strict';
var fs = require('fs');
var path = require('path');
var ROOT_PATH = __dirname;
module.exports = function(path) {
var fnames = fs.readdirSync(ROOT_PATH);
var paths = fnames.map(function(fname) {
return path.join(ROOT_PATH, fname, '.js');
});
var modules = paths.map(function(path) {
return require(path);
});
return modules.map(function(module) {
// do something with the module
// i.e. if it's this is a mongoose schema bootstrapping utility, register the model.
// if this is a express controller bootstrapping utility, parse its methods and register
// matching routes.
// etc.
});
};
```
This allows us to abstract whole parts of the code, later on - even treat them as completely separate modules:
``` javascript
// app.js
var controllers = require('./controllers')
// bootstrap the controllers
controllers(/* something would come here such as an express app or a mongoose.Connection */);
```
If we could do this with browserify, things like bootstrapping an Ember app, could be automated. I know there are a couple of problems.
[Here's an analogous example that works](https://gist.github.com/yamadapc/9976164), though not exactly as one would hope it'd.
I know this has to do with limitations on the browserify module (which may be impossible to address) but this would be **really** cool.
Perhaps we could even parse things like:
``` javascript
['./a', './b'].map(function(name) {
return require(name);
});
```
Into:
``` javascript
[require('./a'), require('./b')];
```
Or provide an utility `requireMap` thing that resolved into that.
I don't know if this is a relevant issue. But I though this would be a nice thing, if it was possible.
At it's core, the main point was simply to add:
``` javascript
var fs = require('fs');
var files = fs.readdirSync('.');
```
To be transformed into:
``` javascript
var fs = require('fs');
var files = ['a.js', 'b.js', 'main.js' /* etc. */];
```
What do you think?
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.