From 1b491dee85a533449ab950147e4125d22ffbb3b0 Mon Sep 17 00:00:00 2001 From: Hage Yaapa Date: Fri, 28 Aug 2015 11:49:46 +0530 Subject: [PATCH] Resolve ${var} values in component-config.json App variables can now be specified in component-config.json using the ${var} format. The value of var is looked up using app.get(). This allows loopback explorer and other loopback components which require app variables, to be loaded declaratively using component-config.json. --- lib/executor.js | 5 +- test/executor.test.js | 82 +++++++++++++++++++++++++++++++ test/fixtures/simple-component.js | 7 +++ 3 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/simple-component.js diff --git a/lib/executor.js b/lib/executor.js index a8a3031..428d3de 100644 --- a/lib/executor.js +++ b/lib/executor.js @@ -316,12 +316,12 @@ function setupMiddleware(app, instructions) { } assert(typeof factory === 'function', 'Middleware factory must be a function'); - data.config = getUpdatedConfigObject(data.config, app); + data.config = getUpdatedConfigObject(app, data.config); app.middlewareFromConfig(factory, data.config); }); } -function getUpdatedConfigObject(config, app) { +function getUpdatedConfigObject(app, config) { var DYNAMIC_CONFIG_PARAM = /\$\{(\w+)\}$/; @@ -364,6 +364,7 @@ function setupComponents(app, instructions) { instructions.components.forEach(function(data) { debug('Configuring component %j', data.sourceFile); var configFn = require(data.sourceFile); + data.config = getUpdatedConfigObject(app, data.config); configFn(app, data.config); }); } diff --git a/test/executor.test.js b/test/executor.test.js index 3c910ef..5ecb977 100644 --- a/test/executor.test.js +++ b/test/executor.test.js @@ -496,6 +496,77 @@ describe('executor', function() { }); + describe('with component-config.json', function() { + + it('should parse a simple config variable', function(done) { + boot.execute(app, simpleComponentConfig( + { path: '${restApiRoot}' } + )); + + supertest(app).get('/component').end(function(err, res) { + if (err) return done(err); + expect(res.body.path).to.equal(app.get('restApiRoot')); + done(); + }); + }); + + it('should parse multiple config variables', function(done) { + boot.execute(app, simpleComponentConfig( + { path: '${restApiRoot}', env: '${env}' } + )); + + supertest(app).get('/component').end(function(err, res) { + if (err) return done(err); + expect(res.body.path).to.equal(app.get('restApiRoot')); + expect(res.body.env).to.equal(app.get('env')); + done(); + }); + }); + + it('should parse config variables in an array', function(done) { + boot.execute(app, simpleComponentConfig( + { paths: ['${restApiRoot}'] } + )); + + supertest(app).get('/component').end(function(err, res) { + if (err) return done(err); + expect(res.body.paths).to.eql( + [app.get('restApiRoot')] + ); + done(); + }); + }); + + it('should parse config variables in an object', function(done) { + boot.execute(app, simpleComponentConfig( + { info: { path: '${restApiRoot}' } } + )); + + supertest(app).get('/component').end(function(err, res) { + if (err) return done(err); + expect(res.body.info).to.eql({ + path: app.get('restApiRoot') + }); + done(); + }); + }); + + it('should parse config variables in a nested object', function(done) { + boot.execute(app, simpleComponentConfig( + { nested: { info: { path: '${restApiRoot}' } } } + )); + + supertest(app).get('/component').end(function(err, res) { + if (err) return done(err); + expect(res.body.nested).to.eql({ + info: { path: app.get('restApiRoot') } + }); + done(); + }); + }); + + }); + it('calls function exported by boot/init.js', function() { var file = appdir.writeFileSync('boot/init.js', 'module.exports = function(app) { app.fnCalled = true; };'); @@ -671,6 +742,17 @@ function simpleMiddlewareConfig(phase, params) { }); } +function simpleComponentConfig(config) { + return someInstructions({ + components: [ + { + sourceFile: path.join(__dirname, './fixtures/simple-component.js'), + config: config + } + ] + }); +} + function assertValidDataSource(dataSource) { // has methods assert.isFunc(dataSource, 'createModel'); diff --git a/test/fixtures/simple-component.js b/test/fixtures/simple-component.js new file mode 100644 index 0000000..952e5ae --- /dev/null +++ b/test/fixtures/simple-component.js @@ -0,0 +1,7 @@ +module.exports = function(loopbackApp, params) { + + loopbackApp.use('/component', function(req, res, next) { + res.send(params); + }); + +};