@@ -18,16 +18,31 @@ var signals = require('./signals');
1818
1919function run ( options ) {
2020 var cmd = config . command . raw ;
21+ // moved up
22+ // we need restart function below in the global scope for run.kill
23+ /*jshint validthis:true*/
24+ restart = run . bind ( this , options ) ;
25+ run . restart = restart ;
26+
27+ // binding options with instance of run
28+ // so that we can use it in run.kill
29+ run . options = options ;
2130
2231 var runCmd = ! options . runOnChangeOnly || config . lastStarted !== 0 ;
2332 if ( runCmd ) {
2433 utils . log . status ( 'starting `' + config . command . string + '`' ) ;
34+ } else {
35+ // should just watch file if command is not to be run
36+ // had another alternate approach
37+ // to stop process being forked/spawned in the below code
38+ // but this approach does early exit and makes code cleaner
39+ debug ( 'start watch on: %s' , config . options . watch ) ;
40+ if ( config . options . watch !== false ) {
41+ watch ( ) ;
42+ return ;
43+ }
2544 }
2645
27- /*jshint validthis:true*/
28- restart = run . bind ( this , options ) ;
29- run . restart = restart ;
30-
3146 config . lastStarted = Date . now ( ) ;
3247
3348 var stdio = [ 'pipe' , 'pipe' , 'pipe' ] ;
@@ -237,53 +252,9 @@ function run(options) {
237252 }
238253 } ) ;
239254
240- run . kill = function ( noRestart , callback ) {
241- // I hate code like this :( - Remy (author of said code)
242- if ( typeof noRestart === 'function' ) {
243- callback = noRestart ;
244- noRestart = false ;
245- }
246-
247- if ( ! callback ) {
248- callback = noop ;
249- }
250-
251- if ( child !== null ) {
252- // if the stdin piping is on, we need to unpipe, but also close stdin on
253- // the child, otherwise linux can throw EPIPE or ECONNRESET errors.
254- if ( options . stdin ) {
255- process . stdin . unpipe ( child . stdin ) ;
256- }
257-
258- // For the on('exit', ...) handler above the following looks like a
259- // crash, so we set the killedAfterChange flag if a restart is planned
260- if ( ! noRestart ) {
261- killedAfterChange = true ;
262- }
263-
264- /* Now kill the entire subtree of processes belonging to nodemon */
265- var oldPid = child . pid ;
266- if ( child ) {
267- kill ( child , config . signal , function ( ) {
268- // this seems to fix the 0.11.x issue with the "rs" restart command,
269- // though I'm unsure why. it seems like more data is streamed in to
270- // stdin after we close.
271- if ( child && options . stdin && child . stdin && oldPid === child . pid ) {
272- child . stdin . end ( ) ;
273- }
274- callback ( ) ;
275- } ) ;
276- }
277- } else if ( ! noRestart ) {
278- // if there's no child, then we need to manually start the process
279- // this is because as there was no child, the child.on('exit') event
280- // handler doesn't exist which would normally trigger the restart.
281- bus . once ( 'start' , callback ) ;
282- restart ( ) ;
283- } else {
284- callback ( ) ;
285- }
286- } ;
255+ // moved the run.kill outside to handle both the cases
256+ // intial start
257+ // no start
287258
288259 // connect stdin to the child process (options.stdin is on by default)
289260 if ( options . stdin ) {
@@ -381,12 +352,54 @@ function kill(child, signal, callback) {
381352 }
382353}
383354
384- // stubbed out for now, filled in during run
385- run . kill = function ( flag , callback ) {
386- if ( callback ) {
355+ run . kill = function ( noRestart , callback ) {
356+ // I hate code like this :( - Remy (author of said code)
357+ if ( typeof noRestart === 'function' ) {
358+ callback = noRestart ;
359+ noRestart = false ;
360+ }
361+
362+ if ( ! callback ) {
363+ callback = noop ;
364+ }
365+
366+ if ( child !== null ) {
367+ // if the stdin piping is on, we need to unpipe, but also close stdin on
368+ // the child, otherwise linux can throw EPIPE or ECONNRESET errors.
369+ if ( run . options . stdin ) {
370+ process . stdin . unpipe ( child . stdin ) ;
371+ }
372+
373+ // For the on('exit', ...) handler above the following looks like a
374+ // crash, so we set the killedAfterChange flag if a restart is planned
375+ if ( ! noRestart ) {
376+ killedAfterChange = true ;
377+ }
378+
379+ /* Now kill the entire subtree of processes belonging to nodemon */
380+ var oldPid = child . pid ;
381+ if ( child ) {
382+ kill ( child , config . signal , function ( ) {
383+ // this seems to fix the 0.11.x issue with the "rs" restart command,
384+ // though I'm unsure why. it seems like more data is streamed in to
385+ // stdin after we close.
386+ if ( child && run . options . stdin && child . stdin && oldPid === child . pid ) {
387+ child . stdin . end ( ) ;
388+ }
389+ callback ( ) ;
390+ } ) ;
391+ }
392+ } else if ( ! noRestart ) {
393+ // if there's no child, then we need to manually start the process
394+ // this is because as there was no child, the child.on('exit') event
395+ // handler doesn't exist which would normally trigger the restart.
396+ bus . once ( 'start' , callback ) ;
397+ run . restart ( ) ;
398+ } else {
387399 callback ( ) ;
388400 }
389401} ;
402+
390403run . restart = noop ;
391404
392405bus . on ( 'quit' , function onQuit ( code ) {
0 commit comments