From 17da11881bd2202227a296492c8325cd1c0ff564 Mon Sep 17 00:00:00 2001 From: ClarkXia Date: Wed, 26 May 2021 17:26:21 +0800 Subject: [PATCH 1/7] feat: enhance api registerMethod --- packages/build-scripts/CHANGELOG.md | 4 ++ packages/build-scripts/package.json | 2 +- packages/build-scripts/src/core/Context.ts | 46 +++++++++++++++++----- 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/packages/build-scripts/CHANGELOG.md b/packages/build-scripts/CHANGELOG.md index 7aef8f6..db17c6d 100644 --- a/packages/build-scripts/CHANGELOG.md +++ b/packages/build-scripts/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 1.0.2 + +- [feat] enhance regsiterMethod API, make it possible to get plugin name when applyMethod + ## 1.0.1 - [chore] bump version because of 1.0.0 has been previously published diff --git a/packages/build-scripts/package.json b/packages/build-scripts/package.json index 4189f8e..afd11d7 100644 --- a/packages/build-scripts/package.json +++ b/packages/build-scripts/package.json @@ -1,6 +1,6 @@ { "name": "build-scripts", - "version": "1.0.1", + "version": "1.0.2", "license": "MIT", "description": "scripts core", "main": "lib/index.js", diff --git a/packages/build-scripts/src/core/Context.ts b/packages/build-scripts/src/core/Context.ts index d5583ea..ff3578b 100644 --- a/packages/build-scripts/src/core/Context.ts +++ b/packages/build-scripts/src/core/Context.ts @@ -118,12 +118,26 @@ export interface ICancelTask { (name: string): void; } -export interface IMethodFunction { +export interface IMethodRegistration { (args?: any): void; } +export interface IMethodCurry { + (data?: any): IMethodRegistration; +} + +export type IMethodFunction = IMethodRegistration | IMethodCurry; + +export interface IMethodOptions { + pluginName?: boolean; +} + +export interface IRegsiterMethodAPI { + (name: string, fn: IMethodFunction, options: IMethodOptions): void; +} + export interface IRegsiterMethod { - (name: string, fn: IMethodFunction): void; + (name: string, fn: IMethodFunction, data?: any): void; } export interface IApplyMethod { @@ -159,7 +173,7 @@ export interface IPluginAPI { getValue: (name: string) => any; registerUserConfig: (args: MaybeArray) => void; registerCliOption: (args: MaybeArray) => void; - registerMethod: IRegsiterMethod; + registerMethod: IRegsiterMethodAPI; applyMethod: IApplyMethod; modifyUserConfig: IModifyUserConfig; } @@ -270,7 +284,7 @@ class Context { private cliOptionRegistration: ICliOptionRegistration - private methodRegistration: IHash + private methodRegistration: {[name: string]: [IMethodFunction, any]} private cancelTaskNames: string[] @@ -495,17 +509,23 @@ class Context { } } - public registerMethod: IRegsiterMethod = (name, fn) => { + public registerMethod: IRegsiterMethod = (name, fn, data) => { if (this.methodRegistration[name]) { throw new Error(`[Error] method '${name}' already registered`); } else { - this.methodRegistration[name] = fn; + const registration = [fn, data] as [IMethodFunction, any]; + this.methodRegistration[name] = registration; } } public applyMethod: IApplyMethod = (name, ...args) => { if (this.methodRegistration[name]) { - return this.methodRegistration[name](...args); + const [registerMethod, data] = this.methodRegistration[name]; + if (data) { + return (registerMethod as IMethodCurry)(data)(...args); + } else { + return (registerMethod as IMethodRegistration)(...args); + } } else { return new Error(`apply unkown method ${name}`); } @@ -602,10 +622,16 @@ class Context { private runPlugins = async (): Promise => { for (const pluginInfo of this.plugins) { - const { fn, options } = pluginInfo; + const { fn, options, name: pluginName } = pluginInfo; const pluginContext = _.pick(this, PLUGIN_CONTEXT_KEY); - + const registerMethod: IRegsiterMethodAPI = (methodName, methodNameFn, registerOptions) => { + if (registerOptions?.pluginName) { + this.registerMethod(methodName, methodNameFn, pluginName); + } else { + this.registerMethod(methodName, methodNameFn); + } + }; const pluginAPI = { log, context: pluginContext, @@ -620,7 +646,7 @@ class Context { getValue: this.getValue, registerUserConfig: this.registerUserConfig, registerCliOption: this.registerCliOption, - registerMethod: this.registerMethod, + registerMethod, applyMethod: this.applyMethod, hasMethod: this.hasMethod, modifyUserConfig: this.modifyUserConfig, From 42ff5021099f4c7a3575db4a95e13947dea95d62 Mon Sep 17 00:00:00 2001 From: ClarkXia Date: Thu, 27 May 2021 16:28:36 +0800 Subject: [PATCH 2/7] fix: bind plugin name when call applyMethod --- packages/build-scripts/src/core/Context.ts | 43 +++++++++++----------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/packages/build-scripts/src/core/Context.ts b/packages/build-scripts/src/core/Context.ts index ff3578b..cc4e575 100644 --- a/packages/build-scripts/src/core/Context.ts +++ b/packages/build-scripts/src/core/Context.ts @@ -132,15 +132,17 @@ export interface IMethodOptions { pluginName?: boolean; } -export interface IRegsiterMethodAPI { - (name: string, fn: IMethodFunction, options: IMethodOptions): void; -} - export interface IRegsiterMethod { - (name: string, fn: IMethodFunction, data?: any): void; + (name: string, fn: IMethodFunction, options?: IMethodOptions): void; } +type IMethod = [string, string]; + export interface IApplyMethod { + (config: IMethod, ...args: any[]): any; +} + +export interface IApplyMethodAPI { (name: string, ...args: any[]): any; } @@ -173,8 +175,8 @@ export interface IPluginAPI { getValue: (name: string) => any; registerUserConfig: (args: MaybeArray) => void; registerCliOption: (args: MaybeArray) => void; - registerMethod: IRegsiterMethodAPI; - applyMethod: IApplyMethod; + registerMethod: IRegsiterMethod; + applyMethod: IApplyMethodAPI; modifyUserConfig: IModifyUserConfig; } @@ -509,20 +511,21 @@ class Context { } } - public registerMethod: IRegsiterMethod = (name, fn, data) => { + public registerMethod: IRegsiterMethod = (name, fn, options) => { if (this.methodRegistration[name]) { throw new Error(`[Error] method '${name}' already registered`); } else { - const registration = [fn, data] as [IMethodFunction, any]; + const registration = [fn, options] as [IMethodFunction, IMethodOptions]; this.methodRegistration[name] = registration; } } - public applyMethod: IApplyMethod = (name, ...args) => { - if (this.methodRegistration[name]) { - const [registerMethod, data] = this.methodRegistration[name]; - if (data) { - return (registerMethod as IMethodCurry)(data)(...args); + public applyMethod: IApplyMethod = (config, ...args) => { + const [methodName, pluginName] = Array.isArray(config) ? config : [config]; + if (this.methodRegistration[methodName]) { + const [registerMethod, methodOptions] = this.methodRegistration[methodName]; + if (methodOptions?.pluginName) { + return (registerMethod as IMethodCurry)(pluginName)(...args); } else { return (registerMethod as IMethodRegistration)(...args); } @@ -625,12 +628,8 @@ class Context { const { fn, options, name: pluginName } = pluginInfo; const pluginContext = _.pick(this, PLUGIN_CONTEXT_KEY); - const registerMethod: IRegsiterMethodAPI = (methodName, methodNameFn, registerOptions) => { - if (registerOptions?.pluginName) { - this.registerMethod(methodName, methodNameFn, pluginName); - } else { - this.registerMethod(methodName, methodNameFn); - } + const applyMethod: IApplyMethodAPI = (methodName, ...args) => { + this.applyMethod([methodName, pluginName], ...args); }; const pluginAPI = { log, @@ -646,8 +645,8 @@ class Context { getValue: this.getValue, registerUserConfig: this.registerUserConfig, registerCliOption: this.registerCliOption, - registerMethod, - applyMethod: this.applyMethod, + registerMethod: this.registerMethod, + applyMethod, hasMethod: this.hasMethod, modifyUserConfig: this.modifyUserConfig, modifyConfigRegistration: this.modifyConfigRegistration, From c4994161ecf1f137cb4a09a7f53981fcd0ef2dd1 Mon Sep 17 00:00:00 2001 From: ClarkXia Date: Thu, 27 May 2021 17:48:36 +0800 Subject: [PATCH 3/7] fix: return value of applyMethod --- packages/build-scripts/src/core/Context.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/build-scripts/src/core/Context.ts b/packages/build-scripts/src/core/Context.ts index cc4e575..55ea946 100644 --- a/packages/build-scripts/src/core/Context.ts +++ b/packages/build-scripts/src/core/Context.ts @@ -629,7 +629,7 @@ class Context { const pluginContext = _.pick(this, PLUGIN_CONTEXT_KEY); const applyMethod: IApplyMethodAPI = (methodName, ...args) => { - this.applyMethod([methodName, pluginName], ...args); + return this.applyMethod([methodName, pluginName], ...args); }; const pluginAPI = { log, From c11cd1c0307550572257a02a0a5489e19f3ac04e Mon Sep 17 00:00:00 2001 From: ClarkXia Date: Thu, 27 May 2021 17:52:15 +0800 Subject: [PATCH 4/7] fix: undefined value --- packages/build-scripts/src/core/Context.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/build-scripts/src/core/Context.ts b/packages/build-scripts/src/core/Context.ts index 55ea946..9d65cc4 100644 --- a/packages/build-scripts/src/core/Context.ts +++ b/packages/build-scripts/src/core/Context.ts @@ -530,7 +530,7 @@ class Context { return (registerMethod as IMethodRegistration)(...args); } } else { - return new Error(`apply unkown method ${name}`); + return new Error(`apply unkown method ${methodName}`); } } From 2c20b59a0bd95c47cbbc284db5ab79460e717df2 Mon Sep 17 00:00:00 2001 From: ClarkXia Date: Thu, 27 May 2021 20:12:02 +0800 Subject: [PATCH 5/7] fix: throw error when call undefined method --- packages/build-scripts/src/core/Context.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/build-scripts/src/core/Context.ts b/packages/build-scripts/src/core/Context.ts index 9d65cc4..59e64d6 100644 --- a/packages/build-scripts/src/core/Context.ts +++ b/packages/build-scripts/src/core/Context.ts @@ -530,7 +530,7 @@ class Context { return (registerMethod as IMethodRegistration)(...args); } } else { - return new Error(`apply unkown method ${methodName}`); + throw new Error(`apply unkown method ${methodName}`); } } From 6a709f9356c6b9f927b09bfe4f9b18d40a96a8f0 Mon Sep 17 00:00:00 2001 From: ClarkXia Date: Mon, 5 Jul 2021 17:00:52 +0800 Subject: [PATCH 6/7] chore: version --- packages/build-scripts/CHANGELOG.md | 2 +- packages/build-scripts/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/build-scripts/CHANGELOG.md b/packages/build-scripts/CHANGELOG.md index db17c6d..1ba3c59 100644 --- a/packages/build-scripts/CHANGELOG.md +++ b/packages/build-scripts/CHANGELOG.md @@ -1,6 +1,6 @@ # Changelog -## 1.0.2 +## 1.1.0 - [feat] enhance regsiterMethod API, make it possible to get plugin name when applyMethod diff --git a/packages/build-scripts/package.json b/packages/build-scripts/package.json index afd11d7..4189f8e 100644 --- a/packages/build-scripts/package.json +++ b/packages/build-scripts/package.json @@ -1,6 +1,6 @@ { "name": "build-scripts", - "version": "1.0.2", + "version": "1.0.1", "license": "MIT", "description": "scripts core", "main": "lib/index.js", From c4939f85839f6f3a22eaa239b701c70843f1e43e Mon Sep 17 00:00:00 2001 From: ClarkXia Date: Mon, 5 Jul 2021 17:02:57 +0800 Subject: [PATCH 7/7] fix: typo --- packages/build-scripts/src/core/Context.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/build-scripts/src/core/Context.ts b/packages/build-scripts/src/core/Context.ts index 59e64d6..7356710 100644 --- a/packages/build-scripts/src/core/Context.ts +++ b/packages/build-scripts/src/core/Context.ts @@ -530,7 +530,7 @@ class Context { return (registerMethod as IMethodRegistration)(...args); } } else { - throw new Error(`apply unkown method ${methodName}`); + throw new Error(`apply unknown method ${methodName}`); } }