source: imaps-frontend/node_modules/readdirp/README.md@ 0c6b92a

main
Last change on this file since 0c6b92a was 0c6b92a, checked in by stefan toskovski <stefantoska84@…>, 5 weeks ago

Pred finalna verzija

  • Property mode set to 100644
File size: 6.3 KB
Line 
1# readdirp [![Weekly downloads](https://img.shields.io/npm/dw/readdirp.svg)](https://github.com/paulmillr/readdirp)
2
3Recursive version of [fs.readdir](https://nodejs.org/api/fs.html#fs_fs_readdir_path_options_callback). Exposes a **stream API** and a **promise API**.
4
5Supports both ESM and common.js.
6
7```sh
8npm install readdirp
9```
10
11```javascript
12// Use streams to achieve small RAM & CPU footprint.
13// 1) Streams example with for-await.
14import readdirp from 'readdirp';
15for await (const entry of readdirp('.')) {
16 const {path} = entry;
17 console.log(`${JSON.stringify({path})}`);
18}
19
20// 2) Streams example, non for-await.
21// Print out all JS files along with their size within the current folder & subfolders.
22import readdirp from 'readdirp';
23readdirp('.', {alwaysStat: true, fileFilter: (f) => f.basename.endsWith('.js')})
24 .on('data', (entry) => {
25 const {path, stats: {size}} = entry;
26 console.log(`${JSON.stringify({path, size})}`);
27 })
28 // Optionally call stream.destroy() in `warn()` in order to abort and cause 'close' to be emitted
29 .on('warn', error => console.error('non-fatal error', error))
30 .on('error', error => console.error('fatal error', error))
31 .on('end', () => console.log('done'));
32
33// 3) Promise example. More RAM and CPU than streams / for-await.
34import { readdirpPromise } from 'readdirp';
35const files = await readdirpPromise('.');
36console.log(files.map(file => file.path));
37
38// Other options.
39import readdirp from 'readdirp';
40readdirp('test', {
41 fileFilter: (f) => f.basename.endsWith('.js'),
42 directoryFilter: (d) => d.basename !== '.git',
43 // directoryFilter: (di) => di.basename.length === 9
44 type: 'files_directories',
45 depth: 1
46});
47```
48
49## API
50
51`const stream = readdirp(root[, options])` — **Stream API**
52
53- Reads given root recursively and returns a `stream` of [entry infos](#entryinfo)
54- Optionally can be used like `for await (const entry of stream)` with node.js 10+ (`asyncIterator`).
55- `on('data', (entry) => {})` [entry info](#entryinfo) for every file / dir.
56- `on('warn', (error) => {})` non-fatal `Error` that prevents a file / dir from being processed. Example: inaccessible to the user.
57- `on('error', (error) => {})` fatal `Error` which also ends the stream. Example: illegal options where passed.
58- `on('end')` — we are done. Called when all entries were found and no more will be emitted.
59- `on('close')` — stream is destroyed via `stream.destroy()`.
60 Could be useful if you want to manually abort even on a non fatal error.
61 At that point the stream is no longer `readable` and no more entries, warning or errors are emitted
62- To learn more about streams, consult the very detailed [nodejs streams documentation](https://nodejs.org/api/stream.html)
63 or the [stream-handbook](https://github.com/substack/stream-handbook)
64
65`const entries = await readdirp.promise(root[, options])` — **Promise API**. Returns a list of [entry infos](#entryinfo).
66
67First argument is awalys `root`, path in which to start reading and recursing into subdirectories.
68
69### options
70
71- `fileFilter`: filter to include or exclude files
72 - **Function**: a function that takes an entry info as a parameter and returns true to include or false to exclude the entry
73- `directoryFilter`: filter to include/exclude directories found and to recurse into. Directories that do not pass a filter will not be recursed into.
74- `depth: 5`: depth at which to stop recursing even if more subdirectories are found
75- `type: 'files'`: determines if data events on the stream should be emitted for `'files'` (default), `'directories'`, `'files_directories'`, or `'all'`. Setting to `'all'` will also include entries for other types of file descriptors like character devices, unix sockets and named pipes.
76- `alwaysStat: false`: always return `stats` property for every file. Default is `false`, readdirp will return `Dirent` entries. Setting it to `true` can double readdir execution time - use it only when you need file `size`, `mtime` etc. Cannot be enabled on node <10.10.0.
77- `lstat: false`: include symlink entries in the stream along with files. When `true`, `fs.lstat` would be used instead of `fs.stat`
78
79### `EntryInfo`
80
81Has the following properties:
82
83- `path: 'assets/javascripts/react.js'`: path to the file/directory (relative to given root)
84- `fullPath: '/Users/dev/projects/app/assets/javascripts/react.js'`: full path to the file/directory found
85- `basename: 'react.js'`: name of the file/directory
86- `dirent: fs.Dirent`: built-in [dir entry object](https://nodejs.org/api/fs.html#fs_class_fs_dirent) - only with `alwaysStat: false`
87- `stats: fs.Stats`: built in [stat object](https://nodejs.org/api/fs.html#fs_class_fs_stats) - only with `alwaysStat: true`
88
89## Changelog
90
91- 4.0 (Aug 25, 2024) rewritten in typescript, producing hybrid common.js / esm module.
92 - Remove glob support and all dependencies
93 - Make sure you're using `let {readdirp} = require('readdirp')` in common.js
94- 3.5 (Oct 13, 2020) disallows recursive directory-based symlinks.
95 Before, it could have entered infinite loop.
96- 3.4 (Mar 19, 2020) adds support for directory-based symlinks.
97- 3.3 (Dec 6, 2019) stabilizes RAM consumption and enables perf management with `highWaterMark` option. Fixes race conditions related to `for-await` looping.
98- 3.2 (Oct 14, 2019) improves performance by 250% and makes streams implementation more idiomatic.
99- 3.1 (Jul 7, 2019) brings `bigint` support to `stat` output on Windows. This is backwards-incompatible for some cases. Be careful. It you use it incorrectly, you'll see "TypeError: Cannot mix BigInt and other types, use explicit conversions".
100- 3.0 brings huge performance improvements and stream backpressure support.
101- Upgrading 2.x to 3.x:
102 - Signature changed from `readdirp(options)` to `readdirp(root, options)`
103 - Replaced callback API with promise API.
104 - Renamed `entryType` option to `type`
105 - Renamed `entryType: 'both'` to `'files_directories'`
106 - `EntryInfo`
107 - Renamed `stat` to `stats`
108 - Emitted only when `alwaysStat: true`
109 - `dirent` is emitted instead of `stats` by default with `alwaysStat: false`
110 - Renamed `name` to `basename`
111 - Removed `parentDir` and `fullParentDir` properties
112- Supported node.js versions:
113 - 4.x: node 14+
114 - 3.x: node 8+
115 - 2.x: node 0.6+
116
117## License
118
119Copyright (c) 2012-2019 Thorsten Lorenz, Paul Miller (<https://paulmillr.com>)
120
121MIT License, see [LICENSE](LICENSE) file.
Note: See TracBrowser for help on using the repository browser.