2020-04-29 18:44:27 +03:00
|
|
|
const Nconf = require('nconf');
|
2021-02-19 10:53:42 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
const os = require('os');
|
2020-04-29 18:44:27 +03:00
|
|
|
const path = require('path');
|
|
|
|
const _debug = require('ghost-ignition').debug._base;
|
|
|
|
const debug = _debug('ghost:config');
|
|
|
|
const localUtils = require('./utils');
|
|
|
|
const env = process.env.NODE_ENV || 'development';
|
|
|
|
const _private = {};
|
2017-02-02 15:46:30 +03:00
|
|
|
|
|
|
|
_private.loadNconf = function loadNconf(options) {
|
2017-02-17 18:27:02 +03:00
|
|
|
debug('config start');
|
2017-02-02 15:46:30 +03:00
|
|
|
options = options || {};
|
|
|
|
|
2020-04-29 18:44:27 +03:00
|
|
|
const baseConfigPath = options.baseConfigPath || __dirname;
|
|
|
|
const customConfigPath = options.customConfigPath || process.cwd();
|
|
|
|
const nconf = new Nconf.Provider();
|
2017-02-02 15:46:30 +03:00
|
|
|
|
|
|
|
/**
|
2020-04-29 18:44:27 +03:00
|
|
|
* no channel can override the overrides
|
|
|
|
*/
|
2017-02-02 15:46:30 +03:00
|
|
|
nconf.file('overrides', path.join(baseConfigPath, 'overrides.json'));
|
|
|
|
|
|
|
|
/**
|
2020-04-29 18:44:27 +03:00
|
|
|
* command line arguments
|
|
|
|
*/
|
2017-02-02 15:46:30 +03:00
|
|
|
nconf.argv();
|
|
|
|
|
|
|
|
/**
|
2020-04-29 18:44:27 +03:00
|
|
|
* env arguments
|
|
|
|
*/
|
2017-02-02 15:46:30 +03:00
|
|
|
nconf.env({
|
2018-10-28 22:06:44 +03:00
|
|
|
separator: '__',
|
|
|
|
parseValues: true
|
2017-02-02 15:46:30 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
nconf.file('custom-env', path.join(customConfigPath, 'config.' + env + '.json'));
|
2020-04-29 20:52:59 +03:00
|
|
|
if (env !== 'testing') {
|
|
|
|
nconf.file('local-env', path.join(customConfigPath, 'config.local.json'));
|
|
|
|
}
|
2017-02-02 15:46:30 +03:00
|
|
|
nconf.file('default-env', path.join(baseConfigPath, 'env', 'config.' + env + '.json'));
|
|
|
|
nconf.file('defaults', path.join(baseConfigPath, 'defaults.json'));
|
|
|
|
|
|
|
|
/**
|
2020-04-29 18:44:27 +03:00
|
|
|
* transform all relative paths to absolute paths
|
|
|
|
* transform sqlite filename path for Ghost-CLI
|
|
|
|
*/
|
2017-02-02 15:46:30 +03:00
|
|
|
nconf.makePathsAbsolute = localUtils.makePathsAbsolute.bind(nconf);
|
|
|
|
nconf.isPrivacyDisabled = localUtils.isPrivacyDisabled.bind(nconf);
|
|
|
|
nconf.getContentPath = localUtils.getContentPath.bind(nconf);
|
2017-02-11 21:02:12 +03:00
|
|
|
nconf.sanitizeDatabaseProperties = localUtils.sanitizeDatabaseProperties.bind(nconf);
|
2017-06-07 12:31:01 +03:00
|
|
|
nconf.doesContentPathExist = localUtils.doesContentPathExist.bind(nconf);
|
2017-02-02 15:46:30 +03:00
|
|
|
|
2017-02-11 21:02:12 +03:00
|
|
|
nconf.sanitizeDatabaseProperties();
|
2017-02-02 15:46:30 +03:00
|
|
|
nconf.makePathsAbsolute(nconf.get('paths'), 'paths');
|
2019-03-06 12:28:10 +03:00
|
|
|
if (nconf.get('database:client') === 'sqlite3') {
|
|
|
|
nconf.makePathsAbsolute(nconf.get('database:connection'), 'database:connection');
|
2021-02-19 10:53:42 +03:00
|
|
|
|
|
|
|
// In the default SQLite test config we set the path to /tmp/ghost-test.db,
|
|
|
|
// but this won't work on Windows, so we need to replace the /tmp bit with
|
|
|
|
// the Windows temp folder
|
|
|
|
const filename = nconf.get('database:connection:filename');
|
|
|
|
if (_.isString(filename) && filename.match(/^\/tmp/)) {
|
|
|
|
nconf.set('database:connection:filename', filename.replace(/^\/tmp/, os.tmpdir()));
|
|
|
|
}
|
2019-03-06 12:28:10 +03:00
|
|
|
}
|
2017-05-21 19:00:11 +03:00
|
|
|
/**
|
2020-04-29 18:44:27 +03:00
|
|
|
* Check if the URL in config has a protocol
|
|
|
|
*/
|
2017-05-21 19:00:11 +03:00
|
|
|
nconf.checkUrlProtocol = localUtils.checkUrlProtocol.bind(nconf);
|
|
|
|
nconf.checkUrlProtocol();
|
|
|
|
|
2017-06-07 12:31:01 +03:00
|
|
|
/**
|
2020-04-29 18:44:27 +03:00
|
|
|
* Ensure that the content path exists
|
|
|
|
*/
|
2017-06-07 12:31:01 +03:00
|
|
|
nconf.doesContentPathExist();
|
|
|
|
|
2017-02-02 15:46:30 +03:00
|
|
|
/**
|
2020-04-29 18:44:27 +03:00
|
|
|
* values we have to set manual
|
|
|
|
*/
|
2017-02-02 15:46:30 +03:00
|
|
|
nconf.set('env', env);
|
|
|
|
|
2017-02-17 18:27:02 +03:00
|
|
|
// Wrap this in a check, because else nconf.get() is executed unnecessarily
|
|
|
|
// To output this, use DEBUG=ghost:*,ghost-config
|
|
|
|
if (_debug.enabled('ghost-config')) {
|
|
|
|
debug(nconf.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
debug('config end');
|
2017-02-02 15:46:30 +03:00
|
|
|
return nconf;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = _private.loadNconf();
|
|
|
|
module.exports.loadNconf = _private.loadNconf;
|