Skip to content

Commit

Permalink
fix: configuration bugs (#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
kurten authored Feb 25, 2020
1 parent 080b496 commit a6a18b2
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 3 deletions.
12 changes: 11 additions & 1 deletion packages/midway-core/src/context/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export class ContainerConfiguration implements IContainerConfiguration {
const subContainerConfiguration = this.container.createConfiguration();
const subPackageDir = this.resolvePackageBaseDir(importPackage, baseDir);
debug('import package => %s dir => %s.', importPackage, subPackageDir);
subContainerConfiguration.addLoadDir(subPackageDir);
subContainerConfiguration.load(subPackageDir);
}
}
Expand All @@ -38,6 +37,10 @@ export class ContainerConfiguration implements IContainerConfiguration {
}
}

getImportObjects() {
return this.importObjects;
}

addImportConfigs(importConfigs: string[], baseDir: string) {
debug('import configs %j baseDir => %s.', importConfigs, baseDir);
if (importConfigs && importConfigs.length) {
Expand Down Expand Up @@ -77,16 +80,23 @@ export class ContainerConfiguration implements IContainerConfiguration {
this.namespace = pkg.midwayNamespace ? pkg.midwayNamespace : pkg.name;
}
let cfgFile;
let loadDir;
if (pkg.main) {
const cfgFileDir = dirname(join(packageBaseDir, pkg.main));
cfgFile = join(cfgFileDir, 'configuration');
configuration = safeRequire(cfgFile);
debug('configuration file path one => %s.', cfgFile);
loadDir = cfgFileDir;
}
if (!configuration) {
cfgFile = `${packageBaseDir}/configuration`;
configuration = safeRequire(cfgFile);
debug('configuration file path two => %s.', cfgFile);
loadDir = packageBaseDir;
}
if (loadDir) {
this.addLoadDir(loadDir);
debug('add loadDir => %s namespace => %s.', loadDir, this.namespace);
}
}
debug('packageName => %s namespace => %s configuration file => %s.',
Expand Down
20 changes: 20 additions & 0 deletions packages/midway-core/src/context/midwayContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ export class MidwayContainer extends Container implements IMidwayContainer {
// loadDir
this.loadDirectory(opts);

this.registerImportObjects(configuration.getImportObjects());

// load configuration
for (const [namespace, containerConfiguration] of this.configurationMap) {
const subDirs = containerConfiguration.getImportDirectory();
Expand All @@ -99,6 +101,9 @@ export class MidwayContainer extends Container implements IMidwayContainer {
namespace
});
}

this.registerImportObjects(containerConfiguration.getImportObjects(),
containerConfiguration.namespace);
}
}

Expand Down Expand Up @@ -378,6 +383,21 @@ export class MidwayContainer extends Container implements IMidwayContainer {
await this.configService.load();
}
}
/**
* 注册 importObjects
* @param objs configuration 中的 importObjects
* @param namespace namespace
*/
private registerImportObjects(objs: any, namespace?: string) {
if (objs) {
const keys = Object.keys(objs);
for (const key of keys) {
if (typeof objs[key] !== undefined) {
this.registerObject(generateProvideId(key, namespace), objs[key]);
}
}
}
}
}

// TODO configuration 依赖去重
1 change: 1 addition & 0 deletions packages/midway-core/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ export interface IContainerConfiguration {
load(packageName: string);
loadConfiguration(configuration: IContainerConfiguration, baseDir: string);
getImportDirectory(): string[];
getImportObjects(): any;
}

export interface IMidwayContainer extends IContainer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@ import { Configuration } from '@midwayjs/decorator';
'../../midway-plugin-mock/src',
'../../midway-plugin-ok/src'
],
importObjects: {
aa: 123
}
})
export class AutoConfiguraion {}
class AutoConfiguraion {}

module.exports = AutoConfiguraion;
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,23 @@ export class BaseService {
@Inject('@midway-plugin-mock:articleManager')
articleManager;

@Inject('aa')
aa: any;

@Inject('@midway-plugin-mock:cc')
cc: any;

async getInformation() {
const result1 = await this.userManager.getUser();
const result2 = await this.articleManager.getOne();
return result1 + ',' + result2;
}

getAaa() {
return this.aa;
}

getCcc() {
return this.cc;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Configuration } from '@midwayjs/decorator';

@Configuration({
namespace: ''
namespace: '',
importObjects: {
bb: 123,
cc: 'mock'
}
})
export class AutoConfiguraion {}
2 changes: 2 additions & 0 deletions packages/midway-core/test/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ describe('/test/loader.test.ts', () => {
const appCtx = loader.getApplicationContext();
const baseService: any = await appCtx.getAsync('baseService');
assert((await baseService.getInformation()) === 'harry,one article');
assert.equal(baseService.getAaa(), 123);
assert.equal(baseService.getCcc(), 'mock');
});

it('should load config.*.ts by default env', async () => {
Expand Down

0 comments on commit a6a18b2

Please sign in to comment.