Ghost/core/frontend/services/settings/loader.js
Naz Gargol 0bf1542bc6
Extracted settings service part manipulating routes.yaml (#10800)
refs #10790
refs #9528

- The settings service was designed to handle more settings then just routing, but till this day there wasn't anything else added. As routes.yaml is only being used by frontend router so conceptually it fits better to have this code in frontend, so that it doesn't have to reach out to server
- The code left in server settings is the one that interacts with the database `settings` table and only partially provides information to frontend. That part is known as 'settings cache' and will be accessed through API controllers.
2019-06-25 18:33:56 +02:00

39 lines
1.4 KiB
JavaScript

const fs = require('fs-extra'),
path = require('path'),
debug = require('ghost-ignition').debug('services:settings:settings-loader'),
common = require('../../../server/lib/common'),
config = require('../../../server/config'),
yamlParser = require('./yaml-parser'),
validate = require('./validate');
/**
* Reads the desired settings YAML file and passes the
* file to the YAML parser which then returns a JSON object.
* @param {String} setting the requested settings as defined in setting knownSettings
* @returns {Object} settingsFile
*/
module.exports = function loadSettings(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);
try {
const file = fs.readFileSync(filePath, 'utf8');
debug('settings file found for', setting);
const object = yamlParser(file, fileName);
return validate(object);
} catch (err) {
if (common.errors.utils.isIgnitionError(err)) {
throw err;
}
throw new common.errors.GhostError({
message: common.i18n.t('errors.services.settings.loader', {setting: setting, path: contentPath}),
context: filePath,
err: err
});
}
};