-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
65 lines (58 loc) · 1.66 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
var app = require('app');
var remote = require('chrome-remote-interface');
var chokidar = require('chokidar');
var fs = require('fs');
var scriptIds = {};
var watcher;
function connectToClient(port) {
return new Promise(function(resolve, reject) {
remote({
port: port
}, function(client) {
resolve(client);
}).on('error', function() {
reject(Error('Unable to connect to client'));
});
});
}
module.exports = function(options) {
options = options || {};
options.port = options.port || 9222;
app.commandLine.appendSwitch('remote-debugging-port', options.port.toString());
app.on('quit', function() {
if (watcher) {
watcher.close();
watcher = null;
}
});
app.on('ready', function() {
connectToClient(options.port).then(function(client) {
client.Debugger.scriptParsed(function(script) {
if (script.url.indexOf('file://') === -1) return;
var url = script.url.replace('file://', '');
if (scriptIds[url]) return;
scriptIds[url] = script.scriptId;
if (!watcher) {
watcher = chokidar.watch(url);
watcher.on('change', function(path) {
var scriptId = scriptIds[path];
fs.readFile(path, {
encoding: 'utf-8'
}, function(err, data) {
if (err) throw err;
client.Debugger.setScriptSource({
scriptId: scriptId,
scriptSource: data
}, function(err) {
if (err) throw err;
});
});
});
} else {
watcher.add(url);
}
});
client.Debugger.enable();
});
});
};