Syncronize files using fs-extra and Chokidar node.js

319 Views Asked by At

I am new to node.js in general and trying to monitor a file directory using chokidar below code is working fine but now what I want to achieve is when the file is or directory has changed a function will run and copy the files into a folder.

I am thinking of using fs-extra to copy the files but every time I put a function inside on('change') i encounter an error. Any suggestions would be great!

var fs = require('fs-extra');
var chokidar = require('chokidar');

    var watcher = chokidar.watch('data/2018/05', {ignored: /^\./, persistent: true});

    watcher  
      .on('change', function(path) {console.log('File', path, 'has been changed');})

Here is my code with function.

function UpdateFile(){

 fs.copy('data/2018/05', 'data/2018/05/new', function(err){
 if (err) return console.error(err);
 console.log("updated success!")
  }); //copies directory, even if it has subdirectories or files

}


    watcher.on('change', function(path) {

    console.log("CHANGE DETECTED: " + path);
     updateFile();


   });
0

There are 0 best solutions below