2020-04-29 18:44:27 +03:00
|
|
|
const fs = require('fs-extra');
|
2021-06-15 19:01:22 +03:00
|
|
|
const debug = require('@tryghost/debug')('frontend:services:settings:settings-loader');
|
2021-09-26 22:21:49 +03:00
|
|
|
const tpl = require('@tryghost/tpl');
|
2020-05-22 21:22:20 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2021-09-27 15:38:48 +03:00
|
|
|
const validate = require('./validate');
|
2021-09-23 17:46:10 +03: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 04:34:03 +03:00
|
|
|
|
2021-09-30 14:08:48 +03:00
|
|
|
class SettingsLoader {
|
2021-09-30 15:25:36 +03:00
|
|
|
/**
|
|
|
|
* @param {Object} options
|
|
|
|
* @param {Function} options.parseYaml yaml parser
|
2021-11-23 19:17:38 +03:00
|
|
|
* @param {String} options.settingFilePath routes settings file path
|
2021-09-30 15:25:36 +03:00
|
|
|
*/
|
2021-11-23 19:17:38 +03:00
|
|
|
constructor({parseYaml, settingFilePath}) {
|
2021-09-30 15:25:36 +03:00
|
|
|
this.parseYaml = parseYaml;
|
2021-09-30 14:08:48 +03:00
|
|
|
|
2021-11-23 19:17:38 +03:00
|
|
|
this.settingFilePath = settingFilePath;
|
2021-09-30 15:45:11 +03:00
|
|
|
}
|
2020-09-09 15:28:12 +03:00
|
|
|
|
2021-09-30 14:08:48 +03:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* @returns {Promise<Object>} settingsFile
|
|
|
|
*/
|
|
|
|
async loadSettings() {
|
|
|
|
try {
|
2021-09-30 21:16:00 +03:00
|
|
|
const file = await fs.readFile(this.settingFilePath, 'utf8');
|
|
|
|
debug('routes settings file found for:', this.settingFilePath);
|
2020-09-09 15:28:12 +03:00
|
|
|
|
2021-09-30 18:33:17 +03:00
|
|
|
const object = this.parseYaml(file);
|
2021-09-30 21:16:00 +03:00
|
|
|
debug('YAML settings file parsed:', this.settingFilePath);
|
2021-09-30 18:33:17 +03:00
|
|
|
|
2021-09-30 14:08:48 +03:00
|
|
|
return validate(object);
|
|
|
|
} catch (err) {
|
2021-12-01 13:22:01 +03:00
|
|
|
if (errors.utils.isGhostError(err)) {
|
2021-09-30 14:08:48 +03:00
|
|
|
throw err;
|
|
|
|
}
|
2020-09-09 15:28:12 +03:00
|
|
|
|
2021-12-01 13:22:01 +03:00
|
|
|
throw new errors.InternalServerError({
|
2021-09-30 14:08:48 +03:00
|
|
|
message: tpl(messages.settingsLoaderError, {
|
2021-09-30 21:16:00 +03:00
|
|
|
setting: 'routes',
|
|
|
|
path: this.settingFilePath
|
2021-09-30 14:08:48 +03:00
|
|
|
}),
|
|
|
|
err: err
|
|
|
|
});
|
2020-09-09 15:28:12 +03:00
|
|
|
}
|
2021-09-30 15:25:36 +03:00
|
|
|
}
|
2020-09-09 15:28:12 +03:00
|
|
|
|
2021-09-30 14:08:48 +03:00
|
|
|
/**
|
|
|
|
* Reads the routes.yaml settings file and passes the
|
|
|
|
* file to the YAML parser which then returns a JSON object.
|
|
|
|
*
|
|
|
|
* @returns {Object} settingsFile in following format: {routes: {}, collections: {}, resources: {}}
|
|
|
|
*/
|
|
|
|
loadSettingsSync() {
|
|
|
|
try {
|
2021-09-30 21:16:00 +03:00
|
|
|
const file = fs.readFileSync(this.settingFilePath, 'utf8');
|
|
|
|
debug('routes settings file found for:', this.settingFilePath);
|
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 04:34:03 +03:00
|
|
|
|
2021-09-30 18:33:17 +03:00
|
|
|
const object = this.parseYaml(file);
|
2021-09-30 21:16:00 +03:00
|
|
|
debug('YAML settings file parsed:', this.settingFilePath);
|
|
|
|
|
2021-09-30 14:08:48 +03:00
|
|
|
return validate(object);
|
|
|
|
} catch (err) {
|
2021-12-01 13:22:01 +03:00
|
|
|
if (errors.utils.isGhostError(err)) {
|
2021-09-30 14:08:48 +03:00
|
|
|
throw err;
|
|
|
|
}
|
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 04:34:03 +03:00
|
|
|
|
2021-12-01 13:22:01 +03:00
|
|
|
throw new errors.InternalServerError({
|
2021-09-30 14:08:48 +03:00
|
|
|
message: tpl(messages.settingsLoaderError, {
|
2021-09-30 21:16:00 +03:00
|
|
|
setting: 'routes',
|
|
|
|
path: this.settingFilePath
|
2021-09-30 14:08:48 +03:00
|
|
|
}),
|
|
|
|
err: err
|
|
|
|
});
|
2018-04-20 16:25:06 +03:00
|
|
|
}
|
|
|
|
}
|
2021-09-30 14:08:48 +03:00
|
|
|
}
|
2020-09-09 15:28:12 +03:00
|
|
|
|
2021-09-30 14:08:48 +03:00
|
|
|
module.exports = SettingsLoader;
|