2020-04-29 18:44:27 +03:00
|
|
|
const fs = require('fs-extra');
|
|
|
|
const path = require('path');
|
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');
|
2020-05-27 20:47:53 +03:00
|
|
|
const config = require('../../../shared/config');
|
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
|
|
|
|
*/
|
|
|
|
constructor({parseYaml}) {
|
|
|
|
this.parseYaml = parseYaml;
|
2021-09-30 14:08:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* NOTE: this method will have to go to an external module to reuse in redirects settings
|
|
|
|
* @param {String} setting type of the settings to load, e.g:'routes' or 'redirects'
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
|
|
|
getSettingFilePath(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);
|
|
|
|
|
|
|
|
return {
|
|
|
|
fileName,
|
|
|
|
contentPath,
|
|
|
|
filePath
|
|
|
|
};
|
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() {
|
|
|
|
const setting = 'routes';
|
|
|
|
const {fileName, contentPath, filePath} = this.getSettingFilePath(setting);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const file = await fs.readFile(filePath, 'utf8');
|
|
|
|
debug('settings file found for', setting);
|
2020-09-09 15:28:12 +03:00
|
|
|
|
2021-09-30 15:25:36 +03:00
|
|
|
const object = this.parseYaml(file, fileName);
|
2021-09-30 14:08:48 +03:00
|
|
|
return validate(object);
|
|
|
|
} catch (err) {
|
|
|
|
if (errors.utils.isIgnitionError(err)) {
|
|
|
|
throw err;
|
|
|
|
}
|
2020-09-09 15:28:12 +03:00
|
|
|
|
2021-09-30 14:08:48 +03:00
|
|
|
throw new errors.GhostError({
|
|
|
|
message: tpl(messages.settingsLoaderError, {
|
|
|
|
setting: setting,
|
|
|
|
path: contentPath
|
|
|
|
}),
|
|
|
|
context: filePath,
|
|
|
|
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() {
|
|
|
|
const setting = 'routes';
|
|
|
|
const {fileName, contentPath, filePath} = this.getSettingFilePath(setting);
|
2020-09-09 15:28:12 +03:00
|
|
|
|
2021-09-30 14:08:48 +03:00
|
|
|
try {
|
|
|
|
const file = fs.readFileSync(filePath, 'utf8');
|
|
|
|
debug('settings file found for', setting);
|
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 15:25:36 +03:00
|
|
|
const object = this.parseYaml(file, fileName);
|
2021-09-30 14:08:48 +03:00
|
|
|
return validate(object);
|
|
|
|
} catch (err) {
|
|
|
|
if (errors.utils.isIgnitionError(err)) {
|
|
|
|
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-09-30 14:08:48 +03:00
|
|
|
throw new errors.GhostError({
|
|
|
|
message: tpl(messages.settingsLoaderError, {
|
|
|
|
setting: setting,
|
|
|
|
path: contentPath
|
|
|
|
}),
|
|
|
|
context: filePath,
|
|
|
|
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;
|