Skip to content

Commit

Permalink
fix: configuration use package name (midwayjs#485)
Browse files Browse the repository at this point in the history
  • Loading branch information
kurten authored May 6, 2020
1 parent 8ddfc38 commit a00cb18
Show file tree
Hide file tree
Showing 27 changed files with 276 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ package-lock.json
.nodejs-cache
.tmp
packages/midway/_package.json
public
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"version": "1.0.0",
"devDependencies": {
"@types/chai": "^4.2.11",
"@types/lodash": "^4.14.149",
"@types/lodash": "^4.14.150",
"@types/mocha": "^5.2.7",
"@types/node": "^10.17.18",
"@types/node": "^10.17.21",
"autocannon": "^2.4.1",
"chalk": "^3.0.0",
"debug": "^4.1.1",
Expand Down
18 changes: 12 additions & 6 deletions packages/midway-core/src/context/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const debug = require('debug')('midway:container:configuration');
export class ContainerConfiguration implements IContainerConfiguration {
container: IMidwayContainer;
namespace: string;
packageName: string;
loadDirs: string[] = [];
importObjects: object = new Map();

Expand Down Expand Up @@ -107,6 +108,7 @@ export class ContainerConfiguration implements IContainerConfiguration {
let cfgFile;
let loadDir;
if (pkg) {
this.packageName = pkg.name;
if (this.namespace !== MAIN_MODULE_KEY) {
this.namespace =
pkg.midwayNamespace !== undefined ? pkg.midwayNamespace : pkg.name;
Expand Down Expand Up @@ -156,14 +158,18 @@ export class ContainerConfiguration implements IContainerConfiguration {
this.namespace = configurationOptions.namespace;
}

if (!this.packageName) {
this.packageName = this.namespace;
}

if (
this.container.containsConfiguration(this.namespace) &&
this.container.containsConfiguration(this.packageName) &&
this.namespace !== ''
) {
debug(`configuration ${this.namespace} exist than ignore.`);
debug(`configuration ${this.namespace}/${this.packageName} exist than ignore.`);
return;
} else {
debug(`configuration ${this.namespace} not exist than add.`);
debug(`configuration ${this.namespace}/${this.packageName} not exist than add.`);
this.container.addConfiguration(this);
}
this.addImports(configurationOptions.imports, baseDir);
Expand All @@ -174,13 +180,13 @@ export class ContainerConfiguration implements IContainerConfiguration {
}
} else {
if (
this.container.containsConfiguration(this.namespace) &&
this.container.containsConfiguration(this.packageName) &&
this.namespace !== ''
) {
debug(`configuration ${this.namespace} exist than ignore.`);
debug(`configuration ${this.namespace}/${this.packageName} exist than ignore.`);
return;
} else {
debug(`configuration ${this.namespace} not exist than add.`);
debug(`configuration ${this.namespace}/${this.packageName} not exist than add.`);
this.container.addConfiguration(this);
}
}
Expand Down
7 changes: 4 additions & 3 deletions packages/midway-core/src/context/midwayContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ export class MidwayContainer extends Container implements IMidwayContainer {
this.registerImportObjects(configuration.getImportObjects());

// load configuration
for (const [namespace, containerConfiguration] of this.configurationMap) {
for (const [packageName, containerConfiguration] of this.configurationMap) {
debug(`load ${packageName}`);
// main 的需要 skip 掉
if (namespace === MAIN_MODULE_KEY) {
if (containerConfiguration.namespace === MAIN_MODULE_KEY) {
continue;
}

Expand Down Expand Up @@ -221,7 +222,7 @@ export class MidwayContainer extends Container implements IMidwayContainer {
if (configuration.namespace === '') {
this.likeMainConfiguration.push(configuration);
} else {
this.configurationMap.set(configuration.namespace, configuration);
this.configurationMap.set(configuration.packageName, configuration);
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/midway-core/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ export const MAIN_MODULE_KEY = '__main__';

export interface IContainerConfiguration {
namespace: string;
packageName: string;
addLoadDir(dir: string);
addImports(imports: string[], baseDir?: string);
addImportObjects(importObjects: any[]);
Expand Down
8 changes: 0 additions & 8 deletions packages/midway-core/test/context/container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
Parent,
BaseService,
BaseServiceAsync,
BaseServiceGenerator,
Katana,
Ninja,
Samurai,
Expand Down Expand Up @@ -194,13 +193,6 @@ describe('/test/context/container.test.ts', () => {
expect(ins.foodNumber).to.equal(20);
});

xit('should execute generator init method when object created', async () => {
const container = new Container();
container.bind(BaseServiceGenerator);
const ins = await container.getAsync(BaseServiceGenerator) as BaseServiceGenerator;
expect(ins.foodNumber).to.equal(20);
});

it('should support constructor inject', async () => {
const container = new Container();
container.bind('engine', Turbo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { Configuration } from '@midwayjs/decorator';
imports: [
'../../midway-plugin-mock/src',
'../../midway-plugin-ok/src',
'../../midway-plugin-no-pkg-json/dist'
'../../midway-plugin-no-pkg-json/dist',
'../../midway-plugin-oktwo/src'
],
importObjects: {
aa: 123
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "midway-plugin-mock",
"name": "midway-plugin-ok",
"main": "src/index.ts"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "midway-plugin-oktwo",
"main": "src/index.ts"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export = {
ok: {
text: 'oktwo'
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export = {
ok: {
text: 'oktwo'
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Configuration } from '@midwayjs/decorator';

@Configuration({
namespace: 'ok',
importConfigs: [
'./config/config.default',
'./config/config.local'
]
})
export class AutoConfiguraion {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Config, Provide, Inject} from '@midwayjs/decorator';

@Provide()
export class ReplaceManagerTwo {
hello;
constructor(@Inject() ctx: any) {
this.hello = ctx;
}
@Inject()
ctx: any;

@Config('ok.text')
config;

async getOne() {
return this.config;
}
}
7 changes: 5 additions & 2 deletions packages/midway-core/test/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,14 @@ describe('/test/loader.test.ts', () => {

const appCtx = loader.getApplicationContext();
const replaceManager: any = await appCtx.getAsync('@ok:replaceManager');
assert((await replaceManager.getOne()) === 'ok1');
assert((await replaceManager.getOne()) === 'oktwo');
const replaceManagerno: any = await appCtx.getAsync(
'@midway-plugin-no-pkg-json:replaceManager'
);
assert((await replaceManagerno.getOne()) === 'ok1');
assert((await replaceManagerno.getOne()) === 'oktwo');

const replaceManagerTwo: any = await appCtx.getAsync('@ok:replaceManagerTwo');
assert((await replaceManagerTwo.getOne()) === 'oktwo');
mm.restore();
});

Expand Down
46 changes: 44 additions & 2 deletions packages/midway-web/test/enhance.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const assert = require('assert');
const request = require('supertest');
import { clearAllModule } from '@midwayjs/decorator';
import * as path from 'path';
import path = require('path');
import urllib = require('urllib');

const utils = require('./utils');
const mm = require('mm');
Expand Down Expand Up @@ -534,10 +535,28 @@ describe('/test/enhance.test.ts', () => {
app = utils.app('enhance/base-app-hackernews', {
typescript: false,
});
const originRequest = urllib.HttpClient2.prototype.request;
mm(urllib.HttpClient2.prototype, 'request', (url, args, callback) => {
if (url) {
if (url.includes('https://hacker-news.firebaseio.com/v0/item')) {
return { data: JSON.parse('{"by":"pg","descendants":15,"id":1,"kids":[15,234509,487171,454426,454424,454410,82729],"score":57,"time":1160418111,"title":"Y Combinator","type":"story","url":"http://ycombinator.com"}')};
}
if (url.includes('https://hacker-news.firebaseio.com/v0/user')) {
return { data: JSON.parse('{"created":1344225010,"id":"stevage","karma":164,"submitted":[23038727,23013820,23013797,22995592,22820177,22819227,22817427,22659470,22624885,22624467,22621483,22333639,22305974,22143659,22069408,21987055,21987045,21807698,21807677,21799835,21662201,20438536,20290644,20261053,20102070,20018617,19134123,19134104,19134065,19134056,18803141,18803098,17922891,17902520,17850980,17780847,17534650,17435464,17386143,17335732,17161325,15890590,15414238,14785201,14493573,14393971,14251559,14176015,14029087,13793286,13621128,13621127,13274921,13138573,12497739,4343630]}')};
}
if (url.includes('https://hacker-news.firebaseio.com/v0/topstories')) {
return { data: JSON.parse('{"12":23064974,"8":23072690,"19":23076081,"23":23071190,"4":23074435,"15":23070821,"11":23075484,"9":23076341,"22":23071134,"26":23064859,"13":23076241,"24":23072696,"16":23075556,"5":23073126,"10":23072956,"21":23069372,"6":23069114,"1":23073000,"17":23075097,"25":23074312,"14":23075893,"20":23065902,"27":23072443,"2":23072333,"18":23073109,"30":23073455,"7":23070567,"29":23070151,"3":23076007,"28":23071867}')};
}
}
return originRequest(url, args, callback);
});
return app.ready();
});

after(() => app.close());
after(() => {
mm.restore();
return app.close();
});

it('news should be ok', async () => {
await app.httpRequest()
Expand All @@ -564,4 +583,27 @@ describe('/test/enhance.test.ts', () => {
.expect(200);
});
});

describe('plugin error should be ok', () => {
it('error', async () => {
const originJoin = path.join;
mm(path, 'join', (...args) => {
if (args[0] === path.resolve(__dirname, '../src/loader')) {
return originJoin(__dirname, '/../node_modules');
}
return originJoin.apply(path, args);
});
const app = utils.app('enhance/base-app-plugin-error', {
typescript: true,
});
let msg = '';
try {
await app.ready();
} catch (e) {
msg = e.message;
}
assert.ok(msg.includes('Can not find plugin plugin2 in'));
mm.restore();
});
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { UserService } from './userService';
import { Scope, Inject, ScopeEnum } from '@midwayjs/decorator';
import { Scope, Inject, ScopeEnum, Controller, Provide } from '@midwayjs/decorator';

@Scope(ScopeEnum.Request)
@Controller('/tt')
@Provide()
export class UserController {
@Inject('ctx')
ctx;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
daily
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "ali-demo"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

exports.index = function*() {
this.body = 'hello';
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = function(app) {
app.get('/api/index', 'api');
app.get('/api', app.controller.api.index);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

export const keys = 'key';

export const hello = {
a: 1,
b: 2,
d: [1, 2, 3],
};

export const plugins = {
bucLogin: false,
};

export const container = {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

module.exports = {
// 默认开启的插件

/**
* 支持各个 bu 的健康检查
*/

plugin3: {
enable: true,
path: '../node_modules/plugin3'
},
plugin2: {
enable: true,
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { provide, inject } from '../../../../../../src';

@provide()
export class UserService {
@inject()
ctx;

async hello() {
return 'world,' + Object.keys(this.ctx.query).length;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ export const hello = {
export const plugins = {
bucLogin: false,
};

export const container = {};
Loading

0 comments on commit a00cb18

Please sign in to comment.