Sha256: 3a89a1a547d2e2ab3985b81d231b4697e906b856678a81bf56a90584419da20a

Contents?: true

Size: 1.06 KB

Versions: 10

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

module ShopifyCLI
  module Theme
    module DevServer
      class HotReload
        class SectionsIndex
          def initialize(theme)
            @theme = theme
          end

          def section_names_by_type
            index = {}

            files.each do |file|
              section_hash(file).each do |key, value|
                next unless key
                next unless value.is_a?(Hash)
                next unless (type = value&.dig("type"))

                index[type] = [] unless index[type]
                index[type] << key
              end
            end

            index
          end

          private

          def section_hash(file)
            content = JSON.parse(file.read)
            return [] unless content.is_a?(Hash)

            sections = content["sections"]
            return [] if sections.nil?

            sections
          rescue JSON::JSONError
            []
          end

          def files
            @theme.json_files.filter(&:template?)
          end
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
shopify-cli-2.24.0 lib/shopify_cli/theme/dev_server/hot_reload/sections_index.rb
shopify-cli-2.23.0 lib/shopify_cli/theme/dev_server/hot_reload/sections_index.rb
shopify-cli-2.22.0 lib/shopify_cli/theme/dev_server/hot_reload/sections_index.rb
shopify-cli-2.21.0 lib/shopify_cli/theme/dev_server/hot_reload/sections_index.rb
shopify-cli-2.20.1 lib/shopify_cli/theme/dev_server/hot_reload/sections_index.rb
shopify-cli-2.20.0 lib/shopify_cli/theme/dev_server/hot_reload/sections_index.rb
shopify-cli-2.19.0 lib/shopify_cli/theme/dev_server/hot_reload/sections_index.rb
shopify-cli-2.18.1 lib/shopify_cli/theme/dev_server/hot_reload/sections_index.rb
shopify-cli-2.18.0 lib/shopify_cli/theme/dev_server/hot_reload/sections_index.rb
shopify-cli-2.17.0 lib/shopify_cli/theme/dev_server/hot_reload/sections_index.rb