Sha256: 97c87cd545fd31dce24c34e7e3ad88d9242f5392bb1eaddfb8a758eedc45bb9c

Contents?: true

Size: 1.09 KB

Versions: 3

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true

module Extension
  module Forms
    class Register < ShopifyCli::Form
      flag_arguments :api_key

      attr_reader :app

      def ask
        self.app = ask_app
      end

      private

      attr_writer :app

      def ask_app
        apps = load_apps

        if !api_key.nil?
          found_app = apps.find { |app| app.api_key == api_key }
          ctx.abort(ctx.message('register.invalid_api_key', api_key)) if found_app.nil?
          found_app
        else
          CLI::UI::Prompt.ask(ctx.message('register.ask_app')) do |handler|
            apps.each do |app|
              handler.option("#{app.title} by #{app.business_name}") { app }
            end
          end
        end
      end

      def load_apps
        ctx.puts(@ctx.message('register.loading_apps'))
        apps = Tasks::GetApps.call(context: ctx)

        apps.empty? ? abort_no_apps : apps
      end

      def abort_no_apps
        ctx.puts(@ctx.message('register.no_apps'))
        ctx.puts(@ctx.message('register.learn_about_apps'))
        raise ShopifyCli::AbortSilent
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
shopify-cli-0.9.2 lib/project_types/extension/forms/register.rb
shopify-cli-0.9.1 lib/project_types/extension/forms/register.rb
shopify-cli-0.9.0 lib/project_types/extension/forms/register.rb