-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new mixin loadCustomLoader in Loader (#202)
<!-- Thank you for your pull request. Please review below requirements. Bug fixes and new features should include tests and possibly benchmarks. Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md 感谢您贡献代码。请确认下列 checklist 的完成情况。 Bug 修复和新功能必须包含测试,必要时请附上性能测试。 Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md --> ##### Checklist <!-- Remove items that do not apply. For completed items, change [ ] to [x]. --> - [x] `npm test` passes - [x] tests and/or benchmarks are included - [x] documentation is changed or added - [x] commit message follows commit guidelines ##### Affected core subsystem(s) <!-- Provide affected core subsystem(s). --> ##### Description of change <!-- Provide a description of the change below this comment. -->
- Loading branch information
Showing
14 changed files
with
253 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ test/fixtures/egg/node_modules/egg-core | |
.idea | ||
.nyc_output | ||
package-lock.json | ||
run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const path = require('path'); | ||
|
||
module.exports = { | ||
loadCustomLoader() { | ||
const loader = this; | ||
assert(loader.config, 'should loadConfig first'); | ||
const customLoader = loader.config.customLoader || {}; | ||
|
||
for (const property of Object.keys(customLoader)) { | ||
const loaderConfig = Object.assign({}, customLoader[property]); | ||
assert(loaderConfig.directory, `directory is required for config.customLoader.${property}`); | ||
|
||
let directory; | ||
if (loaderConfig.loadunit === true) { | ||
directory = this.getLoadUnits().map(unit => path.join(unit.path, loaderConfig.directory)); | ||
} else { | ||
directory = path.join(loader.appInfo.baseDir, loaderConfig.directory); | ||
} | ||
// don't override directory | ||
delete loaderConfig.directory; | ||
|
||
const inject = loaderConfig.inject || 'app'; | ||
// don't override inject | ||
delete loaderConfig.inject; | ||
|
||
switch (inject) { | ||
case 'ctx': { | ||
const defaultConfig = { | ||
caseStyle: 'lower', | ||
fieldClass: `${property}Classes`, | ||
}; | ||
loader.loadToContext(directory, property, Object.assign(defaultConfig, loaderConfig)); | ||
break; | ||
} | ||
case 'app': { | ||
const defaultConfig = { | ||
caseStyle: 'lower', | ||
initializer(Class) { | ||
return new Class(loader.app); | ||
}, | ||
}; | ||
loader.loadToApp(directory, property, Object.assign(defaultConfig, loaderConfig)); | ||
break; | ||
} | ||
default: | ||
throw new Error('inject only support app or ctx'); | ||
} | ||
} | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use strict'; | ||
|
||
class DockerAdapter { | ||
constructor(app) { | ||
this.app = app; | ||
} | ||
|
||
async inspectDocker() { | ||
return this.app.config.customLoader.adapter; | ||
} | ||
|
||
} | ||
|
||
module.exports = DockerAdapter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict'; | ||
|
||
class UserController { | ||
constructor(ctx) { | ||
this.ctx = ctx; | ||
this.app = ctx.app; | ||
} | ||
|
||
async get() { | ||
this.ctx.body = { | ||
adapter: await this.app.adapter.docker.inspectDocker(), | ||
repository: await this.ctx.repository.user.get(), | ||
}; | ||
} | ||
} | ||
|
||
module.exports = UserController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use strict'; | ||
|
||
class PluginA { | ||
getName() { | ||
return 'plugina'; | ||
} | ||
} | ||
|
||
module.exports = PluginA; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use strict'; | ||
|
||
class UserRepository { | ||
constructor(ctx) { | ||
this.ctx = ctx; | ||
} | ||
|
||
async get() { | ||
return this.ctx.params.name; | ||
} | ||
|
||
} | ||
|
||
module.exports = UserRepository; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict'; | ||
|
||
module.exports = app => { | ||
app.router.get('/users/:name', app.controller.user.get); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use strict'; | ||
|
||
class PluginB { | ||
getName() { | ||
return 'pluginb'; | ||
} | ||
} | ||
|
||
module.exports = PluginB; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"eggPlugin": { | ||
"name": "b" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
customLoader: { | ||
adapter: { | ||
directory: 'app/adapter', | ||
inject: 'app', | ||
}, | ||
repository: { | ||
directory: 'app/repository', | ||
inject: 'ctx', | ||
}, | ||
plugin: { | ||
directory: 'app/plugin', | ||
inject: 'app', | ||
loadunit: true, | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
|
||
exports.b = { | ||
enable: true, | ||
path: path.join(__dirname, 'b'), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "custom-loader" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const request = require('supertest'); | ||
const utils = require('../../utils'); | ||
|
||
describe('test/loader/mixin/load_custom_loader.test.js', function() { | ||
let app; | ||
before(function() { | ||
app = utils.createApp('custom-loader'); | ||
app.loader.loadPlugin(); | ||
app.loader.loadConfig(); | ||
app.loader.loadController(); | ||
app.loader.loadRouter(); | ||
app.loader.loadCustomLoader(); | ||
}); | ||
after(() => app.close()); | ||
|
||
it('should load to app', async () => { | ||
const res = await app.adapter.docker.inspectDocker(); | ||
assert(res); | ||
assert(res.inject === 'app'); | ||
}); | ||
|
||
it('should load to ctx', async () => { | ||
await request(app.callback()) | ||
.get('/users/popomore') | ||
.expect({ | ||
adapter: { | ||
directory: 'app/adapter', | ||
inject: 'app', | ||
}, | ||
repository: 'popomore', | ||
}) | ||
.expect(200); | ||
}); | ||
|
||
it('should support loadunit', async () => { | ||
let name = app.plugin.a.getName(); | ||
assert(name === 'plugina'); | ||
name = app.plugin.b.getName(); | ||
assert(name === 'pluginb'); | ||
}); | ||
|
||
|
||
it('should loadConfig first', () => { | ||
const app = utils.createApp('custom-loader'); | ||
try { | ||
app.loader.loadCustomLoader(); | ||
throw new Error('should not run'); | ||
} catch (err) { | ||
assert(err.message === 'should loadConfig first'); | ||
} finally { | ||
app.close(); | ||
} | ||
}); | ||
|
||
it('support set directory', () => { | ||
const app = utils.createApp('custom-loader'); | ||
try { | ||
app.loader.config = { | ||
customLoader: { | ||
custom: { | ||
}, | ||
}, | ||
}; | ||
app.loader.loadCustomLoader(); | ||
throw new Error('should not run'); | ||
} catch (err) { | ||
assert(err.message === 'directory is required for config.customLoader.custom'); | ||
} finally { | ||
app.close(); | ||
} | ||
}); | ||
|
||
it('inject support app/ctx', () => { | ||
const app = utils.createApp('custom-loader'); | ||
try { | ||
app.loader.config = { | ||
customLoader: { | ||
custom: { | ||
directory: 'a', | ||
inject: 'unknown', | ||
}, | ||
}, | ||
}; | ||
app.loader.loadCustomLoader(); | ||
throw new Error('should not run'); | ||
} catch (err) { | ||
assert(err.message === 'inject only support app or ctx'); | ||
} finally { | ||
app.close(); | ||
} | ||
}); | ||
}); |