@@ -281,14 +281,34 @@ export function appendZeroesToVersion(version: string, requiredVersionLength: nu
281
281
return version ;
282
282
}
283
283
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 > ) {
285
285
return ( target : Object , propertyKey : string , descriptor : TypedPropertyDescriptor < Function > ) => {
286
286
const sink = descriptor . value ;
287
287
descriptor . value = async function ( ...args : any [ ] ) : Promise < any > {
288
+ let newMethods : Function [ ] = null ;
288
289
if ( before ) {
289
- await before ( sink , this , args ) ;
290
+ newMethods = await before ( sink , this , args ) ;
290
291
}
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
+
292
312
if ( after ) {
293
313
return await after ( sink , this , result , args ) ;
294
314
}
@@ -327,7 +347,7 @@ export function hook(commandName: string) {
327
347
return decorateMethod (
328
348
async ( method : any , self : any , args : any [ ] ) => {
329
349
const hooksService = getHooksService ( self ) ;
330
- await hooksService . executeBeforeHooks ( commandName , prepareArguments ( method , args , hooksService ) ) ;
350
+ return hooksService . executeBeforeHooks ( commandName , prepareArguments ( method , args , hooksService ) ) ;
331
351
} ,
332
352
async ( method : any , self : any , resultPromise : any , args : any [ ] ) => {
333
353
const result = await resultPromise ;
0 commit comments