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.36.0 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.35.0 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.34.0 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.33.1 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.33.0 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.32.1 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.32.0 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.31.0 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.30.0 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.29.0 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.28.0 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.27.0 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.26.0 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.25.0 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.24.0 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.23.0 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.22.0 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.21.0 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.20.1 lib/project_types/extension/tasks/choose_next_available_port.rb
shopify-cli-2.20.0 lib/project_types/extension/tasks/choose_next_available_port.rb