Skip to content

Instantly share code, notes, and snippets.

@aleclarson
Created April 24, 2021 05:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aleclarson/6997eee5662d852532b2758361ce0406 to your computer and use it in GitHub Desktop.
Save aleclarson/6997eee5662d852532b2758361ce0406 to your computer and use it in GitHub Desktop.
// Adapted from https://github.com/lukeed/escalade/blob/2477005/src/sync.js
import { dirname, join } from 'path'
import { readdirSync } from 'fs'
/**
* Search each parent directory until a string or false is returned.
*/
export function findFile(
dir: string,
callback: (names: string[], dir: string) => string | false | undefined
) {
let tmp: ReturnType<typeof callback>
for (;;) {
tmp = callback(readdirSync(dir), dir)
if (tmp !== undefined) {
return tmp ? join(dir, tmp) : undefined
}
dir = dirname((tmp = dir))
if (tmp === dir) return
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment