Sha256: c198ae9925c1c217b72325310e722b7fb7ee88822dc3e3bf7ad98376ce7be5c0

Contents?: true

Size: 1.66 KB

Versions: 6

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true
require "listen"
require "observer"

module ShopifyCLI
  module Theme
    module DevServer
      # Watches for file changes and publish events to the theme
      class Watcher
        include Observable

        def initialize(ctx, theme:, syncer:, ignore_filter: nil, poll: false)
          @ctx = ctx
          @theme = theme
          @syncer = syncer
          @ignore_filter = ignore_filter
          @listener = Listen.to(@theme.root, force_polling: poll) do |modified, added, removed|
            changed
            notify_observers(modified, added, removed)
          end

          add_observer(self, :upload_files_when_changed)
        end

        def start
          @listener.start
        end

        def stop
          @listener.stop
        end

        def upload_files_when_changed(modified, added, removed)
          modified_theme_files = filter_theme_files(modified + added)
          if modified_theme_files.any?
            @syncer.enqueue_updates(modified_theme_files)
          end

          removed_theme_files = filter_remote_files(removed)
          if removed_theme_files.any?
            @syncer.enqueue_deletes(removed_theme_files)
          end
        end

        def filter_theme_files(files)
          files
            .select { |file| @theme.theme_file?(file) }
            .map { |file| @theme[file] }
            .reject { |file| @syncer.ignore_file?(file) }
        end

        def filter_remote_files(files)
          files
            .select { |file| @syncer.remote_file?(file) }
            .map { |file| @theme[file] }
            .reject { |file| @syncer.ignore_file?(file) }
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
shopify-cli-2.16.1 lib/shopify_cli/theme/dev_server/watcher.rb
shopify-cli-2.16.0 lib/shopify_cli/theme/dev_server/watcher.rb
shopify-cli-2.15.6 lib/shopify_cli/theme/dev_server/watcher.rb
shopify-cli-2.15.5 lib/shopify_cli/theme/dev_server/watcher.rb
shopify-cli-2.15.4 lib/shopify_cli/theme/dev_server/watcher.rb
shopify-cli-2.15.3 lib/shopify_cli/theme/dev_server/watcher.rb