0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-24 23:48:13 -05:00
ghost/core/server/services/route-settings/yaml-parser.js
Hannah Wolfe 0db7ef849c
Removed remaining use of i18n from core/frontend
- i18n is an old pattern we are getting rid of in favour of tpl
- after removing i18n from helpers, there wasn't many usages of i18n left in the frontend, this removes whats left!
- this was done on a branch at the same time as Naz's commits removing i18n from the settings-related files
- hence some of these changes are minor amends to add additional messages/change names, rather than just straightup i18n->tpl
- it's a merge of both our refactors :)
2021-09-28 11:58:29 +01:00

36 lines
1.3 KiB
JavaScript

const yaml = require('js-yaml');
const debug = require('@tryghost/debug')('frontend:services:settings:yaml-parser');
const tpl = require('@tryghost/tpl');
const errors = require('@tryghost/errors');
const messages = {
error: 'Could not parse {file}: {context}.',
help: 'Check your {file} file for typos and fix the named issues.'
};
/**
* Takes a YAML file, parses it and returns a JSON Object
* @param {YAML} file the YAML file utf8 encoded
* @param {String} fileName the name of the file incl. extension
* @returns {Object} parsed
*/
module.exports = function parseYaml(file, fileName) {
try {
const parsed = yaml.load(file);
debug('YAML settings file parsed:', fileName);
return parsed;
} catch (error) {
// CASE: parsing failed, `js-yaml` tells us exactly what and where in the
// `reason` property as well as in the message.
// As the file uploaded is invalid, the person uploading must fix this - it's a 4xx error
throw new errors.IncorrectUsageError({
message: tpl(messages.error, {file: fileName, context: error.reason}),
code: 'YAML_PARSER_ERROR',
context: error.message,
err: error,
help: tpl(messages.help, {file: fileName})
});
}
};