@@ -4,13 +4,11 @@ import type {
4
4
} from '@angular-devkit/architect' ;
5
5
import { getEnvironmentInfo } from '../environment/index' ;
6
6
import { performance } from 'node:perf_hooks' ;
7
- import { BuilderMetricData , sendData as defaultSendData } from '../sender' ;
7
+ import { BuilderMetricData , sendData as defaultSendData , type SendDataFn } from '../sender' ;
8
8
9
9
type BuilderWrapperFn < S , O extends BuilderOutput = BuilderOutput > =
10
10
( opts : S , ctx : BuilderContext ) => O | Promise < O > ;
11
11
12
- type SendDataFn = ( data : BuilderMetricData , logger ?: { error : ( msg : string ) => void } | undefined ) => Promise < void > ;
13
-
14
12
/**
15
13
* Type of a function that wraps a builder
16
14
*/
@@ -20,11 +18,12 @@ export type BuilderWrapper = <S, O extends BuilderOutput = BuilderOutput>
20
18
/**
21
19
* Wrapper method of a builder to retrieve some metrics around the builder run
22
20
* @param builderFn
21
+ * @param sendData
23
22
*/
24
23
export const createBuilderWithMetrics : BuilderWrapper = ( builderFn , sendData = defaultSendData ) =>
25
24
async ( options , context ) => {
26
25
const startTime = Math . floor ( performance . now ( ) ) ;
27
- let error ;
26
+ let error : any ;
28
27
try {
29
28
const result = await builderFn ( options , context ) ;
30
29
return result ;
@@ -35,38 +34,36 @@ export const createBuilderWithMetrics: BuilderWrapper = (builderFn, sendData = d
35
34
throw err ;
36
35
}
37
36
finally {
38
- try {
39
- const endTime = Math . floor ( performance . now ( ) ) ;
40
- const duration = endTime - startTime ;
41
- // context.builder.builderName does not contain the package name
42
- const builderName = context . builder . name as string ;
43
- context . logger . info ( `${ builderName } run in ${ duration } ms` ) ;
44
- const environment = getEnvironmentInfo ( ) ;
45
- const data : BuilderMetricData = {
46
- environment,
47
- duration,
48
- builder : {
49
- name : builderName ,
50
- options,
51
- ...( context . target
52
- ? {
53
- target : {
54
- name : context . target . target ,
55
- projectName : context . target . project ,
56
- configuration : context . target . configuration
57
- }
58
- } : { }
59
- )
60
- } ,
61
- error
62
- } ;
63
- context . logger . debug ( JSON . stringify ( data , null , 2 ) ) ;
64
- await sendData ( data , context . logger ) ;
65
- } catch ( e : any ) {
37
+ const endTime = Math . floor ( performance . now ( ) ) ;
38
+ const duration = endTime - startTime ;
39
+ // context.builder.builderName does not contain the package name
40
+ const builderName = context . builder . name as string ;
41
+ context . logger . info ( `${ builderName } run in ${ duration } ms` ) ;
42
+ const environment = getEnvironmentInfo ( ) ;
43
+ const data : BuilderMetricData = {
44
+ environment,
45
+ duration,
46
+ builder : {
47
+ name : builderName ,
48
+ options,
49
+ ...( context . target
50
+ ? {
51
+ target : {
52
+ name : context . target . target ,
53
+ projectName : context . target . project ,
54
+ configuration : context . target . configuration
55
+ }
56
+ } : { }
57
+ )
58
+ } ,
59
+ error
60
+ } ;
61
+ context . logger . debug ( JSON . stringify ( data , null , 2 ) ) ;
62
+ void sendData ( data , context . logger ) . catch ( ( e ) => {
66
63
// Do not throw error if we don't manage to collect data
67
64
const err = ( e instanceof Error ? e : new Error ( error ) ) ;
68
65
context . logger . error ( err . stack || err . toString ( ) ) ;
69
- }
66
+ } ) ;
70
67
}
71
68
} ;
72
69
0 commit comments