module TypeScript

Constants

VERSION

Public Class Methods

compile_file(filepath, options={}) click to toggle source

Compiles a TypeScript file to JavaScript.

@param [String] filepath the path to the TypeScript file @param [Hash] options the options for the execution @option options [String] :output the output path @option options [Boolean] :output_dir the directory to output files to @option options [Boolean] :source_map create the source map or not @option options [String] :module module type to compile for (commonjs or amd) @option options [String] :target the target to compile toward: ES3 (default) or ES5

# File lib/ruby-typescript.rb, line 62
def compile_file(filepath, options={})
  options = options.clone
  if options[:output]
    output_filename = options[:output]
  elsif options[:output_dir]
    filename = File.basename(filepath).gsub(/[.]ts$/, '.js')
    output_filename = File.join(options[:output_dir], filename)
  else
    output_filename = filepath.gsub(/[.]ts$/, '.js')
  end

  args = [filepath] + flatten_options(options)
  stdout, stderr, wait_thr = node_compile(*args)

  if wait_thr.nil?
    success = stdout.empty? and stderr.empty?
  else
    success = wait_thr.value == 0
  end

  if success
    result = {
      :js => output_filename,
      :stdout => stdout,
    }
    if options[:source_map]
      result[:source_map] = output_filename + '.map'
    end
    return result
  else
    raise TypeScript::Error, ( stderr.empty? ? stdout : stderr )
  end
end
flatten_options(options) click to toggle source
# File lib/ruby-typescript.rb, line 27
def flatten_options(options)
  args = []
  if options[:output]
    args << '--out' << options[:output]
  end

  if options[:output_dir]
    args << '--outDir' << options[:output_dir]
  end

  if options[:source_map]
    args << '--sourceMap'
  end
  
  if options[:module]
    args << '--module' << options[:module]
  end
  
  if options[:target]
    args << '--target' << options[:target]
  end

  return args
end
node_compile(*args) click to toggle source
# File lib/ruby-typescript.rb, line 15
def node_compile(*args)
  if typescript_path
    cmd = [typescript_path] + args
  else
    cmd = ['tsc'] + args
  end
  cmd = cmd.join(' ')
  stdin, stdout, stderr, wait_thr = Open3.popen3(cmd)
  stdin.close
  [stdout.read, stderr.read, wait_thr]
end
typescript_path() click to toggle source
# File lib/ruby-typescript.rb, line 11
def typescript_path
  ENV['TYPESCRIPT_SOURCE_PATH']
end