0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-06 22:40:14 -05:00

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)
This commit is contained in:
kirrg001 2018-10-17 09:23:56 +02:00 committed by Katharina Irrgang
parent 17feb14e4a
commit 1f55c90037
6 changed files with 169 additions and 0 deletions

View file

@ -67,6 +67,7 @@
},
"api": {
"versions": {
"all": ["v0.1", "v2"],
"active": "v2",
"stable": "",
"deprecated": "v0.1",

View file

@ -14,6 +14,7 @@
var join = require('path').join,
_ = require('lodash'),
themeConfig = require('./config'),
themeEngines = require('./engines'),
config = require('../../config'),
engine = require('./engine'),
// Current instance of ActiveTheme
@ -45,6 +46,9 @@ class ActiveTheme {
// Create a theme config object
this._config = themeConfig.create(this._packageInfo);
// Create a theme engines object
this._engines = themeEngines.create(this._packageInfo);
}
get name() {
@ -83,6 +87,10 @@ class ActiveTheme {
return this._config[key];
}
engine(key) {
return this._engines[key];
}
mount(siteApp) {
// reset the asset hash
// @TODO: set this on the theme instead of globally, or use proper file-based hash

View file

@ -0,0 +1,49 @@
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;
};

View file

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

View file

@ -0,0 +1,5 @@
module.exports = {
get create() {
return require('./create');
}
};

View file

@ -0,0 +1,103 @@
const should = require('should');
const sinon = require('sinon');
const themeEngines = require('../../../../../server/services/themes/engines');
const sandbox = sinon.sandbox.create();
describe('Themes: engines', function () {
afterEach(function () {
sandbox.restore();
});
it('no engines', function () {
const engines = themeEngines.create();
engines.should.eql({
'ghost-api': 'v0.1'
});
});
describe('ghost-api', function () {
it('v2', function () {
const engines = themeEngines.create({
engines: {
'ghost-api': 'v2'
}
});
engines.should.eql({
'ghost-api': 'v2'
});
});
it('v10', function () {
const engines = themeEngines.create({
engines: {
'ghost-api': 'v10'
}
});
engines.should.eql({
'ghost-api': 'v0.1'
});
});
it('^0.1', function () {
const engines = themeEngines.create({
engines: {
'ghost-api': '^0.1'
}
});
engines.should.eql({
'ghost-api': 'v0.1'
});
});
it('^2', function () {
const engines = themeEngines.create({
engines: {
'ghost-api': '^2'
}
});
engines.should.eql({
'ghost-api': 'v2'
});
});
it('2.0.0', function () {
const engines = themeEngines.create({
engines: {
'ghost-api': '2.0.0'
}
});
engines.should.eql({
'ghost-api': 'v2'
});
});
it('2.17.0', function () {
const engines = themeEngines.create({
engines: {
'ghost-api': '2.17.0'
}
});
engines.should.eql({
'ghost-api': 'v2'
});
});
it('3', function () {
const engines = themeEngines.create({
engines: {
'ghost-api': '3'
}
});
engines.should.eql({
'ghost-api': 'v0.1'
});
});
});
});