Sha256: 08c73bbe8e780edeb65956b81169eff26a2054ec227ff9d777d2d86dec32b02b

Contents?: true

Size: 1.8 KB

Versions: 7

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

module Bridgetown
  module Utils
    module Aux
      def self.with_color(name, message)
        return message unless !name.nil? && Bridgetown::Utils::Ansi::COLORS[name.to_sym]

        Bridgetown::Utils::Ansi.send(name, message)
      end

      def self.running_pids
        @running_pids ||= []
      end

      def self.add_pid(pid)
        running_pids << pid
      end

      def self.run_process(name, color, cmd, env: {})
        @mutex ||= Thread::Mutex.new

        Thread.new do
          rd, wr = IO.pipe("BINARY")
          pid = Process.spawn({ "BRIDGETOWN_NO_BUNDLER_REQUIRE" => nil }.merge(env),
                              cmd, out: wr, err: wr, pgroup: true)
          @mutex.synchronize { add_pid(pid) }

          loop do
            line = rd.gets
            line.to_s.lines.map(&:chomp).each do |message|
              next if name == "Frontend" && %r{ELIFECYCLE.*?Command failed}.match?(message)

              output = +""
              output << with_color(color, "[#{name}] ") if color
              output << message
              @mutex.synchronize do
                $stdout.puts output
                $stdout.flush
              end
            end
          end
        end
      end

      def self.group(&block)
        Bridgetown::Deprecator.deprecation_message "Bridgetown::Aux.group method will be removed" \
                                                   "in a future version, use run_process"
        instance_exec(&block)
      end

      def self.kill_processes
        Bridgetown.logger.info "Stopping auxiliary processes..."
        running_pids.each do |pid|
          Process.kill("SIGTERM", -Process.getpgid(pid))
        rescue Errno::ESRCH, Errno::EPERM, Errno::ECHILD # rubocop:disable Lint/SuppressedException
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
bridgetown-core-1.3.4 lib/bridgetown-core/utils/aux.rb
bridgetown-core-1.3.3 lib/bridgetown-core/utils/aux.rb
bridgetown-core-1.3.2 lib/bridgetown-core/utils/aux.rb
bridgetown-core-1.3.1 lib/bridgetown-core/utils/aux.rb
bridgetown-core-1.3.0 lib/bridgetown-core/utils/aux.rb
bridgetown-core-1.3.0.beta3 lib/bridgetown-core/utils/aux.rb
bridgetown-core-1.3.0.beta2 lib/bridgetown-core/utils/aux.rb