-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
74 lines (67 loc) · 2.22 KB
/
index.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
#!/usr/bin/env node
var BrowserDebugView = require('./browser-debug-view');
var RemoteConnectionHub = require('./remote-connection-hub');
var commandLineArgs = require('command-line-args');
var commandLineUsage = require('command-line-usage');
var express = require('express');
var expressWs = require('express-ws');
var path = require('path');
var webpack = require('webpack');
var webpackDevMiddleware = require('webpack-dev-middleware');
var DEFAULT_REMOTE_PORT = 3009;
var commandLineOptions = [
{
name: 'help',
alias: 'h',
type: Boolean,
description: 'Print this usage guide.'
},
{
name: 'remote',
alias: 'r',
type: Boolean,
description:
'Instead of reading stdin (the default), listen for WebSocket connections. Also prints information about how to connect via WebSockets.'
},
{
name: 'port',
alias: 'p',
type: Number,
description:
'Port the debug server will listen on. In remote mode, the default port is ' +
DEFAULT_REMOTE_PORT + '.'
},
];
var options = commandLineArgs(commandLineOptions);
if (options.help) {
console.log(commandLineUsage([
{
header: 'debugview',
content: 'Language-agnostic debugging widgets based on print statements'
},
{header: 'Options', optionList: commandLineOptions}
]));
process.exit();
}
// Create a web server.
var app = express();
expressWs(app); // Handle WebSocket connections.
app.use(express.static(path.join(__dirname, 'public')));
app.use(webpackDevMiddleware(
webpack(require('./webpack.config')), {noInfo: true, quiet: true}));
// Listen for connections on any open port & show the debug view.
var port = options.port || (options.remote ? DEFAULT_REMOTE_PORT : 0);
var listener = app.listen(port, function() {
if (options.remote) {
// Listen for WebSocket connections, creating a new debug view for each one.
new RemoteConnectionHub().listen(app, listener, function(stream) {
new BrowserDebugView().open(app, listener, stream);
});
} else {
// Echo program's stdout to stdout.
process.stdin.pipe(process.stdout);
// Open a debug view based on stdin.
new BrowserDebugView().open(
app, listener, process.stdin, function() { process.exit(); });
}
});