Class: SmallVictories::Watcher

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Watcher

Returns a new instance of Watcher.



7
8
9
# File 'lib/smallvictories/watcher.rb', line 7

def initialize attributes={}
  self.compiler = attributes[:compiler]
end

Instance Attribute Details

#compilerObject

Returns the value of attribute compiler.



5
6
7
# File 'lib/smallvictories/watcher.rb', line 5

def compiler
  @compiler
end

Instance Method Details

#build_listenerObject



11
12
13
14
15
16
17
# File 'lib/smallvictories/watcher.rb', line 11

def build_listener
  Listen.to(
    compiler.config.full_source_path,
    force_polling: true,
    &(listen_handler)
  )
end

#listen_handlerObject



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

def listen_handler
  proc do |modified, added, removed|
    paths = modified + added + removed
    extensions = paths.map{ |path| File.extname(path) }
    extensions.uniq.each do |ext|
      case ext
      when '.scss', '.sass', '.css'
        compiler.compile_css
      when '.coffee', '.js'
        compiler.compile_js
      when '.liquid', '.html'
        compiler.compile_html
      else
      end
    end
  end
end

#sleep_foreverObject



63
64
65
# File 'lib/smallvictories/watcher.rb', line 63

def sleep_forever
  loop { sleep 1000 }
end

#watchObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/smallvictories/watcher.rb', line 37

def watch
  SmallVictories.logger.debug "👋"
  SmallVictories.logger.debug "👀"

  if File.exists?('Guardfile')
    pid = Process.fork { system('guard -i') }
    Process.detach(pid)
  end

  listener = build_listener
  listener.start

  trap("INT") do
    if File.exists?('Guardfile')
      Process.kill "TERM", pid
    end
    listener.stop
    puts "✋  Halting auto-regeneration."
    exit 0
  end

  sleep_forever
rescue ThreadError
  # Ctrl-C
end