<commit_before><commit_msg>source file path/file-find-up<commit_after>// find-pkg <https://github.com/jonschlinkert/find-pkg>
// Copyright (c) 2015, <NAME>.

'use strict'

var fs = require('fs');
var path = require('path');
var resolve = require('./lookup/resolve-dir');
var exists = require('../file/exists');
var cwd = process.cwd();

/**
 * Find a file, starting with the given directory
 */

module.exports = function(filename, cwd, limit, cb) {
  if (typeof cwd === 'function') {
    cb = cwd;
    cwd = null;
  }

  if (typeof limit === 'function') {
    cb = limit;
    limit = Infinity;
  }

  var dir = cwd ? resolve(cwd) : '.';
  var n = 0;

  (function find(dir, next) {
    var fp = path.resolve(dir, filename);

    exists(fp, function(exists) {
      n++;

      if (exists) {
        next(null, fp);
        return;
      }

      if (n >= limit || dir === path.sep || dir === '.') {
        next();
        return;
      }

      find(path.dirname(dir), next);
    });
  }(dir, cb));
};

module.exports.sync = function(filename, cwd, limit) {
  var dir = cwd ? resolve(cwd) : '.';
  var fp = path.join(dir, filename);
  var n = 0;

  if (exists(fp)) {
    return path.resolve(fp);
  }

  if (limit === 0) return null;

  while ((dir = path.dirname(dir))) {
    n++;

    var filepath = path.resolve(dir, filename);
    if (exists(filepath)) {
      return filepath;
    }

    if (n >= limit || dir === '.' || dir === path.sep) {
      return;
    }
  }
};

{'fraction_non_alphanumeric': 0.19008825526137135, 'fraction_numerical': 0.0047522063815342835, 'mean_word_length': 2.44392523364486, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 1, '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': '2066635', 'n_tokens_mistral': 529, 'n_tokens_neox': 511, 'n_words': 132}