Sha256: 8d06922d528ab98b856578a7e42aa9161bd19fd0d7e72c9fb1496ee8ba197d50

Contents?: true

Size: 1.44 KB

Versions: 14

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

module ShopifyCLI
  module Theme
    class 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),
            )
          rescue ShopifyCLI::API::APIRequestNotFoundError
            [404, {}]
          end
        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/hot_reload/remote_file_reloader.rb
shopify-cli-2.35.0 lib/shopify_cli/theme/dev_server/hot_reload/remote_file_reloader.rb
shopify-cli-2.34.0 lib/shopify_cli/theme/dev_server/hot_reload/remote_file_reloader.rb
shopify-cli-2.33.1 lib/shopify_cli/theme/dev_server/hot_reload/remote_file_reloader.rb
shopify-cli-2.33.0 lib/shopify_cli/theme/dev_server/hot_reload/remote_file_reloader.rb
shopify-cli-2.32.1 lib/shopify_cli/theme/dev_server/hot_reload/remote_file_reloader.rb
shopify-cli-2.32.0 lib/shopify_cli/theme/dev_server/hot_reload/remote_file_reloader.rb
shopify-cli-2.31.0 lib/shopify_cli/theme/dev_server/hot_reload/remote_file_reloader.rb
shopify-cli-2.30.0 lib/shopify_cli/theme/dev_server/hot_reload/remote_file_reloader.rb
shopify-cli-2.29.0 lib/shopify_cli/theme/dev_server/hot_reload/remote_file_reloader.rb
shopify-cli-2.28.0 lib/shopify_cli/theme/dev_server/hot_reload/remote_file_reloader.rb
shopify-cli-2.27.0 lib/shopify_cli/theme/dev_server/hot_reload/remote_file_reloader.rb
shopify-cli-2.26.0 lib/shopify_cli/theme/dev_server/hot_reload/remote_file_reloader.rb
shopify-cli-2.25.0 lib/shopify_cli/theme/dev_server/hot_reload/remote_file_reloader.rb