Sha256: ad9b85c828ca5b143808b6b98aa39e97b5ba827e8aec6144818df845093a71e6

Contents?: true

Size: 919 Bytes

Versions: 2

Compression:

Stored size: 919 Bytes

Contents

require 'open3'

module Traceur
  module Node
    class Runner
      def initialize(opts = {})
        @binary = opts.fetch(:binary)
        @modules_path = opts.fetch(:modules_path)
        @env = opts.fetch(:env)
      end

      def run(opts = {})
        input = opts.fetch(:input) { "" }
        arguments = opts.fetch(:arguments) { [] }
        on_error = opts.fetch(:on_error) { ->(r) {} }

        Open3.popen3(env, binary, *arguments) do |stdin, stdout, stderr, wait_thr|
          stdin.print input
          stdin.close

          CommandResult.new(
            stdout: stdout.read,
            stderr: stderr.read,
            status: wait_thr.value
          ).on_error(&on_error)
        end
      end

      private

      def env
        @env.merge("NODE_PATH" => modules_path) do |_, old, new|
          "#{old}:#{new}"
        end
      end

      attr_reader :binary, :modules_path
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
traceur-rb-0.0.4 lib/traceur/node/runner.rb
traceur-rb-0.0.2 lib/traceur/node/runner.rb