2020-04-29 16:44:27 +01:00
|
|
|
const fs = require('fs-extra');
|
2021-06-15 17:01:22 +01:00
|
|
|
const debug = require('@tryghost/debug')('frontend:services:settings:settings-loader');
|
2021-09-26 20:21:49 +01:00
|
|
|
const tpl = require('@tryghost/tpl');
|
2020-05-22 13:22:20 -05:00
|
|
|
const errors = require('@tryghost/errors');
|
2021-09-27 14:38:48 +02:00
|
|
|
const validate = require('./validate');
|
2021-09-23 16:46:10 +02:00
|
|
|
|
|
|
|
const messages = {
|
|
|
|
settingsLoaderError: `Error trying to load YAML setting for {setting} from '{path}'.`
|
|
|
|
};
|
YAML settings loader and parser
closes #9528
These code changes introduce a YAML parser which will load and parse YAML files from the `/content/settings` directory. There are three major parts involved:
1. `ensure-settings.js`: this fn takes care that on bootstrap, the supported files are present in the `/content/settings` directory. If the files are not present, they get copied back from our default files. The default files to copy from are located in `core/server/services/settings`.
2. `loader.js`: the settings loader reads the requested `yaml` file from the disk and passes it to the yaml parser, which returns a `json` object of the file. The settings loader throws an error, if the file is not accessible, e. g. because of permission errors.
3. `yaml-parser`: gets passed a `yaml` file and returns a `json` object. If the file is not parseable, it returns a clear error that contains the information, what and where the parsing error occurred (e. g. line number and reason).
- added a `get()` fn to settings services, that returns the settings object that's asked for. e. g. `settings.get('routes').then(()...` will return the `routes` settings.
- added a `getAll()` fn to settings services, that returns all available settings in an object. The object looks like: `{routes: {routes: {}, collections: {}, resources: {}}, globals: {value: {}}`, assuming that we have to supported settings `routes` and `globals`.
Further additions:
- config `contentPath` for `settings`
- config overrides for default `yaml` files location in `/core/server/services/settings`
**Important**: These code changes are in preparation for Dynamic Routing and not yet used. The process of copying the supported `yaml` files (in this first step, the `routes.yaml` file) is not yet activated.
2018-04-13 09:34:03 +08:00
|
|
|
|
2021-09-30 13:08:48 +02:00
|
|
|
class SettingsLoader {
|
2021-09-30 14:25:36 +02:00
|
|
|
/**
|
|
|
|
* @param {Object} options
|
|
|
|
* @param {Function} options.parseYaml yaml parser
|
2021-11-23 20:17:38 +04:00
|
|
|
* @param {String} options.settingFilePath routes settings file path
|
2021-09-30 14:25:36 +02:00
|
|
|
*/
|
2021-11-23 20:17:38 +04:00
|
|
|
constructor({parseYaml, settingFilePath}) {
|
2021-09-30 14:25:36 +02:00
|
|
|
this.parseYaml = parseYaml;
|
2021-09-30 13:08:48 +02:00
|
|
|
|
2021-11-23 20:17:38 +04:00
|
|
|
this.settingFilePath = settingFilePath;
|
2021-09-30 14:45:11 +02:00
|
|
|
}
|
2020-09-10 00:28:12 +12:00
|
|
|
|
2021-09-30 13:08:48 +02:00
|
|
|
/**
|
2022-04-10 11:40:55 +01:00
|
|
|
* Reads the routes.yaml settings file and passes the
|
|
|
|
* file to the YAML parser which then returns a JSON object.
|
|
|
|
* @returns {Promise<RouteSettings>} settingsFileContents
|
2021-09-30 13:08:48 +02:00
|
|
|
*/
|
|
|
|
async loadSettings() {
|
|
|
|
try {
|
2021-09-30 20:16:00 +02:00
|
|
|
const file = await fs.readFile(this.settingFilePath, 'utf8');
|
|
|
|
debug('routes settings file found for:', this.settingFilePath);
|
2020-09-10 00:28:12 +12:00
|
|
|
|
2021-09-30 17:33:17 +02:00
|
|
|
const object = this.parseYaml(file);
|
2021-09-30 20:16:00 +02:00
|
|
|
debug('YAML settings file parsed:', this.settingFilePath);
|
2021-09-30 17:33:17 +02:00
|
|
|
|
2021-09-30 13:08:48 +02:00
|
|
|
return validate(object);
|
|
|
|
} catch (err) {
|
2021-12-01 10:22:01 +00:00
|
|
|
if (errors.utils.isGhostError(err)) {
|
2021-09-30 13:08:48 +02:00
|
|
|
throw err;
|
|
|
|
}
|
2020-09-10 00:28:12 +12:00
|
|
|
|
2021-12-01 10:22:01 +00:00
|
|
|
throw new errors.InternalServerError({
|
2021-09-30 13:08:48 +02:00
|
|
|
message: tpl(messages.settingsLoaderError, {
|
2021-09-30 20:16:00 +02:00
|
|
|
setting: 'routes',
|
|
|
|
path: this.settingFilePath
|
2021-09-30 13:08:48 +02:00
|
|
|
}),
|
|
|
|
err: err
|
|
|
|
});
|
2020-09-10 00:28:12 +12:00
|
|
|
}
|
2021-09-30 14:25:36 +02:00
|
|
|
}
|
2021-09-30 13:08:48 +02:00
|
|
|
}
|
2020-09-10 00:28:12 +12:00
|
|
|
|
2021-09-30 13:08:48 +02:00
|
|
|
module.exports = SettingsLoader;
|
2022-04-10 11:40:55 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} RouteSettings
|
|
|
|
* @property {Object} routes
|
|
|
|
* @property {Object} collections
|
|
|
|
* @property {Object} taxonomies
|
|
|
|
*/
|