@@ -2,10 +2,12 @@ import lighthouse from 'lighthouse';
2
2
import { RunnerResult } from 'lighthouse/types/externs' ;
3
3
import { launch } from 'chrome-launcher' ;
4
4
import { pipe , concurrent , toAsync , toArray , map } from '@fxts/core' ;
5
+ import EventEmitter from 'events' ;
5
6
6
7
import { Reporter } from './tools' ;
7
8
import { WptContext , WptConfig , WptAuditPath } from './context' ;
8
9
import * as constants from './constants' ;
10
+ import { WptEventHandlersEventMap } from './Wpt.types' ;
9
11
10
12
export interface WebPerformanceTesterProps {
11
13
reporter ?: Reporter ;
@@ -17,21 +19,30 @@ export interface WptAuditPathItem extends WptAuditPath {
17
19
18
20
const context = WptContext ( ) ;
19
21
20
- class WebPerformanceTester {
22
+ class WebPerformanceTester extends EventEmitter {
21
23
private reporter : Reporter ;
22
24
23
25
constructor ( props ?: WebPerformanceTesterProps ) {
26
+ super ( ) ;
24
27
this . reporter = props ?. reporter ?? new Reporter ( ) ;
28
+ this . typedEmit ( 'onReadyWpt' , { context } ) ;
29
+ }
30
+
31
+ private typedEmit (
32
+ eventName : keyof WptEventHandlersEventMap ,
33
+ eventArgs : WptEventHandlersEventMap [ keyof WptEventHandlersEventMap ] ,
34
+ ) {
35
+ this . emit ( eventName , eventArgs ) ;
25
36
}
26
37
27
38
private async createLighthouseReport (
28
39
lighthouseResult : RunnerResult ,
29
40
auditPath : WptAuditPathItem ,
30
41
) {
31
- console . log ( `Reporter:start - " ${ auditPath . name } "` ) ;
42
+ this . typedEmit ( 'onReportStart' , { auditPath, context } ) ;
32
43
this . reporter . saveAuditsReportFile ( lighthouseResult . report , auditPath . name ) ;
33
44
await this . reporter . createAuditsReport ( lighthouseResult . lhr . audits ) ;
34
- console . log ( `Reporter:success - " ${ auditPath . name } "` ) ;
45
+ this . typedEmit ( 'onReportEnd' , { auditPath, context } ) ;
35
46
}
36
47
37
48
private async runLighthouse (
@@ -44,7 +55,7 @@ class WebPerformanceTester {
44
55
port : options . port + index ,
45
56
} ) ;
46
57
try {
47
- console . log ( `Lighthouse:start - " ${ auditPath . name } "` ) ;
58
+ this . typedEmit ( 'onStartedLighthouse' , { auditPath, context } ) ;
48
59
const lighthouseResult : RunnerResult = await lighthouse ( auditPath . url , {
49
60
...options ,
50
61
screenEmulation :
@@ -54,22 +65,22 @@ class WebPerformanceTester {
54
65
port : options . port + index ,
55
66
} ) ;
56
67
await this . createLighthouseReport ( lighthouseResult , auditPath ) ;
57
- console . log (
58
- `Lighthouse:success - Finish the "${ auditPath . name } " wpt test` ,
59
- ) ;
68
+ this . typedEmit ( 'onFinishedLighthouse' , { auditPath, context } ) ;
60
69
} catch ( error ) {
61
- console . error ( `Lighthouse:fail - "${ auditPath . name } "` ) ;
62
- console . error ( 'Lighthouse:fail - ' , error ) ;
70
+ if ( error instanceof Error ) {
71
+ this . typedEmit ( 'onErrorLighthouse' , { auditPath, context, error } ) ;
72
+ } else {
73
+ this . typedEmit ( 'onErrorLighthouse' , { auditPath, context } ) ;
74
+ }
63
75
} finally {
64
76
chrome . kill ( ) ;
65
77
}
66
78
}
67
79
68
80
async run ( ) {
69
81
try {
82
+ this . typedEmit ( 'onStartedWpt' , { context } ) ;
70
83
let index = 0 ;
71
-
72
- console . log ( 'Run:start' ) ;
73
84
const { concurrency, options } = context . config ;
74
85
await pipe (
75
86
context . auditPathsItems ,
@@ -81,9 +92,13 @@ class WebPerformanceTester {
81
92
concurrent ( concurrency ) ,
82
93
( values ) => toArray ( values ) ,
83
94
) ;
84
- console . log ( 'Run:success - All wpt tests were finished' ) ;
95
+ this . typedEmit ( 'onFinishedWpt' , { context } ) ;
85
96
} catch ( error ) {
86
- console . error ( 'Run:fail - ' , error ) ;
97
+ if ( error instanceof Error ) {
98
+ this . typedEmit ( 'onErrorWpt' , { context, error } ) ;
99
+ } else {
100
+ this . typedEmit ( 'onErrorWpt' , { context } ) ;
101
+ }
87
102
}
88
103
}
89
104
}
0 commit comments