javascript - Detect file content changes from node.js - Stack Overflow

I have a simple file watcher build with chokidarrequire('chokidar').watch('.target.txt&

I have a simple file watcher build with chokidar

require('chokidar').watch('./target.txt', {}).on('all', function(event, path) {
  console.log(event, path);
}).on('ready', function() {
  console.log('ready');
});

It causes change event every time when I re-save file even without changes. Is there a way to make this fire events only if actual content has been changed?

I have a simple file watcher build with chokidar

require('chokidar').watch('./target.txt', {}).on('all', function(event, path) {
  console.log(event, path);
}).on('ready', function() {
  console.log('ready');
});

It causes change event every time when I re-save file even without changes. Is there a way to make this fire events only if actual content has been changed?

Share Improve this question asked Oct 22, 2015 at 10:50 just-borisjust-boris 9,7865 gold badges51 silver badges87 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

You can use the stats parameter delivered on add and change. This will only work for changes on the size of the file, which should be enough for the vast majority of cases.

   var watchSize = 0;

    require('chokidar').watch('./target.txt', {}).on('all', function(event, path, stats) {  

        if(stats && stats.size != watchSize) {
            watchSize = stats.size;
            console.log(event);
        }
    }).on('ready', function(path, stats) {
      console.log('ready');
    });

If the few remaining situations are indeed relevant for your case and you have no performance concerns, you can use something like this (following the suggestion in the ments):

var crypto   = require("crypto");
var fs       = require("fs");
var chokidar = require("chokidar");

watchFile("./target.txt");

//----------------------------------------------------
function watchFile(filePath){

    var watchHash;

    chokidar.watch(filePath, {}).on("all", function(event, path, stats) {

        if (event == "add" || event == "change"){

            getHash(filePath, function(hash){
                if (hash != watchHash){
                    watchHash = hash;
                    console.log(event);
                }
            });
        }
    });
}

//----------------------------------------------------
function getHash(filePath, callback){

    var stream = fs.ReadStream(filePath);   
    var md5sum = crypto.createHash("md5");

    stream.on("data", function(data) {
        md5sum.update(data);
    });

    stream.on("end", function() {
        callback(md5sum.digest("hex"));
    });
}

This seems a bit much, though.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745338008a4623197.html

相关推荐

  • javascript - Detect file content changes from node.js - Stack Overflow

    I have a simple file watcher build with chokidarrequire('chokidar').watch('.target.txt&

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信