Sha256: c5fab387817f17e7f1996ffd3324111bbb2e014095829054b156e0b7e9cbe6dc

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

# frozen_string_literal: true

module Extension
  module Models
    class DevelopmentServer
      class DevelopmentServerError < StandardError; end

      include SmartProperties

      EXECUTABLE_DIRECTORY = File.join(ShopifyCLI::ROOT, "ext", "shopify-extensions")

      property :executable, converts: :to_s

      def executable
        super || begin
          case RbConfig::CONFIG.fetch("host_os")
          when /(linux)|(darwin)/
            File.join(EXECUTABLE_DIRECTORY, "shopify-extensions")
          else
            File.join(EXECUTABLE_DIRECTORY, "shopify-extensions.exe")
          end
        end
      end

      def create(server_config)
        CLI::Kit::System.capture3(executable, "create", "-", stdin_data: server_config.to_yaml)
      rescue StandardError => error
        raise error
      end

      def build(server_config)
        _, error, status = CLI::Kit::System.capture3(executable, "build", "-", stdin_data: server_config.to_yaml)
        return if status.success?
        raise DevelopmentServerError, error
      end

      def serve(context, server_config)
        CLI::Kit::System.popen3(executable, "serve", "-") do |input, out, err, status|
          context.puts("Sending configuration data …")
          input << server_config.to_yaml
          input.close

          forward_output_to_user(out, err, context)

          status.value
        end
      end

      def version
        raise NotImplementedError
      end

      private

      def forward_output_to_user(out, err, ctx)
        ctx.puts("Starting monitoring threads …")

        Thread.new do
          ctx.puts("Ready to process standard output")
          while (line = out.gets)
            ctx.puts(line)
          end
        end

        Thread.new do
          ctx.puts("Ready to process standard error")
          while (error = err.gets)
            ctx.puts(error)
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
shopify-cli-2.5.0 lib/project_types/extension/models/development_server.rb