-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
index.js
126 lines (111 loc) · 3.38 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
var http = require('http');
var path = require('path');
var spawn = require('child_process').spawn;
var through = require('through');
var duplex = require('duplexer');
var fs = require('fs');
var xws = require('xhr-write-stream')();
var enstore = require('enstore');
var launch = require('./lib/launch');
var ecstatic = require('ecstatic');
var injectScript = require('html-inject-script');
var destroyable = require('server-destroy');
var extend = require('xtend')
try {
fs.statSync(__dirname + '/static/reporter.js')
} catch (_) {
console.error('Reporter script missing. Run `npm run build` first.')
}
module.exports = function (opts) {
if (!opts) opts = {};
if ('number' == typeof opts) opts = { port: opts };
if (!opts.browser) opts.browser = 'electron';
if (!opts.input) opts.input = 'javascript';
return runner(opts);
};
function runner (opts) {
var empty = true;
var input = through(function (chunk) {
if (empty && chunk.toString().trim() != '') empty = false;
this.queue(chunk);
}, function () {
if (empty) dpl.emit('error', new Error('javascript required'));
this.queue(null);
});
var bundle = enstore();
input.pipe(bundle.createWriteStream());
var output = through();
var dpl = duplex(input, output);
var mockHandler = opts.mock && require(path.resolve('./', opts.mock))
var server = http.createServer(function (req, res) {
if (opts.input === 'javascript') {
if (/^\/bundle\.js/.test(req.url)) {
res.setHeader('content-type', 'application/javascript');
res.setHeader('cache-control', 'no-cache');
bundle.createReadStream().pipe(res);
return;
}
if (req.url == '/') {
fs.createReadStream(__dirname + '/static/index.html').pipe(res);
return;
}
} else if (opts.input === 'html') {
if (req.url == '/') {
bundle.createReadStream().pipe(injectScript(['/reporter.js'])).pipe(res);
return;
}
}
if (req.url == '/xws') {
req.pipe(xws(function (stream) {
stream.pipe(output);
}));
return req.on('end', res.end.bind(res));
}
if (req.url == '/reporter.js') {
res.setHeader('content-type', 'application/javascript');
fs.createReadStream(__dirname + '/static/reporter.js').pipe(res);
return;
}
if (opts.static) {
ecstatic({ root: opts.static })(req, res);
return;
}
if (mockHandler && '/mock' === req.url.substr(0,5)){
return mockHandler(req, res);
}
res.end('not supported');
});
destroyable(server);
var browser;
if (opts.port) {
server.listen(opts.port);
} else {
server.listen(function () {
var address = server.address();
if (!address) return; // already closed
var port = address.port;
launch(extend(opts, {
loc: 'http://localhost:' + port,
name: opts.browser,
bundle: bundle
}), function(err, _browser){
if (err) return dpl.emit('error', err);
browser = _browser;
// electron
if (browser.pipe) {
browser.setEncoding('utf8');
browser.pipe(output);
}
browser.on('exit', function (code, signal) {
try { server.destroy() } catch (e) {}
dpl.emit('exit', code, signal);
});
});
});
}
dpl.stop = function () {
try { server.destroy(); } catch (e) {}
if (browser) browser.kill();
};
return dpl;
}