Class: Condenser::NodeProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/condenser/processors/node_processor.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.setup(environment) ⇒ Object



7
8
# File 'lib/condenser/processors/node_processor.rb', line 7

def self.setup(environment)
end

Instance Method Details

#binary(cmd = 'node') ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/condenser/processors/node_processor.rb', line 26

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

#exec_runtime(script) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/condenser/processors/node_processor.rb', line 10

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

#exec_runtime_error(output) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/condenser/processors/node_processor.rb', line 41

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