mirror of
https://github.com/typicode/json-server.git
synced 2025-07-27 20:23:34 +08:00
Update watch
This commit is contained in:
@ -156,10 +156,13 @@ module.exports = function (argv) {
|
|||||||
if (is.URL(source)) throw new Error('Can\'t watch URL')
|
if (is.URL(source)) throw new Error('Can\'t watch URL')
|
||||||
|
|
||||||
// Watch .js or .json file
|
// Watch .js or .json file
|
||||||
|
// Since lowdb uses atomic writing, directory is watched instead of file
|
||||||
chokidar
|
chokidar
|
||||||
.watch(source)
|
.watch(path.dirname(source))
|
||||||
.on('change', function (file) {
|
.on('change', function (file) {
|
||||||
|
if (file === source) {
|
||||||
if (is.JSON(file)) {
|
if (is.JSON(file)) {
|
||||||
|
console.log(file, fs.readFileSync(file))
|
||||||
var obj = JSON.parse(fs.readFileSync(file))
|
var obj = JSON.parse(fs.readFileSync(file))
|
||||||
// Compare .json file content with in memory database
|
// Compare .json file content with in memory database
|
||||||
var isDatabaseDifferent = !_.eq(obj, app.db.getState())
|
var isDatabaseDifferent = !_.eq(obj, app.db.getState())
|
||||||
@ -168,11 +171,12 @@ module.exports = function (argv) {
|
|||||||
server && server.destroy()
|
server && server.destroy()
|
||||||
start()
|
start()
|
||||||
}
|
}
|
||||||
return
|
} else {
|
||||||
}
|
console.log(chalk.gray(' ' + file + ' has changed, reloading...'))
|
||||||
|
|
||||||
server && server.destroy()
|
server && server.destroy()
|
||||||
start()
|
start()
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// Watch routes
|
// Watch routes
|
||||||
|
Reference in New Issue
Block a user