2013-09-24 07:37:36 +04:00
var fs = require ( 'fs' ) ,
url = require ( 'url' ) ,
when = require ( 'when' ) ,
2013-11-20 17:58:52 +04:00
errors = require ( '../errorHandling' ) ,
2013-10-10 14:44:31 +04:00
path = require ( 'path' ) ,
2013-11-26 07:22:59 +04:00
paths = require ( './paths' ) ,
2013-10-10 14:44:31 +04:00
2013-11-26 07:22:59 +04:00
appRoot = paths ( ) . appRoot ,
configexample = paths ( ) . configExample ,
2013-10-16 10:38:30 +04:00
configFile = process . env . GHOST _CONFIG || paths ( ) . config ;
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-31 22:02:34 +04:00
/*jslint unparam:true*/
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-16 10:38:30 +04:00
write = fs . createWriteStream ( configFile ) ;
2013-09-06 19:54:50 +04:00
write . on ( 'error' , function ( err ) {
2013-10-31 22:02:34 +04:00
/*jslint unparam:true*/
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 {
2013-10-16 10:38:30 +04:00
config = require ( configFile ) [ envVal ] ;
2013-09-24 07:37:36 +04:00
} catch ( ignore ) {
}
// 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 ( ) ;
}
2013-11-26 07:22:59 +04:00
return when . resolve ( config ) ;
2013-09-24 07:37:36 +04:00
}
2013-11-20 17:58:52 +04:00
function loadConfig ( ) {
2013-11-26 07:22:59 +04:00
var loaded = when . defer ( ) ,
pendingConfig ;
2013-09-06 19:54:50 +04:00
/ * 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-16 10:38:30 +04:00
fs . exists ( configFile , function checkConfig ( configExists ) {
2013-11-26 07:22:59 +04:00
if ( ! configExists ) {
pendingConfig = writeConfigFile ( ) ;
2013-09-06 19:54:50 +04:00
}
2013-11-26 07:22:59 +04:00
when ( pendingConfig ) . then ( validateConfigEnvironment ) . then ( loaded . resolve ) . otherwise ( loaded . reject ) ;
2013-09-06 19:54:50 +04:00
} ) ;
return loaded . promise ;
2013-11-20 17:58:52 +04:00
}
module . exports = loadConfig ;