Ghost/core/frontend/services/themes/engines/create.js
Hannah Wolfe c02b0a19ac 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
2021-04-21 18:08:17 +01:00

46 lines
1.3 KiB
JavaScript

const semver = require('semver');
const config = require('../../../../shared/config');
/**
* Valid definitions for "ghost-api":
*
* ^2
* ^2.0.0
* 2.0.0
* v4
* v3
* v2
* canary
*
* Goal: Extract major version from input.
*
* @param packageJson
* @returns {*}
*/
module.exports = (packageJson) => {
let themeEngines = {'ghost-api': config.get('api:versions:default')};
if (packageJson && Object.prototype.hasOwnProperty.call(packageJson, 'engines')) {
// CASE: validate
if (packageJson.engines['ghost-api']) {
const availableApiVersions = {};
config.get('api:versions:all').forEach((version) => {
if (version === 'canary') {
availableApiVersions.canary = version;
} else {
availableApiVersions[semver.major(semver.coerce(version).version)] = version;
}
});
const apiVersion = packageJson.engines['ghost-api'];
const apiVersionMajor = apiVersion === 'canary' ? 'canary' : semver.major(semver.coerce(apiVersion).version);
if (availableApiVersions[apiVersionMajor]) {
themeEngines['ghost-api'] = availableApiVersions[apiVersionMajor];
}
}
}
return themeEngines;
};