Skip to content

Commit

Permalink
调整预处理器实现
Browse files Browse the repository at this point in the history
  • Loading branch information
han-feng committed May 15, 2019
1 parent e36b2e3 commit 7a1f44a
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 194 deletions.
28 changes: 16 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ language: node_js

node_js:
- stable
- "11"
- "10"
- "8"

Expand All @@ -11,19 +12,22 @@ addons:
token:
secure: $SONAR_CLOUD_TOKEN

before_script:
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
- chmod +x ./cc-test-reporter
- ./cc-test-reporter before-build

script:
- npm run build
- npm test

after_script:
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT

after_success:
- npx codecov --token=$CODECOV_TOKEN
- sonar-scanner
- cat ./coverage/lcov.info | npx codacy-coverage --projectName modular --token $CODACY_PROJECT_TOKEN --username han-feng --accountToken $CODACY_API_TOKEN
jobs:
include:
- stage: reports
if: branch = master
node_js: stable
before_script:
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
- chmod +x ./cc-test-reporter
- ./cc-test-reporter before-build
after_script:
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
after_success:
- npx codecov --token=$CODECOV_TOKEN
- sonar-scanner
- cat ./coverage/lcov.info | npx codacy-coverage --projectName modular --token $CODACY_PROJECT_TOKEN --username han-feng --accountToken $CODACY_API_TOKEN
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
},
"devDependencies": {
"@babel/preset-env": "^7.4.4",
"@types/jest": "^24.0.12",
"@types/lodash": "^4.14.126",
"@types/jest": "^24.0.13",
"@types/lodash": "^4.14.129",
"babel-jest": "^24.8.0",
"chalk": "^2.4.2",
"del": "^4.1.1",
Expand Down
76 changes: 46 additions & 30 deletions src/ExtensionPoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ export interface ExtensionPoint {
export interface Preprocessor {
/**
* 扩展配置预处理
* @param extension 待处理扩展配置对象
* @param extensions 待处理扩展配置对象数组
* @returns 处理后的扩展配置对象,对应于 DefaultExtensionPoint.getExtension() 方法的返回值
*/
process(extension: any, extensionPoint: DefaultExtensionPoint): any
process(extensions: any[], extensionPoint: ExtensionPoint): any
}

/**
Expand All @@ -36,10 +36,18 @@ export interface Preprocessor {
export class DefaultExtensionPoint implements ExtensionPoint {
readonly type: string
module?: string

private extensions: any[] = []
/**
* 原始配置对象
*/
private readonly extensions: any[] = []
/**
* 处理过的配置对象
*/
private extension: any = {}
private preprocessors: Preprocessor[] = []
/**
* 预处理器
*/
private readonly preprocessors: Preprocessor[] = []
private processed = false

constructor(point: ExtensionPoint) {
Expand All @@ -49,21 +57,13 @@ export class DefaultExtensionPoint implements ExtensionPoint {

/**
* 添加扩展
* @param module 扩展提供模块名称
* @param extensions 扩展配置集合
*/
addExtension(...extensions: any[]) {
addExtension(module: string, ...extensions: any[]) {
this.processed = false
extensions.forEach(item => item['@module'] = module)
this.extensions.push(...extensions)
switch (this.type) {
case Type.Single:
this.extension = extensions[extensions.length - 1]
break
case Type.Mixin:
extensions.forEach(item => Object.assign(this.extension, item))
break
case Type.Multiple:
default:
}
}

/**
Expand All @@ -78,10 +78,9 @@ export class DefaultExtensionPoint implements ExtensionPoint {
}

/**
* 获取扩展配置对象数组
* 获取原始配置对象数组,这些配置对象未经任何加工处理
*/
getExtensions() {
this.preprocess()
return this.extensions
}

Expand All @@ -96,28 +95,45 @@ export class DefaultExtensionPoint implements ExtensionPoint {

private preprocess() {
if (!this.processed) {
let extension: any = null
let extension: any = {}
let extensions = this.extensions
switch (this.type) {
case Type.Single:
extension = extensions[extensions.length - 1]
extensions = this.processExtensions([extension])
if (extensions && extensions.length && extensions.length > 0) {
extension = extensions[0]
}
break
case Type.Mixin:
extension = this.extension
extensions = this.processExtensions(extensions)
extensions.forEach(item => Object.assign(extension, item))
delete extension['@module']
break
case Type.Multiple:
default:
extension = this.extensions
extension = extensions
extension = this.processExtensions(extension)
}
this.preprocessors.forEach(processor => {
const result = processor.process(extension, this)
if (result !== null) {
extension = result
}
})
this.extension = extension
if (this.type === Type.Multiple) {
this.extensions = extension
}
this.processed = true
}
}

private processExtensions(extensions: any[]) {
this.preprocessors.forEach(processor => {
let result = null
try {
result = processor.process(extensions, this)
} catch (error) {
// tslint:disable-next-line:no-console
console.error(error)
}
if (result !== null) {
extensions = result
}
})
return extensions
}

}
12 changes: 8 additions & 4 deletions src/Modular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,11 @@ export default class Modular {
}

/**
* 获取指定名称的有效扩展配置
* 获取指定名称的扩展配置对象
* @param name 扩展点名称
* @returns 当扩展点类型为 Multiple 时,返回扩展配置对象数组;
* 当扩展点类型为 Single 时,返回最后加入的扩展配置对象;
* 当扩展点类型为 Mixin 时,返回所有扩展配置对象混合后的结果
*/
getExtension(name: string) {
const point = this.getExtensionPoint(name)
Expand All @@ -86,7 +89,8 @@ export default class Modular {
}

/**
* 获取扩展配置对象数组
* 获取原始配置对象数组,这些配置对象未经任何加工处理
* @param name 扩展点名称
*/
getExtensions(name: string) {
const point = this.getExtensionPoint(name)
Expand Down Expand Up @@ -248,9 +252,9 @@ export default class Modular {
for (const name in ext) {
if (points[name]) {
if (points[name].type === Type.Multiple && Array.isArray(ext[name])) {
points[name].addExtension(...ext[name])
points[name].addExtension(module.name, ...ext[name])
} else {
points[name].addExtension(ext[name])
points[name].addExtension(module.name, ext[name])
}
} else {
this.log(new LogInfo('E06', 'error', { m: module, ep: name }))
Expand Down
Loading

0 comments on commit 7a1f44a

Please sign in to comment.