Skip to content

Commit 3f355b3

Browse files
committed
Check if file is not null when watching dir
1 parent edbb860 commit 3f355b3

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

src/cli/run.js

+22-16
Original file line numberDiff line numberDiff line change
@@ -185,21 +185,25 @@ module.exports = function (argv) {
185185
// Since lowdb uses atomic writing, directory is watched instead of file
186186
const watchedDir = path.dirname(source)
187187
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 {
195203
console.log(chalk.gray(` ${source} has changed, reloading...`))
196204
server && server.destroy()
197205
start()
198206
}
199-
} else {
200-
console.log(chalk.gray(` ${source} has changed, reloading...`))
201-
server && server.destroy()
202-
start()
203207
}
204208
}
205209
})
@@ -208,11 +212,13 @@ module.exports = function (argv) {
208212
if (argv.routes) {
209213
const watchedDir = path.dirname(argv.routes)
210214
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+
}
216222
}
217223
})
218224
}

0 commit comments

Comments
 (0)