<commit_before>import debug from "debug"
import fs from "mz/fs"
import { join } from "path"
import { jsVariants } from "interpret"
const _ = debug("fly:find")

/**
  Find a valid Flyfile from a given path.
  @param {String} file or path to the Flyfile
  @param {Function} use to bind require or process path
  @return {String} path to the Flyfile
*/
export function* find(path, bind = _ => _) {
  _("resolve path to flyfile %o", path)
  const root = join(process.cwd(), path)
  return bind((yield fs.stat(path)).isDirectory()
    ? yield resolve(root)
    : root)
  function* resolve (root) {
    for (let file of function* () {
      for (let ext of Object.keys(jsVariants))
        for (let name of ["Flyfile", "flyfile"])
          yield join(root, `${name}${ext}`)
    }()) try { if (yield fs.stat(file)) return file } catch (_) {}
    throw { code: "ENOENT" }
  }
}<commit_msg>rewrite utils.find. uses globby<commit_after>'use strict';

var path = require('path');
var jsVars = require('interpret').jsVariants;
var globby = require('globby');
var debug = require('debug');
var _ = debug('fly:find');
// promisify `fs` methods
var thenify = require('thenify-all');
var fs = thenify(require('fs'), {}, ['stat']);

/**
 * Find a valid Flyfile from a given path
 * @param {String}   filepath      The file or path to a Flyfile
 * @param {Function} bind          The function to bind, require, or process `filepath`
 * @return {String}   						 The path to the Flyfile
 */
module.exports = function * (filepath, bind) {
	bind = bind || function(_) {
		return _;
	};

	_('resolve path to flyfile %o', filepath);

	var froot = path.join(process.cwd(), filepath);
	var stats = yield fs.stat(filepath);

	if (stats.isDirectory()) {
		var glob = fileOptions();
		froot = yield globby(froot + path.sep + glob);
	}

	return froot;
};

/**
 * Format a Glob string for possible flyfile matching
 * @return {String}
 */
function fileOptions() {
	var extns = Object.keys(jsVars);
	var names = ['Flyfile', 'flyfile'];

	return [names, extns].map(function (obj) {
		return '{' + obj.join('|') + '}';
	}).join('.')
}
{'fraction_non_alphanumeric': 0.15889628924833493, 'fraction_numerical': 0.0, 'mean_word_length': 3.642384105960265, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '2082936', 'n_tokens_mistral': 701, 'n_tokens_neox': 686, 'n_words': 226}