mirror of
https://github.com/typicode/json-server.git
synced 2025-08-01 09:13:32 +08:00
Check if file is not null when watching dir
This commit is contained in:
@ -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()
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user