Class: Condenser::NodeProcessor

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(npm_dir = nil) ⇒ NodeProcessor

Returns a new instance of NodeProcessor.



22
23
24
# File 'lib/condenser/processors/node_processor.rb', line 22

def initialize(npm_dir = nil)
  self.npm_path = npm_dir
end

Instance Attribute Details

#npm_pathObject

Returns the value of attribute npm_path.



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

def npm_path
  @npm_path
end

Class Method Details

.call(environment, input) ⇒ Object



16
17
18
19
20
# File 'lib/condenser/processors/node_processor.rb', line 16

def self.call(environment, input)
  @instances ||= {}
  @instances[environment] ||= new(environment.npm_path)
  @instances[environment].call(environment, input)
end

.setup(environment) ⇒ Object



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

def self.setup(environment)
end

Instance Method Details

#binary(cmd = 'node') ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/condenser/processors/node_processor.rb', line 42

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



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

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



67
68
69
70
71
72
73
74
# File 'lib/condenser/processors/node_processor.rb', line 67

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



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

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.instance_variable_set(:@path, source_file)
  error
end

#nameObject



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

def name
  self.class.name
end

#npm_install(*packages) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/condenser/processors/node_processor.rb', line 76

def npm_install(*packages)
  return if packages.empty?
  packages.flatten!
  packages.select! do |package|
    !Dir.exist?(File.join(npm_module_path, package))
  end
  
  Dir.chdir(npm_path) do
    if !packages.empty?
      if File.exist?(File.join(npm_path, 'package.json'))
        system("npm", "install", "--silent", *packages)
      else
        system("npm", "install", "--silent", *packages)
      end
    end
  end
end

#npm_module_path(package = nil) ⇒ Object



94
95
96
# File 'lib/condenser/processors/node_processor.rb', line 94

def npm_module_path(package=nil)
  File.join(*[npm_path, 'node_modules', package].compact)
end