-
Notifications
You must be signed in to change notification settings - Fork 92
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
Ref eggjs/egg#3480
- Loading branch information
Showing
10 changed files
with
164 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,45 @@ | ||
'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}`); | ||
|
||
const directory = path.join(loader.appInfo.baseDir, loaderConfig.directory); | ||
const inject = loaderConfig.inject || 'app'; | ||
// don't override inject and directory | ||
delete loaderConfig.directory; | ||
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: | ||
} | ||
} | ||
}, | ||
}; |
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,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,14 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
customLoader: { | ||
adapter: { | ||
directory: 'app/adapter', | ||
inject: 'app', | ||
}, | ||
repository: { | ||
directory: 'app/repository', | ||
inject: '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,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,50 @@ | ||
'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('sss', () => { | ||
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(); | ||
} | ||
}); | ||
|
||
}); |