From bf45e62b7a3819afb5e1d1dded8b7ab8bb26582c Mon Sep 17 00:00:00 2001 From: Typicode Date: Fri, 1 Jul 2016 13:31:11 +0200 Subject: [PATCH] Update watch --- src/cli/run.js | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/cli/run.js b/src/cli/run.js index 70c6e04..8a6a138 100644 --- a/src/cli/run.js +++ b/src/cli/run.js @@ -156,23 +156,27 @@ module.exports = function (argv) { if (is.URL(source)) throw new Error('Can\'t watch URL') // Watch .js or .json file + // Since lowdb uses atomic writing, directory is watched instead of file chokidar - .watch(source) + .watch(path.dirname(source)) .on('change', function (file) { - if (is.JSON(file)) { - var obj = JSON.parse(fs.readFileSync(file)) - // Compare .json file content with in memory database - var isDatabaseDifferent = !_.eq(obj, app.db.getState()) - if (isDatabaseDifferent) { + if (file === source) { + if (is.JSON(file)) { + console.log(file, fs.readFileSync(file)) + var obj = JSON.parse(fs.readFileSync(file)) + // 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...')) server && server.destroy() start() } - return } - - server && server.destroy() - start() }) // Watch routes