<commit_before>'use strict'
var Observable = require('../observable')
var ISNODE = require('../util/is/node')
var merge = require('../util/merge')
var include = require('../util/include')
var resolveNameSpace = require('./util/resolveNameSpace')
var findPackage = ISNODE && require('./util/findPackage')
var parseArgv = ISNODE && require('./util/parseArgv')
var path = ISNODE && require('path')

const OBJ = 'object'

exports.define = {
  generateConstructor: function () {
    return function Config (options, event, parent, key) {
      var config = this
      var nameSpace = config._nameSpace && config._nameSpace.val

      var val = {}

      var packageDir = (options && options._packageDir)

      var pkg = config._findPackage && config._findPackage.val &&
        findPackage && findPackage(packageDir)
      if (pkg) {
        var pkgObj = resolveNameSpace(pkg.obj, nameSpace)
        merge(val, pkgObj)
      }

      if (ISNODE) {
        var topVal = {}
        // environment variables and inline arguments go in topVal first, to be
        // used to evaluate configFiles

        merge(topVal, val)

        findEnv(topVal)

        var mergeFiles = []

        if (val.mergeFiles) {
          include(mergeFiles, val.mergeFiles)
        }
        if (topVal.mergeFiles) {
          include(mergeFiles, topVal.mergeFiles)
        }

        var setArgv = parseArgv && config._argv && config._argv.val && parseArgv(val)
        if (setArgv) {
          merge(topVal, setArgv)
          if (topVal.mergeFiles) {
            include(mergeFiles, topVal.mergeFiles)
          }
        }

        if (pkg) {
          let configFiles = topVal.configFiles || val.configFiles
          mergeFromFiles(val, configFiles, pkg.dir, nameSpace)
          mergeFromFiles(val, mergeFiles, pkg.dir, nameSpace)
        }

        // now actually apply env and inline on val
        findEnv(val)
        if (setArgv) {
          merge(val, setArgv)
        }

        delete val.configFiles
        delete val.mergeFiles
      }

      if (options) {
        options = resolveNameSpace(options, nameSpace)
        merge(val, options)
      }

      Observable.call(config, val, false, parent, key)
      if (config.autoMerge) {
        config.merge(config.autoMerge)
      }
    }
  }
}

function findEnv (obj) {
  var env = obj._env
  if (env && env in process.env) {
    obj.val = process.env[env]
  }
  for (let key in obj) {
    var val = obj[key]
    if (typeof val === OBJ) {
      findEnv(val)
    }
  }
}

function mergeFromFiles (obj, files, pkgDir, nameSpace) {
  for (let key in files) {
    let configFile = files[key]
    if (!path.isAbsolute(configFile)) {
      configFile = path.join(pkgDir, configFile)
    }
    try {
      let configObj = require(configFile)
      configObj = resolveNameSpace(configObj, nameSpace)
      merge(obj, configObj)
    } catch (e) {
      // console.log('could not require configFile', configFile)
    }
  }
}<commit_msg>fix mergeFiles and configFiles to work starting from cwd<commit_after>'use strict'
var Observable = require('../observable')
var ISNODE = require('../util/is/node')
var merge = require('../util/merge')
var include = require('../util/include')
var resolveNameSpace = require('./util/resolveNameSpace')
var findPackage = ISNODE && require('./util/findPackage')
var parseArgv = ISNODE && require('./util/parseArgv')
var path = ISNODE && require('path')

const OBJ = 'object'

exports.define = {
  generateConstructor: function () {
    return function Config (options, event, parent, key) {
      var config = this
      var nameSpace = config._nameSpace && config._nameSpace.val

      var val = {}

      var packageDir = (options && options._packageDir)

      var pkg = config._findPackage && config._findPackage.val &&
        findPackage && findPackage(packageDir)
      if (pkg) {
        var pkgObj = resolveNameSpace(pkg.obj, nameSpace)
        merge(val, pkgObj)
      }

      if (ISNODE) {
        var topVal = {}
        // environment variables and inline arguments go in topVal first, to be
        // used to evaluate configFiles

        merge(topVal, val)

        findEnv(topVal)

        var mergeFiles = []

        if (val.mergeFiles) {
          include(mergeFiles, val.mergeFiles)
        }
        if (topVal.mergeFiles) {
          include(mergeFiles, topVal.mergeFiles)
        }

        var setArgv = parseArgv && config._argv && config._argv.val && parseArgv(val)
        if (setArgv) {
          merge(topVal, setArgv)
          if (topVal.mergeFiles) {
            include(mergeFiles, topVal.mergeFiles)
          }
        }

        if (pkg) {
          let configFiles = topVal.configFiles || val.configFiles
          mergeFromFiles(val, configFiles, pkg.dir, nameSpace)
          mergeFromFiles(val, mergeFiles, pkg.dir, nameSpace)
        }

        // now actually apply env and inline on val
        findEnv(val)
        if (setArgv) {
          merge(val, setArgv)
        }

        delete val.configFiles
        delete val.mergeFiles
      }

      if (options) {
        options = resolveNameSpace(options, nameSpace)
        merge(val, options)
      }

      Observable.call(config, val, false, parent, key)
      if (config.autoMerge) {
        config.merge(config.autoMerge)
      }
    }
  }
}

function findEnv (obj) {
  var env = obj._env
  if (env && env in process.env) {
    obj.val = process.env[env]
  }
  for (let key in obj) {
    var val = obj[key]
    if (typeof val === OBJ) {
      findEnv(val)
    }
  }
}

function mergeFromFiles (obj, files, pkgDir, nameSpace) {
  for (let key in files) {
    let configFile = files[key]
    if (!path.isAbsolute(configFile)) {
      configFile = path.join(process.cwd(), configFile)
    }
    try {
      let configObj = require(configFile)
      configObj = resolveNameSpace(configObj, nameSpace)
      merge(obj, configObj)
    } catch (e) {
      // console.log('could not require configFile', configFile)
    }
  }
}
{'fraction_non_alphanumeric': 0.11643835616438356, 'fraction_numerical': 0.0, 'mean_word_length': 2.2895604395604394, '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': '2234141', 'n_tokens_mistral': 1814, 'n_tokens_neox': 1774, 'n_words': 452}