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:
parent
eb49c7a386
commit
129938bacd
4
index.js
4
index.js
|
@ -110,6 +110,10 @@ var addInstructionsToBrowserify = require('./lib/bundler');
|
||||||
* `model-config.json`. Defaults to `appRootDir`.
|
* `model-config.json`. Defaults to `appRootDir`.
|
||||||
* @property {String} [dsRootDir] Directory to use when loading
|
* @property {String} [dsRootDir] Directory to use when loading
|
||||||
* `datasources.json`. Defaults to `appRootDir`.
|
* `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`
|
* @property {String} [env] Environment type, defaults to `process.env.NODE_ENV`
|
||||||
* or `development`. Common values are `development`, `staging` and
|
* or `development`. Common values are `development`, `staging` and
|
||||||
* `production`; however the applications are free to use any names.
|
* `production`; however the applications are free to use any names.
|
||||||
|
|
|
@ -52,15 +52,14 @@ module.exports = function compile(options) {
|
||||||
ConfigLoader.loadDataSources(dsRootDir, env);
|
ConfigLoader.loadDataSources(dsRootDir, env);
|
||||||
assertIsValidConfig('data source', dataSourcesConfig);
|
assertIsValidConfig('data source', dataSourcesConfig);
|
||||||
|
|
||||||
// not configurable yet
|
var middlewareRootDir = options.middlewareRootDir || appRootDir;
|
||||||
var middlewareRootDir = appRootDir;
|
|
||||||
|
|
||||||
var middlewareConfig = options.middleware ||
|
var middlewareConfig = options.middleware ||
|
||||||
ConfigLoader.loadMiddleware(middlewareRootDir, env);
|
ConfigLoader.loadMiddleware(middlewareRootDir, env);
|
||||||
var middlewareInstructions =
|
var middlewareInstructions =
|
||||||
buildMiddlewareInstructions(middlewareRootDir, middlewareConfig);
|
buildMiddlewareInstructions(middlewareRootDir, middlewareConfig);
|
||||||
|
|
||||||
var componentRootDir = appRootDir; // not configurable yet
|
var componentRootDir = options.componentRootDir || appRootDir;
|
||||||
var componentConfig = options.components ||
|
var componentConfig = options.components ||
|
||||||
ConfigLoader.loadComponents(componentRootDir, env);
|
ConfigLoader.loadComponents(componentRootDir, env);
|
||||||
var componentInstructions =
|
var componentInstructions =
|
||||||
|
|
|
@ -1595,6 +1595,39 @@ describe('compiler', function() {
|
||||||
sourceFileForUrlNotFound);
|
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() {
|
it('fails when a module middleware cannot be resolved', function() {
|
||||||
appdir.writeConfigFileSync('middleware.json', {
|
appdir.writeConfigFileSync('middleware.json', {
|
||||||
final: {
|
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() {
|
it('loads component relative to appRootDir', function() {
|
||||||
appdir.writeConfigFileSync('./component-config.json', {
|
appdir.writeConfigFileSync('./component-config.json', {
|
||||||
'./index': { },
|
'./index': { },
|
||||||
|
|
Loading…
Reference in New Issue