Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c549c6b
commit 9144b48
Showing
28 changed files
with
325 additions
and
132 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
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
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 |
---|---|---|
@@ -1,13 +1,29 @@ | ||
import { IContainer } from 'injection'; | ||
|
||
export interface IContainerConfiguration { | ||
addImports(imports: string[]); | ||
addImports(imports: string[], baseDir?: string); | ||
addImportObjects(importObjects: any[]); | ||
addImportConfigs(importConfigs: string[], baseDir: string); | ||
load(packageName: string); | ||
loadConfiguration(configuration: any); | ||
loadConfiguration(configuration: any, baseDir: string); | ||
getImportDirectory(): string[]; | ||
} | ||
|
||
export interface IMidwayContainer extends IContainer { | ||
createConfiguration(); | ||
getConfigService(): IConfigService; | ||
getEnvironmentService(): IEnvironmentService; | ||
getCurrentEnv(): string; | ||
} | ||
|
||
export interface IConfigService { | ||
add(configFilePaths: string[]); | ||
addObject(obj: object); | ||
load(); | ||
getConfiguration(); | ||
} | ||
|
||
export interface IEnvironmentService { | ||
getCurrentEnvironment(): string; | ||
setCurrentEnvironment(environment: string); | ||
} |
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
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,99 @@ | ||
import * as extend from 'extend2'; | ||
import * as is from 'is-type-of'; | ||
import { basename } from 'path'; | ||
import { IConfigService, IMidwayContainer } from '../interface'; | ||
|
||
const debug = require('debug')('midway:config'); | ||
|
||
export class MidwayConfigService implements IConfigService { | ||
|
||
envDirMap: Map<string, Set<string>>; | ||
configuration; | ||
isReady = false; | ||
container: IMidwayContainer; | ||
externalObject: object[] = []; | ||
|
||
constructor(container) { | ||
this.container = container; | ||
this.envDirMap = new Map(); | ||
} | ||
|
||
add(configFilePaths) { | ||
for (const dir of configFilePaths) { | ||
const env = this.getConfigEnv(dir); | ||
const envSet = this.getEnvSet(env); | ||
envSet.add(dir); | ||
} | ||
} | ||
|
||
addObject(obj: object) { | ||
if (this.isReady) { | ||
extend(true, this.configuration, obj); | ||
} else { | ||
this.externalObject.push(obj); | ||
} | ||
} | ||
|
||
getEnvSet(env) { | ||
if (!this.envDirMap.has(env)) { | ||
this.envDirMap.set(env, new Set()); | ||
} | ||
return this.envDirMap.get(env); | ||
} | ||
|
||
getConfigEnv(configFilePath) { | ||
// parse env | ||
const configFileBaseName = basename(configFilePath); | ||
const splits = configFileBaseName.split('.'); | ||
const suffix = splits.pop(); | ||
if (suffix !== 'js' && suffix !== 'ts') { | ||
return suffix; | ||
} | ||
return splits.pop(); | ||
} | ||
|
||
async load() { | ||
// get default | ||
const defaultSet = this.getEnvSet('default'); | ||
// get current set | ||
const currentEnvSet = this.getEnvSet(this.container.getCurrentEnv()); | ||
// merge set | ||
const target = {}; | ||
for (const filename of [...defaultSet, ...currentEnvSet]) { | ||
const config = await this.loadConfig(filename); | ||
|
||
if (!config) { | ||
continue; | ||
} | ||
|
||
debug('Loaded config %s, %j', filename, config); | ||
extend(true, target, config); | ||
} | ||
if (this.externalObject.length) { | ||
for (const externalObject of this.externalObject) { | ||
if (externalObject) { | ||
debug('Loaded external object %j', externalObject); | ||
extend(true, target, externalObject); | ||
} | ||
} | ||
} | ||
this.configuration = target; | ||
} | ||
|
||
async getConfiguration() { | ||
if (!this.isReady) { | ||
await this.load(); | ||
this.isReady = true; | ||
} | ||
return this.configuration; | ||
} | ||
|
||
async loadConfig(configFilename): Promise<object> { | ||
const exports = require(configFilename); | ||
let result = exports; | ||
if (is.function(exports)) { | ||
result = await exports.apply(null, [].concat(this.container)); | ||
} | ||
return result; | ||
} | ||
} |
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,16 @@ | ||
import { IEnvironmentService } from '../interface'; | ||
|
||
export class MidwayEnvironmentService implements IEnvironmentService { | ||
environment: string; | ||
|
||
getCurrentEnvironment() { | ||
if (!this.environment) { | ||
this.environment = process['MIDWAY_SERVER_ENV'] || process['NODE_ENV'] || 'production'; | ||
} | ||
return this.environment; | ||
} | ||
|
||
setCurrentEnvironment(environment: string) { | ||
this.environment = environment; | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/midway-core/test/fixtures/app-with-configuration/midway-plugin-ok/package.json
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,4 @@ | ||
{ | ||
"name": "midway-plugin-mock", | ||
"main": "src/index.ts" | ||
} |
Oops, something went wrong.