Ghost/core/config-loader.js
Adam Howard c5fa7ae1a6 Refactor the initial boot of Ghost, allowing Ghost updates to keep current configuration intact.
Extracts all express-server-related code in index.js to core/server.js, leaving index.js purely for booting up Ghost's core components in a sensible order.

Aside from the project's tidiness, this means that we can perform asynchronous configuration loading/checks before requiring any modules that read the config.
2013-09-14 14:04:08 +01:00

48 lines
1.5 KiB
JavaScript

var fs = require('fs'),
when = require('when');
function writeConfigFile() {
var written = when.defer();
/* Check for config file and copy from config.example.js
if one doesn't exist. After that, start the server. */
fs.exists('config.example.js', function checkTemplate(templateExists) {
var read,
write;
if (!templateExists) {
throw new Error('Could not locate a configuration file. Please check your deployment for config.js or config.example.js.');
}
// Copy config.example.js => config.js
read = fs.createReadStream('config.example.js');
read.on('error', function (err) {
throw new Error('Could not open config.example.js for read.');
});
read.on('end', written.resolve);
write = fs.createWriteStream('config.js');
write.on('error', function (err) {
throw new Error('Could not open config.js for write.');
});
read.pipe(write);
});
return written.promise;
}
exports.loadConfig = function () {
var loaded = when.defer();
/* Check for config file and copy from config.example.js
if one doesn't exist. After that, start the server. */
fs.exists('config.js', function checkConfig(configExists) {
if (configExists) {
loaded.resolve();
} else {
writeConfigFile().then(loaded.resolve).otherwise(loaded.reject);
}
});
return loaded.promise;
};