Created
January 18, 2012 09:26
-
-
Save hokaccha/1632134 to your computer and use it in GitHub Desktop.
File watch and exec command cli tool
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
#!/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