2020-04-29 18:44:27 +03:00
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs-extra');
|
2021-06-18 23:19:16 +03:00
|
|
|
const os = require('os');
|
2020-04-29 18:44:27 +03:00
|
|
|
const _ = require('lodash');
|
2016-09-13 18:20:44 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* transform all relative paths to absolute paths
|
2016-10-18 11:04:44 +03:00
|
|
|
* @TODO: re-write this function a little bit so we don't have to add the parent path - that is hard to understand
|
|
|
|
*
|
|
|
|
* Path must be string.
|
|
|
|
* Path must match minimum one / or \
|
|
|
|
* Path can be a "." to re-present current folder
|
2016-09-13 18:20:44 +03:00
|
|
|
*/
|
2021-06-18 23:19:16 +03:00
|
|
|
const makePathsAbsolute = function makePathsAbsolute(nconf, obj, parent) {
|
2016-10-18 11:04:44 +03:00
|
|
|
_.each(obj, function (configValue, pathsKey) {
|
2016-09-13 18:20:44 +03:00
|
|
|
if (_.isObject(configValue)) {
|
2021-06-18 23:19:16 +03:00
|
|
|
makePathsAbsolute(nconf, configValue, parent + ':' + pathsKey);
|
2017-07-06 01:04:18 +03:00
|
|
|
} else if (
|
|
|
|
_.isString(configValue) &&
|
|
|
|
(configValue.match(/\/+|\\+/) || configValue === '.') &&
|
|
|
|
!path.isAbsolute(configValue)
|
|
|
|
) {
|
2021-06-18 23:19:16 +03:00
|
|
|
nconf.set(parent + ':' + pathsKey, path.normalize(path.join(__dirname, '../../..', configValue)));
|
2016-09-13 18:20:44 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2016-09-13 23:24:57 +03:00
|
|
|
|
2021-06-18 23:19:16 +03:00
|
|
|
const doesContentPathExist = function doesContentPathExist(contentPath) {
|
|
|
|
if (!fs.pathExistsSync(contentPath)) {
|
2021-06-16 17:05:51 +03:00
|
|
|
// new Error is allowed here, as we do not want config to depend on @tryghost/error
|
|
|
|
// @TODO: revisit this decision when @tryghost/error is no longer dependent on all of ghost-ignition
|
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
2017-06-07 12:31:01 +03:00
|
|
|
throw new Error('Your content path does not exist! Please double check `paths.contentPath` in your custom config file e.g. config.production.json.');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-05-21 19:00:11 +03:00
|
|
|
/**
|
|
|
|
* Check if the URL in config has a protocol and sanitise it if not including a warning that it should be changed
|
|
|
|
*/
|
2021-06-18 23:19:16 +03:00
|
|
|
const checkUrlProtocol = function checkUrlProtocol(url) {
|
2017-05-21 19:00:11 +03:00
|
|
|
if (!url.match(/^https?:\/\//i)) {
|
2021-06-16 17:05:51 +03:00
|
|
|
// new Error is allowed here, as we do not want config to depend on @tryghost/error
|
|
|
|
// @TODO: revisit this decision when @tryghost/error is no longer dependent on all of ghost-ignition
|
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
2017-05-21 19:00:11 +03:00
|
|
|
throw new Error('URL in config must be provided with protocol, eg. "http://my-ghost-blog.com"');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-02-11 21:02:12 +03:00
|
|
|
/**
|
|
|
|
* nconf merges all database keys together and this can be confusing
|
|
|
|
* e.g. production default database is sqlite, but you override the configuration with mysql
|
|
|
|
*
|
|
|
|
* this.clear('key') does not work
|
|
|
|
* https://github.com/indexzero/nconf/issues/235#issuecomment-257606507
|
|
|
|
*/
|
2021-06-18 23:19:16 +03:00
|
|
|
const sanitizeDatabaseProperties = function sanitizeDatabaseProperties(nconf) {
|
2022-03-02 18:13:03 +03:00
|
|
|
if (nconf.get('database:client') === 'mysql') {
|
|
|
|
nconf.set('database:client', 'mysql2');
|
|
|
|
}
|
|
|
|
|
2021-06-18 23:19:16 +03:00
|
|
|
const database = nconf.get('database');
|
2017-02-11 21:02:12 +03:00
|
|
|
|
2022-03-02 18:13:03 +03:00
|
|
|
if (nconf.get('database:client') === 'mysql2') {
|
2017-02-11 21:02:12 +03:00
|
|
|
delete database.connection.filename;
|
|
|
|
} else {
|
|
|
|
delete database.connection.host;
|
|
|
|
delete database.connection.user;
|
|
|
|
delete database.connection.password;
|
|
|
|
delete database.connection.database;
|
|
|
|
}
|
|
|
|
|
2021-06-18 23:19:16 +03:00
|
|
|
nconf.set('database', database);
|
|
|
|
|
|
|
|
if (nconf.get('database:client') === 'sqlite3') {
|
|
|
|
makePathsAbsolute(nconf, nconf.get('database:connection'), 'database:connection');
|
|
|
|
|
|
|
|
// 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()));
|
|
|
|
}
|
|
|
|
}
|
2017-02-11 21:02:12 +03:00
|
|
|
};
|
2021-06-16 17:05:51 +03:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
makePathsAbsolute,
|
|
|
|
doesContentPathExist,
|
|
|
|
checkUrlProtocol,
|
|
|
|
sanitizeDatabaseProperties
|
|
|
|
};
|