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');
|
2021-06-15 19:01:22 +03:00
|
|
|
const _debug = require('@tryghost/debug')._base;
|
2020-04-29 18:44:27 +03:00
|
|
|
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
|
|
|
|
2021-06-16 14:03:56 +03:00
|
|
|
// ## Load Config
|
|
|
|
|
|
|
|
// no channel can override the overrides
|
2017-02-02 15:46:30 +03:00
|
|
|
nconf.file('overrides', path.join(baseConfigPath, 'overrides.json'));
|
|
|
|
|
2021-06-16 14:03:56 +03:00
|
|
|
// command line arguments take precedence, then environment variables
|
2017-02-02 15:46:30 +03:00
|
|
|
nconf.argv();
|
2021-06-16 14:03:56 +03:00
|
|
|
nconf.env({separator: '__', parseValues: true});
|
2017-02-02 15:46:30 +03:00
|
|
|
|
2021-06-16 14:03:56 +03:00
|
|
|
// Now load various config json files
|
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'));
|
2021-06-16 14:03:56 +03:00
|
|
|
|
|
|
|
// Finally, we load defaults, if nothing else has a value this will
|
2017-02-02 15:46:30 +03:00
|
|
|
nconf.file('defaults', path.join(baseConfigPath, 'defaults.json'));
|
|
|
|
|
2021-06-16 14:03:56 +03:00
|
|
|
// ## Config Methods
|
|
|
|
|
|
|
|
// Bind internal-only methods, not sure this is needed
|
2017-02-02 15:46:30 +03:00
|
|
|
nconf.makePathsAbsolute = localUtils.makePathsAbsolute.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);
|
2021-06-16 14:03:56 +03:00
|
|
|
nconf.checkUrlProtocol = localUtils.checkUrlProtocol.bind(nconf);
|
2017-02-02 15:46:30 +03:00
|
|
|
|
2021-06-16 14:03:56 +03:00
|
|
|
// Expose dynamic utility methods
|
|
|
|
nconf.isPrivacyDisabled = localUtils.isPrivacyDisabled.bind(nconf);
|
|
|
|
nconf.getContentPath = localUtils.getContentPath.bind(nconf);
|
|
|
|
|
|
|
|
// ## Sanitization
|
|
|
|
|
|
|
|
// transform all relative paths to absolute paths
|
2017-02-02 15:46:30 +03:00
|
|
|
nconf.makePathsAbsolute(nconf.get('paths'), 'paths');
|
2021-06-16 14:03:56 +03:00
|
|
|
|
|
|
|
// transform sqlite filename path for Ghost-CLI
|
|
|
|
nconf.sanitizeDatabaseProperties();
|
|
|
|
|
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
|
|
|
}
|
2021-06-16 14:03:56 +03:00
|
|
|
|
|
|
|
// Check if the URL in config has a protocol
|
2017-05-21 19:00:11 +03:00
|
|
|
nconf.checkUrlProtocol();
|
|
|
|
|
2021-06-16 14:03:56 +03:00
|
|
|
// Ensure that the content path exists
|
2017-06-07 12:31:01 +03:00
|
|
|
nconf.doesContentPathExist();
|
|
|
|
|
2021-06-16 14:03:56 +03:00
|
|
|
// ## Other Stuff!
|
|
|
|
|
|
|
|
// Manually set values
|
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;
|