2013-09-24 07:37:36 +04:00
var fs = require ( 'fs' ) ,
url = require ( 'url' ) ,
when = require ( 'when' ) ,
2013-10-10 14:44:31 +04:00
errors = require ( './server/errorHandling' ) ,
path = require ( 'path' ) ,
appRoot = path . resolve ( _ _dirname , '../' ) ,
configexample = path . join ( appRoot , 'config.example.js' ) ,
config = path . join ( appRoot , 'config.js' ) ;
2013-09-06 19:54:50 +04:00
function writeConfigFile ( ) {
var written = when . defer ( ) ;
/ * C h e c k f o r c o n f i g f i l e a n d c o p y f r o m c o n f i g . e x a m p l e . j s
if one doesn ' t exist . After that , start the server . * /
2013-10-10 14:44:31 +04:00
fs . exists ( configexample , function checkTemplate ( templateExists ) {
2013-09-06 19:54:50 +04:00
var read ,
write ;
if ( ! templateExists ) {
2013-10-10 14:44:31 +04:00
return errors . logError ( new Error ( 'Could not locate a configuration file.' ) , appRoot , 'Please check your deployment for config.js or config.example.js.' ) ;
2013-09-06 19:54:50 +04:00
}
// Copy config.example.js => config.js
2013-10-10 14:44:31 +04:00
read = fs . createReadStream ( configexample ) ;
2013-09-06 19:54:50 +04:00
read . on ( 'error' , function ( err ) {
2013-10-10 14:44:31 +04:00
return errors . logError ( new Error ( 'Could not open config.example.js for read.' ) , appRoot , 'Please check your deployment for config.js or config.example.js.' ) ;
2013-09-06 19:54:50 +04:00
} ) ;
read . on ( 'end' , written . resolve ) ;
2013-10-10 14:44:31 +04:00
write = fs . createWriteStream ( config ) ;
2013-09-06 19:54:50 +04:00
write . on ( 'error' , function ( err ) {
2013-10-10 14:44:31 +04:00
return errors . logError ( new Error ( 'Could not open config.js for write.' ) , appRoot , 'Please check your deployment for config.js or config.example.js.' ) ;
2013-09-06 19:54:50 +04:00
} ) ;
read . pipe ( write ) ;
} ) ;
return written . promise ;
}
2013-09-24 07:37:36 +04:00
function validateConfigEnvironment ( ) {
var envVal = process . env . NODE _ENV || 'undefined' ,
2013-10-10 19:11:35 +04:00
hasHostAndPort ,
hasSocket ,
2013-09-24 07:37:36 +04:00
config ,
parsedUrl ;
try {
config = require ( '../config' ) [ envVal ] ;
} catch ( ignore ) {
}
2013-10-10 19:11:35 +04:00
2013-09-24 07:37:36 +04:00
// Check if we don't even have a config
if ( ! config ) {
errors . logError ( new Error ( 'Cannot find the configuration for the current NODE_ENV' ) , "NODE_ENV=" + envVal , 'Ensure your config.js has a section for the current NODE_ENV value' ) ;
return when . reject ( ) ;
}
// Check that our url is valid
2013-10-14 18:42:08 +04:00
parsedUrl = url . parse ( config . url || 'invalid' , false , true ) ;
if ( ! parsedUrl . host ) {
2013-09-24 07:37:36 +04:00
errors . logError ( new Error ( 'Your site url in config.js is invalid.' ) , config . url , 'Please make sure this is a valid url before restarting' ) ;
return when . reject ( ) ;
}
// Check that we have database values
if ( ! config . database ) {
errors . logError ( new Error ( 'Your database configuration in config.js is invalid.' ) , JSON . stringify ( config . database ) , 'Please make sure this is a valid Bookshelf database configuration' ) ;
return when . reject ( ) ;
}
2013-10-10 19:11:35 +04:00
hasHostAndPort = config . server && ! ! config . server . host && ! ! config . server . port ;
hasSocket = config . server && ! ! config . server . socket ;
2013-09-24 07:37:36 +04:00
// Check for valid server host and port values
2013-10-10 19:11:35 +04:00
if ( ! config . server || ! ( hasHostAndPort || hasSocket ) ) {
errors . logError ( new Error ( 'Your server values (socket, or host and port) in config.js are invalid.' ) , JSON . stringify ( config . server ) , 'Please provide them before restarting.' ) ;
2013-09-24 07:37:36 +04:00
return when . reject ( ) ;
}
return when . resolve ( ) ;
}
2013-09-06 19:54:50 +04:00
exports . loadConfig = function ( ) {
var loaded = when . defer ( ) ;
/ * C h e c k f o r c o n f i g f i l e a n d c o p y f r o m c o n f i g . e x a m p l e . j s
if one doesn ' t exist . After that , start the server . * /
2013-10-10 14:44:31 +04:00
fs . exists ( config , function checkConfig ( configExists ) {
2013-09-06 19:54:50 +04:00
if ( configExists ) {
2013-09-24 07:37:36 +04:00
validateConfigEnvironment ( ) . then ( loaded . resolve ) . otherwise ( loaded . reject ) ;
2013-09-06 19:54:50 +04:00
} else {
2013-09-24 07:37:36 +04:00
writeConfigFile ( ) . then ( validateConfigEnvironment ) . then ( loaded . resolve ) . otherwise ( loaded . reject ) ;
2013-09-06 19:54:50 +04:00
}
} ) ;
return loaded . promise ;
} ;