Sha256: ebe5ff827d6650e38c038b3534bead6ed24017982e00a5b3156e38770d92bdd9

Contents?: true

Size: 1008 Bytes

Versions: 4

Compression:

Stored size: 1008 Bytes

Contents

# frozen_string_literal: true

require 'open3'

# Public: Builds on top of Ruby I/O open3 providing a friendlier experience.
module ViteRuby::IO
  class << self
    # Internal: A modified version of capture3 that can continuosly print stdout.
    # NOTE: Streaming output provides a better UX when running bin/vite build.
    def capture(*cmd, with_output: $stdout.method(:puts), stdin_data: '', **opts)
      return Open3.capture3(*cmd, **opts) unless with_output

      Open3.popen3(*cmd, **opts) { |stdin, stdout, stderr, wait_threads|
        stdin << stdin_data
        stdin.close
        out = Thread.new { read_lines(stdout, &with_output) }
        err = Thread.new { stderr.read }
        [out.value, err.value.to_s, wait_threads.value.success?]
      }
    end

    # Internal: Reads and yield every line in the stream. Returns the full content.
    def read_lines(io)
      buffer = +''
      while line = io.gets
        buffer << line
        yield line
      end
      buffer
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
vite_ruby-3.2.8 lib/vite_ruby/io.rb
vite_ruby-3.2.7 lib/vite_ruby/io.rb
vite_ruby-3.2.6 lib/vite_ruby/io.rb
vite_ruby-3.2.5 lib/vite_ruby/io.rb