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.



24
25
26
# File 'lib/condenser/processors/node_processor.rb', line 24

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

Instance Attribute Details

#npm_pathObject

Returns the value of attribute npm_path.



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

def npm_path
  @npm_path
end

Class Method Details

.call(environment, input) ⇒ Object



18
19
20
21
22
# File 'lib/condenser/processors/node_processor.rb', line 18

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

.setup(environment) ⇒ Object



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

def self.setup(environment)
end

Instance Method Details

#binary(cmd = 'node') ⇒ Object



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

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



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

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



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

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



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

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



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

def name
  self.class.name
end

#npm_install(*packages) ⇒ Object



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

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



96
97
98
# File 'lib/condenser/processors/node_processor.rb', line 96

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