Sha256: 57f0005de0ad46a7fbeb216b83f6fd582d8a7abf1c4bc9848b5f5af6cb27a4d1

Contents?: true

Size: 1.98 KB

Versions: 2

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true
require "shopify_cli"

module Extension
  class ExtensionProject < ShopifyCli::Project
    class << self
      def write_cli_file(context:, type:)
        ShopifyCli::Project.write(
          context,
          project_type: :extension,
          organization_id: nil,
          "#{ExtensionProjectKeys::EXTENSION_TYPE_KEY}": type
        )
      end

      def write_env_file(context:, title:, api_key: "", api_secret: "", registration_id: nil)
        ShopifyCli::Resources::EnvFile.new(
          api_key: api_key,
          secret: api_secret,
          extra: {
            ExtensionProjectKeys::TITLE_KEY => title,
            ExtensionProjectKeys::REGISTRATION_ID_KEY => registration_id,
          }.compact
        ).write(context)

        current.reload unless project_empty?
      end

      private

      def project_empty?
        directory(Dir.pwd).nil?
      end
    end

    def app
      Models::App.new(api_key: env["api_key"], secret: env["secret"])
    end

    def registered?
      property_present?("api_key") && property_present?("secret") && registration_id?
    end

    def title
      get_extra_field(ExtensionProjectKeys::TITLE_KEY)
    end

    def extension_type_identifier
      config[ExtensionProjectKeys::EXTENSION_TYPE_KEY]
    end

    def registration_id?
      extra_property_present?(ExtensionProjectKeys::REGISTRATION_ID_KEY) &&
        integer?(get_extra_field(ExtensionProjectKeys::REGISTRATION_ID_KEY)) &&
        registration_id > 0
    end

    def registration_id
      get_extra_field(ExtensionProjectKeys::REGISTRATION_ID_KEY).to_i
    end

    def reload
      @env = nil
    end

    private

    def get_extra_field(key)
      extra = env[:extra] || {}
      extra[key]
    end

    def extra_property_present?(key)
      env[:extra].key?(key) && !get_extra_field(key).nil?
    end

    def property_present?(key)
      !env[key].nil? && !env[key].strip.empty?
    end

    def integer?(value)
      value.to_i.to_s == value.to_s
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
shopify-cli-1.7.1 lib/project_types/extension/extension_project.rb
shopify-cli-1.7.0 lib/project_types/extension/extension_project.rb