Sha256: f5ca9a1c7d04bd20b36723fb2c83eabed6af7974c7790f5881e7c65a404a2588

Contents?: true

Size: 829 Bytes

Versions: 57

Compression:

Stored size: 829 Bytes

Contents

# frozen_string_literal: true
require "shopify_cli"
require "socket"

module Extension
  module Tasks
    class ChooseNextAvailablePort
      include ShopifyCLI::MethodObject

      property! :from
      property! :to, default: -> { from + 10 }
      property! :host, default: "127.0.0.1"

      def call
        available_port = port_range(from: from, to: to).find { |p| available?(host, p) }
        raise ArgumentError, "Ports between #{from} and #{to} are unavailable" if available_port.nil?
        available_port
      end

      private

      def port_range(from:, to:)
        (from..to)
      end

      def available?(host, port)
        Socket.tcp(host, port, connect_timeout: 1) do |socket|
          socket.close
          false
        end
      rescue Errno::ECONNREFUSED
        true
      end
    end
  end
end

Version data entries

57 entries across 57 versions & 1 rubygems

Version Path
shopify-cli-2.19.0 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.18.1 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.18.0 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.17.0 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.16.1 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.16.0 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.15.6 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.15.5 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.15.4 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.15.3 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.15.2 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.15.1 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.15.0 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.14.0 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.13.0 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.12.0 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.11.2 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.11.1 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.11.0 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.10.2 lib/project_types/extension/tasks/choose_next_available_port.rb