-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
46 lines (40 loc) · 1.11 KB
/
server.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
var fs = require('fs');
var http = require('http');
var print = require('util').print;
var spawn = require('child_process').spawn;
var static = require('node-static');
function createServer(dir, cmd) {
dir = dir || '.';
// Server Start
var file = new static.Server(fs.realpathSync(dir));
var server = http.createServer(function(request, response) {
request.addListener('end', function() {
file.serve(request, response);
}).resume();
});
server.listen(8000, function() {
if (cmd) {
var args = cmd.split(' ');
args = args.map(function(arg) {
return arg.trim();
});
args = args.filter(function(arg) {
return arg.length;
});
var runner = spawn(args[0], args.slice(1));
runner.stdout.on('data', function(data) {
print(data.valueOf());
});
runner.on('exit', function(code) {
if (code === 127) {
print(cmd + ' not available');
}
server.close();
process.exit(code);
});
}
});
}
var dir = process.argv[2] || '.';
var cmd = process.argv.slice(3);
createServer(dir, cmd.join(' '));