-
Notifications
You must be signed in to change notification settings - Fork 577
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support @configuration decorator
- Loading branch information
1 parent
4b9461d
commit 0584494
Showing
34 changed files
with
447 additions
and
380 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"trailingComma": "es5", | ||
"tabWidth": 2, | ||
"singleQuote": true, | ||
"quoteProps": "consistent" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} |
Oops, something went wrong.