unified

Project: vfile/vfile-find-up

Package: vfile-find-up@6.0.0

  1. Dependents: 0
  2. vfile utility to find one or more files by searching the file system upwards
  1. util 147
  2. utility 143
  3. file 34
  4. virtual 19
  5. vfile 17
  6. vfile-util 15
  7. find 11
  8. walk 8

vfile-find-up

Build Coverage Downloads Sponsors Backers Chat

vfile utility to find files by searching the file system upwards.

Contents

What is this?

This utility lets you find one or many files upwards.

When should I use this?

You can use this utility if you want to find, say, a config file. If you instead want to find files downwards, such as all markdown files in a folder, you can use vfile-find-down.

Install

This package is ESM only. In Node.js (version 12.20+, 14.14+, 16.0+, or 18.0+), install with npm:

npm install vfile-find-up

Use

import {findUp} from 'vfile-find-up'

console.log(await findUp('package.json'))

Yields:

[ VFile {
  data: {},
  messages: [],
  history: [ '/Users/tilde/projects/oss/vfile-find-up/package.json' ],
  cwd: '/Users/tilde/projects/oss/vfile-find-up' } ]

API

This package exports the identifiers findUp, findUpOne, INCLUDE, and BREAK. There is no default export.

findUp(tests[, path][, callback])

Search for tests upwards. Calls callback with either an error or an array of files passing tests.

👉 Note: files are not read (their value is not populated).

Sigantures
Parameters
tests

Things to search for (string, Function, or Array<tests>).

If a string is passed in, the basename or extname of files must match it for them to be included.

If an array is passed in, any test must match a given file for it to be included.

Otherwise, they must be Assert.

path

Place to searching from (string, default: process.cwd()).

callback

Function called with all matching files (function cb(error[, files])).

findUpOne(tests[, path][, callback])

Like findUp, but either calls callback with the first found file or null, or returns a promise that resolved to a file or null.

function assert(file)

Check whether a virtual file should be included. Called with a vfile.

Returns

The different flags can be combined by using the pipe operator: INCLUDE | BREAK.

Types

This package is fully typed with TypeScript. It exports the additional types Assert and Test.

Compatibility

Projects maintained by the unified collective are compatible with all maintained versions of Node.js. As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+. Our projects sometimes work with older versions, but this is not guaranteed.

Contribute

See contributing.md in vfile/.github for ways to get started. See support.md for ways to get help.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.

License

MIT © Titus Wormer