https://github.com/dawsbot/if-file-read
Read the first valid file in an array
https://github.com/dawsbot/if-file-read
filesystem read
Last synced: 8 months ago
JSON representation
Read the first valid file in an array
- Host: GitHub
- URL: https://github.com/dawsbot/if-file-read
- Owner: dawsbot
- License: mit
- Created: 2017-01-20T09:22:15.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2022-06-14T21:05:39.000Z (about 4 years ago)
- Last Synced: 2025-07-22T17:47:54.459Z (11 months ago)
- Topics: filesystem, read
- Language: JavaScript
- Size: 4.88 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
# if-file-read
[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![XO code style][xo-image]][xo-url]
> Return the content of the first file in array which exists
## Install
```
npm install --save if-file-read
```
## Usage
```js
const ifFileRead = require('if-file-read');
// Where c.txt is the only file:
ifFileRead(['a.txt', 'b.txt', 'c.txt']);
//=> 'contents of c.txt file'
// Where src/c.txt is the only file
ifFileRead(['a.txt', 'b.txt', 'c.txt'], {prepend: 'src'});
//=> 'contents of src/c.txt file'
```
## API
### ifFileRead(files, options)
Returns a `readFileSync` for the first valid file
#### Arguments
| Name | Description | Type | Default |
| ------- | ------------------------------- | -------- | ------- |
| files | Array of file paths | `array` | arg required |
|options.prepend| string to prepend to each file| `string` |'' |
#### Returns
Type: `string`
Throws `Error` `"No files found"` if none of the files exist
## License
MIT © [Dawson Botsford](http://dawsonbotsford.com)
[npm-image]: https://badge.fury.io/js/if-file-read.svg
[npm-url]: https://npmjs.org/package/if-file-read
[travis-image]: https://travis-ci.org/dawsbot/if-file-read.svg?branch=master
[travis-url]: https://travis-ci.org/dawsbot/if-file-read
[xo-image]: https://img.shields.io/badge/code_style-XO-5ed9c7.svg
[xo-url]: https://github.com/sindresorhus/xo