Skip to content

Commit bb117b7

Browse files
authored
feat: switch to stderr and default port (#7)
1 parent 7f1e920 commit bb117b7

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
lines changed

bin/c8.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,13 @@
22

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

9-
getPort().then(async port => {
10-
foreground(
11-
['node', `--inspect-brk=${port}`].concat(process.argv.slice(2)),
12-
(done) => {
13-
console.info('actually got here')
14-
}
15-
)
7+
;(async () => {
168
try {
17-
await waitTillPortOpen(port)
18-
const client = await CRI({port: port})
9+
info = await spawn(process.execPath,
10+
[`--inspect-brk=0`].concat(process.argv.slice(2)))
11+
const client = await CRI({port: info.port})
1912

2013
const {Debugger, Runtime, Profiler} = client
2114
await Runtime.runIfWaitingForDebugger()
@@ -38,7 +31,7 @@ getPort().then(async port => {
3831
console.error(err)
3932
process.exit(1)
4033
}
41-
})
34+
})()
4235

4336
async function outputCoverage (Profiler) {
4437
const IGNORED_PATHS = [

lib/spawn.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const {spawn} = require('child_process')
2+
3+
const debuggerRe = /Debugger listening on ws:\/\/[^:]*:([^/]*)/
4+
5+
module.exports = function (execPath, args=[]) {
6+
const info = {
7+
port: -1
8+
}
9+
return new Promise((resolve, reject) => {
10+
const proc = spawn(execPath, args, {
11+
stdio: [process.stdin, process.stdout, 'pipe'],
12+
env: process.env,
13+
cwd: process.cwd()
14+
});
15+
16+
proc.stderr.on('data', (outBuffer) => {
17+
const outString = outBuffer.toString('utf8')
18+
const match = outString.match(debuggerRe)
19+
if (match && !info.url) {
20+
info.port = Number(match[1])
21+
return resolve(info)
22+
} else {
23+
console.error(outString)
24+
}
25+
})
26+
27+
proc.on('close', (code) => {
28+
if (info.port === -1) {
29+
return reject(Error('could not connect to inspector'))
30+
}
31+
})
32+
})
33+
}

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@
2323
"license": "ISC",
2424
"dependencies": {
2525
"chrome-remote-interface": "^0.25.2",
26-
"foreground-child": "^1.5.6",
27-
"get-port": "^3.2.0",
28-
"wait-till-port-open": "^1.0.0",
2926
"yargs": "^10.0.3"
3027
},
3128
"devDependencies": {

0 commit comments

Comments
 (0)