Sha256: abcb0fd174ec9db86241b799256bba496dbf8103a83918af8317f7522e0d2594

Contents?: true

Size: 1.71 KB

Versions: 14

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true
require "shopify_cli/file_system_listener"
require "forwardable"

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

        def_delegators :@listener, :add_observer, :changed, :notify_observers

        def initialize(ctx, theme:, syncer:, ignore_filter: nil, poll: false)
          @ctx = ctx
          @theme = theme
          @syncer = syncer
          @ignore_filter = ignore_filter
          @listener = FileSystemListener.new(root: @theme.root, force_poll: poll,
            ignore_regex: @ignore_filter&.regexes)

          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

14 entries across 14 versions & 1 rubygems

Version Path
shopify-cli-2.36.0 lib/shopify_cli/theme/dev_server/watcher.rb
shopify-cli-2.35.0 lib/shopify_cli/theme/dev_server/watcher.rb
shopify-cli-2.34.0 lib/shopify_cli/theme/dev_server/watcher.rb
shopify-cli-2.33.1 lib/shopify_cli/theme/dev_server/watcher.rb
shopify-cli-2.33.0 lib/shopify_cli/theme/dev_server/watcher.rb
shopify-cli-2.32.1 lib/shopify_cli/theme/dev_server/watcher.rb
shopify-cli-2.32.0 lib/shopify_cli/theme/dev_server/watcher.rb
shopify-cli-2.31.0 lib/shopify_cli/theme/dev_server/watcher.rb
shopify-cli-2.30.0 lib/shopify_cli/theme/dev_server/watcher.rb
shopify-cli-2.29.0 lib/shopify_cli/theme/dev_server/watcher.rb
shopify-cli-2.28.0 lib/shopify_cli/theme/dev_server/watcher.rb
shopify-cli-2.27.0 lib/shopify_cli/theme/dev_server/watcher.rb
shopify-cli-2.26.0 lib/shopify_cli/theme/dev_server/watcher.rb
shopify-cli-2.25.0 lib/shopify_cli/theme/dev_server/watcher.rb