Moved all settings module bootstrap logic into init method

refs refs https://linear.app/tryghost/issue/CORE-35/refactor-route-and-redirect-settings

- The initialization outside the init method is causing problems when the configs are mocked during the test setup
This commit is contained in:
Naz 2021-11-23 12:38:49 +04:00 committed by naz
parent cbec6aa49e
commit 2fed8dbbd5
2 changed files with 40 additions and 22 deletions

View File

@ -1,37 +1,54 @@
const RouteSettings = require('./route-settings');
const SettingsLoader = require('./settings-loader');
const config = require('../../../shared/config');
const parseYaml = require('./yaml-parser');
const DefaultSettingsManager = require('./default-settings-manager');
const defaultSettingsManager = new DefaultSettingsManager({
type: 'routes',
extension: '.yaml',
destinationFolderPath: config.getContentPath('settings'),
sourceFolderPath: config.get('paths').defaultSettings
});
const settingsLoader = new SettingsLoader({
parseYaml,
storageFolderPath: config.getContentPath('settings')
});
const routeSettings = new RouteSettings();
let settingsLoader;
let routeSettings;
module.exports = {
init: async () => {
const RouteSettings = require('./route-settings');
const SettingsLoader = require('./settings-loader');
const DefaultSettingsManager = require('./default-settings-manager');
routeSettings = new RouteSettings();
const defaultSettingsManager = new DefaultSettingsManager({
type: 'routes',
extension: '.yaml',
destinationFolderPath: config.getContentPath('settings'),
sourceFolderPath: config.get('paths').defaultSettings
});
settingsLoader = new SettingsLoader({
parseYaml,
storageFolderPath: config.getContentPath('settings')
});
return await defaultSettingsManager.ensureSettingsFileExists();
},
loadRouteSettingsSync: settingsLoader.loadSettingsSync.bind(settingsLoader),
loadRouteSettings: settingsLoader.loadSettings.bind(settingsLoader),
getDefaultHash: routeSettings.getDefaultHash.bind(routeSettings),
get loadRouteSettingsSync() {
return settingsLoader.loadSettingsSync.bind(settingsLoader);
},
get loadRouteSettings() {
return settingsLoader.loadSettings.bind(settingsLoader);
},
get getDefaultHash() {
return routeSettings.getDefaultHash.bind(routeSettings);
},
/**
* Methods used in the API
*/
api: {
setFromFilePath: routeSettings.setFromFilePath.bind(routeSettings),
get: routeSettings.get.bind(routeSettings),
getCurrentHash: routeSettings.getCurrentHash.bind(routeSettings)
get setFromFilePath() {
return routeSettings.setFromFilePath.bind(routeSettings);
},
get get() {
return routeSettings.get.bind(routeSettings);
},
get getCurrentHash() {
return routeSettings.getCurrentHash.bind(routeSettings);
}
}
};

View File

@ -11,6 +11,7 @@ const defaultSettings = require('../../../../../core/server/data/schema/default-
// Routes are yaml so we can require the file directly
const routeSettings = require('../../../../../core/server/services/route-settings');
routeSettings.init();
const validateRouteSettings = require('../../../../../core/server/services/route-settings/validate');
/**