Sha256: 2101590eeb1a306cf72ccf7e4bcc2afd45cbc418b6e10c032d89df7b7ab36430

Contents?: true

Size: 1.56 KB

Versions: 8

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true
require_relative "theme"

require "socket"
require "securerandom"

module ShopifyCli
  module Theme
    class DevelopmentTheme < Theme
      def id
        ShopifyCli::DB.get(:development_theme_id)
      end

      def name
        ShopifyCli::DB.get(:development_theme_name) || generate_theme_name
      end

      def role
        "development"
      end

      def ensure_exists!
        if exists?
          @ctx.debug("Using temporary development theme: ##{id} #{name}")
        else
          create
          @ctx.debug("Created temporary development theme: #{@id}")
          ShopifyCli::DB.set(development_theme_id: @id)
        end
      end

      def exists?
        return false unless id

        ShopifyCli::AdminAPI.rest_request(
          @ctx,
          shop: shop,
          path: "themes/#{id}.json",
          api_version: "unstable",
        )
      rescue ShopifyCli::API::APIRequestNotFoundError
        false
      end

      def delete
        super if exists?
        ShopifyCli::DB.del(:development_theme_id) if ShopifyCli::DB.exists?(:development_theme_id)
        ShopifyCli::DB.del(:development_theme_name) if ShopifyCli::DB.exists?(:development_theme_name)
      end

      def self.delete(ctx)
        new(ctx).delete
      end

      private

      def generate_theme_name
        hostname = Socket.gethostname.split(".").shift
        hash = SecureRandom.hex(3)

        theme_name = "Development (#{hash}-#{hostname})"

        ShopifyCli::DB.set(development_theme_name: theme_name)

        theme_name
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
shopify-cli-2.3.0 lib/shopify-cli/theme/development_theme.rb
shopify-cli-2.2.2 lib/shopify-cli/theme/development_theme.rb
shopify-cli-2.2.1 lib/shopify-cli/theme/development_theme.rb
shopify-cli-2.2.0 lib/shopify-cli/theme/development_theme.rb
shopify-cli-2.1.0 lib/shopify-cli/theme/development_theme.rb
shopify-cli-2.0.2 lib/shopify-cli/theme/development_theme.rb
shopify-cli-2.0.1 lib/shopify-cli/theme/development_theme.rb
shopify-cli-2.0.0 lib/shopify-cli/theme/development_theme.rb