Skip to content

Instantly share code, notes, and snippets.

@atez
Last active March 28, 2017 07:55
Show Gist options
  • Save atez/0e1c190756b20cd174626a6538f6049b to your computer and use it in GitHub Desktop.
Save atez/0e1c190756b20cd174626a6538f6049b to your computer and use it in GitHub Desktop.
const fs = require('fs');
fs.watchFile('./test.txt', function (cur, pre) {
if (!pre.isFile() && cur.isFile()) {
console.log('==>file created');
}
else if (pre.isFile() && !cur.isFile()) {
console.log('==>file deleted');
}
else if (pre.isFile() && cur.isFile() && Date.parse(pre.mtime) !== Date.parse(cur.mtime)) {
console.log('==>file modified');
}
});
//开始监视时test.txt必须存在
const watcher = fs.watch('./test.txt', function (event, filename) {
// event为change或rename
console.log(event, filename);
if (event === 'rename') {
console.log('文件不存在,监视停止');
watcher.close();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment