Skip to content

Instantly share code, notes, and snippets.

@vancura
Created November 1, 2011 09:41
Show Gist options
  • Save vancura/1330225 to your computer and use it in GitHub Desktop.
Save vancura/1330225 to your computer and use it in GitHub Desktop.
Folder watcher (replace "less-compile.sh" with your own command)
#!/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>
# original by Brett Terpstra <http://brettterpstra.com/watch-for-file-changes-and-refresh-your-browser-automatically/>
# fork by Vaclav Vancura / SAY Media <http://saymedia.com>
trap("SIGINT") { exit }
filetypes = ['less']
watch_folder = 'content/media/less'
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?
hash = new_hash
diff_hash.each do |df|
puts "Detected change in #{df[0]}, refreshing"
%x{bash less-compile.sh}
end
end
sleep 0.2
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment