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

Calls legacyController if present #681

Merged
merged 1 commit into from
Dec 9, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 16 additions & 6 deletions lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const _ = require('lodash'),
path = require('path'),
siteService = require('./services/sites'),
attachRoutes = require('./services/attachRoutes'),
render = require('./render'),
responses = require('./responses'),
files = require('./files'),
plugins = require('./services/plugins'),
Expand Down Expand Up @@ -120,6 +121,20 @@ function addCORS(site) {
};
}

function setRoutes(controller, router, site) {
if (_.isFunction(controller.legacyController)) {
controller.legacyController(router, render, site);
}

if (Array.isArray(controller.routes)) {
attachRoutes(router, controller.routes, site);
}

if (!controller.legacyController && !controller.routes) {
log('warn', `There is no router for site: ${site.slug}`);
}
}

/**
* Default way to load site controllers.
*
Expand Down Expand Up @@ -154,12 +169,7 @@ function addSiteController(router, site, providers) {
router.use(controller.middleware);
}

if (Array.isArray(controller.routes)) {
attachRoutes(router, controller.routes, site);
} else {
log('warn', `There is no router for site: ${site.slug}`);
}

setRoutes(controller, router, site);
// Providers are the same across all sites, but this is a convenient
// place to store it so that it's available everywhere
_.set(site, 'providers', providers);
Expand Down
13 changes: 9 additions & 4 deletions lib/routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,19 +388,24 @@ describe(_.startCase(filename), function () {
fn(router, site);
});

it('does nothing if site controller is not a function', function () {
const siteDir = 'some-location',
it('calls legacyController if defined', function () {
const controller = sandbox.stub(),
siteDir = 'some-location',
router = {},
site = { dir: siteDir };

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

files.tryRequire.returns({});
files.tryRequire.returns({
legacyController: controller
});

fn(router, site);

sinon.assert.called(controller);
});


it('calls `attachRoutes` if the `routes` Array is defined', function () {
const siteDir = 'some-location',
paths = [],
Expand Down