Class: Frontsau::Assets::Watcher

Inherits:
Object
  • Object
show all
Defined in:
lib/frontsau/assets/watcher.rb

Constant Summary collapse

EXT_MAP =
{
    ".haml" => ".html",
    ".less" => ".css",
    ".sass" => ".css",
    ".coffee" => ".js",
}

Instance Method Summary collapse

Constructor Details

#initialize(&callback) ⇒ Watcher

Returns a new instance of Watcher.



12
13
14
15
16
# File 'lib/frontsau/assets/watcher.rb', line 12

def initialize &callback
  @callback = callback
  @watches = Frontsau.config[:assets][:sources].map{|s| Dir[s] }.flatten.uniq
  filewatcher()
end

Instance Method Details

#filewatcherObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/frontsau/assets/watcher.rb', line 18

def filewatcher
 FileWatcher.new(@watches).watch do |file|
   next if File.directory? file
   @watches.each do |p|
     if p.start_with? p
       file = file.gsub "#{p}/", ""
     end
     file
   end
   ext = File.extname file
   base = File.basename file, ext
   dir = File.dirname file
   if EXT_MAP[ext.downcase].present?
     ext = EXT_MAP[ext.downcase]
   end
   @callback.call "#{dir}/#{base}#{ext}"
 end
end

#fseventObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/frontsau/assets/watcher.rb', line 37

def fsevent
  fsevent = FSEvent.new
  opts = {
      watch_root: true,
      file_events: true
  }
  fsevent.watch @watches do |path|
    puts path
  end
  fsevent.run
end

#inotifyObject



49
50
51
52
53
54
55
56
57
# File 'lib/frontsau/assets/watcher.rb', line 49

def inotify
  notifier = INotify::Notifier.new
  @watches.each do |path|
    notifier.watch path do
      puts "wee #{path}"
    end
  end
  notifier.run
end