@@ -185,21 +185,25 @@ module.exports = function (argv) {
185
185
// Since lowdb uses atomic writing, directory is watched instead of file
186
186
const watchedDir = path . dirname ( source )
187
187
fs . watch ( watchedDir , ( event , file ) => {
188
- const watchedFile = path . resolve ( watchedDir , file )
189
- if ( watchedFile === path . resolve ( source ) ) {
190
- if ( is . JSON ( watchedFile ) ) {
191
- var obj = JSON . parse ( fs . readFileSync ( watchedFile ) )
192
- // Compare .json file content with in memory database
193
- var isDatabaseDifferent = ! _ . isEqual ( obj , app . db . getState ( ) )
194
- if ( isDatabaseDifferent ) {
188
+ // https://github.com/typicode/json-server/issues/420
189
+ // file can be null
190
+ if ( file ) {
191
+ const watchedFile = path . resolve ( watchedDir , file )
192
+ if ( watchedFile === path . resolve ( source ) ) {
193
+ if ( is . JSON ( watchedFile ) ) {
194
+ var obj = JSON . parse ( fs . readFileSync ( watchedFile ) )
195
+ // Compare .json file content with in memory database
196
+ var isDatabaseDifferent = ! _ . isEqual ( obj , app . db . getState ( ) )
197
+ if ( isDatabaseDifferent ) {
198
+ console . log ( chalk . gray ( ` ${ source } has changed, reloading...` ) )
199
+ server && server . destroy ( )
200
+ start ( )
201
+ }
202
+ } else {
195
203
console . log ( chalk . gray ( ` ${ source } has changed, reloading...` ) )
196
204
server && server . destroy ( )
197
205
start ( )
198
206
}
199
- } else {
200
- console . log ( chalk . gray ( ` ${ source } has changed, reloading...` ) )
201
- server && server . destroy ( )
202
- start ( )
203
207
}
204
208
}
205
209
} )
@@ -208,11 +212,13 @@ module.exports = function (argv) {
208
212
if ( argv . routes ) {
209
213
const watchedDir = path . dirname ( argv . routes )
210
214
fs . watch ( watchedDir , ( event , file ) => {
211
- const watchedFile = path . resolve ( watchedDir , file )
212
- if ( watchedFile === path . resolve ( argv . routes ) ) {
213
- console . log ( chalk . gray ( ` ${ argv . routes } has changed, reloading...` ) )
214
- server && server . destroy ( )
215
- start ( )
215
+ if ( file ) {
216
+ const watchedFile = path . resolve ( watchedDir , file )
217
+ if ( watchedFile === path . resolve ( argv . routes ) ) {
218
+ console . log ( chalk . gray ( ` ${ argv . routes } has changed, reloading...` ) )
219
+ server && server . destroy ( )
220
+ start ( )
221
+ }
216
222
}
217
223
} )
218
224
}
0 commit comments