Skip to content

Instantly share code, notes, and snippets.

@stringfellow
Created February 10, 2012 13:32
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 stringfellow/1789652 to your computer and use it in GitHub Desktop.
Save stringfellow/1789652 to your computer and use it in GitHub Desktop.
Watch for file changes and refresh your browser automatically
#!/usr/bin/env ruby
# watch.rb by Brett Terpstra, 2011 <http://brettterpstra.com>
# with credit to Carlo Zottmann <https://github.com/carlo/haml-sass-file-watcher>
# edited by Dave Arter to refresh Chrome using code suggested in Brett's original blog post: <http://brettterpstra.com/watch-for-file-changes-and-refresh-your-browser-automatically/>
# edited more by Steve Pike to not do any of that, and instead recompile coffee.
trap("SIGINT") { exit }
if ARGV.length < 1
puts "Usage: #{$0} watch_folder"
puts "Example: #{$0} ."
exit
end
dev_extension = 'dev'
filetypes = ['coffee']
watch_folder = ARGV[0]
puts "Watching #{watch_folder} and subfolders for changes in project files..."
while true do
files = []
filetypes.each {|type|
files += Dir.glob( File.join( watch_folder, "**", "*.#{type}" ) )
}
new_hash = files.collect {|f| [ f, File.stat(f).mtime.to_i ] }
hash ||= new_hash
diff_hash = new_hash - hash
unless diff_hash.empty?
sleep 0.5
hash = new_hash
diff_hash.each do |df|
puts "Detected change in #{df[0]}, refreshing"
%x{coffee -c #{ARGV[0]}}
end
end
sleep 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment