.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / @mrmlnc / readdir-enhanced / README.md
1 Enhanced `fs.readdir()`
2 =======================
3
4 > :warning: This is «fork» for original `readdir-enhanced` package but with some monkey fixes.
5
6 [![Build Status](https://api.travis-ci.org/BigstickCarpet/readdir-enhanced.svg?branch=master)](https://travis-ci.org/BigstickCarpet/readdir-enhanced)
7 [![Windows Build Status](https://ci.appveyor.com/api/projects/status/github/bigstickcarpet/readdir-enhanced?svg=true&branch=master&failingText=Windows%20build%20failing&passingText=Windows%20build%20passing)](https://ci.appveyor.com/project/BigstickCarpet/readdir-enhanced/branch/master)
8
9 [![Coverage Status](https://coveralls.io/repos/github/BigstickCarpet/readdir-enhanced/badge.svg?branch=master)](https://coveralls.io/github/BigstickCarpet/readdir-enhanced?branch=master)
10 [![Codacy Score](https://api.codacy.com/project/badge/Grade/178a817b6c864de7813fef457c0ed5ae)](https://www.codacy.com/public/jamesmessinger/readdir-enhanced)
11 [![Inline docs](http://inch-ci.org/github/BigstickCarpet/readdir-enhanced.svg?branch=master&style=shields)](http://inch-ci.org/github/BigstickCarpet/readdir-enhanced)
12 [![Dependencies](https://david-dm.org/BigstickCarpet/readdir-enhanced.svg)](https://david-dm.org/BigstickCarpet/readdir-enhanced)
13
14 [![npm](https://img.shields.io/npm/v/readdir-enhanced.svg?maxAge=43200)](https://www.npmjs.com/package/readdir-enhanced)
15 [![License](https://img.shields.io/npm/l/readdir-enhanced.svg?maxAge=2592000)](LICENSE)
16
17 `readdir-enhanced` is a [backward-compatible](#backward-compatible) drop-in replacement for [`fs.readdir()`](https://nodejs.org/api/fs.html#fs_fs_readdir_path_options_callback) and [`fs.readdirSync()`](https://nodejs.org/api/fs.html#fs_fs_readdirsync_path_options) with tons of extra features ([filtering](#filter), [recursion](#deep), [absolute paths](#basepath), [stats](#stats), and more) as well as additional APIs for Promises, Streams, and EventEmitters.
18
19
20 Pick Your API
21 -----------------
22 `readdir-enhanced` has multiple APIs, so you can pick whichever one you prefer.  There are three main APIs:
23
24 - **Synchronous API**<br>
25 aliases: `readdir.sync`, `readdir.readdirSync`<br>
26 Blocks the thread until all directory contents are read, and then returns all the results.
27
28 - **Async API**<br>
29 aliases: `readdir`, `readdir.async`, `readdir.readdirAsync`<br>
30 Reads the starting directory contents asynchronously and buffers all the results until all contents have been read. Supports callback or Promise syntax (see example below).
31
32 - **Streaming API**<br>
33 aliases: `readdir.stream`, `readdir.readdirStream`<br>
34 The streaming API reads the starting directory asynchronously and returns the results in real-time as they are read. The results can be [piped](https://nodejs.org/api/stream.html#stream_readable_pipe_destination_options) to other Node.js streams, or you can listen for specific events via the [EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter) interface. (see example below)
35
36 ```javascript
37 var readdir = require('readdir-enhanced');
38 var through2 = require('through2');
39
40 // Synchronous API
41 var files = readdir.sync('my/directory');
42
43 // Callback API
44 readdir.async('my/directory', function(err, files) { ... });
45
46 // Promises API
47 readdir.async('my/directory')
48   .then(function(files) { ... })
49   .catch(function(err) { ... });
50
51 // EventEmitter API
52 readdir.stream('my/directory')
53   .on('data', function(path) { ... })
54   .on('file', function(path) { ... })
55   .on('directory', function(path) { ... })
56   .on('symlink', function(path) { ... })
57   .on('error', function(err) { ... });
58
59 // Streaming API
60 var stream = readdir.stream('my/directory')
61   .pipe(through2.obj(function(data, enc, next) {
62     console.log(data);
63     this.push(data);
64     next();
65   });
66 ```
67
68
69 <a id="options"></a>
70 Enhanced Features
71 -----------------
72 `readdir-enhanced` adds several features to the built-in `fs.readdir()` function.  All of the enhanced features are opt-in, which makes `readdir-enhanced` [fully backward compatible by default](#backward-compatible).  You can enable any of the features by passing-in an `options` argument as the second parameter.
73
74
75 <a id="deep"></a>
76 ### Recursion
77 By default, `readdir-enhanced` will only return the top-level contents of the starting directory. But you can set the `deep` option to recursively traverse the subdirectories and return their contents as well.
78
79 #### Crawl ALL subdirectories
80
81 The `deep` option can be set to `true` to traverse the entire directory structure.
82
83 ```javascript
84 var readdir = require('readdir-enhanced');
85
86 readdir('my/directory', {deep: true}, function(err, files) {
87   console.log(files);
88   // => subdir1
89   // => subdir1/file.txt
90   // => subdir1/subdir2
91   // => subdir1/subdir2/file.txt
92   // => subdir1/subdir2/subdir3
93   // => subdir1/subdir2/subdir3/file.txt
94 });
95 ```
96
97 #### Crawl to a specific depth
98 The `deep` option can be set to a number to only traverse that many levels deep.  For example, calling `readdir('my/directory', {deep: 2})` will return `subdir1/file.txt` and `subdir1/subdir2/file.txt`, but it _won't_ return `subdir1/subdir2/subdir3/file.txt`.
99
100 ```javascript
101 var readdir = require('readdir-enhanced');
102
103 readdir('my/directory', {deep: 2}, function(err, files) {
104   console.log(files);
105   // => subdir1
106   // => subdir1/file.txt
107   // => subdir1/subdir2
108   // => subdir1/subdir2/file.txt
109   // => subdir1/subdir2/subdir3
110 });
111 ```
112
113 #### Crawl subdirectories by name
114 For simple use-cases, you can use a [regular expression](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) or a [glob pattern](https://github.com/isaacs/node-glob#glob-primer) to crawl only the directories whose path matches the pattern.  The path is relative to the starting directory by default, but you can customize this via [`options.basePath`](#basepath).
115
116 > **NOTE:** Glob patterns [_always_ use forward-slashes](https://github.com/isaacs/node-glob#windows), even on Windows. This _does not_ apply to regular expressions though. Regular expressions should use the appropraite path separator for the environment. Or, you can match both types of separators using `[\\/]`.
117
118 ```javascript
119 var readdir = require('readdir-enhanced');
120
121 // Only crawl the "lib" and "bin" subdirectories
122 // (notice that the "node_modules" subdirectory does NOT get crawled)
123 readdir('my/directory', {deep: /lib|bin/}, function(err, files) {
124   console.log(files);
125   // => bin
126   // => bin/cli.js
127   // => lib
128   // => lib/index.js
129   // => node_modules
130   // => package.json
131 });
132 ```
133
134 #### Custom recursion logic
135 For more advanced recursion, you can set the `deep` option to a function that accepts an [`fs.Stats`](https://nodejs.org/api/fs.html#fs_class_fs_stats) object and returns a truthy value if the starting directory should be crawled.
136
137 > **NOTE:** The [`fs.Stats`](https://nodejs.org/api/fs.html#fs_class_fs_stats) object that's passed to the function has additional `path` and `depth` properties. The `path` is relative to the starting directory by default, but you can customize this via [`options.basePath`](#basepath). The `depth` is the number of subdirectories beneath the base path (see [`options.deep`](#deep)).
138
139 ```javascript
140 var readdir = require('readdir-enhanced');
141
142 // Crawl all subdirectories, except "node_modules"
143 function ignoreNodeModules (stats) {
144   return stats.path.indexOf('node_modules') === -1;
145 }
146
147 readdir('my/directory', {deep: ignoreNodeModules}, function(err, files) {
148   console.log(files);
149   // => bin
150   // => bin/cli.js
151   // => lib
152   // => lib/index.js
153   // => node_modules
154   // => package.json
155 });
156 ```
157
158
159 <a id="filter"></a>
160 ### Filtering
161 The `filter` option lets you limit the results based on any criteria you want.
162
163 #### Filter by name
164 For simple use-cases, you can use a [regular expression](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) or a [glob pattern](https://github.com/isaacs/node-glob#glob-primer) to filter items by their path.  The path is relative to the starting directory by default, but you can customize this via [`options.basePath`](#basepath).
165
166 > **NOTE:** Glob patterns [_always_ use forward-slashes](https://github.com/isaacs/node-glob#windows), even on Windows. This _does not_ apply to regular expressions though. Regular expressions should use the appropraite path separator for the environment. Or, you can match both types of separators using `[\\/]`.
167
168 ```javascript
169 var readdir = require('readdir-enhanced');
170
171 // Find all .txt files
172 readdir('my/directory', {filter: '*.txt'});
173
174 // Find all package.json files
175 readdir('my/directory', {filter: '**/package.json', deep: true});
176
177 // Find everything with at least one number in the name
178 readdir('my/directory', {filter: /\d+/});
179 ```
180
181 #### Custom filtering logic
182 For more advanced filtering, you can specify a filter function that accepts an [`fs.Stats`](https://nodejs.org/api/fs.html#fs_class_fs_stats) object and returns a truthy value if the item should be included in the results.
183
184 > **NOTE:** The [`fs.Stats`](https://nodejs.org/api/fs.html#fs_class_fs_stats) object that's passed to the filter function has additional `path` and `depth` properties. The `path` is relative to the starting directory by default, but you can customize this via [`options.basePath`](#basepath). The `depth` is the number of subdirectories beneath the base path (see [`options.deep`](#deep)).
185
186 ```javascript
187 var readdir = require('readdir-enhanced');
188
189 // Only return file names containing an underscore
190 function myFilter(stats) {
191   return stats.isFile() && stats.path.indexOf('_') >= 0;
192 }
193
194 readdir('my/directory', {filter: myFilter}, function(err, files) {
195   console.log(files);
196   // => __myFile.txt
197   // => my_other_file.txt
198   // => img_1.jpg
199   // => node_modules
200 });
201 ```
202
203
204 <a id="basepath"></a>
205 ### Base Path
206 By default all `readdir-enhanced` functions return paths that are relative to the starting directory. But you can use the `basePath` option to customize this.  The `basePath` will be prepended to all of the returned paths.  One common use-case for this is to set `basePath` to the absolute path of the starting directory, so that all of the returned paths will be absolute.
207
208 ```javascript
209 var readdir = require('readdir-enhanced');
210 var path = require('path');
211
212 // Get absolute paths
213 var absPath = path.resolve('my/dir');
214 readdir('my/directory', {basePath: absPath}, function(err, files) {
215   console.log(files);
216   // => /absolute/path/to/my/directory/file1.txt
217   // => /absolute/path/to/my/directory/file2.txt
218   // => /absolute/path/to/my/directory/subdir
219 });
220
221 // Get paths relative to the working directory
222 readdir('my/directory', {basePath: 'my/directory'}, function(err, files) {
223   console.log(files);
224   // => my/directory/file1.txt
225   // => my/directory/file2.txt
226   // => my/directory/subdir
227 });
228 ```
229
230
231 <a id="sep"></a>
232 ### Path Separator
233 By default, `readdir-enhanced` uses the correct path separator for your OS (`\` on Windows, `/` on Linux & MacOS). But you can set the `sep` option to any separator character(s) that you want to use instead.  This is usually used to ensure consistent path separators across different OSes.
234
235 ```javascript
236 var readdir = require('readdir-enhanced');
237
238 // Always use Windows path separators
239 readdir('my/directory', {sep: '\\', deep: true}, function(err, files) {
240   console.log(files);
241   // => subdir1
242   // => subdir1\file.txt
243   // => subdir1\subdir2
244   // => subdir1\subdir2\file.txt
245   // => subdir1\subdir2\subdir3
246   // => subdir1\subdir2\subdir3\file.txt
247 });
248 ```
249
250 <a id="fs"></a>
251 ### Custom FS methods
252 By default, `readdir-enhanced` uses the default [Node.js FileSystem module](https://nodejs.org/api/fs.html) for methods like `fs.stat`, `fs.readdir` and `fs.lstat`. But in some situations, you can want to use your own FS methods (FTP, SSH, remote drive and etc). So you can provide your own implementation of FS methods by setting `options.fs` or specific methods, such as `options.fs.stat`.
253
254 ```javascript
255 var readdir = require('readdir-enhanced');
256
257 function myCustomReaddirMethod(dir, callback) {
258   callback(null, ['__myFile.txt']);
259 }
260
261 var options = {
262   fs: {
263     readdir: myCustomReaddirMethod
264   }
265 };
266
267 readdir('my/directory', options, function(err, files) {
268   console.log(files);
269   // => __myFile.txt
270 });
271 ```
272
273 <a id="stats"></a>
274 Get `fs.Stats` objects instead of strings
275 ------------------------
276 All of the `readdir-enhanced` functions listed above return an array of strings (paths). But in some situations, the path isn't enough information.  So, `readdir-enhanced` provides alternative versions of each function, which return an array of [`fs.Stats`](https://nodejs.org/api/fs.html#fs_class_fs_stats) objects instead of strings.  The `fs.Stats` object contains all sorts of useful information, such as the size, the creation date/time, and helper methods such as `isFile()`, `isDirectory()`, `isSymbolicLink()`, etc.
277
278 > **NOTE:** The [`fs.Stats`](https://nodejs.org/api/fs.html#fs_class_fs_stats) objects that are returned also have additional `path` and `depth` properties. The `path` is relative to the starting directory by default, but you can customize this via [`options.basePath`](#basepath). The `depth` is the number of subdirectories beneath the base path (see [`options.deep`](#deep)).
279
280 To get `fs.Stats` objects instead of strings, just add the word "Stat" to the function name.  As with the normal functions, each one is aliased (e.g. `readdir.async.stat` is the same as `readdir.readdirAsyncStat`), so you can use whichever naming style you prefer.
281
282 ```javascript
283 var readdir = require('readdir-enhanced');
284
285 // Synchronous API
286 var stats = readdir.sync.stat('my/directory');
287 var stats = readdir.readdirSyncStat('my/directory');
288
289 // Async API
290 readdir.async.stat('my/directory', function(err, stats) { ... });
291 readdir.readdirAsyncStat('my/directory', function(err, stats) { ... });
292
293 // Streaming API
294 readdir.stream.stat('my/directory')
295   .on('data', function(stat) { ... })
296   .on('file', function(stat) { ... })
297   .on('directory', function(stat) { ... })
298   .on('symlink', function(stat) { ... });
299
300 readdir.readdirStreamStat('my/directory')
301   .on('data', function(stat) { ... })
302   .on('file', function(stat) { ... })
303   .on('directory', function(stat) { ... })
304   .on('symlink', function(stat) { ... });
305
306 ```
307
308 <a id="backward-compatible"></a>
309 Backward Compatible
310 --------------------
311 `readdir-enhanced` is fully backward-compatible with Node.js' built-in `fs.readdir()` and `fs.readdirSync()` functions, so you can use it as a drop-in replacement in existing projects without affecting existing functionality, while still being able to use the enhanced features as needed.
312
313 ```javascript
314 var readdir = require('readdir-enhanced');
315 var readdirSync = readdir.sync;
316
317 // Use it just like Node's built-in fs.readdir function
318 readdir('my/directory', function(err, files) { ... });
319
320 // Use it just like Node's built-in fs.readdirSync function
321 var files = readdirSync('my/directory');
322 ```
323
324
325
326 Contributing
327 --------------------------
328 I welcome any contributions, enhancements, and bug-fixes.  [File an issue](https://github.com/BigstickCarpet/readdir-enhanced/issues) on GitHub and [submit a pull request](https://github.com/BigstickCarpet/readdir-enhanced/pulls).
329
330 #### Building
331 To build the project locally on your computer:
332
333 1. __Clone this repo__<br>
334 `git clone https://github.com/bigstickcarpet/readdir-enhanced.git`
335
336 2. __Install dependencies__<br>
337 `npm install`
338
339 3. __Run the tests__<br>
340 `npm test`
341
342
343
344 License
345 --------------------------
346 `readdir-enhanced` is 100% free and open-source, under the [MIT license](LICENSE). Use it however you want.
347