2013-12-06 18:13:15 +04:00
|
|
|
// General entry point for all configuration data
|
|
|
|
//
|
|
|
|
// This file itself is a wrapper for the root level config.js file.
|
|
|
|
// All other files that need to reference config.js should use this file.
|
2013-11-20 17:58:52 +04:00
|
|
|
|
2014-01-05 10:40:53 +04:00
|
|
|
var path = require('path'),
|
2014-08-17 10:17:23 +04:00
|
|
|
Promise = require('bluebird'),
|
2014-01-05 10:40:53 +04:00
|
|
|
url = require('url'),
|
|
|
|
_ = require('lodash'),
|
2014-05-15 07:28:10 +04:00
|
|
|
knex = require('knex'),
|
2014-02-28 23:52:32 +04:00
|
|
|
requireTree = require('../require-tree').readAll,
|
2013-12-06 18:13:15 +04:00
|
|
|
theme = require('./theme'),
|
2014-01-05 10:40:53 +04:00
|
|
|
configUrl = require('./url'),
|
|
|
|
ghostConfig = {},
|
|
|
|
appRoot = path.resolve(__dirname, '../../../'),
|
2014-05-15 07:28:10 +04:00
|
|
|
corePath = path.resolve(appRoot, 'core/'),
|
2014-07-17 18:33:21 +04:00
|
|
|
testingEnvs = ['testing', 'testing-mysql', 'testing-pg'],
|
|
|
|
defaultConfig = {},
|
2014-05-15 07:28:10 +04:00
|
|
|
knexInstance;
|
2014-01-05 10:40:53 +04:00
|
|
|
|
2014-02-20 07:22:02 +04:00
|
|
|
// Are we using sockets? Custom socket or the default?
|
|
|
|
function getSocket() {
|
|
|
|
if (ghostConfig.server.hasOwnProperty('socket')) {
|
|
|
|
return _.isString(ghostConfig.server.socket) ? ghostConfig.server.socket : path.join(ghostConfig.paths.contentPath, process.env.NODE_ENV + '.socket');
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-05 10:40:53 +04:00
|
|
|
function updateConfig(config) {
|
2014-07-17 18:33:21 +04:00
|
|
|
var localPath = '',
|
2014-01-05 10:40:53 +04:00
|
|
|
contentPath,
|
|
|
|
subdir;
|
|
|
|
|
|
|
|
// Merge passed in config object onto
|
|
|
|
// the cached ghostConfig object
|
|
|
|
_.merge(ghostConfig, config);
|
|
|
|
|
|
|
|
// Protect against accessing a non-existant object.
|
|
|
|
// This ensures there's always at least a paths object
|
|
|
|
// because it's referenced in multiple places.
|
|
|
|
ghostConfig.paths = ghostConfig.paths || {};
|
|
|
|
|
|
|
|
// Parse local path location
|
|
|
|
if (ghostConfig.url) {
|
|
|
|
localPath = url.parse(ghostConfig.url).path;
|
|
|
|
// Remove trailing slash
|
|
|
|
if (localPath !== '/') {
|
|
|
|
localPath = localPath.replace(/\/$/, '');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
subdir = localPath === '/' ? '' : localPath;
|
|
|
|
|
|
|
|
// Allow contentPath to be over-written by passed in config object
|
|
|
|
// Otherwise default to default content path location
|
|
|
|
contentPath = ghostConfig.paths.contentPath || path.resolve(appRoot, 'content');
|
|
|
|
|
2014-07-17 18:33:21 +04:00
|
|
|
if (!knexInstance && ghostConfig.database && ghostConfig.database.client) {
|
2014-05-15 07:28:10 +04:00
|
|
|
knexInstance = knex(ghostConfig.database);
|
|
|
|
}
|
|
|
|
|
2014-01-05 10:40:53 +04:00
|
|
|
_.merge(ghostConfig, {
|
2014-05-15 07:28:10 +04:00
|
|
|
database: {
|
|
|
|
knex: knexInstance
|
|
|
|
},
|
2014-01-05 10:40:53 +04:00
|
|
|
paths: {
|
|
|
|
'appRoot': appRoot,
|
|
|
|
'subdir': subdir,
|
2014-02-08 19:41:15 +04:00
|
|
|
'config': ghostConfig.paths.config || path.join(appRoot, 'config.js'),
|
2014-01-05 10:40:53 +04:00
|
|
|
'configExample': path.join(appRoot, 'config.example.js'),
|
|
|
|
'corePath': corePath,
|
|
|
|
|
|
|
|
'contentPath': contentPath,
|
|
|
|
'themePath': path.resolve(contentPath, 'themes'),
|
|
|
|
'appPath': path.resolve(contentPath, 'apps'),
|
|
|
|
'imagesPath': path.resolve(contentPath, 'images'),
|
|
|
|
'imagesRelPath': 'content/images',
|
|
|
|
|
|
|
|
'adminViews': path.join(corePath, '/server/views/'),
|
|
|
|
'helperTemplates': path.join(corePath, '/server/helpers/tpl/'),
|
|
|
|
'exportPath': path.join(corePath, '/server/data/export/'),
|
|
|
|
'lang': path.join(corePath, '/shared/lang/'),
|
|
|
|
'debugPath': subdir + '/ghost/debug/',
|
|
|
|
|
2014-07-17 18:33:21 +04:00
|
|
|
'availableThemes': ghostConfig.paths.availableThemes || {},
|
|
|
|
'availableApps': ghostConfig.paths.availableApps || {},
|
2014-01-05 10:40:53 +04:00
|
|
|
'builtScriptPath': path.join(corePath, 'built/scripts/')
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Also pass config object to
|
|
|
|
// configUrl object to maintain
|
|
|
|
// clean depedency tree
|
|
|
|
configUrl.setConfig(ghostConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
function initConfig(rawConfig) {
|
|
|
|
// Cache the config.js object's environment
|
|
|
|
// object so we can later refer to it.
|
|
|
|
// Note: this is not the entirety of config.js,
|
|
|
|
// just the object appropriate for this NODE_ENV
|
2014-07-17 18:33:21 +04:00
|
|
|
updateConfig(rawConfig);
|
2014-01-05 10:40:53 +04:00
|
|
|
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.all([requireTree(ghostConfig.paths.themePath), requireTree(ghostConfig.paths.appPath)]).then(function (paths) {
|
2014-01-05 10:40:53 +04:00
|
|
|
ghostConfig.paths.availableThemes = paths[0];
|
|
|
|
ghostConfig.paths.availableApps = paths[1];
|
|
|
|
return ghostConfig;
|
|
|
|
});
|
|
|
|
}
|
2013-11-20 17:58:52 +04:00
|
|
|
|
2014-07-17 18:33:21 +04:00
|
|
|
if (testingEnvs.indexOf(process.env.NODE_ENV) > -1) {
|
|
|
|
defaultConfig = require('../../../config.example')[process.env.NODE_ENV];
|
2013-11-26 07:22:59 +04:00
|
|
|
}
|
2013-11-20 17:58:52 +04:00
|
|
|
|
2014-07-17 18:33:21 +04:00
|
|
|
// Init config
|
|
|
|
updateConfig(defaultConfig);
|
|
|
|
|
|
|
|
module.exports = ghostConfig;
|
2014-01-05 10:40:53 +04:00
|
|
|
module.exports.init = initConfig;
|
|
|
|
module.exports.theme = theme;
|
2014-02-20 07:22:02 +04:00
|
|
|
module.exports.getSocket = getSocket;
|
2014-01-05 10:40:53 +04:00
|
|
|
module.exports.urlFor = configUrl.urlFor;
|
2014-02-27 06:44:09 +04:00
|
|
|
module.exports.urlForPost = configUrl.urlForPost;
|