Skip to content

Instantly share code, notes, and snippets.

@jeanlaurent
Created July 9, 2013 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeanlaurent/5956171 to your computer and use it in GitHub Desktop.
Save jeanlaurent/5956171 to your computer and use it in GitHub Desktop.
Run some script when file changes
#!/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