Skip to content

Commit

Permalink
fix: cannot debug child forked process (#1188)
Browse files Browse the repository at this point in the history
  • Loading branch information
ulivz authored and sorrycc committed Oct 11, 2018
1 parent 8691f32 commit 69ff575
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion packages/af-webpack/src/fork.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
import { fork } from 'child_process';
import send, { RESTART } from './send';

let usedPorts = [];

export default function start(scriptPath) {
const child = fork(scriptPath, process.argv.slice(2));
const execArgv = process.execArgv.slice(0);
const inspectArgvIndex = execArgv.findIndex(argv =>
argv.includes('--inspect-brk'),
);

if (inspectArgvIndex > -1) {
const inspectArgv = execArgv[inspectArgvIndex];
execArgv.splice(
inspectArgvIndex,
1,
inspectArgv.replace(/--inspect-brk=(.*)/, (match, s1) => {
let port;
try {
port = parseInt(s1) + 1;
} catch (e) {
port = 9230; // node default inspect port plus 1.
}
if (usedPorts.includes(port)) {
port++;
}
usedPorts.push(port);
return `--inspect-brk=${port}`;
}),
);
}

const child = fork(scriptPath, process.argv.slice(2), { execArgv });

child.on('message', data => {
const type = (data && data.type) || null;
Expand Down

0 comments on commit 69ff575

Please sign in to comment.