forked from meteor/meteor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inspector.js
100 lines (84 loc) · 2.79 KB
/
inspector.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
var assert = require("assert");
var inspector = require("node-inspector");
var inspectorBinPath = require.resolve("node-inspector/bin/inspector");
var spawn = require("child_process").spawn;
var chalk = require("chalk");
var EOL = require("os").EOL;
var debugPortToProcess = [];
var hasOwn = Object.prototype.hasOwnProperty;
function start(debugPort) {
debugPort = +(debugPort || 5858);
// Port 8080 is the default port that node-inspector uses for its web
// server, and port 5858 is the default port that node listens on when
// it receives the --debug or --debug-brk flags. Developers familiar
// with node-inspector may have http://localhost:8080/debug?port=5858
// saved in their browser history already, so let's stick with these
// conventions in the default case (unless of course the developer runs
// `meteor debug --debug-port <some other port>`).
var webPort = 8080 + debugPort - 5858;
if (hasOwn.call(debugPortToProcess, debugPort)) {
return debugPortToProcess[debugPort];
}
var proc = spawn(process.execPath, [
inspectorBinPath,
"--web-port", "" + webPort,
"--debug-port", "" + debugPort
]);
proc.url = inspector.buildInspectorUrl(
"localhost",
webPort,
debugPort
);
// Forward error output to process.stderr, but silence normal output.
// proc.stdout.pipe(process.stdout);
proc.stderr.pipe(process.stderr);
proc.on("exit", function(code) {
// Restart the process if it died without us explicitly stopping it.
if (debugPortToProcess[debugPort] === proc) {
delete debugPortToProcess[debugPort];
start(debugPort);
}
});
debugPortToProcess[debugPort] = proc;
return proc;
}
function banner(debugPort) {
debugPort = +(debugPort || 5858);
var proc = debugPortToProcess[debugPort];
assert.strictEqual(typeof proc.url, "string");
return [
"",
chalk.green([
"Your application is now paused and ready for debugging!",
"",
"To debug the server process using a graphical debugging interface, ",
"visit this URL in your web browser:"
].join(EOL)),
chalk.cyan(proc.url),
"",
chalk.green([
"To debug the server process using the command-line node debugger, ",
"execute this command in another terminal window:",
].join(EOL)),
chalk.cyan(process.execPath + " debug localhost:" + debugPort),
EOL
].join(EOL);
}
function stop(debugPort) {
debugPort = +(debugPort || 5858);
var proc = debugPortToProcess[debugPort];
if (proc.kill) {
console.error("killed " + proc.pid);
proc.kill();
}
delete debugPortToProcess[debugPort];
}
require("./cleanup.js").onExit(function killAll() {
for (var debugPort in debugPortToProcess) {
stop(debugPort);
}
debugPortToProcess.length = 0;
});
exports.start = start;
exports.banner = banner;
exports.stop = stop;