Skip to content

Commit

Permalink
fix: tweak inspector event timing (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyGu authored and bcoe committed Dec 1, 2017
1 parent bb117b7 commit 01f654e
Showing 1 changed file with 35 additions and 21 deletions.
56 changes: 35 additions & 21 deletions bin/c8.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,49 @@
#!/usr/bin/env node
'use strict'

const {isAbsolute} = require('path')
const argv = require('yargs').parse()
const CRI = require('chrome-remote-interface')
const spawn = require('../lib/spawn')

;(async () => {
try {
info = await spawn(process.execPath,
[`--inspect-brk=0`].concat(process.argv.slice(2)))
const info = await spawn(process.execPath,
[`--inspect-brk=0`].concat(process.argv.slice(2)))
const client = await CRI({port: info.port})

const initialPause = new Promise((resolve) => {
client.once('Debugger.paused', resolve)
})

const mainContextInfo = new Promise((resolve) => {
client.once('Runtime.executionContextCreated', (message) => {
resolve(message.context)
})
})

const executionComplete = new Promise((resolve) => {
client.on('Runtime.executionContextDestroyed', async (message) => {
if (message.executionContextId === (await mainContextInfo).id) {
resolve(message)
}
})
})

const {Debugger, Runtime, Profiler} = client
await Runtime.runIfWaitingForDebugger()
await Runtime.enable()
await Profiler.enable()
await Profiler.startPreciseCoverage({callCount: true, detailed: true})
await Debugger.enable()
await Debugger.paused()
await Promise.all([
Profiler.enable(),
Runtime.enable(),
Debugger.enable(),
Profiler.startPreciseCoverage({callCount: true, detailed: true}),
Runtime.runIfWaitingForDebugger(),
initialPause
])
await Debugger.resume()

client.on('event', async (message) => {
// console.info(message)
if (message.method === 'Runtime.executionContextDestroyed') {
await outputCoverage(Profiler)
client.close()
}
})
await executionComplete
await outputCoverage(Profiler)
client.close()

} catch (err) {
console.error(err)
Expand All @@ -40,12 +58,8 @@ async function outputCoverage (Profiler) {
/node-spawn-wrap/
]
let {result} = await Profiler.takePreciseCoverage()
result = result.filter(coverage => {
for (var ignored, i = 0; (ignored = IGNORED_PATHS[i]) !== undefined; i++) {
if (ignored.test(coverage.url)) return false
}
if (!/^\//.test(coverage.url)) return false
else return true
result = result.filter(({url}) => {
return isAbsolute(url) && IGNORED_PATHS.every(ignored => !ignored.test(url))
})
console.log(JSON.stringify(result, null, 2))
}

0 comments on commit 01f654e

Please sign in to comment.