Update watch

This commit is contained in:
Typicode
2016-07-01 13:31:11 +02:00
parent 0e6a2913ad
commit bf45e62b7a

View File

@ -156,23 +156,27 @@ 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 (is.JSON(file)) { if (file === source) {
var obj = JSON.parse(fs.readFileSync(file)) if (is.JSON(file)) {
// Compare .json file content with in memory database console.log(file, fs.readFileSync(file))
var isDatabaseDifferent = !_.eq(obj, app.db.getState()) var obj = JSON.parse(fs.readFileSync(file))
if (isDatabaseDifferent) { // Compare .json file content with in memory database
var isDatabaseDifferent = !_.eq(obj, app.db.getState())
if (isDatabaseDifferent) {
console.log(chalk.gray(' ' + file + ' has changed, reloading...'))
server && server.destroy()
start()
}
} else {
console.log(chalk.gray(' ' + file + ' has changed, reloading...')) console.log(chalk.gray(' ' + file + ' has changed, reloading...'))
server && server.destroy() server && server.destroy()
start() start()
} }
return
} }
server && server.destroy()
start()
}) })
// Watch routes // Watch routes