Sha256: 22b8cfba702b489a31fcdd463db9a6979ecfb395df6c13e0344fd91ba6f73086

Contents?: true

Size: 1.18 KB

Versions: 3

Compression:

Stored size: 1.18 KB

Contents

require 'tempfile'
require 'open3'

class Condenser
  class NodeProcessor
    
    def exec_runtime(script)
      Tempfile.open(['script', 'js']) do |scriptfile|
        scriptfile.write(script)
        scriptfile.flush

        stdout, stderr, status = Open3.capture3(binary, scriptfile.path)
        
        if status.success?
          puts stderr if !stderr.strip.empty?
          JSON.parse(stdout)
        else
          raise exec_runtime_error(stdout + stderr)
        end
      end
    end
    
    def binary(cmd='node')
      if File.executable? cmd
        cmd
      else
        path = ENV['PATH'].split(File::PATH_SEPARATOR).find { |p|
          full_path = File.join(p, cmd)
          File.executable?(full_path) && File.file?(full_path)
        }
        if path.nil?
          raise Condenser::CommandNotFoundError, "Could not find executable #{cmd}"
        end
        File.expand_path(cmd, path)
      end
    end
    
    def exec_runtime_error(output)
      error = RuntimeError.new(output)
      lines = output.split("\n")
      lineno = lines[0][/:(\d+)$/, 1] if lines[0]
      lineno ||= 1
      error.set_backtrace(["(node):#{lineno}"] + caller)
      error
    end
    
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
condenser-0.0.8 lib/condenser/processors/node_processor.rb
condenser-0.0.7 lib/condenser/processors/node_processor.rb
condenser-0.0.5 lib/condenser/processors/node_processor.rb