Module: ViteRuby::IO

Defined in:
lib/vite_ruby/io.rb

Overview

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

Class Method Summary collapse

Class Method Details

.capture(*cmd, with_output: $stdout.method(:puts), stdin_data: "", **opts) ⇒ Object

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



10
11
12
13
14
15
16
17
18
19
20
# 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.to_s, wait_threads.value]
  }
end

.read_lines(io) ⇒ Object

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



23
24
25
26
27
28
29
30
# File 'lib/vite_ruby/io.rb', line 23

def read_lines(io)
  buffer = +""
  while line = io.gets
    buffer << line
    yield line
  end
  buffer
end