diff --git a/.autod.conf b/.autod.conf new file mode 100644 index 00000000..4e511faf --- /dev/null +++ b/.autod.conf @@ -0,0 +1,17 @@ +'ues strict'; + +module.exports = { + write: true, + prefix: '^', + devprefix: '^', + exclude: [ + 'test/fixtures', + ], + devdep: [ + 'autod', + ], + keep: [ + ], + semver: [ + ], +}; diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 00000000..48f9944f --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,24 @@ + + +##### Checklist + + +- [ ] `npm test` passes +- [ ] tests and/or benchmarks are included +- [ ] documentation is changed or added +- [ ] commit message follows commit guidelines + +##### Affected core subsystem(s) + + + +##### Description of change + diff --git a/README.md b/README.md index c6911864..60ef3ae5 100644 --- a/README.md +++ b/README.md @@ -211,13 +211,23 @@ this.loadExtend('application', app); ### LoaderOptions -- {String|Array} directory - directories to load -- {Object} target: attach object from loaded files, -- {String} ignore - ignore the files when load -- {Function} initializer - custom file exports -- {Boolean} lowercaseFirst - determine whether the fist letter is lowercase -- {Boolean} override: determine whether override the property when get the same name -- {Boolean} call - determine whether invoke when exports is function -- {Object} inject - an object that be the argument when invoke the function +Param | Type | Description +-------------- | -------------- | ------------------------ +directory | `String/Array` | directories to load +target | `Object` | attach object from loaded files +ignore | `String` | ignore the files when load +initializer | `Function` | custom file exports, receive two parameters, first is the inject object, second is an `options` object that contain `path` +lowercaseFirst | `Boolean` | determine whether the fist letter is lowercase +override | `Boolean` | determine whether override the property when get the same name +call | `Boolean` | determine whether invoke when exports is function +inject | `Object` | an object that be the argument when invoke the function + +## Questions & Suggestions + +Please open an issue [here](https://github.com/eggjs/egg/issues). + +## License + +[MIT](LICENSE) [egg]: https://github.com/eggjs/egg diff --git a/lib/loader/egg_loader.js b/lib/loader/egg_loader.js index c6e70c26..67f56fc1 100644 --- a/lib/loader/egg_loader.js +++ b/lib/loader/egg_loader.js @@ -265,6 +265,14 @@ class EggLoader { new ContextLoader(opt).load(); } + get FileLoader() { + return FileLoader; + } + + get ContextLoader() { + return ContextLoader; + } + } /** diff --git a/lib/loader/file_loader.js b/lib/loader/file_loader.js index 3f97a493..f88eee97 100644 --- a/lib/loader/file_loader.js +++ b/lib/loader/file_loader.js @@ -119,7 +119,7 @@ function getExports(fullpath, initializer, isCall, inject) { // process exports as you like if (initializer) { - exports = initializer(exports); + exports = initializer(exports, { path: fullpath }); } // return exports when it's a class or generator diff --git a/package.json b/package.json index 51a1e09b..1a751034 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "A core Plugable framework based on koa", "main": "index.js", "scripts": { + "autod": "autod", "lint": "eslint lib test *.js", "test": "npm run lint && egg-bin test", "cov": "egg-bin cov", @@ -30,29 +31,30 @@ "version": "4, 6" }, "devDependencies": { + "autod": "^2.7.0", "egg-bin": "1", "egg-ci": "1", "eslint": "~3.1.0", "eslint-config-egg": "3", "koa": "1", "koa-router": "4", - "mm": "1", + "mm": "^2.0.0", "pedding": "^1.0.0", - "should": "9", - "supertest": "1" + "should": "^11.1.0", + "supertest": "^2.0.0" }, "dependencies": { "debug": "^2.2.0", "depd": "^1.1.0", - "egg-logger": "^1.1.0", + "egg-logger": "^1.2.0", "extend": "^3.0.0", "globby": "^6.0.0", "inflection": "^1.10.0", "interop-require": "^1.0.0", "is-type-of": "^1.0.0", - "koa": "^1.2.0", + "koa": "^1.2.1", "koa-router": "^5.4.0", "ready-callback": "^1.0.0", "utility": "^1.8.0" } -} +} \ No newline at end of file diff --git a/test/fixtures/load_dirs/dao/TestClass.js b/test/fixtures/load_dirs/dao/TestClass.js index aea1ecbc..e097d78c 100644 --- a/test/fixtures/load_dirs/dao/TestClass.js +++ b/test/fixtures/load_dirs/dao/TestClass.js @@ -1,9 +1,11 @@ 'use strict'; module.exports = class TestClass { - constructor() { + constructor(app, fullpath) { this.user = { name: 'kai.fangk', }; + this.app = app; + this.path = fullpath; } } diff --git a/test/loader/egg_loader.test.js b/test/loader/egg_loader.test.js new file mode 100644 index 00000000..59371249 --- /dev/null +++ b/test/loader/egg_loader.test.js @@ -0,0 +1,18 @@ +'use strict'; + +const should = require('should'); +const utils = require('../utils'); + +describe('test/loader/egg_loader.test.js', () => { + + let app; + before(() => { + app = utils.createApp('nothing'); + }); + + it('should container FileLoader and ContextLoader', () => { + should.exists(app.loader.FileLoader); + should.exists(app.loader.ContextLoader); + }); + +}); diff --git a/test/loader/file_loader.test.js b/test/loader/file_loader.test.js index be0dc149..8addb5e8 100644 --- a/test/loader/file_loader.test.js +++ b/test/loader/file_loader.test.js @@ -155,12 +155,14 @@ describe('test/file_loader.test.js', () => { directory: path.join(dirBase, 'dao'), target: app.dao, ignore: 'util/**', - initializer(exports) { - return new exports(app); + initializer(exports, opt) { + return new exports(app, opt.path); }, }).load(); app.dao.should.have.property('TestClass'); app.dao.TestClass.user.should.eql({ name: 'kai.fangk' }); + app.dao.TestClass.app.should.equal(app); + app.dao.TestClass.path.should.equal(path.join(dirBase, 'dao/TestClass.js')); app.dao.should.have.property('testFunction', { user: { name: 'kai.fangk' } }); app.dao.should.have.property('testReturnFunction', { user: { name: 'kai.fangk' } }); });