From 3fd62672162c00ce4cb0ec6942e745a3df022e88 Mon Sep 17 00:00:00 2001
From: Jakob Moosbrugger <jak.moo54@gmail.com>
Date: Thu, 20 Apr 2017 01:16:27 +0200
Subject: [PATCH] support for --inspect

---
 lib/index.js | 31 +++++++++++++++++++++++++------
 src/index.js | 27 ++++++++++++++++++++++-----
 2 files changed, 47 insertions(+), 11 deletions(-)

diff --git a/lib/index.js b/lib/index.js
index 78476da..a75607e 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -7,7 +7,8 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
 var path = require("path"),
     fork = require("child_process").fork,
     worker = path.join(__dirname, "worker.js"),
-    events = /^(error|message)$/;
+    events = /^(error|message)$/,
+    defaultPorts = { inspect: 9229, debug: 5858 };
 
 var Worker = function () {
 	function Worker(arg) {
@@ -37,15 +38,33 @@ var Worker = function () {
 				options.execArgv = [];
 			}
 
-			var portIndex = debugVars.findIndex(function (debugArg) {
-				//get index of debug port specifier
-				return (/^--(debug|inspect)(-brk)?(=\d+)?$/.test(debugArg)
+			var inspectIndex = debugVars.findIndex(function (debugArg) {
+				//get index of inspect parameter
+				return (/^--inspect(-brk)?(=\d+)?$/.test(debugArg)
 				);
 			});
 
+			var debugIndex = debugVars.findIndex(function (debugArg) {
+				//get index of debug parameter
+				return (/^--debug(-brk)?(=\d+)?$/.test(debugArg)
+				);
+			});
+
+			var portIndex = inspectIndex >= 0 ? inspectIndex : debugIndex; //get index of port, inspect has higher priority
+
 			if (portIndex >= 0) {
-				//set new port, ignore "-brk", it doesn't work
-				debugVars[portIndex] = (/^--debug/.test(debugVars[portIndex]) ? "--debug=" : "--inspect=") + (process.debugPort + 1);
+				var match = /^--(debug|inspect)(?:-brk)?(?:=(\d+))?$/.exec(debugVars[portIndex]); //get port
+				var port = defaultPorts[match[1]];
+				if (match[2]) {
+					port = parseInt(match[2]);
+				}
+				debugVars[portIndex] = "--" + match[1] + "=" + (port + 1); //new parameter
+
+				if (debugIndex >= 0 && debugIndex !== portIndex) {
+					//remove "-brk" from debug if there
+					match = /^(--debug)(?:-brk)?(.*)/.exec(debugVars[debugIndex]);
+					debugVars[debugIndex] = match[1] + (match[2] ? match[2] : "");
+				}
 			}
 			options.execArgv = options.execArgv.concat(debugVars);
 		}
diff --git a/src/index.js b/src/index.js
index 7706ebe..9f37656 100644
--- a/src/index.js
+++ b/src/index.js
@@ -1,7 +1,8 @@
 const path = require("path"),
 	fork = require("child_process").fork,
 	worker = path.join(__dirname, "worker.js"),
-	events = /^(error|message)$/;
+	events = /^(error|message)$/,
+	defaultPorts = {inspect: 9229, debug: 5858};
 
 class Worker {
 	constructor (arg, args = undefined, options = {cwd: process.cwd()}) {
@@ -22,12 +23,28 @@ class Worker {
 				options.execArgv = [];
 			}
 
-			let portIndex = debugVars.findIndex(debugArg => { //get index of debug port specifier
-				return (/^--(debug|inspect)(-brk)?(=\d+)?$/).test(debugArg);
+			let inspectIndex = debugVars.findIndex(debugArg => { //get index of inspect parameter
+				return (/^--inspect(-brk)?(=\d+)?$/).test(debugArg);
 			});
 
-			if (portIndex >= 0) { //set new port, ignore "-brk", it doesn't work
-				debugVars[portIndex] = ((/^--debug/).test(debugVars[portIndex]) ? "--debug=" : "--inspect=") + (process.debugPort + 1);
+			let debugIndex = debugVars.findIndex(debugArg => { //get index of debug parameter
+				return (/^--debug(-brk)?(=\d+)?$/).test(debugArg);
+			});
+
+			let portIndex = inspectIndex >= 0 ? inspectIndex : debugIndex; //get index of port, inspect has higher priority
+
+			if (portIndex >= 0) {
+				var match = (/^--(debug|inspect)(?:-brk)?(?:=(\d+))?$/).exec(debugVars[portIndex]); //get port
+				var port = defaultPorts[match[1]];
+				if (match[2]) {
+					port = parseInt(match[2]);
+				}
+				debugVars[portIndex] = "--" + match[1] + "=" + (port + 1); //new parameter
+
+				if (debugIndex >= 0 && debugIndex !== portIndex) { //remove "-brk" from debug if there
+					match = (/^(--debug)(?:-brk)?(.*)/).exec(debugVars[debugIndex]);
+					debugVars[debugIndex] = match[1] + (match[2] ? match[2] : "");
+				}
 			}
 			options.execArgv = options.execArgv.concat(debugVars);
 
-- 
GitLab