2018-10-17 10:23:56 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
const semver = require('semver');
|
2020-05-27 20:47:53 +03:00
|
|
|
const config = require('../../../../shared/config');
|
2018-10-17 10:23:56 +03:00
|
|
|
const DEFAULTS = require('./defaults');
|
|
|
|
const allowedKeys = ['ghost-api'];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Valid definitions for "ghost-api":
|
|
|
|
*
|
|
|
|
* ^2
|
|
|
|
* ^2.0.0
|
|
|
|
* 2.0.0
|
2021-02-23 13:38:44 +03:00
|
|
|
* v4
|
2019-09-03 10:03:31 +03:00
|
|
|
* v3
|
2018-10-17 10:23:56 +03:00
|
|
|
* v2
|
2019-08-09 17:24:34 +03:00
|
|
|
* canary
|
2019-09-17 00:05:12 +03:00
|
|
|
*
|
2018-10-17 10:23:56 +03:00
|
|
|
* Goal: Extract major version from input.
|
|
|
|
*
|
|
|
|
* @param packageJson
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
module.exports = (packageJson) => {
|
|
|
|
let themeEngines = _.cloneDeep(DEFAULTS);
|
|
|
|
|
2019-08-08 11:47:13 +03:00
|
|
|
if (packageJson && Object.prototype.hasOwnProperty.call(packageJson, 'engines')) {
|
2018-10-17 10:23:56 +03:00
|
|
|
// CASE: validate
|
|
|
|
if (packageJson.engines['ghost-api']) {
|
|
|
|
const availableApiVersions = {};
|
|
|
|
config.get('api:versions:all').forEach((version) => {
|
2019-08-09 17:24:34 +03:00
|
|
|
if (version === 'canary') {
|
|
|
|
availableApiVersions.canary = version;
|
|
|
|
} else {
|
2020-02-24 21:01:53 +03:00
|
|
|
availableApiVersions[semver.major(semver.coerce(version).version)] = version;
|
2019-08-09 17:24:34 +03:00
|
|
|
}
|
2018-10-17 10:23:56 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
const apiVersion = packageJson.engines['ghost-api'];
|
2020-02-24 21:01:53 +03:00
|
|
|
const apiVersionMajor = apiVersion === 'canary' ? 'canary' : semver.major(semver.coerce(apiVersion).version);
|
2018-10-17 10:23:56 +03:00
|
|
|
|
|
|
|
if (availableApiVersions[apiVersionMajor]) {
|
|
|
|
packageJson.engines['ghost-api'] = availableApiVersions[apiVersionMajor];
|
|
|
|
} else {
|
2021-02-24 09:05:25 +03:00
|
|
|
packageJson.engines['ghost-api'] = DEFAULTS['ghost-api'];
|
2018-10-17 10:23:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
themeEngines = _.assign(themeEngines, _.pick(packageJson.engines, allowedKeys));
|
|
|
|
}
|
|
|
|
|
|
|
|
return themeEngines;
|
|
|
|
};
|