<commit_before>_ = require "underscore"
logger = require 'winston'
path = require "path"
glob = require "glob"
yaml = require 'js-yaml'
fs   = require 'fs'

TokenizerFactory = require './tokenizer/TokenizerFactory'

{Detector} = require './detector'
{Strategy} = require './strategy'
{Report} = require './report'

class jscpd

  LANGUAGES: Object.keys TokenizerFactory::LANGUAGES

  readConfig: (file) ->
    file = path.normalize file
    doc = {}
    try
      doc = yaml.safeLoad(fs.readFileSync(file, 'utf8'));
      logger.info "Used config from #{file}"
    catch e
      logger.warn "File #{file} not found in current directory"
    doc

  run: (options)->
    cwd = options.path
    config = @readConfig("#{cwd}/.cpd.yaml")

    options = _.extend
      'min-lines': 5
      'min-tokens': 70
      files: null
      exclude: null
      languages: jscpd::LANGUAGES
      output: null
      path: null
      verbose: off
      debug: off
    , options

    options = _.extend options, config

    if config.path
      options.path = path.normalize "#{cwd}/#{config.path}"
      cwd = options.path

    options.extensions = TokenizerFactory::getExtensionsByLanguages(options.languages)

    logger.profile 'Files search time:'
<commit_msg>Make error message more clear<commit_after>_ = require "underscore"
logger = require 'winston'
path = require "path"
glob = require "glob"
yaml = require 'js-yaml'
fs   = require 'fs'

TokenizerFactory = require './tokenizer/TokenizerFactory'

{Detector} = require './detector'
{Strategy} = require './strategy'
{Report} = require './report'

class jscpd

  LANGUAGES: Object.keys TokenizerFactory::LANGUAGES

  readConfig: (file) ->
    file = path.normalize file
    doc = {}
    try
      doc = yaml.safeLoad(fs.readFileSync(file, 'utf8'));
      logger.info "Used config from #{file}"
    catch e
      logger.warn "File #{file} not found in current directory, or it is broken"
    doc

  run: (options)->
    cwd = options.path
    config = @readConfig("#{cwd}/.cpd.yaml")

    options = _.extend
      'min-lines': 5
      'min-tokens': 70
      files: null
      exclude: null
      languages: jscpd::LANGUAGES
      output: null
      path: null
      verbose: off
      debug: off
    , options

    options = _.extend options, config

    if config.path
      options.path = path.normalize "#{cwd}/#{config.path}"
      cwd = options.path

    options.extensions = TokenizerFactory::getExtensionsByLanguages(options.languages)

    logger.profile 'Files search time:'

{'fraction_non_alphanumeric': 0.12326595323028142, 'fraction_numerical': 0.0031708283789139914, 'mean_word_length': 2.9131782945736435, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '460098', 'n_tokens_mistral': 806, 'n_tokens_neox': 771, 'n_words': 218}