Check if file is not null when watching dir

This commit is contained in:
typicode
2016-11-27 20:12:38 +01:00
parent edbb860300
commit 3f355b3ada

View File

@ -185,6 +185,9 @@ module.exports = function (argv) {
// Since lowdb uses atomic writing, directory is watched instead of file
const watchedDir = path.dirname(source)
fs.watch(watchedDir, (event, file) => {
// https://github.com/typicode/json-server/issues/420
// file can be null
if (file) {
const watchedFile = path.resolve(watchedDir, file)
if (watchedFile === path.resolve(source)) {
if (is.JSON(watchedFile)) {
@ -202,18 +205,21 @@ module.exports = function (argv) {
start()
}
}
}
})
// Watch routes
if (argv.routes) {
const watchedDir = path.dirname(argv.routes)
fs.watch(watchedDir, (event, file) => {
if (file) {
const watchedFile = path.resolve(watchedDir, file)
if (watchedFile === path.resolve(argv.routes)) {
console.log(chalk.gray(` ${argv.routes} has changed, reloading...`))
server && server.destroy()
start()
}
}
})
}
}