Skip to content

Commit 01f654e

Browse files
TimothyGubcoe
authored andcommitted
fix: tweak inspector event timing (bcoe#6)
1 parent bb117b7 commit 01f654e

File tree

1 file changed

+35
-21
lines changed

1 file changed

+35
-21
lines changed

bin/c8.js

+35-21
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,49 @@
11
#!/usr/bin/env node
2+
'use strict'
23

4+
const {isAbsolute} = require('path')
35
const argv = require('yargs').parse()
46
const CRI = require('chrome-remote-interface')
57
const spawn = require('../lib/spawn')
68

79
;(async () => {
810
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)))
1113
const client = await CRI({port: info.port})
1214

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+
1333
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+
])
2042
await Debugger.resume()
2143

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()
2947

3048
} catch (err) {
3149
console.error(err)
@@ -40,12 +58,8 @@ async function outputCoverage (Profiler) {
4058
/node-spawn-wrap/
4159
]
4260
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))
4963
})
5064
console.log(JSON.stringify(result, null, 2))
5165
}

0 commit comments

Comments
 (0)