<commit_before>require 'tmpdir'
require 'tempfile'
require 'typescript-src'
require 'typescript-node/version'
require 'typescript-node/compile_result'
require 'open3'

module TypeScript
  module Node
    class << self

      def tsc_version
        TypeScript::Src.version
      end

      def tsc(*args)
        cmd = [node, Src.tsc_path.to_s, *args]
        Open3.capture3(*cmd)
      end

      # Compile TypeScript source file.
      # @param [String] source_file TypeScript source file
      # @return [TypeScript::Node::CompileResult] compile result
      def compile_file(source_file, *tsc_options)
        Dir.mktmpdir do |output_dir|
          output_file = File.join(output_dir, 'out.js')
          stdout, stderr, exit_status = tsc(*tsc_options, '--out', output_file, source_file)

          output_js = File.exists?(output_file) ? File.read(output_file) : nil
          CompileResult.new(
              output_js,
              exit_status,
              stdout,
              stderr,
          )
        end
      end

      # Compile TypeScript to JavaScript.
      # @param [String] source TypeScript to be compiled
      # @return [String] Resulted JavaScript
      def compile(source, *tsc_options)
        js_file = Tempfile.new(%w(typescript-node .ts))
        begin
          js_file.write(source)
          js_file.close
          result = compile_file(js_file.path, *tsc_options)
          if result.success?
            result.js
          else
            raise result.stderr + result.stdout
          end
        ensure
          js_file.unlink<commit_msg>Fix Ruby warning: File.exists? is a deprecated name, use File.exist? instead<commit_after>require 'tmpdir'
require 'tempfile'
require 'typescript-src'
require 'typescript-node/version'
require 'typescript-node/compile_result'
require 'open3'

module TypeScript
  module Node
    class << self

      def tsc_version
        TypeScript::Src.version
      end

      def tsc(*args)
        cmd = [node, Src.tsc_path.to_s, *args]
        Open3.capture3(*cmd)
      end

      # Compile TypeScript source file.
      # @param [String] source_file TypeScript source file
      # @return [TypeScript::Node::CompileResult] compile result
      def compile_file(source_file, *tsc_options)
        Dir.mktmpdir do |output_dir|
          output_file = File.join(output_dir, 'out.js')
          stdout, stderr, exit_status = tsc(*tsc_options, '--out', output_file, source_file)

          output_js = File.exist?(output_file) ? File.read(output_file) : nil
          CompileResult.new(
              output_js,
              exit_status,
              stdout,
              stderr,
          )
        end
      end

      # Compile TypeScript to JavaScript.
      # @param [String] source TypeScript to be compiled
      # @return [String] Resulted JavaScript
      def compile(source, *tsc_options)
        js_file = Tempfile.new(%w(typescript-node .ts))
        begin
          js_file.write(source)
          js_file.close
          result = compile_file(js_file.path, *tsc_options)
          if result.success?
            result.js
          else
            raise result.stderr + result.stdout
          end
        ensure
          js_file.unlink
{'fraction_non_alphanumeric': 0.0880893300248139, 'fraction_numerical': 0.0018610421836228288, 'mean_word_length': 2.1341107871720117, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4000852', 'n_tokens_mistral': 960, 'n_tokens_neox': 940, 'n_words': 225}