Sha256: 07064e09f0460cf9c9a007cef108c283fce0b562988ea2f6a61e803448e7602e

Contents?: true

Size: 1.93 KB

Versions: 7

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

require 'open3'

# Public: Executes Vite commands, providing conveniences for debugging.
class ViteRuby::Runner
  def initialize(vite_ruby)
    @vite_ruby = vite_ruby
  end

  # Public: Executes Vite with the specified arguments.
  def run(argv, capture: false)
    Dir.chdir(config.root) {
      cmd = command_for(argv)
      capture ? capture3_with_output(*cmd, chdir: config.root) : Kernel.exec(*cmd)
    }
  rescue Errno::ENOENT => error
    raise ViteRuby::MissingExecutableError, error
  end

private

  extend Forwardable

  def_delegators :@vite_ruby, :config, :logger

  # Internal: Returns an Array with the command to run.
  def command_for(args)
    [config.to_env].tap do |cmd|
      args = args.clone
      cmd.append('node', '--inspect-brk') if args.delete('--inspect')
      cmd.append('node', '--trace-deprecation') if args.delete('--trace_deprecation')
      cmd.append(vite_executable)
      cmd.append(*args)
      cmd.append('--mode', config.mode) unless args.include?('--mode') || args.include?('-m')
    end
  end

  # Internal: Resolves to an executable for Vite.
  def vite_executable
    bin_path = config.vite_bin_path
    File.exist?(bin_path) ? bin_path : "#{ `npm bin`.chomp }/vite"
  end

  # Internal: A modified version of capture3 that continuosly prints stdout.
  # NOTE: This improves the experience of running bin/vite build.
  def capture3_with_output(*cmd, **opts)
    return Open3.capture3(*cmd, opts) if config.hide_build_console_output

    Open3.popen3(*cmd, opts) { |_stdin, stdout, stderr, wait_threads|
      out = Thread.new { read_lines(stdout) { |l| logger.info('vite') { l } } }
      err = Thread.new { stderr.read }
      [out.value, err.value, wait_threads.value]
    }
  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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
vite_ruby-1.2.6 lib/vite_ruby/runner.rb
vite_ruby-1.2.5 lib/vite_ruby/runner.rb
vite_ruby-1.2.4 lib/vite_ruby/runner.rb
vite_ruby-1.2.3 lib/vite_ruby/runner.rb
vite_ruby-1.2.2 lib/vite_ruby/runner.rb
vite_ruby-1.2.1 lib/vite_ruby/runner.rb
vite_ruby-1.2.0 lib/vite_ruby/runner.rb