Sha256: 4735792fc549707fe9ac25285a8c3ecf8c7e42b43d34774afe9e42695a8be1f2

Contents?: true

Size: 1.37 KB

Versions: 4

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

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

          def delete(file)
            retries = 6

            until retries.zero?
              retries -= 1

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

              wait
            end

            notify(file)
          end

          private

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

          def deleted_file?(body)
            remote_checksum = body.dig("asset", "checksum")

            remote_checksum.nil?
          end

          def notify(file)
            @streams.broadcast(JSON.generate(deleted: [file]))
            @ctx.debug("[RemoteFileDeleter] Deleted #{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

4 entries across 4 versions & 1 rubygems

Version Path
shopify-cli-2.24.0 lib/shopify_cli/theme/dev_server/hot_reload/remote_file_deleter.rb
shopify-cli-2.23.0 lib/shopify_cli/theme/dev_server/hot_reload/remote_file_deleter.rb
shopify-cli-2.22.0 lib/shopify_cli/theme/dev_server/hot_reload/remote_file_deleter.rb
shopify-cli-2.21.0 lib/shopify_cli/theme/dev_server/hot_reload/remote_file_deleter.rb