<commit_before>/* global __coverage__ */
var fs = require('fs')
var glob = require('glob')
var micromatch = require('micromatch')
var mkdirp = require('mkdirp')
var appendTransform = require('append-transform')
var path = require('path')
var rimraf = require('rimraf')
var onExit = require('signal-exit')
var stripBom = require('strip-bom')
var resolveFrom = require('resolve-from')
var md5 = require('md5-hex')
var arrify = require('arrify')
var convertSourceMap = require('convert-source-map')
var endsWith = require('ends-with')

/* istanbul ignore next */
if (/index\.covered\.js$/.test(__filename)) {
  require('./lib/self-coverage-helper')
}

function NYC (opts) {
  opts = opts || {}

  this._istanbul = opts.istanbul
  this.subprocessBin = opts.subprocessBin || path.resolve(__dirname, './bin/nyc.js')
  this._tempDirectory = opts.tempDirectory || './.nyc_output'
  this._cacheDirectory = opts.cacheDirectory || './node_modules/.cache/nyc'
  this.cwd = opts.cwd || process.env.NYC_CWD || process.cwd()
  this.reporter = arrify(opts.reporter || 'text')

  // you can specify config in the nyc stanza of package.json.
  var config = require(path.resolve(this.cwd, './package.json')).config || {}
  config = config.nyc || {}

  // load exclude stanza from config.
  this.include = arrify(config.include || ['**'])
  this.include = this._prepGlobPatterns(this.include)

  this.exclude = ['**/node_modules/**'].concat(arrify(config.exclude || ['test/**', 'test{,-*}.js']))
  this.exclude = this._prepGlobPatterns(this.exclude)

  // require extensions can be provided as config in package.json.
  this.require = arrify(config.require || opts.require)

  this._createOutputDirectory()
}

NYC.prototype._loadAdditionalModules = function () {
  var _this = this
  this.require.forEach(function (r) {
    // first attempt to require the module relative to
    // the directory being instrumented.
    var p = resolveFrom(_this.cwd, r)
    if (p) {
      require(p)
      return
    }
    // now try other locations, .e.g, the nyc node_modules folder.
    require(r)
  })
}

NYC.prototype.instrumenter = function () {
  return this._instrumenter || (this._instrumenter = this._createInstrumenter())
}

NYC.prototype._createInstrumenter = function () {
  var configFile = path.resolve(this.cwd, './.istanbul.yml')

  if (!fs.existsSync(configFile)) configFile = undefined

  var instrumenterConfig = this.istanbul().config.loadFile(configFile).instrumentation.config

  return new (this.istanbul()).Instrumenter({
    coverageVariable: '__coverage__',
    embedSource: instrumenterConfig['embed-source'],
    noCompact: !instrumenterConfig.compact,
    preserveComments: instrumenterConfig['preserve-comments']
  })
}

NYC.prototype._prepGlobPatterns = function (patterns) {
  if (!patterns) return patterns

  var result = []

  function add (pattern) {
    if (result.indexOf(pattern) === -1) {
      result.push(pattern)
    }
  }

  patterns.forEach(function (pattern) {
    // Allow gitignore style of directory exclusion
    if (!endsWith(pattern, '/**')) {
      add(pattern.replace(/\/$/, '').concat('/**'))
    }

    add(pattern)
  })

  return result
}

NYC.prototype.addFile = function (filename) {
  var relFile = path.relative(this.cwd, filename)
  var source = stripBom(fs.readFileSync(filename, 'utf8'))
  var content = this._addSource(source, filename, relFile)
  return {
    instrument: !!content,
    relFile: relFile,
    content: content || source
  }
}

NYC.prototype.shouldInstrumentFile = function (filename, relFile) {
  relFile = relFile.replace(/^\.\//, '') // remove leading './'.

  return (micromatch.any(filename, this.include) || micromatch.any(relFile, this.include)) &&
    !(micromatch.any(filename, this.exclude) || micromatch.any(relFile, this.exclude))
}

NYC.prototype.addAllFiles = function () {
  var _this = this

  this._createOutputDirectory()

  glob.sync('**/*.js', {nodir: true, ignore: this.exclude}).forEach(function (filename) {
    var obj = _this.addFile(filename, true)
    if (obj.instrument) {
      module._compile(
        _this.instrumenter().getPreamble(obj.content, obj.relFile),
        filename
      )
    }
  })
<commit_msg>shortcut include glob if not provided<commit_after>/* global __coverage__ */
var fs = require('fs')
var glob = require('glob')
var micromatch = require('micromatch')
var mkdirp = require('mkdirp')
var appendTransform = require('append-transform')
var path = require('path')
var rimraf = require('rimraf')
var onExit = require('signal-exit')
var stripBom = require('strip-bom')
var resolveFrom = require('resolve-from')
var md5 = require('md5-hex')
var arrify = require('arrify')
var convertSourceMap = require('convert-source-map')
var endsWith = require('ends-with')

/* istanbul ignore next */
if (/index\.covered\.js$/.test(__filename)) {
  require('./lib/self-coverage-helper')
}

function NYC (opts) {
  opts = opts || {}

  this._istanbul = opts.istanbul
  this.subprocessBin = opts.subprocessBin || path.resolve(__dirname, './bin/nyc.js')
  this._tempDirectory = opts.tempDirectory || './.nyc_output'
  this._cacheDirectory = opts.cacheDirectory || './node_modules/.cache/nyc'
  this.cwd = opts.cwd || process.env.NYC_CWD || process.cwd()
  this.reporter = arrify(opts.reporter || 'text')

  // you can specify config in the nyc stanza of package.json.
  var config = require(path.resolve(this.cwd, './package.json')).config || {}
  config = config.nyc || {}

  // load exclude stanza from config.
  this.include = false
  if (config.include) {
    this.include = this._prepGlobPatterns(arrify(config.include))
  }

  this.exclude = ['**/node_modules/**'].concat(arrify(config.exclude || ['test/**', 'test{,-*}.js']))
  this.exclude = this._prepGlobPatterns(this.exclude)

  // require extensions can be provided as config in package.json.
  this.require = arrify(config.require || opts.require)

  this._createOutputDirectory()
}

NYC.prototype._loadAdditionalModules = function () {
  var _this = this
  this.require.forEach(function (r) {
    // first attempt to require the module relative to
    // the directory being instrumented.
    var p = resolveFrom(_this.cwd, r)
    if (p) {
      require(p)
      return
    }
    // now try other locations, .e.g, the nyc node_modules folder.
    require(r)
  })
}

NYC.prototype.instrumenter = function () {
  return this._instrumenter || (this._instrumenter = this._createInstrumenter())
}

NYC.prototype._createInstrumenter = function () {
  var configFile = path.resolve(this.cwd, './.istanbul.yml')

  if (!fs.existsSync(configFile)) configFile = undefined

  var instrumenterConfig = this.istanbul().config.loadFile(configFile).instrumentation.config

  return new (this.istanbul()).Instrumenter({
    coverageVariable: '__coverage__',
    embedSource: instrumenterConfig['embed-source'],
    noCompact: !instrumenterConfig.compact,
    preserveComments: instrumenterConfig['preserve-comments']
  })
}

NYC.prototype._prepGlobPatterns = function (patterns) {
  if (!patterns) return patterns

  var result = []

  function add (pattern) {
    if (result.indexOf(pattern) === -1) {
      result.push(pattern)
    }
  }

  patterns.forEach(function (pattern) {
    // Allow gitignore style of directory exclusion
    if (!endsWith(pattern, '/**')) {
      add(pattern.replace(/\/$/, '').concat('/**'))
    }

    add(pattern)
  })

  return result
}

NYC.prototype.addFile = function (filename) {
  var relFile = path.relative(this.cwd, filename)
  var source = stripBom(fs.readFileSync(filename, 'utf8'))
  var content = this._addSource(source, filename, relFile)
  return {
    instrument: !!content,
    relFile: relFile,
    content: content || source
  }
}

NYC.prototype.shouldInstrumentFile = function (filename, relFile) {
  relFile = relFile.replace(/^\.\//, '') // remove leading './'.

  return (!this.include || micromatch.any(filename, this.include) || micromatch.any(relFile, this.include)) &&
    !(micromatch.any(filename, this.exclude) || micromatch.any(relFile, this.exclude))
}

NYC.prototype.addAllFiles = function () {
  var _this = this

  this._createOutputDirectory()

  glob.sync('**/*.js', {nodir: true, ignore: this.exclude}).forEach(function (filename) {
    var obj = _this.addFile(filename, true)
    if (obj.instrument) {
      module._compile(
        _this.instrumenter().getPreamble(obj.content, obj.relFile),
        filename
      )
    }
  })

{'fraction_non_alphanumeric': 0.1533349157370045, 'fraction_numerical': 0.0009494422027059103, 'mean_word_length': 3.8514680483592403, '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': '5943628', 'n_tokens_mistral': 2713, 'n_tokens_neox': 2636, 'n_words': 568}