Sha256: 8b659f0b11f72d795f25f360f7acc7b5c501fcbd6d7f7d59f861b02586cbe418

Contents?: true

Size: 1.32 KB

Versions: 6

Compression:

Stored size: 1.32 KB

Contents

require 'listen'

module SmallVictories
  class Watcher
    attr_accessor :compiler

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

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

    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

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

      pid = Process.fork { system('guard -i --guardfile .sv_guardfile') }
      Process.detach(pid)

      listener = build_listener
      listener.start

      trap("INT") do
        Process.kill "TERM", pid
        listener.stop
        puts "✋  Halting auto-regeneration."
        exit 0
      end

      sleep_forever
    rescue ThreadError
      # Ctrl-C
    end

    def sleep_forever
      loop { sleep 1000 }
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
smallvictories-0.0.11 lib/smallvictories/watcher.rb
smallvictories-0.0.10 lib/smallvictories/watcher.rb
smallvictories-0.0.9 lib/smallvictories/watcher.rb
smallvictories-0.0.8 lib/smallvictories/watcher.rb
smallvictories-0.0.7 lib/smallvictories/watcher.rb
smallvictories-0.0.6 lib/smallvictories/watcher.rb