class WebpackNative::Runner

Constants

Error

Attributes

cmd[R]
exit_status[R]
stderr[R]
stdout[R]

Public Class Methods

new(cmd) click to toggle source

@param cmd [String,Array<String>] command to execute

# File lib/webpack_native/runner.rb, line 29
def initialize(cmd)
  @cmd = cmd.is_a?(Array) ? cmd.join(' ') : cmd
  @stdout = +''
  @stderr = +''
  @exit_status = nil
end
run(*cmd) click to toggle source

Run a command, return runner instance @param cmd [String,Array<String>] command to execute

# File lib/webpack_native/runner.rb, line 8
def self.run(*cmd)
  Runner.new(*cmd).run
end
run!(*cmd) click to toggle source

Run a command, raise Runner::Error if it fails @param cmd [String,Array<String>] command to execute @raise [Runner::Error]

# File lib/webpack_native/runner.rb, line 15
def self.run!(*cmd)
  Runner.new(*cmd).run!
end
run?(*cmd) click to toggle source

Run a command, return true if it succeeds, false if not @param cmd [String,Array<String>] command to execute @return [Boolean]

# File lib/webpack_native/runner.rb, line 22
def self.run?(*cmd)
  Runner.new(*cmd).run?
end

Public Instance Methods

run() click to toggle source

Run the command, return self @return [Runner]

# File lib/webpack_native/runner.rb, line 43
def run
  Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
    until [stdout, stderr].all?(&:eof?)
      readable = IO.select([stdout, stderr])
      next unless readable&.first

      readable.first.each do |stream|
        data = +''
        # rubocop:disable Lint/HandleExceptions
        begin
          stream.read_nonblock(1024, data)
        rescue EOFError
          # ignore, it's expected for read_nonblock to raise EOFError
          # when all is read
        end

        if stream == stdout
          @stdout << data
          $stdout.write(data)
        else
          @stderr << data
          $stderr.write(data)
        end
      end
    end
    @exit_status = wait_thr.value.exitstatus
  end

  self
end
run!() click to toggle source

Run the command and return stdout, raise if fails @return stdout [String] @raise [Runner::Error]

# File lib/webpack_native/runner.rb, line 77
def run!
  return run.stdout if run.success?

  raise(Error, "command failed, exit: %d - stdout: %s / stderr: %s" % [exit_status, stdout, stderr])
end
run?() click to toggle source

Run the command and return true if success, false if failure @return success [Boolean]

# File lib/webpack_native/runner.rb, line 85
def run?
  run.success?
end
success?() click to toggle source

@return [Boolean] success or failure?

# File lib/webpack_native/runner.rb, line 37
def success?
  exit_status.zero?
end