Ghost/core/server/services/themes/engines/create.js
kirrg001 1f55c90037 Supported reading ghost api engine
refs #9866

- we fallback to v0.1 by default
- we support different formats
- this opens the box to switch the ghost api version for the whole blog site
- i had to add a different notation for overrides.json, because the structure is not optimal (i only want the versions, not the shortcuts)
2018-10-18 19:41:07 +02:00

50 lines
1.3 KiB
JavaScript

const _ = require('lodash');
const semver = require('semver');
const config = require('../../../config');
const DEFAULTS = require('./defaults');
const allowedKeys = ['ghost-api'];
/**
* Valid definitions for "ghost-api":
*
* ^0.1
* ^2
* ^0.1.0
* ^2.0.0
* 2.0.0
* v2
* v0.1
*
* Goal: Extract major version from input.
*
* @param packageJson
* @returns {*}
*/
module.exports = (packageJson) => {
let themeEngines = _.cloneDeep(DEFAULTS);
if (packageJson && packageJson.hasOwnProperty('engines')) {
// CASE: validate
if (packageJson.engines['ghost-api']) {
const availableApiVersions = {};
config.get('api:versions:all').forEach((version) => {
availableApiVersions[semver(semver.coerce(version).version).major] = version;
});
const apiVersion = packageJson.engines['ghost-api'];
const apiVersionMajor = semver(semver.coerce(apiVersion).version).major;
if (availableApiVersions[apiVersionMajor]) {
packageJson.engines['ghost-api'] = availableApiVersions[apiVersionMajor];
} else {
packageJson.engines['ghost-api'] = 'v0.1';
}
}
themeEngines = _.assign(themeEngines, _.pick(packageJson.engines, allowedKeys));
}
return themeEngines;
};