From dba37ba7449feca1b2751c2c14c23c5a6566a186 Mon Sep 17 00:00:00 2001 From: Doped Dude Date: Wed, 3 Feb 2016 18:14:09 +0530 Subject: [PATCH 1/6] configurable directory support for component and middleware --- lib/compiler.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/compiler.js b/lib/compiler.js index 43661cf..ba5f5d8 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -47,15 +47,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 = From 9767a823b68c9aacdc1b350abf1933023520e484 Mon Sep 17 00:00:00 2001 From: Doped Dude Date: Wed, 10 Feb 2016 04:46:37 +0530 Subject: [PATCH 2/6] added test cases to validate the middlewares & components configuration custom directory support --- test/compiler.test.js | 73 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 67 insertions(+), 6 deletions(-) diff --git a/test/compiler.test.js b/test/compiler.test.js index ee34e0e..b3c2342 100644 --- a/test/compiler.test.js +++ b/test/compiler.test.js @@ -1546,7 +1546,8 @@ describe('compiler', function() { describe('for middleware', function() { - function testMiddlewareRegistration(middlewareId, sourceFile) { + function testMiddlewareRegistration(middlewareId, sourceFile, + alternativeDirTest) { var json = { initial: { }, @@ -1560,7 +1561,21 @@ describe('compiler', function() { appdir.writeConfigFileSync('middleware.json', json); - var instructions = boot.compile(appdir.PATH); + var instructions; + + if (alternativeDirTest === true) { + var customDir = path.resolve(appdir.PATH, 'custom'); + fs.mkdirsSync(customDir); + fs.renameSync( + path.resolve(appdir.PATH, 'middleware.json'), + path.resolve(customDir, 'middleware.json')); + instructions = boot.compile({ + appRootDir: appdir.PATH, + middlewareRootDir: path.resolve(appdir.PATH, 'custom') + }); + } else { + instructions = boot.compile(appdir.PATH); + } expect(instructions.middleware).to.eql({ phases: ['initial', 'custom'], @@ -1588,11 +1603,23 @@ describe('compiler', function() { sourceFileForUrlNotFound); }); + it('Suports `middlewareRootDir` Option And ' + + 'emits middleware instructions', function() { + testMiddlewareRegistration('loopback/server/middleware/url-not-found', + sourceFileForUrlNotFound, true); + }); + it('emits middleware instructions for fragment', function() { testMiddlewareRegistration('loopback#url-not-found', sourceFileForUrlNotFound); }); + it('Suports `middlewareRootDir` Option And ' + + 'emits middleware instructions for fragment', function() { + testMiddlewareRegistration('loopback#url-not-found', + sourceFileForUrlNotFound, true); + }); + it('fails when a module middleware cannot be resolved', function() { appdir.writeConfigFileSync('middleware.json', { final: { @@ -2209,21 +2236,46 @@ describe('compiler', function() { }); describe('for components', function() { - it('loads component configs from multiple files', function() { + // Validate merging of component configuration from different locations + function testComponentConfigsMerge(alternativeDirTest) { appdir.createConfigFilesSync(); appdir.writeConfigFileSync('component-config.json', { debug: { option: 'value' } }); appdir.writeConfigFileSync('component-config.local.json', { - debug: { local: 'applied' } + debug: {local: 'applied'} }); var env = process.env.NODE_ENV || 'development'; appdir.writeConfigFileSync('component-config.' + env + '.json', { - debug: { env: 'applied' } + debug: {env: 'applied'} }); - var instructions = boot.compile(appdir.PATH); + var instructions; + + if (alternativeDirTest === true) { + var customDir = path.resolve(appdir.PATH, 'custom'); + fs.mkdirsSync(customDir); + + fs.renameSync( + path.resolve(appdir.PATH, 'component-config.json'), + path.resolve(customDir, 'component-config.json')); + + fs.renameSync( + path.resolve(appdir.PATH, 'component-config.local.json'), + path.resolve(customDir, 'component-config.local.json')); + + fs.renameSync( + path.resolve(appdir.PATH, 'component-config.' + env + '.json'), + path.resolve(customDir, 'component-config.' + env + '.json')); + + instructions = boot.compile({ + appRootDir: appdir.PATH, + componentRootDir: path.resolve(appdir.PATH, 'custom') + }); + } else { + instructions = boot.compile(appdir.PATH); + } var component = instructions.components[0]; expect(component).to.eql({ @@ -2234,6 +2286,15 @@ describe('compiler', function() { env: 'applied' } }); + } + + it('loads component configs from multiple files', function() { + testComponentConfigsMerge(); + }); + + it('Suports `componentRootDir` Option And ' + + 'loads component configs from multiple files', function() { + testComponentConfigsMerge(true); }); it('loads component relative to appRootDir', function() { From 2536036d01f2d90af82318a9525656da37d42a04 Mon Sep 17 00:00:00 2001 From: Doped Dude Date: Wed, 10 Feb 2016 04:50:31 +0530 Subject: [PATCH 3/6] fixed formatting conflicts with community --- test/compiler.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/compiler.test.js b/test/compiler.test.js index b3c2342..1f36bc5 100644 --- a/test/compiler.test.js +++ b/test/compiler.test.js @@ -2243,12 +2243,12 @@ describe('compiler', function() { debug: { option: 'value' } }); appdir.writeConfigFileSync('component-config.local.json', { - debug: {local: 'applied'} + debug: { local: 'applied' } }); var env = process.env.NODE_ENV || 'development'; appdir.writeConfigFileSync('component-config.' + env + '.json', { - debug: {env: 'applied'} + debug: { env: 'applied' } }); var instructions; From bf89a97362c9dda38bcdd8d044458cedf3c21d5d Mon Sep 17 00:00:00 2001 From: Doped Dude Date: Fri, 8 Jul 2016 01:49:04 +0530 Subject: [PATCH 4/6] added test cases for middleware/component custom dirs --- test/compiler.test.js | 114 +++++++++++++++++++++--------------------- 1 file changed, 57 insertions(+), 57 deletions(-) diff --git a/test/compiler.test.js b/test/compiler.test.js index d6d1068..779c808 100644 --- a/test/compiler.test.js +++ b/test/compiler.test.js @@ -1548,9 +1548,7 @@ describe('compiler', function() { }); describe('for middleware', function() { - - function testMiddlewareRegistration(middlewareId, sourceFile, - alternativeDirTest) { + function testMiddlewareRegistration(middlewareId, sourceFile) { var json = { initial: { }, @@ -1564,21 +1562,7 @@ describe('compiler', function() { appdir.writeConfigFileSync('middleware.json', json); - var instructions; - - if (alternativeDirTest === true) { - var customDir = path.resolve(appdir.PATH, 'custom'); - fs.mkdirsSync(customDir); - fs.renameSync( - path.resolve(appdir.PATH, 'middleware.json'), - path.resolve(customDir, 'middleware.json')); - instructions = boot.compile({ - appRootDir: appdir.PATH, - middlewareRootDir: path.resolve(appdir.PATH, 'custom') - }); - } else { - instructions = boot.compile(appdir.PATH); - } + var instructions = boot.compile(appdir.PATH); expect(instructions.middleware).to.eql({ phases: ['initial', 'custom'], @@ -1606,21 +1590,42 @@ describe('compiler', function() { sourceFileForUrlNotFound); }); - it('Suports `middlewareRootDir` Option And ' + - 'emits middleware instructions', function() { - testMiddlewareRegistration('loopback/server/middleware/url-not-found', - sourceFileForUrlNotFound, true); - }); - it('emits middleware instructions for fragment', function() { testMiddlewareRegistration('loopback#url-not-found', sourceFileForUrlNotFound); }); - it('Suports `middlewareRootDir` Option And ' + - 'emits middleware instructions for fragment', function() { - testMiddlewareRegistration('loopback#url-not-found', - sourceFileForUrlNotFound, true); + 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() { @@ -2240,7 +2245,7 @@ describe('compiler', function() { describe('for components', function() { // Validate merging of component configuration from different locations - function testComponentConfigsMerge(alternativeDirTest) { + function testComponentConfigsMerge() { appdir.createConfigFilesSync(); appdir.writeConfigFileSync('component-config.json', { debug: { option: 'value' }, @@ -2254,31 +2259,7 @@ describe('compiler', function() { debug: { env: 'applied' }, }); - var instructions; - - if (alternativeDirTest === true) { - var customDir = path.resolve(appdir.PATH, 'custom'); - fs.mkdirsSync(customDir); - - fs.renameSync( - path.resolve(appdir.PATH, 'component-config.json'), - path.resolve(customDir, 'component-config.json')); - - fs.renameSync( - path.resolve(appdir.PATH, 'component-config.local.json'), - path.resolve(customDir, 'component-config.local.json')); - - fs.renameSync( - path.resolve(appdir.PATH, 'component-config.' + env + '.json'), - path.resolve(customDir, 'component-config.' + env + '.json')); - - instructions = boot.compile({ - appRootDir: appdir.PATH, - componentRootDir: path.resolve(appdir.PATH, 'custom') - }); - } else { - instructions = boot.compile(appdir.PATH); - } + var instructions = boot.compile(appdir.PATH); var component = instructions.components[0]; expect(component).to.eql({ @@ -2295,9 +2276,28 @@ describe('compiler', function() { testComponentConfigsMerge(); }); - it('Suports `componentRootDir` Option And ' + - 'loads component configs from multiple files', function() { - testComponentConfigsMerge(true); + 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() { From b2ae93a0cc2f527fb654796e763d063ce063a5c5 Mon Sep 17 00:00:00 2001 From: Doped Dude Date: Fri, 8 Jul 2016 01:56:50 +0530 Subject: [PATCH 5/6] removed comments --- test/compiler.test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/compiler.test.js b/test/compiler.test.js index 779c808..9e2353e 100644 --- a/test/compiler.test.js +++ b/test/compiler.test.js @@ -2244,7 +2244,6 @@ describe('compiler', function() { }); describe('for components', function() { - // Validate merging of component configuration from different locations function testComponentConfigsMerge() { appdir.createConfigFilesSync(); appdir.writeConfigFileSync('component-config.json', { From 47ae47d9aa73d0b0f2ac632280c1ecc45f7cc91e Mon Sep 17 00:00:00 2001 From: Doped Dude Date: Fri, 8 Jul 2016 02:00:15 +0530 Subject: [PATCH 6/6] reverted local updates --- test/compiler.test.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/test/compiler.test.js b/test/compiler.test.js index 9e2353e..41b28c9 100644 --- a/test/compiler.test.js +++ b/test/compiler.test.js @@ -2244,7 +2244,7 @@ describe('compiler', function() { }); describe('for components', function() { - function testComponentConfigsMerge() { + it('loads component configs from multiple files', function() { appdir.createConfigFilesSync(); appdir.writeConfigFileSync('component-config.json', { debug: { option: 'value' }, @@ -2269,10 +2269,6 @@ describe('compiler', function() { env: 'applied', }, }); - } - - it('loads component configs from multiple files', function() { - testComponentConfigsMerge(); }); it('supports `componentRootDir` option', function() {