class SmallVictories::Watcher

Attributes

compiler[RW]

Public Class Methods

new(attributes={}) click to toggle source
# File lib/smallvictories/watcher.rb, line 7
def initialize attributes={}
  self.compiler = attributes[:compiler]
end

Public Instance Methods

build_listener() click to toggle source
# File lib/smallvictories/watcher.rb, line 11
def build_listener
  Listen.to(
    compiler.config.full_source_path,
    force_polling: true,
    &(listen_handler)
  )
end
listen_handler() click to toggle source
# 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_forever() click to toggle source
# File lib/smallvictories/watcher.rb, line 63
def sleep_forever
  loop { sleep 1000 }
end
watch() click to toggle source
# 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