Sha256: 3d254fbbbe7ed90128e6936009b107a304ed1ec43a3e94e7ef747e7a8ca8f2ee

Contents?: true

Size: 1.69 KB

Versions: 24

Compression:

Stored size: 1.69 KB

Contents

require 'listen'

module Locomotive::Wagon

  class Listen < Struct.new(:path, :cache)

    def self.start(path, cache)
      new(path, cache).tap { |instance| instance.apply_definitions }
    end

    def apply_definitions
      self.definitions.each do |definition|
        self.apply(definition)
      end
    end

    protected

    def definitions
      [
        ['config', /\.yml/, [:site, :content_types, :pages, :snippets, :content_entries, :translations]],
        ['app/views', %r{(pages|snippets)/(.+\.liquid).*}, [:pages, :snippets]],
        ['app/content_types', /\.yml/, [:content_types, :content_entries]],
        ['data', /\.yml/, :content_entries],
        ['public', %r{((stylesheets|javascripts)/(.+\.(css|js))).*}, []]
      ]
    end

    def apply(definition)
      reloader  = build_reloader([*definition.last])
      filter    = definition[1]
      _path     = File.expand_path(File.join(self.path, definition.first))

      listener = ::Listen.to(_path, only: filter, &reloader)

      # non blocking listener
      listener.start
    end

    def build_reloader(resources)
      Proc.new do |modified, added, removed|
        resources.each do |resource|
          Locomotive::Common::Logger.info "service=listen action=reload resource=#{resource} timestamp=#{Time.now}"

          clear_cache_for(resource, modified + added + removed)
        end
      end
    end

    def clear_cache_for(resource, files)
      keys = case resource
      when :site  then '_sites'
      when :content_entries
        files.map { |f| "site_1_content_type_#{File.basename(f, '.yml')}_content_entries" }
      else
        "site_1_#{resource}"
      end

      [*keys].each { |key| cache.delete(key) }
    end

  end

end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
locomotivecms_wagon-2.4.1 lib/locomotive/wagon/tools/listen.rb
locomotivecms_wagon-2.4.0 lib/locomotive/wagon/tools/listen.rb
locomotivecms_wagon-2.4.0.rc2 lib/locomotive/wagon/tools/listen.rb
locomotivecms_wagon-2.4.0.rc1 lib/locomotive/wagon/tools/listen.rb
locomotivecms_wagon-2.3.0 lib/locomotive/wagon/tools/listen.rb
locomotivecms_wagon-2.3.0.rc1 lib/locomotive/wagon/tools/listen.rb
locomotivecms_wagon-2.2.0 lib/locomotive/wagon/tools/listen.rb
locomotivecms_wagon-2.2.0.rc3 lib/locomotive/wagon/tools/listen.rb
locomotivecms_wagon-2.2.0.rc2 lib/locomotive/wagon/tools/listen.rb
locomotivecms_wagon-2.2.0.rc1 lib/locomotive/wagon/tools/listen.rb
locomotivecms_wagon-2.2.0.beta1 lib/locomotive/wagon/tools/listen.rb
locomotivecms_wagon-2.1.1 lib/locomotive/wagon/tools/listen.rb
locomotivecms_wagon-2.1.0 lib/locomotive/wagon/tools/listen.rb
locomotivecms_wagon-2.1.0.rc5 lib/locomotive/wagon/tools/listen.rb
locomotivecms_wagon-2.1.0.rc4 lib/locomotive/wagon/tools/listen.rb
locomotivecms_wagon-2.1.0.rc3 lib/locomotive/wagon/tools/listen.rb
locomotivecms_wagon-2.0.1 lib/locomotive/wagon/tools/listen.rb
locomotivecms_wagon-2.0.0 lib/locomotive/wagon/tools/listen.rb
locomotivecms_wagon-2.0.0.rc8 lib/locomotive/wagon/tools/listen.rb
locomotivecms_wagon-2.0.0.rc7 lib/locomotive/wagon/tools/listen.rb