look-it-up
Version:
Find a file or directory by walking up parent directories recursively. Zero dependency.
59 lines (38 loc) • 1.96 kB
Markdown
> Find a file or directory by walking up parent directories recursively. Zero dependency.
[](https://www.npmjs.com/package/look-it-up)
[](https://github.com/exuanbo/look-it-up/actions/workflows/nodejs.yml)
[](https://codecov.io/gh/exuanbo/look-it-up)
[](https://liberamanifesto.com)
```sh
npm install look-it-up
```
```js
import { existsSync } from 'fs'
import { join } from 'path'
import { lookItUp, lookItUpSync, exists } from 'look-it-up'
const contains = async (file, dir) => (await exists(join(dir, file))) ? dir : null
const containsSync = (file, dir) => existsSync(join(dir, file) ? dir : null
;(async () => {
await lookItUp('.zshrc') //=> '~/.zshrc'
await lookItUp(dir => containsSync('.zshrc', dir)) //=> '~'
await lookItUp(async dir => await contains('.zshrc', dir)) //=> '~'
})()
lookItUpSync('.zshrc') //=> '~/.zshrc'
lookItUpSync(dir => containsSync('.zshrc', dir)) //=> '~'
```
```ts
declare type MatcherResult = string | null | symbol
declare type Matcher = string | ((dir: string) => MatcherResult | Promise<MatcherResult>)
declare type MatcherSync = string | ((dir: string) => MatcherResult)
declare const lookItUp: (matcher: Matcher, dir?: string) => Promise<string | null>
declare const lookItUpSync: (matcher: MatcherSync, dir?: string) => string | null | never
declare const exists: (path: string) => Promise<boolean>
declare const stop: unique symbol
export { exists, lookItUp, lookItUpSync, stop }
```
[](https://github.com/exuanbo/look-it-up/blob/main/LICENSE) © 2021 [Exuanbo](https://github.com/exuanbo)