Skip to content

Instantly share code, notes, and snippets.

@hokaccha
Created January 18, 2012 09:26
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hokaccha/1632134 to your computer and use it in GitHub Desktop.
Save hokaccha/1632134 to your computer and use it in GitHub Desktop.
File watch and exec command cli tool
#!/usr/bin/env node
/*
* File watch and exec command cli tool.
*
* Example
*
* $ watcher *.less -- lessc style.less style.css
*
*/
'use strict';
var fs = require('fs');
var exec = require('child_process').exec;
var argv = process.argv.slice(2);
var separator = argv.indexOf('--');
var targets = argv.slice(0, separator);
var command = argv.slice(separator + 1).join(' ');
if (!targets.length || !command.length) {
console.log('Usage: watcher file [file] -- command');
console.log('');
console.log('Example');
console.log('$ watcher *.less -- lessc foo.less foo.css');
process.exit();
}
targets.forEach(function(file) {
fs.watchFile(file, function(curr, prev) {
if (curr.mtime > prev.mtime) {
exec(command, function(err, stdout, stderr) {
if (err) {
console.error(err);
}
if (stdout) {
console.log(stdout);
}
if (stderr) {
console.error(stderr);
}
});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment