Created
July 9, 2013 09:58
-
-
Save jeanlaurent/5956171 to your computer and use it in GitHub Desktop.
Run some script when file changes
This file contains 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 coffee | |
fs = require('fs') | |
exec = require('child_process').exec | |
unless process.argv[3]? | |
console.log """ | |
missing parameters : | |
watch.coffee directory_to_watch script_to_exec_on_file_change | |
""" | |
process.exit -1 | |
target = '.' | |
target = process.argv[2] if process.argv[2]? | |
script = process.argv[3..].join(' ') | |
console.log "when any file in #{target} changes I'll execute #{script}" | |
running = false | |
fs.watch target, (event, filename) -> | |
return "" if running | |
running = true | |
console.log 'change detected' | |
exec script, (error, stdout, stderr) -> | |
console.log "stdout: #{stdout}" if stdout?.length | |
console.log "stderr: #{stderr}" if stderr?.length | |
console.log "exec error: #{error}" if error? | |
running = false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment