1
1
# files-by-directory
2
2
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
+
3
9
[ ![ Latest Stable Version] ( https://img.shields.io/npm/v/files-by-directory.svg )] ( https://www.npmjs.com/package/files-by-directory )
4
10
[ ![ Build Status] ( https://img.shields.io/travis/amercier/files-by-directory/master.svg )] ( https://travis-ci.org/amercier/files-by-directory )
5
11
[ ![ NPM Downloads] ( https://img.shields.io/npm/dm/files-by-directory.svg )] ( https://www.npmjs.com/package/files-by-directory )
12
18
13
19
Prerequisites: [ Node.js] ( https://nodejs.org/ ) 6+, ** npm** 3+.
14
20
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
+ ```
16
74
17
- ## Usage
75
+ ** Notes: **
18
76
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.
20
79
21
- var xxx = require('files-by-directory').default;
80
+ ## Asynchronous iteration
22
81
23
- ### ES2015+
82
+ [ Asynchronous iteration ] using ` for-await-of ` syntax requires Node 10+. For older version of NodeJS, either use:
24
83
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 ) )
26
87
27
88
## Contributing
28
89
@@ -36,4 +97,8 @@ Please refer to the [guidelines for contributing](./CONTRIBUTING.md).
36
97
37
98
---
38
99
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
+
39
104
<sup >_ Created with [ npm-pa ; ckage-skeleton] ( https://github.com/amercier/files-by-directory ) ._ </sup >
0 commit comments