Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shinnn/node-read-glob
Search files with glob pattern and read them asynchronously
https://github.com/shinnn/node-read-glob
asynchronous glob-pattern globbing javascript nodejs readfile
Last synced: 26 days ago
JSON representation
Search files with glob pattern and read them asynchronously
- Host: GitHub
- URL: https://github.com/shinnn/node-read-glob
- Owner: shinnn
- License: isc
- Created: 2014-11-20T01:23:29.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2018-02-26T07:04:01.000Z (over 6 years ago)
- Last Synced: 2024-04-26T03:22:49.825Z (7 months ago)
- Topics: asynchronous, glob-pattern, globbing, javascript, nodejs, readfile
- Language: JavaScript
- Size: 55.7 KB
- Stars: 4
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# read-glob
[![npm version](https://img.shields.io/npm/v/read-glob.svg)](https://www.npmjs.com/package/read-glob)
[![Build Status](https://travis-ci.org/shinnn/node-read-glob.svg?branch=master)](https://travis-ci.org/shinnn/node-read-glob)
[![Build status](https://ci.appveyor.com/api/projects/status/9cf2k7pkog7ax2fs/branch/master?svg=true)](https://ci.appveyor.com/project/ShinnosukeWatanabe/node-read-glob/branch/master)
[![Coverage Status](https://img.shields.io/coveralls/shinnn/node-read-glob.svg)](https://coveralls.io/github/shinnn/node-read-glob)Search files with glob pattern and read them, [Observable](https://github.com/tc39/proposal-observable) way
```javascript
const readGlob = require('read-glob');readGlob('src/*.js').subscribe({
start() {
console.log('Glob started.');
},
next(result) {
result.cwd; //=> '/Users/shinnn/exmaple'
result.path; //=> 'src/a.js'
result.contents; //=>
},
complete() {
console.log('Glob completed.');
}
});
```## Installation
[Use](https://docs.npmjs.com/cli/install) [npm](https://docs.npmjs.com/getting-started/what-is-npm).
```
npm install read-glob
```## API
```javascript
const readGlob = require('read-glob');
```### readGlob(*pattern* [, *options*])
*pattern*: `string` (glob pattern)
*options*: `Object` ([`node-glob`] and [`fs.readFile`] options) or `string` (encoding)
Return: [`Observable`](https://github.com/tc39/proposal-observable#observable) ([zenparsing's implementation](https://github.com/zenparsing/zen-observable))When the `Observable` is [subscribe](https://tc39.github.io/proposal-observable/#observable-prototype-subscribe)d, it starts to search files matching the given glob pattern, read their contents and successively send results to its [`Observer`](https://github.com/tc39/proposal-observable#observer).
#### Results
Each result is the same `Object` as [`glob-observable`'s](https://github.com/shinnn/glob-observable#match-result) with the additional `contents` property, a `Buffer` or `string` of the matched file contents.
`contents` is a `string` when the `encoding` option is specified, otherwise it's a `Buffer`.
```javascript
readGlob('hi.txt').subscribe(result => {
result.contents; //=>
});readGlob('hi.txt', 'utf8').subscribe(result => {
result.contents; //=> 'Hi'
});readGlob('hi.txt', 'base64').subscribe(result => {
result.contents; //=> 'SGk='
});
```#### options
The option object will be directly passed to [`node-glob`] and [`fs.readFile`], or the encoding string sets the encoding of `fs.readFile`.
Unlike the original node-glob API,
* `silent` and `strict` options are `true` by default.
* `nodir` and `mark` options are not supported as it ignores directories by default.```javascript
const readGlob = require('read-glob');// ./directory/.dot.txt: 'Hello'
readGlob('*.txt', {
cwd: 'directory',
dot: true
}).subscribe(({contents}) => {
contents.toString(); //=> 'Hello'
});
```## Related project
* [read-glob-promise](https://github.com/shinnn/read-glob-promise) ([Promise](https://promisesaplus.com/) version)
## License
[ISC License](./LICENSE) © 2017 - 2018 Shinnosuke Watanabe
[`fs.readFile`]: https://nodejs.org/api/fs.html#fs_fs_readfile_path_options_callback
[`node-glob`]: https://github.com/isaacs/node-glob#options