From 129938bacd4b0c91ed7e12202d61c6276b1df494 Mon Sep 17 00:00:00 2001 From: Doped Dude Date: Wed, 3 Feb 2016 18:14:09 +0530 Subject: [PATCH] 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 --- index.js | 4 +++ lib/compiler.js | 5 ++-- test/compiler.test.js | 57 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 9ece35c..7ac2f51 100644 --- a/index.js +++ b/index.js @@ -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. diff --git a/lib/compiler.js b/lib/compiler.js index c6d489c..70a2379 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -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 = diff --git a/test/compiler.test.js b/test/compiler.test.js index 646d32b..41b28c9 100644 --- a/test/compiler.test.js +++ b/test/compiler.test.js @@ -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': { },