Skip to content

Commit

Permalink
Reworked module loader, class and hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
pilsy committed Jun 4, 2014
1 parent 80f9a98 commit 742b503
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 27 deletions.
14 changes: 7 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ env.app.configure(function() {
env.app.use( env.express.compress() );
env.app.use( env.express.favicon() );
env.app.use( env.express.methodOverride() );

// Allow cross-origin requests
env.app.use( cors( env.config.cors ) );
});

// Load all the modules
Expand All @@ -23,26 +26,23 @@ env.moduleLoader.initializeRoutes( injector );
// Add middleware that needs to come after routes
env.app.configure(function() {

// Allow cross-origin requests
env.app.use( cors( env.config.cors ) );

// Attach our router
env.app.use( env.app.router );

// error handler, outputs json since that's usually
// what comes out of this thing
env.app.use(function( err, req, res, next ) {
console.log('Express error catch', err);
res.json(500, {
console.log( 'Express error catch', err );
res.json( 500, {
error: err.toString()
});
});
});

// Listen for requests
env.app.listen( env.webPort, function() {
console.log("Starting server on port " + env.webPort + " in " + env.config.environmentName + " mode");
console.log( "Starting server on port " + env.webPort + " in " + env.config.environmentName + " mode" );
});

// Export the Express app
module.exports = env.app;
module.exports = env.app;
13 changes: 1 addition & 12 deletions lib/classes/ModuleClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ var Class = require( 'uberclass' )
, fs = require( 'fs' )
, debug = require( 'debug' )( 'Modules' )
, config = injector.getInstance( 'config' )
, app = injector.getInstance( 'app' )
, express = injector.getInstance( 'express' )
, moduleLoader = injector.getInstance( 'moduleLoader' );

module.exports = Class.extend(
Expand Down Expand Up @@ -51,12 +49,6 @@ module.exports = Class.extend(
this.config = typeof config[ name ] === 'object'
? config[ name ]
: {};

// Allow the module to configure the express app
if ( typeof this.configureApp === 'function' ) {
debug( 'configureApp hook called for module ' + this.name );
app.configure( this.proxy( 'configureApp', app, express ) );
}

// Set the modules location
this.modulePath = [ path.dirname( path.dirname( __dirname ) ), 'modules', this.name ].join( path.sep );
Expand All @@ -67,10 +59,7 @@ module.exports = Class.extend(
// Add our moduleFolders to the list of paths, and our injector paths
this.Class.moduleFolders.forEach( this.proxy( 'addFolderToPath', injector ) );

this.hook( 'preResources' );

this.loadResources();

// Fire the preInit hook
this.hook( 'preInit' );
},

Expand Down
38 changes: 32 additions & 6 deletions lib/utils/moduleLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,16 @@ var Module = module.exports = Class.extend(
debug( 'Loading modules...' );

// Load each of our modules
packageJson.bundledDependencies.forEach( function ( dep ) {
self.loadModule( dep, env );
} );
packageJson.bundledDependencies.forEach( this.proxy( 'loadModule', env ) );

// Fire the configureApp hook on each of our modules giving them a chance to configure the express app
this.modules.forEach( this.proxy( 'configureAppHook' ) );

// Load each modules resources
this.modules.forEach( this.proxy( 'preResourcesHook' ) );

// Load each modules resources
this.modules.forEach( this.proxy( 'loadModuleResources' ) );

// Fire the modulesLoadedHook hook on each of our modules letting them know that all modules have finished loading
this.modules.forEach( this.proxy( 'modulesLoadedHook' ) );
Expand All @@ -69,7 +76,7 @@ var Module = module.exports = Class.extend(
}
},

loadModule: function( moduleName, env ) {
loadModule: function( env, moduleName ) {
if (typeof env !== "undefined" && env !== null) {
process.env = env;
}
Expand All @@ -83,10 +90,29 @@ var Module = module.exports = Class.extend(
this.modules.push( require( moduleName ) );
},

configureAppHook: function( module ) {
if ( module instanceof moduleClass && typeof module.configureApp === 'function' ) {
debug( 'configureApp hook called for module ' + module.name );
injector.getInstance( 'app' ).configure( module.proxy( 'configureApp', injector.getInstance( 'app' ), injector.getInstance( 'express' ) ) );
}
},

preResourcesHook: function( module ) {
if ( module instanceof moduleClass && typeof module.preResources === 'function' ) {
module.hook( 'preResources' );
}
},

loadModuleResources: function( module ) {
if ( module instanceof moduleClass && typeof module.loadResources === 'function' ) {
debug( [ 'loadResources for module', module.name ].join( ' ' ) );
module.loadResources();
}
},

modulesLoadedHook: function( module ) {
if ( module instanceof moduleClass && typeof module.modulesLoaded === 'function' ) {
debug( [ 'modulesLoaded (hook) for module', module.name ].join( ' ' ) );
module.modulesLoaded();
module.hook( 'modulesLoaded' );
}
},

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"main": "app.js",
"name": "node-seed",
"description": "Cleverstack Node-Seed",
"version": "0.1.3",
"version": "0.1.4",
"author": {
"name": "CleverStack",
"email": "[email protected]",
Expand Down Expand Up @@ -53,4 +53,4 @@
"should": "~2.1.1"
},
"bundledDependencies": []
}
}

0 comments on commit 742b503

Please sign in to comment.