Skip to content

Commit

Permalink
feat: support @configuration decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Jan 29, 2020
1 parent 4b9461d commit 0584494
Show file tree
Hide file tree
Showing 34 changed files with 447 additions and 380 deletions.
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"trailingComma": "es5",
"tabWidth": 2,
"singleQuote": true,
"quoteProps": "consistent"
}
4 changes: 1 addition & 3 deletions packages/midway-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@
"license": "MIT",
"devDependencies": {
"chai": "^4.2.0",
"midway-bin": "^1.16.3",
"xmldom": "^0.1.27"
"midway-bin": "^1.16.3"
},
"dependencies": {
"@midwayjs/decorator": "^1.16.3",
"camelcase": "^5.2.0",
"debug": "^4.1.1",
"globby": "^10.0.1",
"graphviz": "^0.0.8",
Expand Down
62 changes: 62 additions & 0 deletions packages/midway-core/src/configuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { CONFIGURATION_KEY } from '@midwayjs/decorator';
import { join, dirname, isAbsolute } from 'path';
import { getClassMetadata } from 'injection';
import { IContainerConfiguration, IMidwayContainer } from './interface';
import { safeRequire } from './util';

export class ContainerConfiguration implements IContainerConfiguration {
container: IMidwayContainer;
preloadModules;
readyBindModules: Map<string, Set<any>> = new Map();
importDirectory = [];
imports: string[] = [];
importObjects: object = new Map();

constructor(container) {
this.container = container;
}

addImports(imports: string[]) {
// 处理 imports
for (const importPackage of imports) {
this.load(importPackage);
}
this.imports = this.imports.concat(imports);
}

addImportObjects(importObjects: object) {
this.importObjects = importObjects;
}

resolvePackageBaseDir(packageName: string) {
return dirname(require.resolve(packageName));
}

load(packageName: string) {
let configuration;
if (isAbsolute(packageName)) {
configuration = safeRequire(join(packageName, 'configuration'));
} else {
configuration = safeRequire(`${packageName}/configuration`);
}
if (configuration) {
const configurationOptions = getClassMetadata(
CONFIGURATION_KEY,
configuration
);

if (configurationOptions.imports) {
configuration.addImports(configurationOptions.imports);
}
if (configurationOptions.importObjects) {
configuration.addImportObjects(configurationOptions.importObjects);
}
}
}

getImportDirectory() {
return this.imports.map(importModule => {
return this.resolvePackageBaseDir(importModule);
});
}
}
Loading

0 comments on commit 0584494

Please sign in to comment.