Sha256: 4393a6b3ebc62d12d28e1f0bc0282ce6a82e7606bc0cbe4f3aaba7bc2bbfdad1

Contents?: true

Size: 1.44 KB

Versions: 3

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

module ShopifyCLI
  module Theme
    module DevServer
      class HotReload
        class RemoteFileReloader
          def initialize(ctx, theme:, streams:)
            @ctx = ctx
            @theme = theme
            @streams = streams
          end

          def reload(file)
            retries = 6

            until retries.zero?
              retries -= 1

              _status, body = fetch_asset(file)
              retries = 0 if updated_file?(body, file)

              wait
            end

            notify(file)
          end

          private

          def api_client
            @api_client ||= ThemeAdminAPI.new(@ctx, @theme.shop)
          end

          def updated_file?(body, file)
            remote_checksum = body.dig("asset", "checksum")
            local_checksum = file.checksum

            remote_checksum == local_checksum
          end

          def notify(file)
            @streams.broadcast(JSON.generate(modified: [file]))
            @ctx.debug("[RemoteFileReloader] Modified #{file}")
          end

          def wait
            sleep(1)
          end

          def fetch_asset(file)
            api_client.get(
              path: "themes/#{@theme.id}/assets.json",
              query: URI.encode_www_form("asset[key]" => file.relative_path.to_s),
            )
          rescue ShopifyCLI::API::APIRequestNotFoundError
            [404, {}]
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
shopify-cli-2.15.1 lib/shopify_cli/theme/dev_server/hot_reload/remote_file_reloader.rb
shopify-cli-2.15.0 lib/shopify_cli/theme/dev_server/hot_reload/remote_file_reloader.rb
shopify-cli-2.14.0 lib/shopify_cli/theme/dev_server/hot_reload/remote_file_reloader.rb