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
|
|
|
const fs = require('fs-extra'),
|
|
|
|
Promise = require('bluebird'),
|
|
|
|
path = require('path'),
|
2019-11-05 08:29:25 +03:00
|
|
|
debug = require('ghost-ignition').debug('frontend:services:settings:ensure-settings'),
|
2019-06-25 19:33:56 +03:00
|
|
|
common = require('../../../server/lib/common'),
|
|
|
|
config = require('../../../server/config');
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes sure that all supported settings files are in the
|
|
|
|
* `/content/settings` directory. If not, copy the default files
|
|
|
|
* over.
|
|
|
|
* @param {Array} knownSettings
|
|
|
|
* @returns {Promise}
|
|
|
|
* @description Reads the `/settings` folder of the content path and makes
|
|
|
|
* sure that the associated yaml file for each setting exists. If it doesn't
|
|
|
|
* copy the default yaml file over.
|
|
|
|
*/
|
|
|
|
module.exports = function ensureSettingsFiles(knownSettings) {
|
|
|
|
const contentPath = config.getContentPath('settings'),
|
|
|
|
defaultSettingsPath = config.get('paths').defaultSettings;
|
|
|
|
|
|
|
|
return Promise.each(knownSettings, function (setting) {
|
|
|
|
const fileName = `${setting}.yaml`,
|
|
|
|
defaultFileName = `default-${fileName}`,
|
|
|
|
filePath = path.join(contentPath, fileName);
|
|
|
|
|
2018-06-22 21:28:01 +03:00
|
|
|
return fs.readFile(filePath, 'utf8')
|
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
|
|
|
.catch({code: 'ENOENT'}, () => {
|
|
|
|
// CASE: file doesn't exist, copy it from our defaults
|
|
|
|
return fs.copy(
|
|
|
|
path.join(defaultSettingsPath, defaultFileName),
|
|
|
|
path.join(contentPath, fileName)
|
|
|
|
).then(() => {
|
|
|
|
debug(`'${defaultFileName}' copied to ${contentPath}.`);
|
|
|
|
});
|
|
|
|
}).catch((error) => {
|
|
|
|
// CASE: we might have a permission error, as we can't access the directory
|
|
|
|
throw new common.errors.GhostError({
|
|
|
|
message: common.i18n.t('errors.services.settings.ensureSettings', {path: contentPath}),
|
|
|
|
err: error,
|
|
|
|
context: error.path
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|