Skip to content

Commit bf45e62

Browse files
committed
Update watch
1 parent 0e6a291 commit bf45e62

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/cli/run.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,23 +156,27 @@ module.exports = function (argv) {
156156
if (is.URL(source)) throw new Error('Can\'t watch URL')
157157

158158
// Watch .js or .json file
159+
// Since lowdb uses atomic writing, directory is watched instead of file
159160
chokidar
160-
.watch(source)
161+
.watch(path.dirname(source))
161162
.on('change', function (file) {
162-
if (is.JSON(file)) {
163-
var obj = JSON.parse(fs.readFileSync(file))
164-
// Compare .json file content with in memory database
165-
var isDatabaseDifferent = !_.eq(obj, app.db.getState())
166-
if (isDatabaseDifferent) {
163+
if (file === source) {
164+
if (is.JSON(file)) {
165+
console.log(file, fs.readFileSync(file))
166+
var obj = JSON.parse(fs.readFileSync(file))
167+
// Compare .json file content with in memory database
168+
var isDatabaseDifferent = !_.eq(obj, app.db.getState())
169+
if (isDatabaseDifferent) {
170+
console.log(chalk.gray(' ' + file + ' has changed, reloading...'))
171+
server && server.destroy()
172+
start()
173+
}
174+
} else {
167175
console.log(chalk.gray(' ' + file + ' has changed, reloading...'))
168176
server && server.destroy()
169177
start()
170178
}
171-
return
172179
}
173-
174-
server && server.destroy()
175-
start()
176180
})
177181

178182
// Watch routes

0 commit comments

Comments
 (0)