2018-10-17 10:23:56 +03:00
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) => {
|
2021-04-21 19:53:58 +03:00
|
|
|
let themeEngines = {'ghost-api': config.get('api:versions:default')};
|
2018-10-17 10:23:56 +03:00
|
|
|
|
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]) {
|
2021-04-21 19:53:58 +03:00
|
|
|
themeEngines['ghost-api'] = availableApiVersions[apiVersionMajor];
|
2018-10-17 10:23:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return themeEngines;
|
|
|
|
};
|