Sha256: 5d3c5c86bef4be1ed1c0ed0a468b2ac47c388583c48b0c8b642baad11764d101

Contents?: true

Size: 1.79 KB

Versions: 4

Compression:

Stored size: 1.79 KB

Contents

require "fontcustom"
require "listen"

module Fontcustom
  class Watcher
    include Utility

    def initialize(options, is_test = false)
      @base = Fontcustom::Base.new options
      @options = @base.options
      @is_test = is_test

      templates = @options[:templates].dup.map { |template| File.basename(template) }
      packaged = %w|preview css scss scss-rails|
      templates.delete_if { |template| packaged.include?(template) }

      if templates.empty?
        @listener = Listen.to(@options[:input][:vectors])
      else
        @listener = Listen.to(@options[:input][:vectors], @options[:input][:templates])
      end

      @listener = @listener.relative_paths(true)
      @listener = @listener.filter(/(#{templates.join("|")}|.+\.svg)$/)
      @listener = @listener.change(&callback)
      @listener = @listener.polling_fallback_message(false) if @is_test
    end

    def watch
      compile unless @options[:skip_first]
      start
    rescue SignalException # Catches Ctrl + C
      stop
    end

    private

    def start
      if @is_test # Non-blocking listener
        @listener.start
      else
        @listener.start!
      end
    end

    def stop
      @listener.stop
      shell.say "\nFont Custom is signing off. Good night and good luck.", :yellow
    end

    def callback
      Proc.new do |modified, added, removed|
        begin
          say_message :changed, modified.join(", ") unless modified.empty?
          say_message :added, added.join(", ") unless added.empty?
          say_message :removed, removed.join(", ") unless removed.empty?
          changed = modified + added + removed
          compile unless changed.empty?
        rescue Fontcustom::Error => e
          say_message :error, e.message
        end
      end
    end

    def compile
      @base.compile
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
fontcustom-1.3.3 lib/fontcustom/watcher.rb
fontcustom-1.3.2 lib/fontcustom/watcher.rb
fontcustom-1.3.1 lib/fontcustom/watcher.rb
fontcustom-1.3.0 lib/fontcustom/watcher.rb