<commit_before>{Directory, File} = require 'atom'
fs = require 'fs'
HS = -> require '../hs/hs.min.js'

module.exports = Util =
  isDirectory: (dir) ->
    switch
      when (dir instanceof Directory) or (dir instanceof File)
        return Util.isDirectory(dir.getPath())
      when typeof dir is 'string'
        return ((try fs.statSync(dir).isDirectory()) ? false)
      else
        return false

  getRootDirFallback: (file) ->
    [dir] = atom.project.getDirectories().filter (dir) ->
      dir.contains(file?.getPath?())
    unless dir?
      dir = atom.project.getDirectories()[0]
    if dir?.getPath?() is 'atom://config'
      dir = null
    unless Util.isDirectory(dir)
      dir = file?.getParent?() ? new Directory '.'
    dir

  getRootDir: (bufferOrFileOrString) ->
    dirHasCabalFile = (d) ->
      return false unless d?
      d.getEntriesSync().some (file) ->
        file.isFile() and file.getBaseName().endsWith '.cabal'
    dirHasSandboxFile = (d) ->
      return false unless d?
      d.getEntriesSync().some (file) ->
        file.isFile() and (file.getBaseName() is 'cabal.sandbox.config')
    findProjectRoot = (d, check) ->
      until d?.isRoot?() or not d? or check d
        d = d?.getParent?()
      d if check d
    file =
      switch
        when bufferOrFileOrString.file?
          bufferOrFileOrString.file
        when bufferOrFileOrString instanceof File
          bufferOrFileOrString
        when bufferOrFileOrString instanceof Directory
          bufferOrFileOrString
        when typeof bufferOrFileOrString is 'string'
          new File(bufferOrFileOrString)
    dir =
      if Util.isDirectory(file)
        file
      else
        file?.getParent?() ? Util.getRootDirFallback file
    dir = findProjectRoot(dir, dirHasCabalFile) ? findProjectRoot(dir, dirHasSandboxFile)
    unless Util.isDirectory(dir)
      dir = Util.getRootDirFallback file
    return dir

  parseDotCabal: (a...) -> HS().parseDotCabal(a...)
  getComponentFromFile: (a...) -> HS().getComponentFromFile(a...)
  parseDotCabalSync: (a...) -> HS().parseDotCabalSync(a...)
  getComponentFromFileSync: (a...) -> HS().getComponentFromFileSync(a...)
  unlit: (a...) -> HS().unlit(a...)
  unlitSync: (a...) -> HS().unlitSync(a...)<commit_msg>Fix bug with last commit<commit_after>{Directory, File} = require 'atom'
fs = require 'fs'
HS = -> require '../hs/hs.min.js'

module.exports = Util =
  isDirectory: (dir) ->
    switch
      when (dir instanceof Directory) or (dir instanceof File)
        return Util.isDirectory(dir.getPath())
      when typeof dir is 'string'
        return ((try fs.statSync(dir).isDirectory()) ? false)
      else
        return false

  getRootDirFallback: (file) ->
    [dir] = atom.project.getDirectories().filter (dir) ->
      dir.contains(file?.getPath?())
    unless dir?
      dir = atom.project.getDirectories()[0]
    if dir?.getPath?() is 'atom://config'
      dir = null
    unless Util.isDirectory(dir)
      dir = file?.getParent?() ? new Directory '.'
    dir

  getRootDir: (bufferOrFileOrString) ->
    dirHasCabalFile = (d) ->
      return false unless d?
      d.getEntriesSync().some (file) ->
        file.isFile() and file.getBaseName().endsWith '.cabal'
    dirHasSandboxFile = (d) ->
      return false unless d?
      d.getEntriesSync().some (file) ->
        file.isFile() and (file.getBaseName() is 'cabal.sandbox.config')
    findProjectRoot = (d, check) ->
      until d?.isRoot?() or not d? or check d
        d = d?.getParent?()
      d if check d
    file =
      switch
        when bufferOrFileOrString.file?
          bufferOrFileOrString.file
        when bufferOrFileOrString instanceof File
          bufferOrFileOrString
        when bufferOrFileOrString instanceof Directory
          bufferOrFileOrString
        when typeof bufferOrFileOrString is 'string'
          new File(bufferOrFileOrString)
    dir =
      if Util.isDirectory(file)
        new Directory(file.getPath())
      else
        file?.getParent?() ? Util.getRootDirFallback file
    dir = findProjectRoot(dir, dirHasCabalFile) ? findProjectRoot(dir, dirHasSandboxFile)
    unless Util.isDirectory(dir)
      dir = Util.getRootDirFallback file
    return dir

  parseDotCabal: (a...) -> HS().parseDotCabal(a...)
  getComponentFromFile: (a...) -> HS().getComponentFromFile(a...)
  parseDotCabalSync: (a...) -> HS().parseDotCabalSync(a...)
  getComponentFromFileSync: (a...) -> HS().getComponentFromFileSync(a...)
  unlit: (a...) -> HS().unlit(a...)
  unlitSync: (a...) -> HS().unlitSync(a...)
{'fraction_non_alphanumeric': 0.13854625550660793, 'fraction_numerical': 0.0004405286343612335, 'mean_word_length': 2.6415396952686447, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, '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': '5238244', 'n_tokens_mistral': 1441, 'n_tokens_neox': 1409, 'n_words': 332}