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

.node_modules_pathObject



11
12
13
# File 'lib/condenser/processors/node_processor.rb', line 11

def self.node_modules_path
  File.expand_path('../node_modules', __FILE__)
end

.setup(environment) ⇒ Object



15
16
# File 'lib/condenser/processors/node_processor.rb', line 15

def self.setup(environment)
end

Instance Method Details

#binary(cmd = 'node') ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/condenser/processors/node_processor.rb', line 34

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



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/condenser/processors/node_processor.rb', line 18

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



58
59
60
61
62
63
64
65
# File 'lib/condenser/processors/node_processor.rb', line 58

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

#exec_syntax_error(output, source_file) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/condenser/processors/node_processor.rb', line 49

def exec_syntax_error(output, source_file)
  error = Condenser::SyntaxError.new(output)
  lines = output.split("\n")
  lineno = lines[0][/\((\d+):\d+\)$/, 1] if lines[0]
  lineno ||= 1
  error.set_backtrace(["#{source_file}:#{lineno}"] + caller)
  error
end

#node_modules_pathObject



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

def node_modules_path
  File.expand_path('../node_modules', __FILE__)
end