Skip to content

Instantly share code, notes, and snippets.

@lgriffin
Created January 19, 2016 15:47
Show Gist options
  • Save lgriffin/9ede2e9f7b9be1fb8936 to your computer and use it in GitHub Desktop.
Save lgriffin/9ede2e9f7b9be1fb8936 to your computer and use it in GitHub Desktop.
Directory watcher and Total Size reported
var chokidar = require('chokidar');
var dir = '.';
var du = require('du');
var watcher = chokidar.watch(dir, {ignored: /[\/\\]\./});
function getSize()
{
du(dir, function (err, size) {
console.log('The current size of ' + dir + ' is:', size, 'bytes');
});
}
watcher
.on('change', function(path) {
console.log('File', path, 'has been changed');
getSize();
})
.on('unlink', function(path) {
console.log('File', path, 'has been removed');
getSize();
})
.on('add', function(path) {
//console.log('File', path, 'has been added');
// Deliberately leaving this commented out to avoid wall of initial spam!!
})
.on('unlinkDir', function(path) {
console.log('Directory', path, 'has been removed');
getSize();
});
@lgriffin
Copy link
Author

watcher.on('all', function(event, path) {
  console.log(event, path);
});

Is also valid for a catch all, the gist above gives more fine grained control and can minimise noise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment