<commit_before>%x{
  var warnings = {};
  function handle_unsupported_feature(message) {
    switch (Opal.config.unsupported_features_severity) {
    case 'error':
      #{Kernel.raise NotImplementedError, `message`}
      break;
    case 'warning':
      warn(message)
      break;
    default: // ignore
      // noop
    }
  }
  function warn(string) {
    if (warnings[string]) {
      return;
    }
    warnings[string] = true;
    #{warn(`string`)};
  }
  function executeIOAction(action) {
    try {
      return action();
    } catch (error) {
      if (error.code === 'EACCES' ||
          error.code === 'EISDIR' ||
          error.code === 'EMFILE' ||
          error.code === 'ENOENT' ||
          error.code === 'EPERM') {
        throw Opal.IOError.$new(error.message)
      }
      throw error;
    }
  }
}

class File < IO
  include ::IO::Writable
  include ::IO::Readable

  @__fs__ = node_require :fs
  @__path__ = node_require :path
  `var __fs__ = #{@__fs__}`
  `var __path__ = #{@__path__}`

  if `__path__.sep !== #{Separator}`
    ALT_SEPARATOR = `__path__.sep`
  end

  def self.read path
    `return executeIOAction(function(){return __fs__.readFileSync(#{path}).toString()})`
  end

  def self.write path, data
    `executeIOAction(function(){return __fs__.writeFileSync(#{path}, #{data})})`
    data.size
  end

  def self.exist? path
    path = path.path if path.respond_to? :path
    `return executeIOAction(function(){return __fs__.existsSync(#{path})})`
  end

  def self.realpath(pathname, dir_string = nil, cache = nil, &block)
    pathname = join(dir_string, pathname) if dir_string
    if block_given?
      `
      __fs__.realpath(#{pathname}, #{cache}, function(error, realpath){
        if (error) Opal.IOError.$new(error.message)
        else #{block.call(`realpath`)}
      })
      `
    else
      `return executeIOAction(function(){return __fs__.realpathSync(#{pathname}, #{cache})})`
    end
  end

  def self.join(*paths)
    `__path__.join.apply(__path__, #{paths})`
  end

  def self.directory? path
    return false unless exist? path
    `return executeIOAction(function(){return !!__fs__.lstatSync(path).isDirectory()})`
  end

  def self.file? path
    return false unless exist? path
    `return executeIOAction(function(){return !!__fs__.lstatSync(path).isFile()})`
  end

  def self.readable? path
    return false unless exist? path
    %{
      try {
        __fs__.accessSync(path, __fs__.R_OK);
        return true;
      } catch (error) {
        return false;
      }
    }
  end

  def self.size path
    `return executeIOAction(function(){return __fs__.lstatSync(path).size})`
  end

  def self.open path, flags
    file = new(path, flags)
    if block_given?
      begin
        yield(file)
      ensure
        file.close
      end
    else
      file
    end
  end

  def self.stat path
    path = path.path if path.respond_to? :path
    File::Stat.new(path)<commit_msg>Nodejs File.open now accepts one arg only<commit_after>%x{
  var warnings = {};
  function handle_unsupported_feature(message) {
    switch (Opal.config.unsupported_features_severity) {
    case 'error':
      #{Kernel.raise NotImplementedError, `message`}
      break;
    case 'warning':
      warn(message)
      break;
    default: // ignore
      // noop
    }
  }
  function warn(string) {
    if (warnings[string]) {
      return;
    }
    warnings[string] = true;
    #{warn(`string`)};
  }
  function executeIOAction(action) {
    try {
      return action();
    } catch (error) {
      if (error.code === 'EACCES' ||
          error.code === 'EISDIR' ||
          error.code === 'EMFILE' ||
          error.code === 'ENOENT' ||
          error.code === 'EPERM') {
        throw Opal.IOError.$new(error.message)
      }
      throw error;
    }
  }
}

class File < IO
  include ::IO::Writable
  include ::IO::Readable

  @__fs__ = node_require :fs
  @__path__ = node_require :path
  `var __fs__ = #{@__fs__}`
  `var __path__ = #{@__path__}`

  if `__path__.sep !== #{Separator}`
    ALT_SEPARATOR = `__path__.sep`
  end

  def self.read path
    `return executeIOAction(function(){return __fs__.readFileSync(#{path}).toString()})`
  end

  def self.write path, data
    `executeIOAction(function(){return __fs__.writeFileSync(#{path}, #{data})})`
    data.size
  end

  def self.exist? path
    path = path.path if path.respond_to? :path
    `return executeIOAction(function(){return __fs__.existsSync(#{path})})`
  end

  def self.realpath(pathname, dir_string = nil, cache = nil, &block)
    pathname = join(dir_string, pathname) if dir_string
    if block_given?
      `
      __fs__.realpath(#{pathname}, #{cache}, function(error, realpath){
        if (error) Opal.IOError.$new(error.message)
        else #{block.call(`realpath`)}
      })
      `
    else
      `return executeIOAction(function(){return __fs__.realpathSync(#{pathname}, #{cache})})`
    end
  end

  def self.join(*paths)
    `__path__.join.apply(__path__, #{paths})`
  end

  def self.directory? path
    return false unless exist? path
    `return executeIOAction(function(){return !!__fs__.lstatSync(path).isDirectory()})`
  end

  def self.file? path
    return false unless exist? path
    `return executeIOAction(function(){return !!__fs__.lstatSync(path).isFile()})`
  end

  def self.readable? path
    return false unless exist? path
    %{
      try {
        __fs__.accessSync(path, __fs__.R_OK);
        return true;
      } catch (error) {
        return false;
      }
    }
  end

  def self.size path
    `return executeIOAction(function(){return __fs__.lstatSync(path).size})`
  end

  def self.open(path, mode = 'r')
    file = new(path, mode)
    if block_given?
      begin
        yield(file)
      ensure
        file.close
      end
    else
      file
    end
  end

  def self.stat path
    path = path.path if path.respond_to? :path
    File::Stat.new(path)
{'fraction_non_alphanumeric': 0.1368795527697781, 'fraction_numerical': 0.0, 'mean_word_length': 2.7108736643620364, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6921430', 'n_tokens_mistral': 1957, 'n_tokens_neox': 1937, 'n_words': 433}