<commit_before>'use babel'

import FS from 'fs'
import Path from 'path'
import Minimatch from 'minimatch'
import Glob from 'glob'

const DEFAULT_CONFIG = {
  plugins: [],
  rules: []
}

/** Finding Helpers */

export function findFile(root, fileName) {
  try {
    FS.accessSync(root, FS.R_OK)
  } catch (_) {
    throw new Error(`Can not read directory ${root}`)
  }
  const chunks = root.split(Path.sep)
  while (chunks.length) {
    const filePath = Path.join(chunks.join(Path.sep), fileName)
    if (filePath === fileName) {
      break
    }
    try {
      FS.accessSync(filePath, FS.R_OK)
      return filePath
    } catch (_) {}
    chunks.pop()
  }
  return null
}

export function findRoot(path, root) {
  if (root !== null) {
    return root
  } else if (path.indexOf('*') !== -1) {
    throw new Error('Options.root must be specified with glob')
  }
  let stat
  try {
    stat = FS.statSync(path)
  } catch (_) {
    throw new Error(`Error reading ${path}`)
  }
  if (stat.isDirectory()) {
    return path
  } else if (stat.isFile()) {
    const configFile = findFile(path, '.ucompilerrc')
    if (configFile === null) {
      return Path.dirname(path)
    } else {
      return Path.dirname(configFile)
    }
  } else throw new Error(`${path} is neither a file nor a directory`)
}

export function scanFiles(path, {root, ignored}) {
  if (path.indexOf('*') === -1) {
    // Non-Glob
    const absPath = Path.isAbsolute(path) ? path : Path.join(path, root)
    const stat = FS.statSync(absPath)
    if (stat.isFile()) {
      return [absPath]
    } else if (stat.isDirectory()) {
      let files = []
      FS.readdirSync(absPath).forEach(function(file) {
        const absFilePath = Path.join(absPath, file)
        const relativeFilePath = Path.relative(root, absFilePath)
        if (ignored.some(function(ignored) {
            return Minimatch(relativeFilePath, ignored)
          })) {
          return
        }
        let stat
        try {
          stat = FS.lstatSync(absFilePath)
        } catch (_) {
          throw new Error(`Error reading ${absFilePath}`)
        }
        if (!stat.isSymbolicLink()) {
          if (stat.isFile()) {
            files.push(relativeFilePath)
          } else if (stat.isDirectory()) {
            files = files.concat(scanFiles(absFilePath, {root, ignored}))
          }
        }
      })
      return files
    } else return []
  } else {
    // Glob
    return Glob.sync(path, {cwd: root, ignore: ignored})
  }
}

/** Config Helper */

export function getConfig(root) {
  const configFile = findFile(root, '.ucompilerrc')
  if (configFile !== null) {
    let config
    try {
      config = JSON.parse(FS.readFileSync(configFile, {encoding: 'utf8'}))
    } catch (e) {
      throw new Error(`Malformed configuration file located at ${configFile}`)
    }
    return Object.assign({}, DEFAULT_CONFIG, config)
  } else return DEFAULT_CONFIG
}

/** Saving Helper */

export function saveFile({contents, file, config}) {
  //console.log(contents)
}<commit_msg>:bug: Fix a minor bug<commit_after>'use babel'

import FS from 'fs'
import Path from 'path'
import Minimatch from 'minimatch'
import Glob from 'glob'

const DEFAULT_CONFIG = {
  plugins: [],
  rules: []
}

/** Finding Helpers */

export function findFile(root, fileName) {
  try {
    FS.accessSync(root, FS.R_OK)
  } catch (_) {
    throw new Error(`Can not read directory ${root}`)
  }
  const chunks = root.split(Path.sep)
  while (chunks.length) {
    const filePath = Path.join(chunks.join(Path.sep), fileName)
    if (filePath === fileName) {
      break
    }
    try {
      FS.accessSync(filePath, FS.R_OK)
      return filePath
    } catch (_) {}
    chunks.pop()
  }
  return null
}

export function findRoot(path, root) {
  if (root !== null) {
    return root
  } else if (path.indexOf('*') !== -1) {
    throw new Error('Options.root must be specified with glob')
  }
  let stat
  try {
    stat = FS.statSync(path)
  } catch (_) {
    throw new Error(`Error reading ${path}`)
  }
  if (stat.isDirectory()) {
    return path
  } else if (stat.isFile()) {
    const fileDir = Path.dirname(path)
    const configFile = findFile(fileDir, '.ucompilerrc')
    if (configFile === null) {
      return fileDir
    } else {
      return Path.dirname(configFile)
    }
  } else throw new Error(`${path} is neither a file nor a directory`)
}

export function scanFiles(path, {root, ignored}) {
  if (path.indexOf('*') === -1) {
    // Non-Glob
    const absPath = Path.isAbsolute(path) ? path : Path.join(path, root)
    const stat = FS.statSync(absPath)
    if (stat.isFile()) {
      return [absPath]
    } else if (stat.isDirectory()) {
      let files = []
      FS.readdirSync(absPath).forEach(function(file) {
        const absFilePath = Path.join(absPath, file)
        const relativeFilePath = Path.relative(root, absFilePath)
        if (ignored.some(function(ignored) {
            return Minimatch(relativeFilePath, ignored)
          })) {
          return
        }
        let stat
        try {
          stat = FS.lstatSync(absFilePath)
        } catch (_) {
          throw new Error(`Error reading ${absFilePath}`)
        }
        if (!stat.isSymbolicLink()) {
          if (stat.isFile()) {
            files.push(relativeFilePath)
          } else if (stat.isDirectory()) {
            files = files.concat(scanFiles(absFilePath, {root, ignored}))
          }
        }
      })
      return files
    } else return []
  } else {
    // Glob
    return Glob.sync(path, {cwd: root, ignore: ignored})
  }
}

/** Config Helper */

export function getConfig(root) {
  const configFile = findFile(root, '.ucompilerrc')
  if (configFile !== null) {
    let config
    try {
      config = JSON.parse(FS.readFileSync(configFile, {encoding: 'utf8'}))
    } catch (e) {
      throw new Error(`Malformed configuration file located at ${configFile}`)
    }
    return Object.assign({}, DEFAULT_CONFIG, config)
  } else return DEFAULT_CONFIG
}

/** Saving Helper */

export function saveFile({contents, file, config}) {
  console.log(contents)
}
{'fraction_non_alphanumeric': 0.13665515540207201, 'fraction_numerical': 0.000986679822397632, 'mean_word_length': 2.5671554252199416, 'pattern_counts': {'":': 0, '<': 3, '<?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': '602319', 'n_tokens_mistral': 1946, 'n_tokens_neox': 1932, 'n_words': 495}