Ghost/core/server/services/settings/loader.js
kirrg001 0ac19dcf84 Load yaml settings files synchronously
refs https://github.com/TryGhost/Team/issues/65

- it's easier for the architecture if we read the setting files synchronously,
  because the dynamic routing component is part of the express bootstrap and
  the whole routing bootstrap is synchronously
- for now: we only read one file anyway
- it's for now easier to read the file synchronously, then i don't have to change
  any existing express bootstrap architecture
2018-04-20 15:25:06 +02:00

40 lines
1.3 KiB
JavaScript

'use strict';
const fs = require('fs-extra'),
path = require('path'),
debug = require('ghost-ignition').debug('services:settings:settings-loader'),
common = require('../../lib/common'),
config = require('../../config'),
yamlParser = require('./yaml-parser');
/**
* 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);
// yamlParser returns a JSON object
return yamlParser(file, fileName);
} 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
});
}
};