mirror of
https://github.com/typicode/json-server.git
synced 2025-08-01 06:34:02 +08:00
Merge branch 'master' of https://github.com/zhangbg/json-server into zhangbg-master
This commit is contained in:
@ -190,13 +190,22 @@ module.exports = function (argv) {
|
|||||||
// Since lowdb uses atomic writing, directory is watched instead of file
|
// Since lowdb uses atomic writing, directory is watched instead of file
|
||||||
const watchedDir = path.dirname(source)
|
const watchedDir = path.dirname(source)
|
||||||
fs.watch(watchedDir, (event, file) => {
|
fs.watch(watchedDir, (event, file) => {
|
||||||
|
<<<<<<< HEAD
|
||||||
// https://github.com/typicode/json-server/issues/420
|
// https://github.com/typicode/json-server/issues/420
|
||||||
// file can be null
|
// file can be null
|
||||||
if (file) {
|
if (file) {
|
||||||
const watchedFile = path.resolve(watchedDir, file)
|
const watchedFile = path.resolve(watchedDir, file)
|
||||||
if (watchedFile === path.resolve(source)) {
|
if (watchedFile === path.resolve(source)) {
|
||||||
if (is.JSON(watchedFile)) {
|
if (is.JSON(watchedFile)) {
|
||||||
var obj = JSON.parse(fs.readFileSync(watchedFile))
|
var obj
|
||||||
|
try {
|
||||||
|
obj = JSON.parse(fs.readFileSync(watchedFile))
|
||||||
|
} catch (e) {
|
||||||
|
console.log('Error reading JSON file');
|
||||||
|
console.dir(e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Compare .json file content with in memory database
|
// Compare .json file content with in memory database
|
||||||
var isDatabaseDifferent = !_.isEqual(obj, app.db.getState())
|
var isDatabaseDifferent = !_.isEqual(obj, app.db.getState())
|
||||||
if (isDatabaseDifferent) {
|
if (isDatabaseDifferent) {
|
||||||
@ -204,10 +213,6 @@ module.exports = function (argv) {
|
|||||||
server && server.destroy()
|
server && server.destroy()
|
||||||
start()
|
start()
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
console.log(chalk.gray(` ${source} has changed, reloading...`))
|
|
||||||
server && server.destroy()
|
|
||||||
start()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user