0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00
ghost/core/server/services/route-settings/loader.js
Naz ba964c549f Moved route settings "getter" to the backend
refs https://linear.app/tryghost/issue/CORE-35/refactor-route-and-redirect-settings

- Frontend is not meant to know about the underlying source of the "routes" configuration, so any reads/edits/validations are being moved into a backend service. This should also simplify the coupling of the backend with the frontend where the latter will get a JSON blob with all needed configuration during the boot
- Nother problem the "get" method had was hiding an underlying function it was doing - reading the file from the filesystem SYNCRONOUSLY. It might be a thing we need to do during the "web" app initialization, but there's no clear need to do this in a sync fassion during the bootup for example. Also having a more explicit name should help :)
2021-09-28 04:59:41 +13:00

93 lines
2.9 KiB
JavaScript

const fs = require('fs-extra');
const path = require('path');
const debug = require('@tryghost/debug')('frontend:services:settings:settings-loader');
const errors = require('@tryghost/errors');
const config = require('../../../shared/config');
const yamlParser = require('../../../frontend/services/settings/yaml-parser');
const validate = require('../../../frontend/services/settings/validate');
const tpl = require('@tryghost/tpl');
const messages = {
settingsLoaderError: `Error trying to load YAML setting for {setting} from '{path}'.`
};
const getSettingFilePath = (setting) => {
// we only support the `yaml` file extension. `yml` will be ignored.
const fileName = `${setting}.yaml`;
const contentPath = config.getContentPath('settings');
const filePath = path.join(contentPath, fileName);
return {
fileName,
contentPath,
filePath
};
};
/**
* Functionally same as loadSettingsSync with exception of loading
* settings asynchronously. This method is used at new places to read settings
* to prevent blocking the eventloop
*
* @param {String} setting the requested settings as defined in setting knownSettings
* @returns {Promise<Object>} settingsFile
*/
const loadSettings = async (setting) => {
const {fileName, contentPath, filePath} = getSettingFilePath(setting);
try {
const file = await fs.readFile(filePath, 'utf8');
debug('settings file found for', setting);
const object = yamlParser(file, fileName);
return validate(object);
} catch (err) {
if (errors.utils.isIgnitionError(err)) {
throw err;
}
throw new errors.GhostError({
message: tpl(messages.settingsLoaderError, {
setting: setting,
path: contentPath
}),
context: filePath,
err: err
});
}
};
/**
* Reads the desired settings YAML file and passes the
* file to the YAML parser which then returns a JSON object.
* NOTE: loading happens synchronously
*
* @param {String} setting the requested settings as defined in setting knownSettings
* @returns {Object} settingsFile
*/
module.exports.loadSettingsSync = function loadSettingsSync(setting) {
const {fileName, contentPath, filePath} = getSettingFilePath(setting);
try {
const file = fs.readFileSync(filePath, 'utf8');
debug('settings file found for', setting);
const object = yamlParser(file, fileName);
return validate(object);
} catch (err) {
if (errors.utils.isIgnitionError(err)) {
throw err;
}
throw new errors.GhostError({
message: tpl(messages.settingsLoaderError, {
setting: setting,
path: contentPath
}),
context: filePath,
err: err
});
}
};
module.exports.loadSettings = loadSettings;