@@ -281,14 +281,34 @@ export function appendZeroesToVersion(version: string, requiredVersionLength: nu
281281 return version ;
282282}
283283
284- export function decorateMethod ( before : ( method1 : any , self1 : any , args1 : any [ ] ) => Promise < void > , after : ( method2 : any , self2 : any , result2 : any , args2 : any [ ] ) => Promise < any > ) {
284+ export function decorateMethod ( before : ( method1 : any , self1 : any , args1 : any [ ] ) => Promise < any > , after : ( method2 : any , self2 : any , result2 : any , args2 : any [ ] ) => Promise < any > ) {
285285 return ( target : Object , propertyKey : string , descriptor : TypedPropertyDescriptor < Function > ) => {
286286 const sink = descriptor . value ;
287287 descriptor . value = async function ( ...args : any [ ] ) : Promise < any > {
288+ let newMethods : Function [ ] = null ;
288289 if ( before ) {
289- await before ( sink , this , args ) ;
290+ newMethods = await before ( sink , this , args ) ;
290291 }
291- const result = sink . apply ( this , args ) ;
292+
293+ let hasBeenReplaced = false ;
294+ let result : any ;
295+ if ( newMethods && newMethods . length ) {
296+ const replacementMethods = _ . filter ( newMethods , f => _ . isFunction ( f ) ) ;
297+ if ( replacementMethods . length > 0 ) {
298+ if ( replacementMethods . length > 1 ) {
299+ const $logger = $injector . resolve < ILogger > ( "logger" ) ;
300+ $logger . warn ( `Multiple methods detected which try to replace ${ sink . name } . Will execute only the first of them.` ) ;
301+ }
302+
303+ hasBeenReplaced = true ;
304+ result = _ . head ( replacementMethods ) ( args , sink . bind ( this ) ) ;
305+ }
306+ }
307+
308+ if ( ! hasBeenReplaced ) {
309+ result = sink . apply ( this , args ) ;
310+ }
311+
292312 if ( after ) {
293313 return await after ( sink , this , result , args ) ;
294314 }
@@ -327,7 +347,7 @@ export function hook(commandName: string) {
327347 return decorateMethod (
328348 async ( method : any , self : any , args : any [ ] ) => {
329349 const hooksService = getHooksService ( self ) ;
330- await hooksService . executeBeforeHooks ( commandName , prepareArguments ( method , args , hooksService ) ) ;
350+ return hooksService . executeBeforeHooks ( commandName , prepareArguments ( method , args , hooksService ) ) ;
331351 } ,
332352 async ( method : any , self : any , resultPromise : any , args : any [ ] ) => {
333353 const result = await resultPromise ;
0 commit comments