Skip to content

Instantly share code, notes, and snippets.

@subtleGradient
Created July 23, 2013 02:33
Show Gist options
  • Select an option

  • Save subtleGradient/6059432 to your computer and use it in GitHub Desktop.

Select an option

Save subtleGradient/6059432 to your computer and use it in GitHub Desktop.
Usage: watch.node.js SomeFile.whatever "clear; time test.sh"
#!/usr/bin/env node
/*jshint asi:true*/
fs = require('fs')
child_process = require('child_process')
if (module.id == '.') watchForChangesAndThenExec(process.argv[2] || process.env.PWD, process.argv[3] || "echo")
module.exports = exports = watchForChangesAndThenExec
function watchForChangesAndThenExec($FILENAME, $SCRIPT){
console.warn("Watching '%s' for changes", $FILENAME)
console.warn("Executing '%s' after change", $SCRIPT)
return watch()
function watch(){
fs.watch($FILENAME).on('change', function(event, filename){
console.warn(event, filename)
this.close()
callback()
})
}
function callback(){
console.warn("Executing '%s' after change", $SCRIPT)
var child = child_process.exec($SCRIPT, function(code, out, err){
process.nextTick(watch)
})
child.stdout.pipe(process.stdout)
child.stderr.pipe(process.stderr)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment