find-in-directory
v3.0.0
Published
Find file or directory by names in a directory.
Readme
find-in-directory
Find file or directory by names in a directory.
Install
yarn add find-in-directoryUsage
import {findFile, findDirectory} from 'find-in-directory'
console.log(await findFile(['foo.config.js', 'foo.config.json']))
// "/path/to/foo.config.json"
console.log(await findDirectory(['node_modules', '.yarn']))
// "/path/to/node_modules"API
find{File,Directory}(nameOrNames, options?)
nameOrNames
The file/directory name or names to find.
Type: string[] | string
options.cwd
The directory to find.
Type: URL | string
Default: process.cwd()
options.filter
Type: (fileOrDirectory: {name: string, path: string, stats: fs.Stats}) => Promise<boolean>
import fs from 'node:fs/promises'
import {findFile} from 'find-in-directory'
const file = await findFile(['foo.js', 'bar.js'], {
async filter({path: file}) {
const content = await fs.readFile(file, 'utf8')
return content.startsWith('#!/usr/bin/env node')
},
})
// "/path/to/bar.js"options.allowSymlinks
Should allow symlinks or not.
Type: boolean
Related
search-closest- Find closest file or directory.
