Class: Traceur::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/traceur/compiler.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Compiler

Returns a new instance of Compiler.



5
6
7
8
9
# File 'lib/traceur/compiler.rb', line 5

def initialize(opts = {})
  @runner = opts.fetch(:runner)
  @compile_script_path = opts.fetch(:compile_script_path)
  @default_compilation_options = opts.fetch(:default_compilation_options)
end

Instance Method Details

#compile(source, opts = {}) ⇒ Object



11
12
13
# File 'lib/traceur/compiler.rb', line 11

def compile(source, opts = {})
  compile_file(tempfile(source), opts)
end

#compile_file(infile, opts = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/traceur/compiler.rb', line 15

def compile_file(infile, opts = {})
  options = default_compilation_options.merge(opts)
  outfile = Tempfile.new('out.js')
  runner.run(
    arguments: [compile_script_path, infile.path, outfile.path, options.to_json],
    on_error: ->(r) { raise CompilationError.parse(r.stderr) }
  ).stdout
  outfile.read
end