diff --git a/lib/_inspect.js b/lib/_inspect.js index ac32e0a..afc0cbd 100644 --- a/lib/_inspect.js +++ b/lib/_inspect.js @@ -42,7 +42,7 @@ const [ InspectClient, createRepl ] = const debuglog = util.debuglog('inspect'); -const DEBUG_PORT_PATTERN = /^--(?:debug|inspect)-port=(\d+)$/; +const DEBUG_PORT_PATTERN = /^--(?:debug|inspect)(?:-port|-brk)?=(\d{1,5})$/; function getDefaultPort() { for (const arg of process.execArgv) { const match = arg.match(DEBUG_PORT_PATTERN); @@ -56,8 +56,7 @@ function getDefaultPort() { function runScript(script, scriptArgs, inspectPort, childPrint) { return new Promise((resolve) => { const args = [ - '--inspect', - `--debug-brk=${inspectPort}`, + `--inspect-brk=${inspectPort}`, ].concat([script], scriptArgs); const child = spawn(process.execPath, args); child.stdout.setEncoding('utf8'); @@ -68,7 +67,7 @@ function runScript(script, scriptArgs, inspectPort, childPrint) { let output = ''; function waitForListenHint(text) { output += text; - if (/chrome-devtools:\/\//.test(output)) { + if (/^Debugger listening on/.test(output)) { child.stderr.removeListener('data', waitForListenHint); resolve(child); } @@ -295,6 +294,7 @@ function parseArgv([target, ...args]) { const hostMatch = target.match(/^([^:]+):(\d+)$/); const portMatch = target.match(/^--port=(\d+)$/); + if (hostMatch) { // Connecting to remote debugger // `node-inspect localhost:9229` @@ -303,16 +303,15 @@ function parseArgv([target, ...args]) { isRemote = true; script = null; } else if (portMatch) { - // Start debugger on custom port - // `node debug --port=8058 app.js` + // start debugee on custom port + // `node inspect --port=9230 script.js` port = parseInt(portMatch[1], 10); script = args[0]; scriptArgs = args.slice(1); } return { - host, port, - isRemote, script, scriptArgs, + host, port, isRemote, script, scriptArgs, }; } diff --git a/test/cli/launch.test.js b/test/cli/launch.test.js index b57c08a..0690cc4 100644 --- a/test/cli/launch.test.js +++ b/test/cli/launch.test.js @@ -5,6 +5,27 @@ const { test } = require('tap'); const startCLI = require('./start-cli'); +test('custom port', (t) => { + const CUSTOM_PORT = '9230'; + const script = Path.join('examples', 'three-lines.js'); + + const cli = startCLI([`--port=${CUSTOM_PORT}`, script]); + + return cli.waitForInitialBreak() + .then(() => cli.waitForPrompt()) + .then(() => { + t.match(cli.output, 'debug>', 'prints a prompt'); + t.match( + cli.output, + new RegExp(`< Debugger listening on [^\n]*${CUSTOM_PORT}`), + 'forwards child output'); + }) + .then(() => cli.quit()) + .then((code) => { + t.equal(code, 0, 'exits with success'); + }); +}); + test('examples/three-lines.js', (t) => { const script = Path.join('examples', 'three-lines.js'); const cli = startCLI([script]); @@ -13,6 +34,10 @@ test('examples/three-lines.js', (t) => { .then(() => cli.waitForPrompt()) .then(() => { t.match(cli.output, 'debug>', 'prints a prompt'); + t.match( + cli.output, + new RegExp(`< Debugger listening on [^\n]*9229`), + 'forwards child output'); }) .then(() => cli.command('["hello", "world"].join(" ")')) .then(() => { diff --git a/test/cli/start-cli.js b/test/cli/start-cli.js index 56c3e25..ae90430 100644 --- a/test/cli/start-cli.js +++ b/test/cli/start-cli.js @@ -102,7 +102,7 @@ function startCLI(args) { return this.waitFor(/break (?:on start )?in/i, timeout) .then(() => { if (/Break on start/.test(this.output)) { - return this.command('n') + return this.command('next', false) .then(() => this.waitFor(/break in/, timeout)); } }); @@ -127,8 +127,10 @@ function startCLI(args) { .map((match) => +match[1]); }, - command(input) { - this.flushOutput(); + command(input, flush = true) { + if (flush) { + this.flushOutput(); + } child.stdin.write(input); child.stdin.write('\n'); return this.waitForPrompt();