1
1
#!/usr/bin/env node
2
+ 'use strict'
2
3
4
+ const { isAbsolute} = require ( 'path' )
3
5
const argv = require ( 'yargs' ) . parse ( )
4
6
const CRI = require ( 'chrome-remote-interface' )
5
7
const spawn = require ( '../lib/spawn' )
6
8
7
9
; ( async ( ) => {
8
10
try {
9
- info = await spawn ( process . execPath ,
10
- [ `--inspect-brk=0` ] . concat ( process . argv . slice ( 2 ) ) )
11
+ const info = await spawn ( process . execPath ,
12
+ [ `--inspect-brk=0` ] . concat ( process . argv . slice ( 2 ) ) )
11
13
const client = await CRI ( { port : info . port } )
12
14
15
+ const initialPause = new Promise ( ( resolve ) => {
16
+ client . once ( 'Debugger.paused' , resolve )
17
+ } )
18
+
19
+ const mainContextInfo = new Promise ( ( resolve ) => {
20
+ client . once ( 'Runtime.executionContextCreated' , ( message ) => {
21
+ resolve ( message . context )
22
+ } )
23
+ } )
24
+
25
+ const executionComplete = new Promise ( ( resolve ) => {
26
+ client . on ( 'Runtime.executionContextDestroyed' , async ( message ) => {
27
+ if ( message . executionContextId === ( await mainContextInfo ) . id ) {
28
+ resolve ( message )
29
+ }
30
+ } )
31
+ } )
32
+
13
33
const { Debugger, Runtime, Profiler} = client
14
- await Runtime . runIfWaitingForDebugger ( )
15
- await Runtime . enable ( )
16
- await Profiler . enable ( )
17
- await Profiler . startPreciseCoverage ( { callCount : true , detailed : true } )
18
- await Debugger . enable ( )
19
- await Debugger . paused ( )
34
+ await Promise . all ( [
35
+ Profiler . enable ( ) ,
36
+ Runtime . enable ( ) ,
37
+ Debugger . enable ( ) ,
38
+ Profiler . startPreciseCoverage ( { callCount : true , detailed : true } ) ,
39
+ Runtime . runIfWaitingForDebugger ( ) ,
40
+ initialPause
41
+ ] )
20
42
await Debugger . resume ( )
21
43
22
- client . on ( 'event' , async ( message ) => {
23
- // console.info(message)
24
- if ( message . method === 'Runtime.executionContextDestroyed' ) {
25
- await outputCoverage ( Profiler )
26
- client . close ( )
27
- }
28
- } )
44
+ await executionComplete
45
+ await outputCoverage ( Profiler )
46
+ client . close ( )
29
47
30
48
} catch ( err ) {
31
49
console . error ( err )
@@ -40,12 +58,8 @@ async function outputCoverage (Profiler) {
40
58
/ n o d e - s p a w n - w r a p /
41
59
]
42
60
let { result} = await Profiler . takePreciseCoverage ( )
43
- result = result . filter ( coverage => {
44
- for ( var ignored , i = 0 ; ( ignored = IGNORED_PATHS [ i ] ) !== undefined ; i ++ ) {
45
- if ( ignored . test ( coverage . url ) ) return false
46
- }
47
- if ( ! / ^ \/ / . test ( coverage . url ) ) return false
48
- else return true
61
+ result = result . filter ( ( { url} ) => {
62
+ return isAbsolute ( url ) && IGNORED_PATHS . every ( ignored => ! ignored . test ( url ) )
49
63
} )
50
64
console . log ( JSON . stringify ( result , null , 2 ) )
51
65
}
0 commit comments