Created
July 23, 2013 02:33
-
-
Save subtleGradient/6059432 to your computer and use it in GitHub Desktop.
Usage: watch.node.js SomeFile.whatever "clear; time test.sh"
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 | |
| /*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