# find-and-read

> Finding and Reading Files Made a Little Easier

Latest version **1.2.0** (published 2022-05-28) · CC0-1.0 license · 0 weekly downloads

## Install

```sh
npm install find-and-read
pnpm add find-and-read
yarn add find-and-read
bun add find-and-read
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2022-05-28 |
| First published | 2021-05-22 |
| Weekly downloads | 0 |
| License | CC0-1.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 15.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Daniel J. Dufour |
| Maintainers | danieljdufour |
| Keywords | async, file, find, fs, grep, open, read, search, smart, sync |

## Links

- npm: https://www.npmjs.com/package/find-and-read
- Repository: https://github.com/DanielJDufour/find-and-read
- Homepage: https://github.com/DanielJDufour/find-and-read#readme
- Issues: https://github.com/DanielJDufour/find-and-read/issues
- npm.io page: https://npm.io/package/find-and-read

## Alternatives

- [unionfs](https://npm.io/package/unionfs.md) — 2.2M weekly downloads
- [path-starts-with](https://npm.io/package/path-starts-with.md) — 35.9K weekly downloads
- [redzip](https://npm.io/package/redzip.md) — 1.2K weekly downloads
- [vscode-anymatch](https://npm.io/package/vscode-anymatch.md) — 848 weekly downloads
- [@ledgerhq/coin-filecoin](https://npm.io/package/@ledgerhq/coin-filecoin.md) — 793 weekly downloads

## Recent versions

- 1.2.0 (latest) — 2022-05-28
- 1.1.0 — 2021-11-11
- 1.0.2 — 2021-10-20
- 1.0.1 — 2021-10-20
- 1.0.0 — 2021-05-22
- 0.0.1 — 2021-05-22
- 0.0.0 — 2021-05-22

## README

# find-and-read
> Finding and Reading Files Made a Little Easier

# what?
This library exposes a single function `findAndRead`, which takes in a filename.
It iteratively searches up, down and across your folder hierarchy for the file.
When it finds the file, it runs NodeJS' built-in [readFileSync](https://nodejs.org/api/fs.html#fs_fs_readfilesync_path_options) on it.

# why?
I work on a lot of projects that separate the data used for tests from the actual test scripts. It's often a pain to determine the correct path to pass into readFileSync. 
My code would often look like `readFileSync("../../../test-data/images/example.png")`.
It would be a lot easier if I could just run `findAndRead("example.png")`, so I created a library that does that.

# features
- Dependency Free (only uses NodeJS builtins)
- Automatically Finds Closest Matching File (least amount of change directory steps)
- Small Code Base
- Memory Efficient
- TypeScript Support

# install
```bash
npm install find-and-read
```

# basic usage
Automatically find the closest file with the given name and run [readFileSync](https://nodejs.org/api/fs.html#fs_fs_readfilesync_path_options) on it
```javascript
const findAndRead = require("find-and-read");

const buffer = findAndRead("example.png");
```

# advanced usage
You can pass in the same options as you would to [readFileSync](https://nodejs.org/api/fs.html#fs_fs_readfilesync_path_options), "encoding" and "flag";
```js
const findAndRead = require("find-and-read");

const json = findAndRead("example.json", { encoding: 'utf-8' });
```

#### adjusting start
When a file calls `findAndRead`, it will start looking in the folder of the caller file.  When you run `findAndRead` in a REPL, it will start looking in the folder where you started the REPL.  If you would like to start at a different place, pass in a `start` parameter like below:
```js
const findAndRead = require("find-and-read");

// look for an image starting in the /tmp folder
const buffer = findAndRead("image.jpg", { start: '/tmp' });
```

#### custom stop function
By default, `findAndRead` doesn't navigate into node_modules and hidden folders (like `.git`).  When run inside a git repository, it also doesn't navigate up outside the git repo.  You can turn this off by passing `stop: null` or create own custom function for determining when to stop on a path.
```js
const findAndRead = require("find-and-read");

const buffer = findAndRead("test-image.jpg", {
  stop: ({
    dirpath, // the path to the directory that we are navigating to
    from, // the path to the directory that we are navigating from
    direction // "up" or "down"
  }) => {
    // don't search inside of the env folder
    return dirpath.includes('env');
  }
});
```

#### warnings
By default, `findAndRead` will issue a console.warn if it's unable to find a file.
You can turn off this behavior by passing in `warn: false`.
```js
const findAndRead = require("find-and-read");

findAndRead("unfindable.txt");
// logs `[find-and-read] unable to find "unfindable.txt"`

findAndRead("unfindable.txt", { warn: false });
// nothing is logged
```

---
_Source: https://npm.io/package/find-and-read · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
