0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00

Extracted yaml parsing into DI for settings loader

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

- When the yaml parser is injected through a DI it's easier to test and later on the redirects service initialization would use same pattern with exactly the same yamlParse funciton
- Next step is getting yaml parser into an outside module
- Also simplified getSettingFilePath method while swapping to an updated yaml parser implementation. Now this method function is exactly like the one used in redirects
This commit is contained in:
Naz 2021-09-30 17:33:17 +02:00
parent 99a2f12cb7
commit 7619ad31d4
2 changed files with 11 additions and 10 deletions

View file

@ -1,6 +1,7 @@
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({
@ -10,7 +11,7 @@ const defaultSettingsManager = new DefaultSettingsManager({
sourceFolderPath: config.get('paths').defaultSettings
});
const settingsLoader = new SettingsLoader();
const settingsLoader = new SettingsLoader({parseYaml});
module.exports = {
init: async () => {

View file

@ -22,7 +22,7 @@ class SettingsLoader {
/**
* NOTE: this method will have to go to an external module to reuse in redirects settings
* @param {String} setting type of the settings to load, e.g:'routes' or 'redirects'
* @returns {Object}
* @returns {String} setting file path
*/
getSettingFilePath(setting) {
// we only support the `yaml` file extension. `yml` will be ignored.
@ -30,10 +30,7 @@ class SettingsLoader {
const contentPath = config.getContentPath('settings');
const filePath = path.join(contentPath, fileName);
return {
fileName,
filePath
};
return filePath;
}
/**
@ -44,13 +41,16 @@ class SettingsLoader {
*/
async loadSettings() {
const setting = 'routes';
const {fileName, filePath} = this.getSettingFilePath(setting);
const filePath = this.getSettingFilePath(setting);
try {
const file = await fs.readFile(filePath, 'utf8');
debug('settings file found for', setting);
const object = this.parseYaml(file, fileName);
const object = this.parseYaml(file);
debug('YAML settings file parsed:', filePath);
return validate(object);
} catch (err) {
if (errors.utils.isIgnitionError(err)) {
@ -75,13 +75,13 @@ class SettingsLoader {
*/
loadSettingsSync() {
const setting = 'routes';
const {fileName, filePath} = this.getSettingFilePath(setting);
const filePath = this.getSettingFilePath(setting);
try {
const file = fs.readFileSync(filePath, 'utf8');
debug('settings file found for', setting);
const object = this.parseYaml(file, fileName);
const object = this.parseYaml(file);
return validate(object);
} catch (err) {
if (errors.utils.isIgnitionError(err)) {