source: imaps-frontend/node_modules/readdirp/README.md@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

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