Skip to content

Commit c3923a7

Browse files
committed
Added API documentation, updated usage/installation
1 parent 26a940d commit c3923a7

File tree

1 file changed

+71
-6
lines changed

1 file changed

+71
-6
lines changed

README.md

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# files-by-directory
22

3+
> List files by directory, recursively, using [asynchronous iteration].
4+
>
5+
> This can be particularly useful for directory structures with lots of files, or slow files
6+
> systems, since you can start treating the results straight away, without having to wait for the
7+
> entire structure to be scanned.
8+
39
[![Latest Stable Version](https://img.shields.io/npm/v/files-by-directory.svg)](https://www.npmjs.com/package/files-by-directory)
410
[![Build Status](https://img.shields.io/travis/amercier/files-by-directory/master.svg)](https://travis-ci.org/amercier/files-by-directory)
511
[![NPM Downloads](https://img.shields.io/npm/dm/files-by-directory.svg)](https://www.npmjs.com/package/files-by-directory)
@@ -12,17 +18,72 @@
1218

1319
Prerequisites: [Node.js](https://nodejs.org/) 6+, **npm** 3+.
1420

15-
npm install --save files-by-directory
21+
```bash
22+
npm install --save files-by-directory
23+
```
24+
25+
## API
26+
27+
### `filesByDirectory(paths: string[]): AsyncIterator<string[]>`
28+
29+
Scan directories recursively, and generate 1 array per directory, containing the file paths.
30+
31+
```bash
32+
# Directory structure:
33+
level1
34+
├── level2a
35+
│ ├── level3
36+
│ │ ├── file3a
37+
│ │ └── file3b
38+
│ └── file2a
39+
├── level2b
40+
│ └── file2b
41+
├── file1a
42+
└── file1b
43+
```
44+
45+
```js
46+
const filesByDirectory = require('files-by-directory');
47+
48+
for await (const files of filesByDirectory(['level1'])) {
49+
console.log(files);
50+
console.log('---');
51+
}
52+
```
53+
54+
```
55+
[
56+
'level1/level2/level3/file3a',
57+
'level1/level2/level3/file3b'
58+
]
59+
---
60+
[
61+
'level1/level2a/file2a'
62+
]
63+
---
64+
[
65+
'level1/level2b/file2b'
66+
]
67+
---
68+
[
69+
'level1/file1a',
70+
'level1/file1b'
71+
]
72+
---
73+
```
1674

17-
## Usage
75+
**Notes:**
1876

19-
### ES5
77+
- If a path is encountered twice, it is only generated once.
78+
- Symbolic links are treated as regular files, even though they link to directories.
2079

21-
var xxx = require('files-by-directory').default;
80+
## Asynchronous iteration
2281

23-
### ES2015+
82+
[Asynchronous iteration] using `for-await-of` syntax requires Node 10+. For older version of NodeJS, either use:
2483

25-
import xxx from 'files-by-directory';
84+
- [Babel] with [@babel/transform-async-generator-functions], or
85+
- Use `async/await` syntax without `for-await-of` (NodeJS 8+, see [demo-node-8.js](demo-node-8.js))
86+
- Use Promises with a custom `invoke` function (NodeJS 6+, see [demo-node-6.js](demo-node-6.js))
2687

2788
## Contributing
2889

@@ -36,4 +97,8 @@ Please refer to the [guidelines for contributing](./CONTRIBUTING.md).
3697

3798
---
3899

100+
[asynchronous iteration]: http://2ality.com/2016/10/asynchronous-iteration.html
101+
[babel]: https://babeljs.io/
102+
[@babel/transform-async-generator-functions]: https://babeljs.io/docs/en/babel-plugin-proposal-async-generator-functions
103+
39104
<sup>_Created with [npm-p&#97;ckage-skeleton](https://github.com/amercier/files-by-directory)._</sup>

0 commit comments

Comments
 (0)