Used new default API version in theme engines

refs: 9f50e941eb
refs: bf0823c9a2

- Still working towards splitting the theme service into logical components
- The engine defaults were required in the index file, in a way that creates tight coupling across what would otherwise
be distinct components
- Also meant there was another hardcoded 'v4' in the codebase
- This fixes both issues by depending on the value from config
- Currently this adds Yet Another Config Require, but it should be fine for now until we have a new pattern for the frontend
- Note: We only care about the ghost-api engine, we used to care about both ghost and ghost-api. Now that there is only one there was no need for the more complex code structures
This commit is contained in:
Hannah Wolfe 2021-04-21 17:53:58 +01:00
parent e9b21fdbd1
commit c02b0a19ac
4 changed files with 6 additions and 14 deletions

View File

@ -1,8 +1,5 @@
const _ = require('lodash');
const semver = require('semver');
const config = require('../../../../shared/config');
const DEFAULTS = require('./defaults');
const allowedKeys = ['ghost-api'];
/**
* Valid definitions for "ghost-api":
@ -21,7 +18,7 @@ const allowedKeys = ['ghost-api'];
* @returns {*}
*/
module.exports = (packageJson) => {
let themeEngines = _.cloneDeep(DEFAULTS);
let themeEngines = {'ghost-api': config.get('api:versions:default')};
if (packageJson && Object.prototype.hasOwnProperty.call(packageJson, 'engines')) {
// CASE: validate
@ -39,13 +36,9 @@ module.exports = (packageJson) => {
const apiVersionMajor = apiVersion === 'canary' ? 'canary' : semver.major(semver.coerce(apiVersion).version);
if (availableApiVersions[apiVersionMajor]) {
packageJson.engines['ghost-api'] = availableApiVersions[apiVersionMajor];
} else {
packageJson.engines['ghost-api'] = DEFAULTS['ghost-api'];
themeEngines['ghost-api'] = availableApiVersions[apiVersionMajor];
}
}
themeEngines = _.assign(themeEngines, _.pick(packageJson.engines, allowedKeys));
}
return themeEngines;

View File

@ -1,3 +0,0 @@
{
"ghost-api": "v4"
}

View File

@ -10,7 +10,6 @@ const validate = require('./validate');
const i18n = require('./i18n');
const list = require('./list');
const settingsCache = require('../../../server/services/settings/cache');
const engineDefaults = require('./engines/defaults');
module.exports = {
// Init themes module
@ -79,7 +78,9 @@ module.exports = {
if (this.getActive()) {
return this.getActive().engine('ghost-api');
} else {
return engineDefaults['ghost-api'];
// @TODO: refactor so we don't have to require config here?
const config = require('../../../shared/config');
return config.get('api:versions:default');
}
},
activate: function (themeName) {

View File

@ -14,6 +14,7 @@ const themeEngines = require('../../../../../core/frontend/services/themes/engin
* 3. Add the a protective test for when next verstion (v6?) is planned it has to be changed again
*/
describe('Themes: engines', function () {
// NOTE: This is deliberately hard-coded so that when the default version is upgraded this test fails and needs reading!
const DEFAULT_ENGINE_VERSION = 'v4';
afterEach(function () {