Ghost/core/server/services/route-settings/settings-loader.js

106 lines
3.3 KiB
JavaScript
Raw Normal View History

const fs = require('fs-extra');
const path = require('path');
const debug = require('@tryghost/debug')('frontend:services:settings:settings-loader');
const tpl = require('@tryghost/tpl');
Refactored `common` lib import to use destructuring (#11835) * refactored `core/frontend/apps` to destructure common imports * refactored `core/frontend/services/{apps, redirects, routing}` to destructure common imports * refactored `core/frontend/services/settings` to destructure common imports * refactored remaining `core/frontend/services` to destructure common imports * refactored `core/server/adapters` to destructure common imports * refactored `core/server/data/{db, exporter, schema, validation}` to destructure common imports * refactored `core/server/data/importer` to destructure common imports * refactored `core/server/models/{base, plugins, relations}` to destructure common imports * refactored remaining `core/server/models` to destructure common imports * refactored `core/server/api/canary/utils/serializers/output` to destructure common imports * refactored remaining `core/server/api/canary/utils` to destructure common imports * refactored remaining `core/server/api/canary` to destructure common imports * refactored `core/server/api/shared` to destructure common imports * refactored `core/server/api/v2/utils` to destructure common imports * refactored remaining `core/server/api/v2` to destructure common imports * refactored `core/frontend/meta` to destructure common imports * fixed some tests referencing `common.errors` instead of `@tryghost/errors` - Not all of them need to be updated; only updating the ones that are causing failures * fixed errors import being shadowed by local scope
2020-05-22 21:22:20 +03:00
const errors = require('@tryghost/errors');
const config = require('../../../shared/config');
const validate = require('./validate');
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
class SettingsLoader {
/**
* @param {Object} options
* @param {Function} options.parseYaml yaml parser
*/
constructor({parseYaml}) {
this.parseYaml = parseYaml;
}
/**
* 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
};
};
/**
* 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);
const object = this.parseYaml(file, fileName);
return validate(object);
} catch (err) {
if (errors.utils.isIgnitionError(err)) {
throw err;
}
throw new errors.GhostError({
message: tpl(messages.settingsLoaderError, {
setting: setting,
path: contentPath
}),
context: filePath,
err: err
});
}
}
/**
* 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);
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
const object = this.parseYaml(file, fileName);
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
throw new errors.GhostError({
message: tpl(messages.settingsLoaderError, {
setting: setting,
path: contentPath
}),
context: filePath,
err: err
});
}
}
}
module.exports = SettingsLoader;