Skip to content

Commit

Permalink
Improved robustness
Browse files Browse the repository at this point in the history
  • Loading branch information
billyzkid committed Dec 8, 2012
1 parent f39209e commit 1635ad3
Showing 1 changed file with 78 additions and 76 deletions.
154 changes: 78 additions & 76 deletions bin/nodebug.js
Expand Up @@ -25,124 +25,126 @@ if (!argv["_"].length) {
showError("script required", true);
}

try {
var inspectorProcess = startInspector();
var browserProcess = launchBrowser();
var scriptProcess = executeScript();
} catch (error) {
showError(error.message, false);
var inspectorPath = findInspector();
var chromePath = findChrome();

if (!inspectorPath) {
showError("node-inspector not found", false);
}

process.on("exit", function () {
if (!argv["keep-alive"]) {
if (scriptProcess) {
scriptProcess.kill();
}
if (!chromePath) {
showError("chrome not found", false);
}

if (inspectorProcess) {
inspectorProcess.kill();
}
var scriptProcess = executeScript();
var inspectorProcess = startInspector();
var chromeProcess = launchChrome();

if (browserProcess) {
browserProcess.kill();
}
process.on("exit", function () {
if (!argv["keep-alive"]) {
scriptProcess.kill();
inspectorProcess.kill();
chromeProcess.kill();
}
});

/* Shows the help message and exits with success code */
/* Shows the help message and exits */
function showHelp() {
var help = optimist.help().trim();
console.log(help);
console.log(optimist.help().trim());
process.exit(0);
}

/* Shows an error message and exits with failure code */
/* Shows an error message and exits */
function showError(message, includeHelp) {
if (includeHelp) {
var help = optimist.help().trim();
console.error("Error: " + message + "\n" + help);
} else {
console.error("Error: " + message);
}
console.error("Error: " + message);

process.exit(1);
}

/* Executes the node script */
function executeScript() {
var nodePath = process.execPath;
var nodeArgs = [];

if (argv["debug-brk"]) {
nodeArgs.push("--debug-brk=" + argv["debug-port"]);
} else {
nodeArgs.push("--debug=" + argv["debug-port"]);
if (includeHelp) {
console.log(optimist.help().trim());
}

nodeArgs = nodeArgs.concat(argv["_"]);

return child_process.spawn(nodePath, nodeArgs, { stdio: "inherit" }).on("exit", process.exit);
process.exit(1);
}

/* Starts node-inspector */
function startInspector() {
var searchPaths = [];
/* Searches for chrome */
function findChrome() {
var paths = [];

switch (process.platform) {
case "win32":
searchPaths.push(path.join(__dirname, "..", "node_modules", ".bin", "node-inspector.cmd"));
paths.push(path.join(process.env["LocalAppData"], "Google", "Chrome", "Application", "chrome.exe"));
paths.push(path.join(process.env["ProgramFiles"], "Google", "Chrome", "Application", "chrome.exe"));
paths.push(path.join(process.env["ProgramFiles(x86)"], "Google", "Chrome", "Application", "chrome.exe"));
break;

case "darwin":
paths.push(path.join("/", "Applications", "Google Chrome.app", "Contents", "MacOS", "Google Chrome"));
break;

default:
searchPaths.push(path.join(__dirname, "..", "node_modules", ".bin", "node-inspector"));
paths.push(path.join("/", "opt", "google", "chrome", "google-chrome"));
break;
}

var inspectorPath = firstExistingPath("node-inspector", searchPaths);
var inspectorArgs = [];

inspectorArgs.push("--web-host=" + argv["web-host"]);
inspectorArgs.push("--web-port=" + argv["web-port"]);

return child_process.execFile(inspectorPath, inspectorArgs);
return firstExistingPath(paths);
}

/* Launches a web browser (i.e. Chrome) */
function launchBrowser() {
var searchPaths = [];
/* Searches for node-inspector */
function findInspector() {
var paths = [];

switch (process.platform) {
case "win32":
searchPaths.push(path.join(process.env["LocalAppData"], "Google", "Chrome", "Application", "chrome.exe"));
searchPaths.push(path.join(process.env["ProgramFiles"], "Google", "Chrome", "Application", "chrome.exe"));
searchPaths.push(path.join(process.env["ProgramFiles(x86)"], "Google", "Chrome", "Application", "chrome.exe"));
break;

case "darwin":
searchPaths.push(path.join("/", "Applications", "Google Chrome.app", "Contents", "MacOS", "Google Chrome"));
paths.push(path.join(__dirname, "..", "node_modules", ".bin", "node-inspector.cmd"));
break;

default:
searchPaths.push(path.join("/", "opt", "google", "chrome", "google-chrome"));
paths.push(path.join(__dirname, "..", "node_modules", ".bin", "node-inspector"));
break;
}

var browserPath = firstExistingPath("chrome", searchPaths);
var browserArgs = [];

browserArgs.push("--app=http://" + argv["web-host"] + ":" + argv["web-port"] + "/debug?port=" + argv["debug-port"]);
browserArgs.push("--user-data-dir=" + path.join(__dirname, "..", "ChromeProfile"));

return child_process.execFile(browserPath, browserArgs);
return firstExistingPath(paths);
}

/* Searches an array of paths for the first one that exists */
function firstExistingPath(description, paths) {
/* Searches paths for the first one that exists */
function firstExistingPath(paths) {
for (var i = 0; i < paths.length; i++) {
if (fs.existsSync(paths[i])) {
return paths[i];
}
}

throw new Error(description + " not found");
}
return null;
}

/* Executes the script with the node debugger attached */
function executeScript() {
var args = [];

if (argv["debug-brk"]) {
args.push("--debug-brk=" + argv["debug-port"]);
} else {
args.push("--debug=" + argv["debug-port"]);
}

args = args.concat(argv["_"]);

return child_process.spawn(process.execPath, args, { stdio: "inherit" }).on("exit", process.exit);
}

/* Starts node-inspector */
function startInspector() {
var args = [];
args.push("--web-host=" + argv["web-host"]);
args.push("--web-port=" + argv["web-port"]);

return child_process.spawn(inspectorPath, args);
}

/* Launches chrome */
function launchChrome() {
var args = [];
args.push("--app=http://" + argv["web-host"] + ":" + argv["web-port"] + "/debug?port=" + argv["debug-port"]);
args.push("--user-data-dir=" + path.join(__dirname, "..", "ChromeProfile"));

return child_process.execFile(chromePath, args).on("exit", process.exit);
}

0 comments on commit 1635ad3

Please sign in to comment.