Skip to content

Commit

Permalink
feat(helpers): Implemented helpers for app
Browse files Browse the repository at this point in the history
  • Loading branch information
pilsy committed Jun 25, 2014
1 parent 968d6a1 commit 501d8e9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var cluster = require( 'cluster' )
, debug = require( 'debug' )( cluster.isMaster ? 'Cluster' : 'Worker' )
, debug = require( 'debug' )( cluster.isMaster ? 'Master' : 'Worker' )
, config = require( './config' )
, os = require( 'os' )
, numWorkers = config.numChildren ? config.numChildren : os.cpus()
Expand Down
51 changes: 51 additions & 0 deletions lib/utils/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
var os = require( 'os' )
, fs = require( 'fs' )
, path = require( 'path' )
, packageJson = require( path.resolve( path.join( __dirname, '..', '..', 'package.json' ) ) )
, isWin = /^win32/.test( os.platform() );

module.exports = {

/**
* Makes sure that the NODE_PATH is correctly set in the current process so that child processes
* have access to cleverstack modules as well as magic modules like require( 'config' )
*
* @return {String} Returns the path joined by the correct delimiter for the operating system
*/
nodePath: function() {
var delimiter = isWin ? ';' : ':'
, currentPaths = process.env.NODE_PATH ? process.env.NODE_PATH.split( delimiter ) : []
, appRoot = path.resolve( path.join( __dirname, '..', '..' ) )
, paths = [ path.join( appRoot, 'lib' ), path.join( appRoot, 'modules' ) ];

currentPaths.forEach( function( _path ) {
if ( !/lib(\/|\\)?$|modules(\/|\\)?$/.test( _path ) ) {
paths.push( _path );
}
});

return paths.join( delimiter );
},

/**
* Helper function that will load a file based on its name from every enabled module in your application
*
* @param {String} fileName Name of the file you want to load
* @param {Object} config Applications config object
* @param {Object} cluster NodeJS Cluster module
* @param {Function} debug Function that can be called with debugging information
*
* @return {null}
*/
loadModulesFileByName: function( fileName, config, cluster, debug ) {
packageJson.bundledDependencies.forEach(function( moduleName ) {
var file = path.resolve( path.join( __dirname, '..', '..', 'modules', moduleName, 'bin', 'app.js' ) );
if ( fs.existsSync( file ) ) {

debug( 'Loading ' + fileName + ' from ' + moduleName );
require( file )( cluster, config, packageJson, debug );

}
});
}
}

0 comments on commit 501d8e9

Please sign in to comment.