Skip to content

Commit

Permalink
feat: add new mixin loadCustomLoader in Loader
Browse files Browse the repository at this point in the history
  • Loading branch information
popomore committed Feb 28, 2019
1 parent 1ce5549 commit a39dac1
Show file tree
Hide file tree
Showing 10 changed files with 164 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
45 changes: 45 additions & 0 deletions lib/loader/mixin/custom_loader.js
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:
}
}
},
};
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;
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);
};
14 changes: 14 additions & 0 deletions test/fixtures/custom-loader/config/config.default.js
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',
},
},
};
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"
}
50 changes: 50 additions & 0 deletions test/loader/mixin/load_custom_loader.test.js
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();
}
});

});

0 comments on commit a39dac1

Please sign in to comment.