From 6b575164dd6adf6389668f0104003b72fc0e97c8 Mon Sep 17 00:00:00 2001 From: Drew Fyock Date: Thu, 27 Mar 2014 09:37:21 -0400 Subject: [PATCH] ensure NODE_ENV is valid, default regex for walk --- config/config.js | 14 +++++++++++--- config/express.js | 9 ++++----- config/passport.js | 4 ++-- config/utilities.js | 5 +++-- karma.conf.js | 4 ++-- server.js | 7 +------ 6 files changed, 23 insertions(+), 20 deletions(-) diff --git a/config/config.js b/config/config.js index 2e63967481..1823cae41c 100644 --- a/config/config.js +++ b/config/config.js @@ -1,6 +1,14 @@ 'use strict'; -var _ = require('lodash'); +var _ = require('lodash'), + utilities = require('./utilities'); -// Load app configuration -module.exports = _.merge(require(__dirname + '/../config/env/all.js'), require(__dirname + '/../config/env/' + process.env.NODE_ENV + '.js') || {}); \ No newline at end of file +process.env.NODE_ENV = ~utilities.walk('./config/env', /(.*)\.js$/).map(function(file) { + return file.split('/').pop().slice(0, -3); +}).indexOf(process.env.NODE_ENV) ? process.env.NODE_ENV : 'development'; + +// Load app configurations +module.exports = _.extend( + require('./env/all'), + require('./env/' + process.env.NODE_ENV) || {} +); diff --git a/config/express.js b/config/express.js index 4b6f8b8127..d83ee8fb92 100755 --- a/config/express.js +++ b/config/express.js @@ -9,7 +9,6 @@ var express = require('express'), flash = require('connect-flash'), config = require('./config'), consolidate = require('consolidate'), - swig = require('swig'), path = require('path'), utilities = require('./utilities'); @@ -18,7 +17,7 @@ module.exports = function(db) { var app = express(); // Initialize models - utilities.walk('./app/models', /(.*)\.(js$|coffee$)/).forEach(function(modelPath) { + utilities.walk('./app/models').forEach(function(modelPath) { require(path.resolve(modelPath)); }); @@ -58,7 +57,7 @@ module.exports = function(db) { // Application Configuration for development environment app.configure('development', function() { - // Enable logger + // Enable logger app.use(express.logger('dev')); // Disable views cache @@ -106,7 +105,7 @@ module.exports = function(db) { app.use(express.static(config.root + '/public')); // Load Routes - utilities.walk('./app/routes', /(.*)\.(js$|coffee$)/).forEach(function(routePath) { + utilities.walk('./app/routes').forEach(function(routePath) { require(path.resolve(routePath))(app); }); @@ -133,4 +132,4 @@ module.exports = function(db) { }); return app; -}; \ No newline at end of file +}; diff --git a/config/passport.js b/config/passport.js index 9941f783ab..cbd25e6a90 100755 --- a/config/passport.js +++ b/config/passport.js @@ -21,7 +21,7 @@ module.exports = function() { }); // Initialize strategies - utilities.walk('./config/strategies', /(.*)\.(js$|coffee$)/).forEach(function(strategyPath) { + utilities.walk('./config/strategies').forEach(function(strategyPath) { require(path.resolve(strategyPath))(); }); -}; \ No newline at end of file +}; diff --git a/config/utilities.js b/config/utilities.js index b046ac5d55..cb6da59221 100644 --- a/config/utilities.js +++ b/config/utilities.js @@ -9,8 +9,9 @@ var fs = require('fs'); var _walk = function(root, includeRegex, excludeRegex, removePath) { var output = []; var directories = []; + includeRegex = includeRegex || /(.*)\.(js|coffee)$/; - // First read through files + // First read through files fs.readdirSync(root).forEach(function(file) { var newPath = root + '/' + file; var stat = fs.statSync(newPath); @@ -35,4 +36,4 @@ var _walk = function(root, includeRegex, excludeRegex, removePath) { /** * Exposing the walk function */ -exports.walk = _walk; \ No newline at end of file +exports.walk = _walk; diff --git a/karma.conf.js b/karma.conf.js index 302e63b48d..e4c06df54a 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -6,7 +6,7 @@ var utilities = require('./config/utilities'); // Grabbing module files using the walk function -var modulesJSFiles = utilities.walk('./public/modules', /(.*)\.(js)/, null, null); +var modulesJSFiles = utilities.walk('./public/modules', /(.*)\.js$/); // Karma configuration module.exports = function(config) { @@ -63,4 +63,4 @@ module.exports = function(config) { // If true, it capture browsers, run tests and exit singleRun: true }); -}; \ No newline at end of file +}; diff --git a/server.js b/server.js index 0339de2253..732e2ea9e1 100755 --- a/server.js +++ b/server.js @@ -1,10 +1,5 @@ 'use strict'; -/** - * First we set the node enviornment variable if not set before - */ -process.env.NODE_ENV = process.env.NODE_ENV || 'development'; - /** * Module dependencies. */ @@ -31,4 +26,4 @@ app.listen(config.port); exports = module.exports = app; // Logging initialization -console.log('Express app started on port ' + config.port); \ No newline at end of file +console.log('Express app started on port ' + config.port);