Skip to content

Commit

Permalink
Merge branch 'zhangbg-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
typicode committed Dec 8, 2016
2 parents 760c37d + 7cf8584 commit da7d618
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/cli/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,24 +190,29 @@ 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) => {
<<<<<<< HEAD
// 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)) {
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
var isDatabaseDifferent = !_.isEqual(obj, app.db.getState())
if (isDatabaseDifferent) {
console.log(chalk.gray(` ${source} has changed, reloading...`))
server && server.destroy()
start()
}
} else {
console.log(chalk.gray(` ${source} has changed, reloading...`))
server && server.destroy()
start()
}
}
}
Expand Down

0 comments on commit da7d618

Please sign in to comment.