support fs.readdir and fs.readdirSync
See original GitHub issueI 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:
// 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:
// 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, 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:
['./a', './b'].map(function(name) {
return require(name);
});
Into:
[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:
var fs = require('fs');
var files = fs.readdirSync('.');
To be transformed into:
var fs = require('fs');
var files = ['a.js', 'b.js', 'main.js' /* etc. */];
What do you think?
Issue Analytics
- State:
- Created 9 years ago
- Comments:9
Top GitHub Comments
it would be cool if there was some way this could work:
@callumlocke finally made it and working fine! https://github.com/cancerberoSgx/br-fs-to-json packing a glob of files is what I always wanted and since you will be requiring a “strange” library fs-to-json I make sure it doesn’t contaminate your project with its dependencies. Much more practical than brfs IMO