Sha256: bf48d11d1b2bbed014a0aa579d26a411c31125491e4e61f693106ee5a9a55046

Contents?: true

Size: 1.67 KB

Versions: 3

Compression:

Stored size: 1.67 KB

Contents

require "fontcustom"
require "listen"

module Fontcustom
  class Watcher
    def initialize(opts)
      @opts = opts
      @listener = Listen.to(@opts[:input])
                        .relative_paths(true)
                        .filter(/\.(eps|svg)$/)
                        .change(&callback)
      @opts[:blocking] = @opts[:blocking] == false ? false : true
      @listener = @listener.polling_fallback_message(false) unless @opts[:blocking]
    end

    def watch
      puts "Font Custom is watching your icons at #{@opts[:input]}. Press Ctrl + C to stop."
      compile unless @opts[:skip_first]
      @listener.start @opts[:blocking]

    rescue Fontcustom::Error => e
      show_error e

    # Catches Ctrl + C
    # TODO Does the listen gem have a better way of handling this?
    rescue SignalException
      stop
    end

    def stop
      # Adding a newline so message is not prepended with ^C on SIGTERM
      puts "\nFont Custom is signing off. Good night and good luck."
      @listener.stop
    end

    private

    def callback
      Proc.new do |modified, added, removed|
        begin
          puts "  >> Changed: " + modified.join(", ") unless modified.empty?
          puts "  >> Added: " + added.join(", ") unless added.empty?
          puts "  >> Removed: " + removed.join(", ") unless removed.empty?

          changed = modified + added + removed
          compile unless changed.empty?
        rescue Fontcustom::Error => e
          show_error e
        end
      end
    end

    def compile
      Fontcustom::Generator::Font.start [@opts]
      Fontcustom::Generator::Template.start [@opts]
    end

    def show_error(err)
      puts "ERROR: #{err.message}"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fontcustom-1.0.0 lib/fontcustom/watcher.rb
fontcustom-1.0.0.pre2 lib/fontcustom/watcher.rb
fontcustom-1.0.0.pre lib/fontcustom/watcher.rb