Module: TypeScript::Ruby
- Defined in:
- lib/tsc-ruby.rb,
lib/tsc-ruby/version.rb,
lib/tsc-ruby/compile_result.rb
Defined Under Namespace
Classes: CompileResult, NodeNotFound
Constant Summary collapse
- VERSION =
'0.1.1'
Class Method Summary collapse
-
.compile(script, *tsc_options) ⇒ String
Compile TypeScript string to JavaScript string.
-
.compile_file(source_file, *tsc_options) ⇒ TypeScript::Ruby::CompileResult
Compile TypeScript script file to JavaScript script file.
- .tsc(*args) ⇒ Object
- .tsc_version ⇒ Object
Class Method Details
.compile(script, *tsc_options) ⇒ String
Compile TypeScript string to JavaScript string
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/tsc-ruby.rb', line 30 def compile(script, *) script = script.read if script.respond_to?(:read) js_file = Tempfile.new(%w(tsc-ruby .ts)) begin js_file.write(script) js_file.close result = compile_file(js_file.path, *) if result.success? result.js else raise result.stderr + result.stdout end ensure js_file.unlink end end |
.compile_file(source_file, *tsc_options) ⇒ TypeScript::Ruby::CompileResult
Compile TypeScript script file to JavaScript script file.
Compilation is a one to one process, not implicit concatenation of referenced dependencies is performed.
53 54 55 56 57 58 59 60 |
# File 'lib/tsc-ruby.rb', line 53 def compile_file(source_file, *) Dir.mktmpdir do |output_dir| stdout, stderr, exit_status = tsc(*, '--outDir', output_dir, source_file) output_file = "#{File.join(output_dir, File.basename(source_file, '.ts'))}.js" output_js = File.exist?(output_file) ? File.read(output_file) : nil CompileResult.new(output_js, exit_status, stdout, stderr) end end |
.tsc(*args) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/tsc-ruby.rb', line 19 def tsc(*args) cmd = ['node', TypeScript::Src.tsc_path.to_s, *args] Open3.capture3(*cmd) rescue Errno::ENOENT => e raise '`node` executable not found in PATH' if e. == 'No such file or directory - node' end |
.tsc_version ⇒ Object
15 16 17 |
# File 'lib/tsc-ruby.rb', line 15 def tsc_version TypeScript::Src.version end |