-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathindex.js
More file actions
162 lines (123 loc) · 5.12 KB
/
Copy pathindex.js
File metadata and controls
162 lines (123 loc) · 5.12 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
'use strict';
/*
* 1. Runs node-inspector.
* 2. Runs the supplied script in debug mode
* 3. Opens the user's browser, pointing it at the inspector.
*/
var es = require('event-stream'),
gutil = require('gulp-util'),
merge = require('merge'),
debugServer = require('node-inspector/lib/debug-server'),
Config = require('node-inspector/lib/config'),
packageJson = require('node-inspector/package.json'),
biasedOpen = require('biased-opener'),
open = require('opener'),
fork = require('child_process').fork,
enableDestroy = require('server-destroy');
var PluginError = gutil.PluginError;
var config = new Config([]);
var DebugServer = debugServer.DebugServer;
var log = gutil.log, colors = gutil.colors;
var PLUGIN_NAME = 'gulp-node-debug';
var nodeDebug = function(opt) {
var stream, files = [];
var options = merge(config, opt);
var startDebugServer = function() {
log(PLUGIN_NAME, 'is using node-inspector v' + packageJson.version);
// 1.
var debugServer = new DebugServer(options);
debugServer.on('error', function(err) {
if (err.code === 'EADDRINUSE') {
log(colors.red('There is another process already listening at this address.\nChange "webPort": {port} to use a different port.'));
}
stream.emit('error', PluginError(PLUGIN_NAME, 'Cannot start the server at ' + config.webHost + ':' + config.webPort + '. Error: ' + (err.message || err)));
});
debugServer.on('listening', function() {
enableDestroy(debugServer._httpServer);
var url = this.address().url;
log(colors.green('Node Inspector is now available at', url));
// 2.
startDebuggedProcess(
function startCallback() {
// 3.
openUrl(url);
}, function exitCallback() {
//log(colors.gray('Requesting debugServer to close.'));
debugServer.close();
});
});
debugServer.on('close', function () {
log(colors.gray('DebugServer closed.'));
// WebSocket Server closes the http server only if it was internally created, so it won't do the job for us
// And unfortunately debugServer does not clean up _httpServer at all, so it won't do the job for us too
// ... so its up to us to destroy it, by killing all open connections
debugServer._httpServer.destroy();
done();
});
debugServer.start(config);
};
// try to launch the URL in one of those browsers in the defined order
// (but if one of them is default browser, then it takes priority)
var openUrl = function(url) {
biasedOpen(url, {
preferredBrowsers : ['chrome', 'chromium', 'opera']
}, function (err, okMsg) {
if (err) {
// unable to launch one of preferred browsers for some reason
log(colors.gray('biased-opener:', err.message));
log(colors.gray('Opening url with default browser instead.'));
open(url);
}
});
}
var startDebuggedProcess = function(startCallback, exitCallback) {
if (!files.length) {
log(colors.red('No file to debug provided. Nothing to do.'));
exitCallback();
return;
}
// this might be changed later on. (e.g. picking a file by convention)
if (!files.length > 1) {
log(colors.red('More that one file provided. Only one single file can be debugged.'));
exitCallback();
return;
}
var fileToDebug = files[0];
var subprocDebugOption = (options.debugBrk ? '--debug-brk' : '--debug') + '=' + options.debugPort;
var subprocExecArgs = options.nodejs.concat(subprocDebugOption);
var subprocArgs = options.script || [];
log("Going to debug", fileToDebug, subprocArgs, subprocExecArgs);
var debuggedProcess = fork(
fileToDebug,
subprocArgs,
{ execArgv: subprocExecArgs }
);
debuggedProcess.on('exit', function() {
log(colors.gray('Debugged process exited.'));
exitCallback();
});
startCallback();
}
// -------------
function done() {
// End the stream if it exists
if (stream) {
stream.emit('end');
}
log(colors.gray('Done!'));
}
var queueFile = function (file) {
if (!file) {
stream.emit('error', new PluginError(PLUGIN_NAME, 'got undefined file'));
return;
}
files.push(file.path);
};
var endStream = function () {
startDebugServer();
};
// copied from gulp-karma
stream = es.through(queueFile, endStream);
return stream;
};
module.exports = nodeDebug;