Sha256: 38fa033553d8762b7d525ef32179ad794486e8cb69c488589532a7e00e50ce2b

Contents?: true

Size: 1.37 KB

Versions: 14

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

module ShopifyCLI
  module Theme
    class 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

14 entries across 14 versions & 1 rubygems

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