From 233b3aa0b7bbcb854df78f548cb9380e9399baf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Catriel=20M=C3=BCller?= Date: Tue, 20 Sep 2016 09:25:20 -0300 Subject: [PATCH 1/4] Added feature for override trailpacks configs from the application config --- index.js | 1 + lib/util.js | 10 ++++++++++ test/app.js | 5 +++++ test/trailpack.test.js | 5 +++++ 4 files changed, 21 insertions(+) diff --git a/index.js b/index.js index 36f9b87..ba71680 100644 --- a/index.js +++ b/index.js @@ -44,6 +44,7 @@ module.exports = class Trailpack { } }) + lib.Util.mergeApplicationTrailpackConfig(this.app, pack) lib.Util.mergeApplication(this.app, pack) lib.Util.mergeApplicationConfig(this.app, pack) lib.Util.mergeEnvironmentConfig(pack.config, this.app.config) diff --git a/lib/util.js b/lib/util.js index f2ce677..7aa443a 100644 --- a/lib/util.js +++ b/lib/util.js @@ -86,6 +86,16 @@ module.exports = { }) }, + /** + * Override the trailpack's config with application definition per trailpack + */ + mergeApplicationTrailpackConfig (app, pack) { + if (app.config.main.packsConfig[pack.pkg.name]){ + return _.merge(pack.config.trailpack, app.config.main.packsConfig[pack.pkg.name]) + } + return + }, + mergeDefaultTrailpackConfig (packConfig, defaultConfig) { return _.defaultsDeep(packConfig || { }, defaultConfig.trailpack) } diff --git a/test/app.js b/test/app.js index 3463d00..843c9fc 100644 --- a/test/app.js +++ b/test/app.js @@ -34,6 +34,11 @@ const App = { } }, main: { + packsConfig: { + 'trailpack-test': { + testattr: 'testvalue' + } + }, packs: [ smokesignals.Trailpack, require('./pack') diff --git a/test/trailpack.test.js b/test/trailpack.test.js index a3970c1..29264d0 100644 --- a/test/trailpack.test.js +++ b/test/trailpack.test.js @@ -15,6 +15,10 @@ describe('Trailpack', () => { global.app.after('trailpack:testpack:constructed').then(() => done() ) new TestPack(global.app) }) + it('should override the trailpack config', () => { + const pack = new TestPack(global.app) + assert.equal(pack.config.trailpack.testattr, 'testvalue') + }) }) describe('#name', () => { @@ -45,5 +49,6 @@ describe('Trailpack', () => { pack.log.debug('hello') }) }) + }) From 50d3d13cb4b03fc0f5cd56e5c303fb5faf15fe20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Catriel=20M=C3=BCller?= Date: Thu, 29 Sep 2016 20:19:52 -0300 Subject: [PATCH 2/4] Fix Test --- index.js | 3 +++ package.json | 2 +- test/app.js | 8 ++++++-- test/pack/config/trailpack.js | 23 ++++++++--------------- test/pack/index.js | 6 ++---- test/trailpack.test.js | 4 +++- 6 files changed, 23 insertions(+), 23 deletions(-) diff --git a/index.js b/index.js index ba71680..311fd58 100644 --- a/index.js +++ b/index.js @@ -28,6 +28,9 @@ module.exports = class Trailpack { if (!pack.config) { pack.config = { } } + if (!pack.config.trailpack) { + pack.config.trailpack = { } + } Object.defineProperties(this, { app: { diff --git a/package.json b/package.json index ec0c8b1..00b99fb 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "eslint": "^2.11.1", "eslint-config-trails": "^1.0.7", "mocha": "^2.5.0", - "trails": "^1.0.3", + "trails": "catrielmuller/trails#config-trailpacks-in-app", "smokesignals": "^1.2.2" }, "engines": { diff --git a/test/app.js b/test/app.js index 843c9fc..30e4bca 100644 --- a/test/app.js +++ b/test/app.js @@ -35,8 +35,12 @@ const App = { }, main: { packsConfig: { - 'trailpack-test': { - testattr: 'testvalue' + 'trailpack-testpack': { + lifecycle: { + initialize: { + emit: ['trailpack:testpack:custom'] + } + } } }, packs: [ diff --git a/test/pack/config/trailpack.js b/test/pack/config/trailpack.js index 4643e39..a3ac062 100644 --- a/test/pack/config/trailpack.js +++ b/test/pack/config/trailpack.js @@ -4,34 +4,27 @@ * @see {@link http://trailsjs.io/doc/trailpack/config */ module.exports = { - + type: 'misc', /** - * API and config resources provided by this Trailpack. + * Configure the lifecycle of this pack; that is, how it boots up, and which + * order it loads relative to other trailpacks. */ - provides: { - api: { - controllers: [ ] - // ... - }, - config: [ ] - }, - - events: { + lifecycle: { configure: { /** * List of events that must be fired before the configure lifecycle * method is invoked on this Trailpack */ - listen: [ ], + listen: [], /** * List of events emitted by the configure lifecycle method */ - emit: [ ] + emit: [] }, initialize: { - listen: [ ], - emit: [ ] + listen: [], + emit: [] } } } diff --git a/test/pack/index.js b/test/pack/index.js index 3c54eef..1e20484 100644 --- a/test/pack/index.js +++ b/test/pack/index.js @@ -1,16 +1,14 @@ 'use strict' -const app = { - config: require('./config') -} const Trailpack = require('../../') module.exports = class TestPack extends Trailpack { - constructor () { + constructor (app) { super(app, { config: require('./config'), pkg: require('./package') }) } + } diff --git a/test/trailpack.test.js b/test/trailpack.test.js index 29264d0..331ddf2 100644 --- a/test/trailpack.test.js +++ b/test/trailpack.test.js @@ -16,8 +16,10 @@ describe('Trailpack', () => { new TestPack(global.app) }) it('should override the trailpack config', () => { + global.app.after('trailpack:testpack:constructed').then(() => { + assert.equal(pack.config.lifecycle.initialize.emit[0], 'trailpack:testpack:custom') + }) const pack = new TestPack(global.app) - assert.equal(pack.config.trailpack.testattr, 'testvalue') }) }) From b0ec4e1dfcf4f10ed6cb24e6c44a29a4685f8d58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Catriel=20M=C3=BCller?= Date: Tue, 11 Oct 2016 02:40:17 -0300 Subject: [PATCH 3/4] Validate the basic Configuration --- index.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- test/pack.js | 14 +++++++++++++- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 311fd58..196e38a 100644 --- a/index.js +++ b/index.js @@ -26,11 +26,56 @@ module.exports = class Trailpack { throw new Error('Trailpack is missing package definition ("pack.pkg")') } if (!pack.config) { - pack.config = { } + throw new Error('[' + pack.pkg.name + '] ' + + 'Trailpack is missing package configuration ("pack.config")') } if (!pack.config.trailpack) { - pack.config.trailpack = { } + throw new Error('[' + pack.pkg.name + '] ' + + 'Trailpack is missing configuration ("pack.config.trailpack")') } + if (!pack.config.trailpack.lifecycle + || typeof pack.config.trailpack.lifecycle !== 'object' ) { + throw new Error('[' + pack.pkg.name + '] ' + + 'Trailpack lifecycle configuration should be an object ' + + '("pack.config.trailpack.lifecycle")') + } + if (!pack.config.trailpack.lifecycle.configure + || typeof pack.config.trailpack.lifecycle.configure !== 'object' ) { + throw new Error('[' + pack.pkg.name + '] ' + + 'Trailpack lifecycle configuration should be an object ' + + '("pack.config.trailpack.lifecycle.configure")') + } + if (!pack.config.trailpack.lifecycle.configure.listen + || !Array.isArray(pack.config.trailpack.lifecycle.configure.listen) ) { + throw new Error('[' + pack.pkg.name + '] ' + + 'Trailpack lifecycle configuration listeners should be an array ' + + '("pack.config.trailpack.lifecycle.configure.listen")') + } + if (!pack.config.trailpack.lifecycle.configure.emit + || !Array.isArray(pack.config.trailpack.lifecycle.configure.emit)) { + throw new Error('[' + pack.pkg.name + '] ' + + 'Trailpack lifecycle configuration emitters should be an array ' + + '("pack.config.trailpack.lifecycle.configure.emit")') + } + if (!pack.config.trailpack.lifecycle.initialize + || typeof pack.config.trailpack.lifecycle.initialize !== 'object' ) { + throw new Error('[' + pack.pkg.name + '] ' + + 'Trailpack lifecycle initialize should be an object ' + + '("pack.config.trailpack.lifecycle.initialize")') + } + if (!pack.config.trailpack.lifecycle.initialize.listen + || !Array.isArray(pack.config.trailpack.lifecycle.initialize.listen) ) { + throw new Error('[' + pack.pkg.name + '] ' + + 'Trailpack lifecycle initialize listeners should be an array ' + + '("pack.config.trailpack.lifecycle.initialize.listen")') + } + if (!pack.config.trailpack.lifecycle.initialize.emit + || !Array.isArray(pack.config.trailpack.lifecycle.initialize.emit) ) { + throw new Error('[' + pack.pkg.name + '] ' + + 'Trailpack lifecycle initialize emitters should be an array ' + + '("pack.config.trailpack.lifecycle.initialize.emit")') + } + Object.defineProperties(this, { app: { diff --git a/test/pack.js b/test/pack.js index a986424..3a58d71 100644 --- a/test/pack.js +++ b/test/pack.js @@ -9,7 +9,19 @@ module.exports = class TestPack extends Trailpack { name: 'trailpack-testpack' }, config: { - mypack: {} + mypack: {}, + trailpack: { + lifecycle: { + configure: { + listen: [], + emit: [] + }, + initialize: { + listen: [], + emit: [] + } + } + } } }) } From 84fbd7dcad11a7103ae73f5a56c4d3282d8c730d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Catriel=20M=C3=BCller?= Date: Tue, 27 Dec 2016 14:41:15 -0300 Subject: [PATCH 4/4] Fix Err --- index.js | 60 ++++++++++++-------------------------------------------- 1 file changed, 13 insertions(+), 47 deletions(-) diff --git a/index.js b/index.js index 196e38a..d13ab82 100644 --- a/index.js +++ b/index.js @@ -26,56 +26,22 @@ module.exports = class Trailpack { throw new Error('Trailpack is missing package definition ("pack.pkg")') } if (!pack.config) { - throw new Error('[' + pack.pkg.name + '] ' + - 'Trailpack is missing package configuration ("pack.config")') + pack.config = {} } if (!pack.config.trailpack) { - throw new Error('[' + pack.pkg.name + '] ' + - 'Trailpack is missing configuration ("pack.config.trailpack")') + pack.config.trailpack = { + lifecycle: { + initialize: { + listen: [], + emit: [] + }, + configure: { + listen: [], + emit: [] + } + } + } } - if (!pack.config.trailpack.lifecycle - || typeof pack.config.trailpack.lifecycle !== 'object' ) { - throw new Error('[' + pack.pkg.name + '] ' + - 'Trailpack lifecycle configuration should be an object ' + - '("pack.config.trailpack.lifecycle")') - } - if (!pack.config.trailpack.lifecycle.configure - || typeof pack.config.trailpack.lifecycle.configure !== 'object' ) { - throw new Error('[' + pack.pkg.name + '] ' + - 'Trailpack lifecycle configuration should be an object ' + - '("pack.config.trailpack.lifecycle.configure")') - } - if (!pack.config.trailpack.lifecycle.configure.listen - || !Array.isArray(pack.config.trailpack.lifecycle.configure.listen) ) { - throw new Error('[' + pack.pkg.name + '] ' + - 'Trailpack lifecycle configuration listeners should be an array ' + - '("pack.config.trailpack.lifecycle.configure.listen")') - } - if (!pack.config.trailpack.lifecycle.configure.emit - || !Array.isArray(pack.config.trailpack.lifecycle.configure.emit)) { - throw new Error('[' + pack.pkg.name + '] ' + - 'Trailpack lifecycle configuration emitters should be an array ' + - '("pack.config.trailpack.lifecycle.configure.emit")') - } - if (!pack.config.trailpack.lifecycle.initialize - || typeof pack.config.trailpack.lifecycle.initialize !== 'object' ) { - throw new Error('[' + pack.pkg.name + '] ' + - 'Trailpack lifecycle initialize should be an object ' + - '("pack.config.trailpack.lifecycle.initialize")') - } - if (!pack.config.trailpack.lifecycle.initialize.listen - || !Array.isArray(pack.config.trailpack.lifecycle.initialize.listen) ) { - throw new Error('[' + pack.pkg.name + '] ' + - 'Trailpack lifecycle initialize listeners should be an array ' + - '("pack.config.trailpack.lifecycle.initialize.listen")') - } - if (!pack.config.trailpack.lifecycle.initialize.emit - || !Array.isArray(pack.config.trailpack.lifecycle.initialize.emit) ) { - throw new Error('[' + pack.pkg.name + '] ' + - 'Trailpack lifecycle initialize emitters should be an array ' + - '("pack.config.trailpack.lifecycle.initialize.emit")') - } - Object.defineProperties(this, { app: {