1 | # @nodelib/fs.walk
|
2 |
|
3 | > A library for efficiently walking a directory recursively.
|
4 |
|
5 | ## :bulb: Highlights
|
6 |
|
7 | * :moneybag: Returns useful information: `name`, `path`, `dirent` and `stats` (optional).
|
8 | * :gear: Built-in directories/files and error filtering system.
|
9 | * :link: Can safely work with broken symbolic links.
|
10 |
|
11 | ## Install
|
12 |
|
13 | ```console
|
14 | npm install @nodelib/fs.walk
|
15 | ```
|
16 |
|
17 | ## Usage
|
18 |
|
19 | ```ts
|
20 | import * as fsWalk from '@nodelib/fs.walk';
|
21 |
|
22 | fsWalk.walk('path', (error, entries) => { /* … */ });
|
23 | ```
|
24 |
|
25 | ## API
|
26 |
|
27 | ### .walk(path, [optionsOrSettings], callback)
|
28 |
|
29 | Reads the directory recursively and asynchronously. Requires a callback function.
|
30 |
|
31 | > :book: If you want to use the Promise API, use `util.promisify`.
|
32 |
|
33 | ```ts
|
34 | fsWalk.walk('path', (error, entries) => { /* … */ });
|
35 | fsWalk.walk('path', {}, (error, entries) => { /* … */ });
|
36 | fsWalk.walk('path', new fsWalk.Settings(), (error, entries) => { /* … */ });
|
37 | ```
|
38 |
|
39 | ### .walkStream(path, [optionsOrSettings])
|
40 |
|
41 | Reads the directory recursively and asynchronously. [Readable Stream](https://nodejs.org/dist/latest-v12.x/docs/api/stream.html#stream_readable_streams) is used as a provider.
|
42 |
|
43 | ```ts
|
44 | const stream = fsWalk.walkStream('path');
|
45 | const stream = fsWalk.walkStream('path', {});
|
46 | const stream = fsWalk.walkStream('path', new fsWalk.Settings());
|
47 | ```
|
48 |
|
49 | ### .walkSync(path, [optionsOrSettings])
|
50 |
|
51 | Reads the directory recursively and synchronously. Returns an array of entries.
|
52 |
|
53 | ```ts
|
54 | const entries = fsWalk.walkSync('path');
|
55 | const entries = fsWalk.walkSync('path', {});
|
56 | const entries = fsWalk.walkSync('path', new fsWalk.Settings());
|
57 | ```
|
58 |
|
59 | #### path
|
60 |
|
61 | * Required: `true`
|
62 | * Type: `string | Buffer | URL`
|
63 |
|
64 | A path to a file. If a URL is provided, it must use the `file:` protocol.
|
65 |
|
66 | #### optionsOrSettings
|
67 |
|
68 | * Required: `false`
|
69 | * Type: `Options | Settings`
|
70 | * Default: An instance of `Settings` class
|
71 |
|
72 | An [`Options`](#options) object or an instance of [`Settings`](#settings) class.
|
73 |
|
74 | > :book: When you pass a plain object, an instance of the `Settings` class will be created automatically. If you plan to call the method frequently, use a pre-created instance of the `Settings` class.
|
75 |
|
76 | ### Settings([options])
|
77 |
|
78 | A class of full settings of the package.
|
79 |
|
80 | ```ts
|
81 | const settings = new fsWalk.Settings({ followSymbolicLinks: true });
|
82 |
|
83 | const entries = fsWalk.walkSync('path', settings);
|
84 | ```
|
85 |
|
86 | ## Entry
|
87 |
|
88 | * `name` — The name of the entry (`unknown.txt`).
|
89 | * `path` — The path of the entry relative to call directory (`root/unknown.txt`).
|
90 | * `dirent` — An instance of [`fs.Dirent`](./src/types/index.ts) class.
|
91 | * [`stats`] — An instance of `fs.Stats` class.
|
92 |
|
93 | ## Options
|
94 |
|
95 | ### basePath
|
96 |
|
97 | * Type: `string`
|
98 | * Default: `undefined`
|
99 |
|
100 | By default, all paths are built relative to the root path. You can use this option to set custom root path.
|
101 |
|
102 | In the example below we read the files from the `root` directory, but in the results the root path will be `custom`.
|
103 |
|
104 | ```ts
|
105 | fsWalk.walkSync('root'); // → ['root/file.txt']
|
106 | fsWalk.walkSync('root', { basePath: 'custom' }); // → ['custom/file.txt']
|
107 | ```
|
108 |
|
109 | ### concurrency
|
110 |
|
111 | * Type: `number`
|
112 | * Default: `Infinity`
|
113 |
|
114 | The maximum number of concurrent calls to `fs.readdir`.
|
115 |
|
116 | > :book: The higher the number, the higher performance and the load on the File System. If you want to read in quiet mode, set the value to `4 * os.cpus().length` (4 is default size of [thread pool work scheduling](http://docs.libuv.org/en/v1.x/threadpool.html#thread-pool-work-scheduling)).
|
117 |
|
118 | ### deepFilter
|
119 |
|
120 | * Type: [`DeepFilterFunction`](./src/settings.ts)
|
121 | * Default: `undefined`
|
122 |
|
123 | A function that indicates whether the directory will be read deep or not.
|
124 |
|
125 | ```ts
|
126 | // Skip all directories that starts with `node_modules`
|
127 | const filter: DeepFilterFunction = (entry) => !entry.path.startsWith('node_modules');
|
128 | ```
|
129 |
|
130 | ### entryFilter
|
131 |
|
132 | * Type: [`EntryFilterFunction`](./src/settings.ts)
|
133 | * Default: `undefined`
|
134 |
|
135 | A function that indicates whether the entry will be included to results or not.
|
136 |
|
137 | ```ts
|
138 | // Exclude all `.js` files from results
|
139 | const filter: EntryFilterFunction = (entry) => !entry.name.endsWith('.js');
|
140 | ```
|
141 |
|
142 | ### errorFilter
|
143 |
|
144 | * Type: [`ErrorFilterFunction`](./src/settings.ts)
|
145 | * Default: `undefined`
|
146 |
|
147 | A function that allows you to skip errors that occur when reading directories.
|
148 |
|
149 | For example, you can skip `ENOENT` errors if required:
|
150 |
|
151 | ```ts
|
152 | // Skip all ENOENT errors
|
153 | const filter: ErrorFilterFunction = (error) => error.code == 'ENOENT';
|
154 | ```
|
155 |
|
156 | ### stats
|
157 |
|
158 | * Type: `boolean`
|
159 | * Default: `false`
|
160 |
|
161 | Adds an instance of `fs.Stats` class to the [`Entry`](#entry).
|
162 |
|
163 | > :book: Always use `fs.readdir` with additional `fs.lstat/fs.stat` calls to determine the entry type.
|
164 |
|
165 | ### followSymbolicLinks
|
166 |
|
167 | * Type: `boolean`
|
168 | * Default: `false`
|
169 |
|
170 | Follow symbolic links or not. Call `fs.stat` on symbolic link if `true`.
|
171 |
|
172 | ### `throwErrorOnBrokenSymbolicLink`
|
173 |
|
174 | * Type: `boolean`
|
175 | * Default: `true`
|
176 |
|
177 | Throw an error when symbolic link is broken if `true` or safely return `lstat` call if `false`.
|
178 |
|
179 | ### `pathSegmentSeparator`
|
180 |
|
181 | * Type: `string`
|
182 | * Default: `path.sep`
|
183 |
|
184 | By default, this package uses the correct path separator for your OS (`\` on Windows, `/` on Unix-like systems). But you can set this option to any separator character(s) that you want to use instead.
|
185 |
|
186 | ### `fs`
|
187 |
|
188 | * Type: `FileSystemAdapter`
|
189 | * Default: A default FS methods
|
190 |
|
191 | By default, the built-in Node.js module (`fs`) is used to work with the file system. You can replace any method with your own.
|
192 |
|
193 | ```ts
|
194 | interface FileSystemAdapter {
|
195 | lstat: typeof fs.lstat;
|
196 | stat: typeof fs.stat;
|
197 | lstatSync: typeof fs.lstatSync;
|
198 | statSync: typeof fs.statSync;
|
199 | readdir: typeof fs.readdir;
|
200 | readdirSync: typeof fs.readdirSync;
|
201 | }
|
202 |
|
203 | const settings = new fsWalk.Settings({
|
204 | fs: { lstat: fakeLstat }
|
205 | });
|
206 | ```
|
207 |
|
208 | ## Changelog
|
209 |
|
210 | See the [Releases section of our GitHub project](https://github.com/nodelib/nodelib/releases) for changelog for each release version.
|
211 |
|
212 | ## License
|
213 |
|
214 | This software is released under the terms of the MIT license.
|