Configurable dir for components and middleware

Add two new options `middlewareRootDir` and `componentRootDir`
allowing users to load middleware and/or components from a custom
place
This commit is contained in:
Doped Dude 2016-02-03 18:14:09 +05:30 committed by Miroslav Bajtoš
parent eb49c7a386
commit 129938bacd
3 changed files with 63 additions and 3 deletions

View File

@ -110,6 +110,10 @@ var addInstructionsToBrowserify = require('./lib/bundler');
* `model-config.json`. Defaults to `appRootDir`.
* @property {String} [dsRootDir] Directory to use when loading
* `datasources.json`. Defaults to `appRootDir`.
* @property {String} [middlewareRootDir] Directory to use when loading
* `middleware.json`. Defaults to `appRootDir`.
* @property {String} [componentRootDir] Directory to use when loading
* `component-config.json`. Defaults to `appRootDir`.
* @property {String} [env] Environment type, defaults to `process.env.NODE_ENV`
* or `development`. Common values are `development`, `staging` and
* `production`; however the applications are free to use any names.

View File

@ -52,15 +52,14 @@ module.exports = function compile(options) {
ConfigLoader.loadDataSources(dsRootDir, env);
assertIsValidConfig('data source', dataSourcesConfig);
// not configurable yet
var middlewareRootDir = appRootDir;
var middlewareRootDir = options.middlewareRootDir || appRootDir;
var middlewareConfig = options.middleware ||
ConfigLoader.loadMiddleware(middlewareRootDir, env);
var middlewareInstructions =
buildMiddlewareInstructions(middlewareRootDir, middlewareConfig);
var componentRootDir = appRootDir; // not configurable yet
var componentRootDir = options.componentRootDir || appRootDir;
var componentConfig = options.components ||
ConfigLoader.loadComponents(componentRootDir, env);
var componentInstructions =

View File

@ -1595,6 +1595,39 @@ describe('compiler', function() {
sourceFileForUrlNotFound);
});
it('supports `middlewareRootDir` option', function() {
var middlewareJson = {
initial: {},
custom: {
'loopback/server/middleware/url-not-found': {
params: 'some-config-data',
},
},
};
var customDir = path.resolve(appdir.PATH, 'custom');
fs.mkdirsSync(customDir);
fs.writeJsonSync(path.resolve(customDir, 'middleware.json'),
middlewareJson);
var instructions = boot.compile({
appRootDir: appdir.PATH,
middlewareRootDir: path.resolve(appdir.PATH, 'custom'),
});
expect(instructions.middleware).to.eql({
phases: ['initial', 'custom'],
middleware: [
{
sourceFile: sourceFileForUrlNotFound,
config: {
phase: 'custom',
params: 'some-config-data',
},
},
],
});
});
it('fails when a module middleware cannot be resolved', function() {
appdir.writeConfigFileSync('middleware.json', {
final: {
@ -2238,6 +2271,30 @@ describe('compiler', function() {
});
});
it('supports `componentRootDir` option', function() {
var componentJson = {
debug: {
option: 'value',
},
};
var customDir = path.resolve(appdir.PATH, 'custom');
fs.mkdirsSync(customDir);
fs.writeJsonSync(
path.resolve(customDir, 'component-config.json'), componentJson);
var instructions = boot.compile({
appRootDir: appdir.PATH,
componentRootDir: path.resolve(appdir.PATH, 'custom'),
});
var component = instructions.components[0];
expect(component).to.eql({
sourceFile: require.resolve('debug'),
config: {
option: 'value',
},
});
});
it('loads component relative to appRootDir', function() {
appdir.writeConfigFileSync('./component-config.json', {
'./index': { },