Sha256: 85a856ff2a0949f9e703076738a1a5c5027844de956041ca3f05f311446a59b0

Contents?: true

Size: 1.48 KB

Versions: 4

Compression:

Stored size: 1.48 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 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)
            ShopifyCLI::AdminAPI.rest_request(
              @ctx,
              shop: @theme.shop,
              path: "themes/#{@theme.id}/assets.json",
              method: "GET",
              api_version: "unstable",
              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

4 entries across 4 versions & 1 rubygems

Version Path
shopify-cli-2.13.0 lib/shopify_cli/theme/dev_server/hot_reload/remote_file_reloader.rb
shopify-cli-2.12.0 lib/shopify_cli/theme/dev_server/hot_reload/remote_file_reloader.rb
shopify-cli-2.11.2 lib/shopify_cli/theme/dev_server/hot_reload/remote_file_reloader.rb
shopify-cli-2.11.1 lib/shopify_cli/theme/dev_server/hot_reload/remote_file_reloader.rb