Skip to content

Commit

Permalink
feat: add new mixin loadCustomLoader in Loader (#202)
Browse files Browse the repository at this point in the history
<!--
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
popomore committed Mar 6, 2019
1 parent d7c2c9a commit 3299be4
Show file tree
Hide file tree
Showing 14 changed files with 253 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ test/fixtures/egg/node_modules/egg-core
.idea
.nyc_output
package-lock.json
run
1 change: 1 addition & 0 deletions lib/loader/egg_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ const loaders = [
require('./mixin/middleware'),
require('./mixin/controller'),
require('./mixin/router'),
require('./mixin/custom_loader'),
];

for (const loader of loaders) {
Expand Down
53 changes: 53 additions & 0 deletions lib/loader/mixin/custom_loader.js
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');
}
}
},
};
14 changes: 14 additions & 0 deletions test/fixtures/custom-loader/app/adapter/docker.js
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;
17 changes: 17 additions & 0 deletions test/fixtures/custom-loader/app/controller/user.js
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;
9 changes: 9 additions & 0 deletions test/fixtures/custom-loader/app/plugin/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

class PluginA {
getName() {
return 'plugina';
}
}

module.exports = PluginA;
14 changes: 14 additions & 0 deletions test/fixtures/custom-loader/app/repository/user.js
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;
5 changes: 5 additions & 0 deletions test/fixtures/custom-loader/app/router.js
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);
};
9 changes: 9 additions & 0 deletions test/fixtures/custom-loader/config/b/app/plugin/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

class PluginB {
getName() {
return 'pluginb';
}
}

module.exports = PluginB;
5 changes: 5 additions & 0 deletions test/fixtures/custom-loader/config/b/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"eggPlugin": {
"name": "b"
}
}
19 changes: 19 additions & 0 deletions test/fixtures/custom-loader/config/config.default.js
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,
},
},
};
8 changes: 8 additions & 0 deletions test/fixtures/custom-loader/config/plugin.js
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'),
};
3 changes: 3 additions & 0 deletions test/fixtures/custom-loader/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "custom-loader"
}
95 changes: 95 additions & 0 deletions test/loader/mixin/load_custom_loader.test.js
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();
}
});
});

0 comments on commit 3299be4

Please sign in to comment.