@@ -9,7 +9,8 @@ var tsVersion = require('typescript').version
9
9
var kill = require ( 'tree-kill' )
10
10
var readline = require ( 'readline' )
11
11
12
- module . exports = function ( script , scriptArgs , nodeArgs , opts ) {
12
+
13
+ module . exports = function ( script , scriptArgs , nodeArgs , opts ) {
13
14
if ( typeof script !== 'string' || script . length === 0 ) {
14
15
throw new TypeError ( '`script` must be a string' )
15
16
}
@@ -44,35 +45,37 @@ module.exports = function(script, scriptArgs, nodeArgs, opts) {
44
45
// debounce: parseInt(opts.debounce),
45
46
// recursive: process.platform !== 'linux'
46
47
// })
47
-
48
- function initWatcher ( ) {
48
+
49
+ function initWatcher ( ) {
49
50
var watcher = chokidar . watch ( [ ] , {
50
51
usePolling : opts . poll ,
51
52
interval : parseInt ( opts . interval ) || undefined ,
52
-
53
53
} )
54
54
watcher . on ( 'change' , restart )
55
55
56
- watcher . on ( 'fallback' , function ( limit ) {
57
- log . warn ( 'node-dev ran out of file handles after watching %s files.' , limit )
56
+ watcher . on ( 'fallback' , function ( limit ) {
57
+ log . warn (
58
+ 'node-dev ran out of file handles after watching %s files.' ,
59
+ limit
60
+ )
58
61
log . warn ( 'Falling back to polling which uses more CPU.' )
59
62
log . info ( 'Run ulimit -n 10000 to increase the file descriptor limit.' )
60
63
if ( cfg . deps ) log . info ( '... or add `--no-deps` to use less file handles.' )
61
64
} )
62
65
return watcher
63
66
}
64
67
var watcher = initWatcher ( )
65
-
68
+
66
69
var starting = false
67
-
70
+
68
71
// Read for "rs" from command line
69
72
if ( opts . rs !== false ) {
70
73
const rl = readline . createInterface ( {
71
74
input : process . stdin ,
72
75
output : process . stdout ,
73
- terminal : false
76
+ terminal : false ,
74
77
} )
75
- rl . on ( 'line' , function ( line ) {
78
+ rl . on ( 'line' , function ( line ) {
76
79
if ( line . trim ( ) === 'rs' ) {
77
80
restart ( '' , true )
78
81
}
@@ -99,22 +102,22 @@ module.exports = function(script, scriptArgs, nodeArgs, opts) {
99
102
log . debug ( 'Starting child process %s' , cmd . join ( ' ' ) )
100
103
child = fork ( cmd [ 0 ] , cmd . slice ( 1 ) , {
101
104
cwd : process . cwd ( ) ,
102
- env : process . env
105
+ env : process . env ,
103
106
} )
104
107
starting = false
105
108
//var compileReqWatcher = filewatcher({ forcePolling: opts.poll })
106
109
if ( compileReqWatcher ) {
107
110
compileReqWatcher . close ( )
108
- }
111
+ }
109
112
compileReqWatcher = chokidar . watch ( [ ] , {
110
113
usePolling : opts . poll ,
111
- interval : parseInt ( opts . interval ) || undefined
114
+ interval : parseInt ( opts . interval ) || undefined ,
112
115
} )
113
116
var currentCompilePath
114
117
fs . writeFileSync ( compiler . getCompileReqFilePath ( ) , '' )
115
118
compileReqWatcher . add ( compiler . getCompileReqFilePath ( ) )
116
- compileReqWatcher . on ( 'change' , function ( file ) {
117
- fs . readFile ( file , 'utf-8' , function ( err , data ) {
119
+ compileReqWatcher . on ( 'change' , function ( file ) {
120
+ fs . readFile ( file , 'utf-8' , function ( err , data ) {
118
121
if ( err ) {
119
122
log . error ( 'Error reading compile request file' , err )
120
123
return
@@ -128,19 +131,19 @@ module.exports = function(script, scriptArgs, nodeArgs, opts) {
128
131
if ( compiledPath ) {
129
132
compiler . compile ( {
130
133
compile : compile ,
131
- compiledPath : compiledPath
134
+ compiledPath : compiledPath ,
132
135
} )
133
136
}
134
137
} )
135
138
} )
136
- child . on ( 'message' , function ( message ) {
139
+ child . on ( 'message' , function ( message ) {
137
140
if ( ! message . compiledPath || currentCompilePath === message . compiledPath )
138
141
return
139
142
currentCompilePath = message . compiledPath
140
143
compiler . compile ( message )
141
144
} )
142
145
143
- child . on ( 'exit' , function ( code ) {
146
+ child . on ( 'exit' , function ( code ) {
144
147
log . debug ( 'Child exited with code %s' , code )
145
148
if ( ! child ) return
146
149
if ( ! child . respawn ) process . exit ( code )
@@ -156,18 +159,19 @@ module.exports = function(script, scriptArgs, nodeArgs, opts) {
156
159
}
157
160
158
161
// Listen for `required` messages and watch the required file.
159
- ipc . on ( child , 'required' , function ( m ) {
162
+ ipc . on ( child , 'required' , function ( m ) {
160
163
var isIgnored =
161
164
cfg . ignore . some ( isPrefixOf ( m . required ) ) ||
162
165
cfg . ignore . some ( isRegExpMatch ( m . required ) )
163
166
164
167
if ( ! isIgnored && ( cfg . deps === - 1 || getLevel ( m . required ) <= cfg . deps ) ) {
168
+ log . debug ( m . required , 'added to watcher' )
165
169
watcher . add ( m . required )
166
170
}
167
171
} )
168
172
169
173
// Upon errors, display a notification and tell the child to exit.
170
- ipc . on ( child , 'error' , function ( m ) {
174
+ ipc . on ( child , 'error' , function ( m ) {
171
175
log . debug ( 'Child error' )
172
176
notify ( m . error , m . message , 'error' )
173
177
stop ( m . willTerminate )
@@ -194,7 +198,7 @@ module.exports = function(script, scriptArgs, nodeArgs, opts) {
194
198
log . debug ( 'Disconnecting from child' )
195
199
child . disconnect ( )
196
200
//if (!willTerminate) {
197
- killChild ( )
201
+ killChild ( )
198
202
//}
199
203
}
200
204
}
@@ -219,7 +223,7 @@ module.exports = function(script, scriptArgs, nodeArgs, opts) {
219
223
}
220
224
log . debug ( 'Removing all watchers from files' )
221
225
//watcher.removeAll()ya
222
-
226
+
223
227
watcher . close ( )
224
228
watcher = initWatcher ( )
225
229
starting = true
@@ -235,18 +239,18 @@ module.exports = function(script, scriptArgs, nodeArgs, opts) {
235
239
compiler . restart = restart
236
240
237
241
// Relay SIGTERM
238
- process . on ( 'SIGTERM' , function ( ) {
242
+ process . on ( 'SIGTERM' , function ( ) {
239
243
log . debug ( 'Process got SIGTERM' )
240
244
killChild ( )
241
245
if ( opts [ 'restart-terminated' ] ) {
242
246
var timeout = opts [ 'restart-terminated' ] || 0
243
247
log . info ( 'Restarting terminated in ' + timeout + ' seconds' )
244
- setTimeout ( ( ) => {
248
+ setTimeout ( ( ) => {
245
249
start ( )
246
250
} , timeout )
247
251
} else {
248
252
process . exit ( 0 )
249
- }
253
+ }
250
254
} )
251
255
252
256
start ( )
@@ -273,13 +277,13 @@ function getPrefix(mod) {
273
277
}
274
278
275
279
function isPrefixOf ( value ) {
276
- return function ( prefix ) {
280
+ return function ( prefix ) {
277
281
return value . indexOf ( prefix ) === 0
278
282
}
279
283
}
280
284
281
285
function isRegExpMatch ( value ) {
282
- return function ( regExp ) {
286
+ return function ( regExp ) {
283
287
return new RegExp ( regExp ) . test ( value )
284
288
}
285
289
}
0 commit comments