# frozen_string_literal: true

# 端口清理工具模块
module PortCleaner
  # 清理占用端口的进程
  def self.clean_port(port)
    unless Gem.win_platform?
      # Linux/macOS only for now - simple version
      begin
        pids = `lsof -ti :#{port} 2>/dev/null`.split("\n").select { |pid| pid =~ /^\d+$/ }
        pids.each do |pid| 
          begin
            Process.kill('KILL', pid.to_i) 
          rescue
            # 忽略杀死进程失败的情况
          end
        end
      rescue
        # 忽略执行lsof失败的情况
      end
    end
  end
end