-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
287 lines (217 loc) · 6.24 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
'use strict';
var _ = require( 'lodash' ),
gutil = require( 'gulp-util' ),
Transform = require( 'stream' ).Transform,
fork = require( 'child_process' ).fork,
pluginName = 'gulp-develop-server';
var defaultOptions = {
path: '',
env: _.extend( { NODE_ENV: 'development' }, process.env ),
args: [],
execArgv: [],
delay: 600,
successMessage: /^[Ss]erver listening/,
errorMessage: /[Ee]rror:/,
killSignal: 'SIGTERM'
};
function done( error, message, callback ) {
// fallback arguments
if( typeof message === 'function' ) {
callback = message;
}
// print message
else if( ! error && typeof message === 'string' ) {
gutil.log( message );
}
// run callback
if( typeof callback === 'function' ) {
callback( error || null );
}
return app;
}
function processUncaughtExceptionListener( event ) {
app.kill( function() {
gutil.log( gutil.colors.red( 'Development server has error.' ) );
console.log( event.name, event.message );
console.log( event.toString() );
});
}
function processExitListener() {
app.kill();
}
function handlingProcessEvent() {
// unbind previous process events
process.removeListener( 'uncaughtException', processUncaughtExceptionListener );
process.removeListener( 'exit', processExitListener );
// when other gulp-plugin throw exeption, kill server process
process.on( 'uncaughtException', processUncaughtExceptionListener );
// when gulp exit, kill server process
process.once( 'exit', processExitListener );
}
function serverStream( options ) {
var stream = new Transform( { objectMode: true } ),
isStream = false;
// override default options
if( ! app.child && typeof options === 'object' ) {
_.merge( app.options, options );
}
stream._transform = function( file, encoding, callback ) {
var pushFile = function() {
stream.push( file );
callback();
};
if( ! isStream ) {
isStream = true;
return app.changed( pushFile );
}
pushFile();
};
stream.on( 'finish', function() {
isStream = false;
});
return stream;
}
var app = module.exports = serverStream;
app.child = null;
app.isChanged = false;
app.options = _.cloneDeep( defaultOptions );
app.listen = function( options, callback ) {
// throw error when options is not set
if( ! app.options.path && typeof options.path !== 'string' ) {
throw new gutil.PluginError( pluginName, 'application `path` required.' );
}
// fallback arguments
if( typeof options === 'function' ) {
callback = options;
options = {};
}
// server already started
if( app.child && app.child.connected ) {
return done( 'Development server already started.', callback );
}
// override default options
if( typeof options === 'object' ) {
_.merge( app.options, options );
}
// run server process
var child = fork( app.options.path, app.options.args, {
cwd: app.options.cwd,
env: app.options.env,
execPath: app.options.execPath,
execArgv: app.options.execArgv,
gid: app.options.gid,
uid: app.options.uid,
silent: true
});
var timer;
var initialized = _.once( function( error ) {
if( timer ) {
timer = clearTimeout( timer );
}
if( error ) {
gutil.log( gutil.colors.red( error ) );
done( error, callback );
}
else {
var pid = gutil.colors.magenta( child.pid );
app.child = child;
done( null, 'Development server listening. (PID:' + pid + ')', callback );
}
child.stderr.removeListener( 'data', errorListener );
child.removeListener( 'message', successMessageListener );
});
// initialized by timer using `delay`
if( app.options.delay > 0 ) {
timer = setTimeout( initialized, app.options.delay );
}
// initialized by `successMessage`
var successMessageListener = function( message ) {
if( typeof message === 'string' && message.match( app.options.successMessage ) ) {
initialized();
}
};
child.on( 'message', successMessageListener );
// initialized by `errorMessage` if server printed error
var errorListener = function( error ) {
if( error instanceof Buffer && error.toString().match( app.options.errorMessage ) ) {
initialized( 'Development server has error.' );
}
};
child.stderr.on( 'data', errorListener );
// handling exit of child process
child.once( 'exit', function() {
app.child = null;
});
// pipe child_process's stdout / stderr
child.stdout.pipe( process.stdout );
child.stderr.pipe( process.stderr );
// bind process event
handlingProcessEvent();
return app;
};
app.kill = function( signal, callback ) {
// fallback arguments
if( typeof signal === 'function' ) {
callback = signal;
signal = app.options.killSignal;
}
else {
signal = signal || app.options.killSignal;
}
// send kill signal
if( app.child ) {
var pid = gutil.colors.magenta( app.child.pid );
var stopped = function() {
done( null, 'Development server was stopped. (PID:' + pid + ')', callback );
};
app.child.once( 'close', stopped );
app.child.kill( signal );
return app;
}
// server already stopped
return done( 'Development server already stopped.', callback );
};
app.changed = app.restart = function( callback ) {
// ensure restart when child process cannot connect
if( app.child && !app.child.connected ) {
app.child = null;
}
// already called this function
if( app.isChanged ) {
return done( null, 'Development server already received restart requests.', callback );
}
// restart server
var restarted = function( error ) {
app.isChanged = false;
return done( error, gutil.colors.cyan( 'Development server was restarted.' ), callback );
};
if( app.child ) {
app.isChanged = true;
return app.kill( function() {
app.listen( restarted );
});
}
// if server not started, try to start using options.path
else if( app.options.path ) {
app.isChanged = true;
return app.listen( restarted );
}
// server not started
throw new gutil.PluginError( pluginName, 'Development server not started.' );
};
app.reset = function( signal, callback ) {
// fallback arguments
if( typeof signal === 'function' ) {
callback = signal;
signal = app.options.killSignal;
}
else {
signal = signal || app.options.killSignal;
}
// kill server process and then reset options
var stopped = function( error ) {
app.options = _.cloneDeep( defaultOptions );
done( error, callback );
};
return app.kill( signal, stopped );
};