diff --git a/index.js b/index.js index 36f9b87..d13ab82 100644 --- a/index.js +++ b/index.js @@ -26,7 +26,21 @@ module.exports = class Trailpack { throw new Error('Trailpack is missing package definition ("pack.pkg")') } if (!pack.config) { - pack.config = { } + pack.config = {} + } + if (!pack.config.trailpack) { + pack.config.trailpack = { + lifecycle: { + initialize: { + listen: [], + emit: [] + }, + configure: { + listen: [], + emit: [] + } + } + } } Object.defineProperties(this, { @@ -44,6 +58,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/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 3463d00..30e4bca 100644 --- a/test/app.js +++ b/test/app.js @@ -34,6 +34,15 @@ const App = { } }, main: { + packsConfig: { + 'trailpack-testpack': { + lifecycle: { + initialize: { + emit: ['trailpack:testpack:custom'] + } + } + } + }, packs: [ smokesignals.Trailpack, require('./pack') 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: [] + } + } + } } }) } 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 a3970c1..331ddf2 100644 --- a/test/trailpack.test.js +++ b/test/trailpack.test.js @@ -15,6 +15,12 @@ describe('Trailpack', () => { global.app.after('trailpack:testpack:constructed').then(() => done() ) 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) + }) }) describe('#name', () => { @@ -45,5 +51,6 @@ describe('Trailpack', () => { pack.log.debug('hello') }) }) + })