2019-06-25 19:33:56 +03:00
|
|
|
const _ = require('lodash');
|
2020-09-09 15:28:12 +03:00
|
|
|
const crypto = require('crypto');
|
2021-06-15 19:01:22 +03:00
|
|
|
const debug = require('@tryghost/debug')('frontend:services:settings:index');
|
2019-06-25 19:33:56 +03:00
|
|
|
const SettingsLoader = require('./loader');
|
|
|
|
const ensureSettingsFiles = require('./ensure-settings');
|
|
|
|
|
2020-05-22 21:22:20 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2019-06-25 19:33:56 +03:00
|
|
|
|
2020-09-09 15:28:12 +03:00
|
|
|
/**
|
|
|
|
* md5 hashes of default settings
|
|
|
|
*/
|
|
|
|
const defaultHashes = {
|
|
|
|
routes: '3d180d52c663d173a6be791ef411ed01'
|
|
|
|
};
|
|
|
|
|
|
|
|
const calculateHash = (data) => {
|
|
|
|
return crypto.createHash('md5')
|
|
|
|
.update(data, 'binary')
|
|
|
|
.digest('hex');
|
|
|
|
};
|
|
|
|
|
2019-06-25 19:33:56 +03:00
|
|
|
module.exports = {
|
|
|
|
init: function () {
|
|
|
|
const knownSettings = this.knownSettings();
|
|
|
|
|
|
|
|
debug('init settings service for:', knownSettings);
|
|
|
|
|
|
|
|
// Make sure that supported settings files are available
|
|
|
|
// inside of the `content/setting` directory
|
|
|
|
return ensureSettingsFiles(knownSettings);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Global place to switch on more available settings.
|
|
|
|
*/
|
|
|
|
knownSettings: function knownSettings() {
|
|
|
|
return ['routes'];
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Getter for YAML settings.
|
|
|
|
* Example: `settings.get('routes').then(...)`
|
|
|
|
* will return an Object like this:
|
|
|
|
* {routes: {}, collections: {}, resources: {}}
|
|
|
|
* @param {String} setting type of supported setting.
|
|
|
|
* @returns {Object} settingsFile
|
|
|
|
* @description Returns settings object as defined per YAML files in
|
|
|
|
* `/content/settings` directory.
|
|
|
|
*/
|
|
|
|
get: function get(setting) {
|
|
|
|
const knownSettings = this.knownSettings();
|
|
|
|
|
|
|
|
// CASE: this should be an edge case and only if internal usage of the
|
|
|
|
// getter is incorrect.
|
|
|
|
if (!setting || _.indexOf(knownSettings, setting) < 0) {
|
2020-05-22 21:22:20 +03:00
|
|
|
throw new errors.IncorrectUsageError({
|
2019-06-25 19:33:56 +03:00
|
|
|
message: `Requested setting is not supported: '${setting}'.`,
|
|
|
|
help: `Please use only the supported settings: ${knownSettings}.`
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return SettingsLoader(setting);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Getter for all YAML settings.
|
|
|
|
* Example: `settings.getAll().then(...)`
|
|
|
|
* will return an Object like this (assuming we're supporting `routes`
|
|
|
|
* and `globals`):
|
|
|
|
* {
|
|
|
|
* routes: {
|
|
|
|
* routes: null,
|
|
|
|
* collections: { '/': [Object] },
|
|
|
|
* resources: { tag: '/tag/{slug}/', author: '/author/{slug}/' }
|
|
|
|
* },
|
|
|
|
* globals: {
|
|
|
|
* config: { url: 'testblog.com' }
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* @returns {Object} settingsObject
|
|
|
|
* @description Returns all settings object as defined per YAML files in
|
|
|
|
* `/content/settings` directory.
|
|
|
|
*/
|
|
|
|
getAll: function getAll() {
|
2020-04-29 18:44:27 +03:00
|
|
|
const knownSettings = this.knownSettings();
|
|
|
|
const settingsToReturn = {};
|
2019-06-25 19:33:56 +03:00
|
|
|
|
|
|
|
_.each(knownSettings, function (setting) {
|
|
|
|
settingsToReturn[setting] = SettingsLoader(setting);
|
|
|
|
});
|
|
|
|
|
|
|
|
return settingsToReturn;
|
2020-09-09 15:28:12 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
getDefaulHash: (setting) => {
|
|
|
|
return defaultHashes[setting];
|
|
|
|
},
|
|
|
|
|
|
|
|
getCurrentHash: async (setting) => {
|
|
|
|
const data = await SettingsLoader.loadSettings(setting);
|
|
|
|
|
|
|
|
return calculateHash(JSON.stringify(data));
|
2019-06-25 19:33:56 +03:00
|
|
|
}
|
|
|
|
};
|