Skip to content

Commit

Permalink
Supported reading ghost api engine
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
kirrg001 committed Oct 18, 2018
1 parent 17feb14 commit 1f55c90
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/server/config/overrides.json
Expand Up @@ -67,6 +67,7 @@
},
"api": {
"versions": {
"all": ["v0.1", "v2"],
"active": "v2",
"stable": "",
"deprecated": "v0.1",
Expand Down
8 changes: 8 additions & 0 deletions core/server/services/themes/active.js
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand Down
49 changes: 49 additions & 0 deletions core/server/services/themes/engines/create.js
@@ -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;
};
3 changes: 3 additions & 0 deletions core/server/services/themes/engines/defaults.json
@@ -0,0 +1,3 @@
{
"ghost-api": "v0.1"
}
5 changes: 5 additions & 0 deletions core/server/services/themes/engines/index.js
@@ -0,0 +1,5 @@
module.exports = {
get create() {
return require('./create');
}
};
103 changes: 103 additions & 0 deletions core/test/unit/services/themes/engines/create_spec.js
@@ -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'
});
});
});
});

0 comments on commit 1f55c90

Please sign in to comment.