-
Notifications
You must be signed in to change notification settings - Fork 33
/
index.js
109 lines (88 loc) · 2.77 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
var http = require('http');
var util = require('util');
var child = require('child_process');
var fs = require('fs');
var path = require('path');
var mime = require('mime');
var chokidar = require('chokidar');
var rdbg = require('rdbg');
function serve(options, callback) {
var server = http.createServer(function(request, response) {
if (request.url == '/') {
response.setHeader('content-type', 'text/html');
response.write('<!doctype html><head><meta charset="utf-8"></head><body>');
for (var key in options.scripts) {
response.write('<script src="' + key + '"></script>');
}
response.end('</body>');
} else {
var filename = options.scripts[path.basename(request.url)];
if (!filename) {
var filename = path.join(options.cwd, request.url);
}
fs.exists(filename, function(exists) {
if (exists) {
response.setHeader('content-type', mime.lookup(filename));
response.writeHead(200);
var file = fs.createReadStream(filename);
file.pipe(response);
} else {
response.writeHead(404);
response.end('404');
}
});
}
});
server.listen(options.port, options.host, callback);
return server;
}
function bundle(options, callback) {
var cmd = util.format('%s %s', options.bundler, options.args.join(' '));
return child.exec(cmd, callback);
}
function watch(options, callback) {
var watcher = chokidar.watch(options.cwd, {
ignored: /[\/\\]\./, persistent: true
});
if (callback) {
watcher.once('ready', callback);
}
return watcher;
}
function debug(options, callback) {
var bugger = rdbg.connect(options.debuggerPort, options.debuggerHost, function(targets) {
var target = targets.filter(function(target) {
return target.url.indexOf(options.host) > -1;
})[0];
bugger.on('detatch', function() {
var id = setInterval(function() {
bugger.targets(function(targets) {
if (targets == undefined) {
targets = [];
}
var target = targets.filter(function(target) {
return target.url.indexOf(options.host) > -1;
})[0];
if (target) {
bugger.attach(target, function() {
});
}
});
}, 500);
bugger.once('attach', function() {
clearInterval(id);
});
});
bugger.attach(target, callback);
});
return bugger;
}
function browse(options, callback) {
var cmd = util.format('%s http://%s:%d', options.browser, options.host, options.port);
return child.exec(cmd, callback);
}
module.exports.serve = serve;
module.exports.watch = watch;
module.exports.bundle = bundle;
module.exports.debug = debug;
module.exports.browse = browse;