Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subsite support #674

Merged
merged 22 commits into from
Aug 19, 2019
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions lib/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,28 @@ function bootstrapError(component, e) {
return bluebird.reject();
}

/**
* Bootstraps a site, and if it fails, falls back to the parent sites' bootstrap (for subsites)
*
* @param {Object} site
* @return {Promise}
*/
function bootstrapSite(site) {
// fetches itself if it is the main site
const parent = siteService.getParentSite(site.slug);

// using module.exports for test
return module.exports.bootstrapPath(parent.dir, site)
.catch(() => {
log('debug', `No bootstrap file found for parent site: ${site.slug}`);
})
.then(() => site.subsite ? module.exports.bootstrapPath(site.dir, site) : site)
.catch(() => {
log('debug', `No bootstrap file found for subsite: ${site.slug} ${site.subsite}`);
return site;
});
}

/**
* Bootstrap all sites and the individual
* bootstrap files in each component's directory
Expand All @@ -305,11 +327,7 @@ function bootrapSitesAndComponents() {
})
.flatMap(site => {
return highland(
bootstrapPath(site.dir, site)
.catch(function () {
log('debug', `No bootstrap file found for site: ${site.slug}`);
return site;
})
bootstrapSite(site)
);
})
.collect()
Expand Down Expand Up @@ -338,3 +356,4 @@ module.exports.bootstrapPath = bootstrapPath;
// For testing
module.exports.setLog = mock => log = mock;
module.exports.setDb = mock => db = mock;
module.exports.bootstrapSite = bootstrapSite;
42 changes: 40 additions & 2 deletions lib/bootstrap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ describe(_.startCase(filename), function () {
sitesFake = [{
host: 'example1.com',
dir: 'example1',
prefix: 'example1.com'
prefix: 'example1.com',
slug: 'a'
}, {
host: 'example2.com',
path: '/',
Expand All @@ -33,6 +34,12 @@ describe(_.startCase(filename), function () {
path: '/some-path',
dir: 'example3',
prefix: 'example1.com/some-path'
}, {
host: 'example4.com',
path: '/',
dir: 'example4',
prefix: 'example4.com',
subsite: 'example4'
}];

bootstrapFake = files.getYaml(path.resolve('./test/fixtures/config/bootstrap'));
Expand Down Expand Up @@ -75,14 +82,15 @@ describe(_.startCase(filename), function () {
it('bootstraps', function () {
files.getComponents.returns(['a', 'b', 'c']);
files.getYaml.withArgs('example1').returns({});

siteService.getParentSite.returns(_.cloneDeep(sitesFake[0]));
return fn();
});

it('hits the `then` function when bootstrapPath is run successfully', function () {
files.getComponents.returns(['a', 'b', 'c']);
files.getYaml.withArgs('example1').returns({});
sandbox.stub(lib, 'bootstrapPath').returns(Promise.resolve({}));
siteService.getParentSite.returns(_.cloneDeep(sitesFake[0]));
return fn();
});

Expand All @@ -94,6 +102,36 @@ describe(_.startCase(filename), function () {
});
});

describe('bootstrapSite', function () {
const fn = lib[this.title];

it('catches and logs if missing bootstrap', function () {
siteService.getParentSite.returns(_.cloneDeep(sitesFake[0]));
sandbox.stub(lib, 'bootstrapPath').returns(Promise.reject());
return fn(_.cloneDeep(sitesFake[0])).then(() => {
sinon.assert.calledWith(fakeLog,'debug', 'No bootstrap file found for parent site: a');
});
});

it('if no subsites, bootstrapPath runs once', function () {
siteService.getParentSite.returns(_.cloneDeep(sitesFake[0]));
sandbox.stub(lib, 'bootstrapPath').returns(Promise.resolve({}));

return fn(_.cloneDeep(sitesFake[0])).then(() => {
sinon.assert.calledOnce(lib.bootstrapPath);
});
});

it('if subsite, bootstrapPath runs twice', function () {
siteService.getParentSite.returns(_.cloneDeep(sitesFake[3]));
sandbox.stub(lib, 'bootstrapPath').returns(Promise.resolve({}));

return fn(_.cloneDeep(sitesFake[3])).then(() => {
sinon.assert.calledTwice(lib.bootstrapPath);
});
});
});

describe('bootstrapPath', function () {
const fn = lib[this.title];

Expand Down
19 changes: 17 additions & 2 deletions lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,23 @@ function addCORS(site) {
* @returns {express.Router}
*/
function addSiteController(router, site, providers) {
if (site.dir) {
const controller = files.tryRequire(site.dir);
// fetches itself if it is the main site
const parent = siteService.getParentSite(site.slug);

if (parent && parent.dir) {
const controller = files.tryRequire(parent.dir);

// merge in subsiteController options
if (site.subsite) {
const subsiteController = files.tryRequire(site.dir);

// default _.merge overrides values in arrays :facepalm:
_.mergeWith(controller, subsiteController, (par, sub) => {
jpope19 marked this conversation as resolved.
Show resolved Hide resolved
if (Array.isArray(par) && Array.isArray(sub)) {
return par.concat(sub);
}
});
}

if (controller) {
if (Array.isArray(controller.middleware)) {
Expand Down
44 changes: 38 additions & 6 deletions lib/routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ describe(_.startCase(filename), function () {
describe('addSite', function () {
const fn = lib[this.title];

beforeEach(function () {
sandbox.stub(siteService);
});

it('adds controllers', function () {
const router = createMockRouter(),
innerRouter = createMockRouter();
Expand All @@ -119,7 +123,6 @@ describe(_.startCase(filename), function () {
sandbox.stub(files, 'tryRequire');
sandbox.stub(files, 'getFiles');
sandbox.stub(files, 'getComponents');
sandbox.stub(siteService, 'sites');

files.fileExists.returns(true);
files.tryRequire.returns(_.noop);
Expand All @@ -143,7 +146,7 @@ describe(_.startCase(filename), function () {
sandbox.stub(files, 'tryRequire');
sandbox.stub(files, 'getFiles');
sandbox.stub(files, 'getComponents');
sandbox.stub(siteService, 'sites');
siteService.getParentSite.returns(_.cloneDeep(siteStub));

files.fileExists.returns(true);
files.tryRequire.returns(_.noop);
Expand All @@ -168,7 +171,7 @@ describe(_.startCase(filename), function () {
sandbox.stub(files, 'tryRequire');
sandbox.stub(files, 'getFiles');
sandbox.stub(files, 'getComponents');
sandbox.stub(siteService, 'sites');
siteService.getParentSite.returns(_.cloneDeep(siteStub));

files.fileExists.returns(true);
files.tryRequire.onCall(0).returns({
Expand All @@ -193,7 +196,7 @@ describe(_.startCase(filename), function () {
sandbox.stub(files, 'tryRequire');
sandbox.stub(files, 'getFiles');
sandbox.stub(files, 'getComponents');
sandbox.stub(siteService, 'sites');
siteService.getParentSite.returns(_.cloneDeep(siteStub));

files.fileExists.returns(true);
files.tryRequire.onCall(0).returns(null);
Expand All @@ -217,7 +220,6 @@ describe(_.startCase(filename), function () {
sandbox.stub(files, 'fileExists');
sandbox.stub(files, 'tryRequire');
sandbox.stub(files, 'getComponents');
sandbox.stub(siteService, 'sites');

files.fileExists.returns(true);
siteService.sites.returns({
Expand All @@ -240,7 +242,6 @@ describe(_.startCase(filename), function () {
sandbox.stub(files, 'fileExists');
sandbox.stub(files, 'tryRequire');
sandbox.stub(files, 'getComponents');
sandbox.stub(siteService, 'sites');

files.fileExists.returns(false);
siteService.sites.returns({
Expand Down Expand Up @@ -309,10 +310,16 @@ describe(_.startCase(filename), function () {
describe('addSiteController', function () {
const fn = lib[this.title];

beforeEach(function () {
sandbox.stub(siteService);
});

it('does nothing if there is no site directory', function () {
const router = {},
site = {};

siteService.getParentSite.returns(_.cloneDeep(site));

fn(router, site);
});

Expand All @@ -321,6 +328,7 @@ describe(_.startCase(filename), function () {
router = {},
site = { dir: siteDir };

siteService.getParentSite.returns(_.cloneDeep(site));
sandbox.stub(files, 'tryRequire');

files.tryRequire.returns(false);
Expand All @@ -333,6 +341,7 @@ describe(_.startCase(filename), function () {
router = {},
site = { dir: siteDir };

siteService.getParentSite.returns(_.cloneDeep(site));
sandbox.stub(files, 'tryRequire');

files.tryRequire.returns({});
Expand All @@ -350,6 +359,7 @@ describe(_.startCase(filename), function () {
},
site = { dir: siteDir };

siteService.getParentSite.returns(_.cloneDeep(site));
sandbox.stub(files, 'tryRequire');

files.tryRequire.returns({
Expand All @@ -375,6 +385,7 @@ describe(_.startCase(filename), function () {
},
site = { dir: siteDir };

siteService.getParentSite.returns(_.cloneDeep(site));
sandbox.stub(files, 'tryRequire');

files.tryRequire.returns({
Expand All @@ -391,12 +402,33 @@ describe(_.startCase(filename), function () {
router = {},
site = { dir: siteDir };

siteService.getParentSite.returns(_.cloneDeep(site));
sandbox.stub(files, 'tryRequire');

files.tryRequire.returns({});

fn(router, site);
});

it('merges in subsite controller', function () {
const siteDir = 'some-location',
paths = [],
router = {
get(route) {
paths.push(route);
}
},
site = { dir: siteDir, subsite: 'some-subsite' };

siteService.getParentSite.returns(_.cloneDeep(site));
sandbox.stub(files, 'tryRequire');

files.tryRequire.onFirstCall().returns({ routes: [{ path: '/foo' }], foo: 'bar' });
files.tryRequire.onSecondCall().returns({ routes: [{ path: '/bar' }], foo: 'bat' });

fn(router, site);
expect(paths).to.deep.equal(['/foo', '/bar']);
});
});

describe('addAuthenticationRoutes', function () {
Expand Down
Loading