module ViteRuby::IO

Public: Builds on top of Ruby I/O open3 providing a friendlier experience.

Public Class Methods

capture(*cmd, with_output: $stdout.method(:puts), stdin_data: '', **opts) click to toggle source

Internal: A modified version of capture3 that can continuosly print stdout. NOTE: Streaming output provides a better UX when running bin/vite build.

# File lib/vite_ruby/io.rb, line 10
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, wait_threads.value]
  }
end
read_lines(io) { |line| ... } click to toggle source

Internal: Reads and yield every line in the stream. Returns the full content.

# File lib/vite_ruby/io.rb, line 23
def read_lines(io)
  buffer = +''
  while line = io.gets
    buffer << line
    yield line
  end
  buffer
end