diff --git a/utils/common.js b/utils/common.js index 201abbe..2d725f7 100644 --- a/utils/common.js +++ b/utils/common.js @@ -1,10 +1,29 @@ const fs = require('fs'); const os = require('os'); const net = require('net'); +const path = require('path'); + +// recursive function that checks if a file is still changing +const awaitWriteFinish = (path, prev, cb) => { + fs.stat(path, { bigint: true }, (err, stat) => { + if (err) { + throw err; + } + if (stat.mtimeNs === prev.mtimeNs) { + cb(); + } else { + setTimeout(awaitWriteFinish, 50, path, stat, cb); + } + }); +}; const fileWatch = process.platform !== 'linux' - ? (x, cb) => fs.watch(x, { recursive: true }, cb) + ? (x, cb) => + fs.watch(x, { recursive: true }, (_, filename) => { + const fileChanged = path.join(x, filename); + awaitWriteFinish(fileChanged, {}, cb); + }) : (x, cb) => { if (fs.statSync(x).isDirectory()) { fs.watch(x, cb);