@@ -13,35 +13,42 @@ def tsc_version
1313 TypeScript ::Src . version
1414 end
1515
16+
17+ def tsc ( *args )
18+ cmd = [ node , Src . tsc_path . to_s , *args ]
19+ Open3 . popen3 ( *cmd ) do |stdin , stdout , stderr , wait_thr |
20+ stdin . close
21+ [ wait_thr . value , stdout . read , stderr . read ]
22+ end
23+ end
24+
1625 # Compile TypeScript source file.
1726 # @param [String] source_file TypeScript source file
1827 # @return [TypeScript::Node::CompileResult] compile result
19- def compile_file ( source_file )
28+ def compile_file ( source_file , * tsc_options )
2029 Dir . mktmpdir do |output_dir |
2130 output_file = File . join ( output_dir , "out.js" )
22- cmd = [ node , Src . tsc_path . to_s , "--out" , output_file , source_file ]
23- Open3 . popen3 ( *cmd ) do |stdin , stdout , stderr , wait_thr |
24- exit_status = wait_thr . value
25- output_js = File . exists? ( output_file ) ? File . read ( output_file ) : nil
26- CompileResult . new (
27- output_js ,
28- exit_status ,
29- stdout . read ,
30- stderr . read
31- )
32- end
31+ exit_status , stdout , stderr = tsc ( *tsc_options , '--out' , output_file , source_file )
32+
33+ output_js = File . exists? ( output_file ) ? File . read ( output_file ) : nil
34+ CompileResult . new (
35+ output_js ,
36+ exit_status ,
37+ stdout ,
38+ stderr ,
39+ )
3340 end
3441 end
3542
3643 # Compile TypeScript to JavaScript.
3744 # @param [String] source TypeScript to be compiled
3845 # @return [String] Resulted JavaScript
39- def compile ( source )
46+ def compile ( source , * tsc_options )
4047 js_file = Tempfile . new ( [ "typescript-node" , ".ts" ] )
4148 begin
4249 js_file . write ( source )
4350 js_file . close
44- result = compile_file ( js_file . path )
51+ result = compile_file ( js_file . path , * tsc_options )
4552 if result . success?
4653 result . js
4754 else
@@ -77,7 +84,7 @@ def locate_executable(cmd)
7784 def check_node
7885 unless locate_executable ( node )
7986 raise "typescript-node requires node command, but it's not found. Please install it. " +
80- "Set TS_NODE environmental variable If you want to use node command in non-standard path."
87+ "Set TS_NODE environmental variable If you want to use node command in non-standard path."
8188 end
8289 end
8390 end
0 commit comments