Skip to content

Commit

Permalink
commit by 'npm run commit'
Browse files Browse the repository at this point in the history
  • Loading branch information
alykoshin committed Jul 28, 2015
1 parent 4305266 commit 795b1e9
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 16 deletions.
44 changes: 33 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,24 @@ Options:

Typical task is to run the function for each module required from the directory (like init or shutdown routines).
With this module it is needed to reqursively go through all the properties (i.e.module's exports)
and run the function for each of tehm
and run the function for each of them

If you need to wait until the end of initialization of all the modules, using ```async```
(assuming each module's initialize method accepts callback as a parameter).

Require'd file ```modules/module.js```
Please, be aware, that the methods below applicable only

Require'd files ```modules/module1.js``` and ```modules/module2.js```

```js
module.exports = {
var path = require('path'),
fileExt = path.extname(module.filename),
fileBase = path.basename(module.filename, fileExt);

module.exports = {
initialize: function(cb) {
return cb(false, 'some result');
console.log('module ' + fileBase + ' initialized');
return cb(false, 'result from '+fileBase);
}
};
```
Expand All @@ -96,15 +103,28 @@ var _ = require('lodash');
var async = require('async');
var modules = require('require-dir-all')('modules');

module.exports.initialize = function(cb) {
var initialize = function(callback) {
var initializers = [];

_.forOwn(modules, function(module) {
// for (var module in modules) { if (modules.hasOwnProperty(module)) {
initializers.push( function(cb) { return module.initialize(cb); } );
// } }
initializers.push( function(cb) { return module.initialize(cb); } );
});
async.parallel(initializers, cb);

async.parallel(initializers, callback);
};

initialize(function(err, results) {
console.log('initialize done; results:', results);
});

/*
Output:
module module1 initialized
module module2 initialized
initialize done; results: [ 'result from module1', 'result from module2' ]
*/

```

If you do not need to wait till the finish of initialization of both modules:
Expand All @@ -113,15 +133,17 @@ If you do not need to wait till the finish of initialization of both modules:
var _ = require('lodash');
var modules = require('require-dir-all')('modules');

module.exports.initialize = function(cb) {
module.exports.initialize = function() {
_.forOwn(modules, function(module) {
// for (var module in modules) { if (modules.hasOwnProperty(module)) {
module.initialize(cb); ;
module.initialize(); ;
// } }
});
};
```

See ```demo/initializers``` for an example

### Simple

If you need to require all the ```.js```, ```.json```, ```.coffee``` files in the directory ```modules```, add following line:
Expand Down
1 change: 1 addition & 0 deletions demo/initializers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Demo for npm package ```require-dir-all```
27 changes: 27 additions & 0 deletions demo/initializers/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// See README.md for details.

var _ = require('lodash');
var async = require('async');
var modules = require('require-dir-all')('modules');

var initialize = function(callback) {
var initializers = [];

_.forOwn(modules, function(module) {
initializers.push( function(cb) { return module.initialize(cb); } );
});

async.parallel(initializers, callback);
};

initialize(function(err, results) {
console.log('initialize done; results:', results);
});

/*
Output:
module module1 initialized
module module2 initialized
initialize done; results: [ 'result from module1', 'result from module2' ]
*/
10 changes: 10 additions & 0 deletions demo/initializers/modules/module1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var path = require('path'),
fileExt = path.extname(module.filename),
fileBase = path.basename(module.filename, fileExt);

module.exports = {
initialize: function(cb) {
console.log('module ' + fileBase + ' initialized');
return cb(false, 'result from '+fileBase+'');
}
};
10 changes: 10 additions & 0 deletions demo/initializers/modules/module2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var path = require('path'),
fileExt = path.extname(module.filename),
fileBase = path.basename(module.filename, fileExt);

module.exports = {
initialize: function(cb) {
console.log('module ' + fileBase + ' initialized');
return cb(false, 'result from '+fileBase);
}
};
17 changes: 17 additions & 0 deletions demo/initializers/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "initializers",
"version": "1.0.0",
"description": "Demo for npm package ```require-dir-all```",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "alykoshin <alykoshin@gmail.com>",
"license": "ISC",
"dependencies": {
"async": "^1.4.0",
"lodash": "^3.10.0",
"path": "^0.11.14",
"require-dir-all": "latest"
}
}
16 changes: 11 additions & 5 deletions demo/recursive/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,21 @@ modules: {
"module1": "string exported from module 1",
"module2": "string exported from module 2"
}
*/
/*

// Iterate through all the modules

var iterate = function(modules) {
for (m in modules) {
for (var m in modules) {
if (modules.hasOwnProperty(m)) {
if (typeof modules[m] === 'string') {
console.log('module:', m,'; exports:', modules[m]);
} else {
console.log('subdir:', m);
iterate (modules[m]);
}
}
}
};
iterate(modules);
*/

0 comments on commit 795b1e9

Please sign in to comment.