Skip to content

Commit

Permalink
feat: switch to stderr and default port (bcoe#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe authored Dec 1, 2017
1 parent 7f1e920 commit bb117b7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
19 changes: 6 additions & 13 deletions bin/c8.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,13 @@

const argv = require('yargs').parse()
const CRI = require('chrome-remote-interface')
const getPort = require('get-port');
const foreground = require('foreground-child')
const waitTillPortOpen = require('wait-till-port-open')
const spawn = require('../lib/spawn')

getPort().then(async port => {
foreground(
['node', `--inspect-brk=${port}`].concat(process.argv.slice(2)),
(done) => {
console.info('actually got here')
}
)
;(async () => {
try {
await waitTillPortOpen(port)
const client = await CRI({port: port})
info = await spawn(process.execPath,
[`--inspect-brk=0`].concat(process.argv.slice(2)))
const client = await CRI({port: info.port})

const {Debugger, Runtime, Profiler} = client
await Runtime.runIfWaitingForDebugger()
Expand All @@ -38,7 +31,7 @@ getPort().then(async port => {
console.error(err)
process.exit(1)
}
})
})()

async function outputCoverage (Profiler) {
const IGNORED_PATHS = [
Expand Down
33 changes: 33 additions & 0 deletions lib/spawn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const {spawn} = require('child_process')

const debuggerRe = /Debugger listening on ws:\/\/[^:]*:([^/]*)/

module.exports = function (execPath, args=[]) {
const info = {
port: -1
}
return new Promise((resolve, reject) => {
const proc = spawn(execPath, args, {
stdio: [process.stdin, process.stdout, 'pipe'],
env: process.env,
cwd: process.cwd()
});

proc.stderr.on('data', (outBuffer) => {
const outString = outBuffer.toString('utf8')
const match = outString.match(debuggerRe)
if (match && !info.url) {
info.port = Number(match[1])
return resolve(info)
} else {
console.error(outString)
}
})

proc.on('close', (code) => {
if (info.port === -1) {
return reject(Error('could not connect to inspector'))
}
})
})
}
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
"license": "ISC",
"dependencies": {
"chrome-remote-interface": "^0.25.2",
"foreground-child": "^1.5.6",
"get-port": "^3.2.0",
"wait-till-port-open": "^1.0.0",
"yargs": "^10.0.3"
},
"devDependencies": {
Expand Down

0 comments on commit bb117b7

Please sign in to comment.