vfile-find-up

vfile utility to find one or more files by searching the file system upwards

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
vfile-find-up
606.1.02 months ago7 years agoMinified + gzip package size for vfile-find-up in KB

Readme

vfile-find-up
!Buildbuild-badgebuild !Coveragecoverage-badgecoverage !Downloadsdownloads-badgedownloads !Sponsorssponsors-badgecollective !Backersbackers-badgecollective !Chatchat-badgechat
vfile utility to find files by searching the file system upwards.

Contents

*   [`findUp(tests[, path][, callback])`](#finduptests-path-callback)
*   [`findUpOne(test[, path][, callback])`](#finduponetest-path-callback)
*   [`BREAK`](#break)
*   [`INCLUDE`](#include)
*   [`Assert`](#assert)
*   [`Callback`](#callback)
*   [`CallbackOne`](#callbackone)
*   [`Test`](#test)

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-downvfile-find-down.

Install

This package is ESM onlyesm. In Node.js (version 14.14+ and 16.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 BREAKapi-break, INCLUDEapi-include, findUpapi-find-up, and findUpOneapi-find-up-one. There is no default export.

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

Find files or folders upwards.
👉 Note: files are not read (their value is not populated).
Signatures
  • (test[, path], callback) => void
  • (test[, path]) => Promise<Array<VFile>>
Parameters
— things to search for
  • paths (string, default: process.cwd())
— place to search from
— callback called when done
Returns
Nothing when callback is given (void), otherwise a promise that resolves to files (Array<VFile>vfile).

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

Find the first file or folder upwards.
👉 Note: files are not read (their value is not populated).
Signatures
  • (test[, path], callback) => void
  • (test[, path]) => Promise<VFile>
Parameters
— things to search for
  • path (string, default: process.cwd())
— place to search from
— callback called when done
Returns
Nothing when callback is given (void), otherwise a promise that resolves to a file (VFile | nullvfile).

BREAK

Stop searching (number).

INCLUDE

Include this file (number).

Assert

Handle a file (TypeScript type).
Parameters
— file to handle
Returns
How to handle this file (boolean | number, optional).
true is treated as INCLUDE.

Callback

Callback called when done (TypeScript type).
Parameters
  • error (Error | null)
— error; errors are currently never passed
— files
Returns
Nothing (void).

CallbackOne

Callback called when done finding one file (TypeScript type).
Parameters
  • error (Error | null)
— error; errors are currently never passed
— file
Returns
Nothing (void).

Test

Things to search for (TypeScript type).
For strings, the basename or extname of files must match them. For arrays, any test in them must match.
Type
type Test = Array<Assert | string> | Assert | string

Types

This package is fully typed with TypeScript. It exports the additional types Assertapi-assert, Callbackapi-callback, CallbackOneapi-callback-one, and Testapi-test.

Compatibility

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

Contribute

See contributing.mdcontributing in vfile/.githubhealth for ways to get started. See support.mdsupport for ways to get help.
This project has a code of conductcoc. By interacting with this repository, organization, or community you agree to abide by its terms.

License

MITlicense © Titus Wormerauthor