Last active
August 29, 2015 13:57
-
-
Save athap/9678318 to your computer and use it in GitHub Desktop.
Watch File And List File Information By Spawning A Process
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var spawnProcess = require('child_process').spawn; | |
var filename = 'some/file' | |
fs.watch(filename, function() { | |
var list = spawnProcess('ls', ['-lh', filename]); | |
var info = ''; | |
// listener get invoked every time a chunk of data is available. Data is read in chunks | |
list.stdout.on('data', function(chunk) { | |
output += chunk.toString(); | |
}); | |
// When child process exit, it emits the close event | |
list.on('close', function() { | |
var parts = output.split(/\s+/); | |
console.dir([parts[0], parts[4], parts[8]]); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment