You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

347 lines
15 KiB

4 years ago
  1. Enhanced `fs.readdir()`
  2. =======================
  3. > :warning: This is «fork» for original `readdir-enhanced` package but with some monkey fixes.
  4. [![Build Status](https://api.travis-ci.org/BigstickCarpet/readdir-enhanced.svg?branch=master)](https://travis-ci.org/BigstickCarpet/readdir-enhanced)
  5. [![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)
  6. [![Coverage Status](https://coveralls.io/repos/github/BigstickCarpet/readdir-enhanced/badge.svg?branch=master)](https://coveralls.io/github/BigstickCarpet/readdir-enhanced?branch=master)
  7. [![Codacy Score](https://api.codacy.com/project/badge/Grade/178a817b6c864de7813fef457c0ed5ae)](https://www.codacy.com/public/jamesmessinger/readdir-enhanced)
  8. [![Inline docs](http://inch-ci.org/github/BigstickCarpet/readdir-enhanced.svg?branch=master&style=shields)](http://inch-ci.org/github/BigstickCarpet/readdir-enhanced)
  9. [![Dependencies](https://david-dm.org/BigstickCarpet/readdir-enhanced.svg)](https://david-dm.org/BigstickCarpet/readdir-enhanced)
  10. [![npm](https://img.shields.io/npm/v/readdir-enhanced.svg?maxAge=43200)](https://www.npmjs.com/package/readdir-enhanced)
  11. [![License](https://img.shields.io/npm/l/readdir-enhanced.svg?maxAge=2592000)](LICENSE)
  12. `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.
  13. Pick Your API
  14. -----------------
  15. `readdir-enhanced` has multiple APIs, so you can pick whichever one you prefer. There are three main APIs:
  16. - **Synchronous API**<br>
  17. aliases: `readdir.sync`, `readdir.readdirSync`<br>
  18. Blocks the thread until all directory contents are read, and then returns all the results.
  19. - **Async API**<br>
  20. aliases: `readdir`, `readdir.async`, `readdir.readdirAsync`<br>
  21. 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).
  22. - **Streaming API**<br>
  23. aliases: `readdir.stream`, `readdir.readdirStream`<br>
  24. 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)
  25. ```javascript
  26. var readdir = require('readdir-enhanced');
  27. var through2 = require('through2');
  28. // Synchronous API
  29. var files = readdir.sync('my/directory');
  30. // Callback API
  31. readdir.async('my/directory', function(err, files) { ... });
  32. // Promises API
  33. readdir.async('my/directory')
  34. .then(function(files) { ... })
  35. .catch(function(err) { ... });
  36. // EventEmitter API
  37. readdir.stream('my/directory')
  38. .on('data', function(path) { ... })
  39. .on('file', function(path) { ... })
  40. .on('directory', function(path) { ... })
  41. .on('symlink', function(path) { ... })
  42. .on('error', function(err) { ... });
  43. // Streaming API
  44. var stream = readdir.stream('my/directory')
  45. .pipe(through2.obj(function(data, enc, next) {
  46. console.log(data);
  47. this.push(data);
  48. next();
  49. });
  50. ```
  51. <a id="options"></a>
  52. Enhanced Features
  53. -----------------
  54. `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.
  55. <a id="deep"></a>
  56. ### Recursion
  57. 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.
  58. #### Crawl ALL subdirectories
  59. The `deep` option can be set to `true` to traverse the entire directory structure.
  60. ```javascript
  61. var readdir = require('readdir-enhanced');
  62. readdir('my/directory', {deep: true}, function(err, files) {
  63. console.log(files);
  64. // => subdir1
  65. // => subdir1/file.txt
  66. // => subdir1/subdir2
  67. // => subdir1/subdir2/file.txt
  68. // => subdir1/subdir2/subdir3
  69. // => subdir1/subdir2/subdir3/file.txt
  70. });
  71. ```
  72. #### Crawl to a specific depth
  73. 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`.
  74. ```javascript
  75. var readdir = require('readdir-enhanced');
  76. readdir('my/directory', {deep: 2}, function(err, files) {
  77. console.log(files);
  78. // => subdir1
  79. // => subdir1/file.txt
  80. // => subdir1/subdir2
  81. // => subdir1/subdir2/file.txt
  82. // => subdir1/subdir2/subdir3
  83. });
  84. ```
  85. #### Crawl subdirectories by name
  86. 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).
  87. > **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 `[\\/]`.
  88. ```javascript
  89. var readdir = require('readdir-enhanced');
  90. // Only crawl the "lib" and "bin" subdirectories
  91. // (notice that the "node_modules" subdirectory does NOT get crawled)
  92. readdir('my/directory', {deep: /lib|bin/}, function(err, files) {
  93. console.log(files);
  94. // => bin
  95. // => bin/cli.js
  96. // => lib
  97. // => lib/index.js
  98. // => node_modules
  99. // => package.json
  100. });
  101. ```
  102. #### Custom recursion logic
  103. 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.
  104. > **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)).
  105. ```javascript
  106. var readdir = require('readdir-enhanced');
  107. // Crawl all subdirectories, except "node_modules"
  108. function ignoreNodeModules (stats) {
  109. return stats.path.indexOf('node_modules') === -1;
  110. }
  111. readdir('my/directory', {deep: ignoreNodeModules}, function(err, files) {
  112. console.log(files);
  113. // => bin
  114. // => bin/cli.js
  115. // => lib
  116. // => lib/index.js
  117. // => node_modules
  118. // => package.json
  119. });
  120. ```
  121. <a id="filter"></a>
  122. ### Filtering
  123. The `filter` option lets you limit the results based on any criteria you want.
  124. #### Filter by name
  125. 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).
  126. > **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 `[\\/]`.
  127. ```javascript
  128. var readdir = require('readdir-enhanced');
  129. // Find all .txt files
  130. readdir('my/directory', {filter: '*.txt'});
  131. // Find all package.json files
  132. readdir('my/directory', {filter: '**/package.json', deep: true});
  133. // Find everything with at least one number in the name
  134. readdir('my/directory', {filter: /\d+/});
  135. ```
  136. #### Custom filtering logic
  137. 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.
  138. > **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)).
  139. ```javascript
  140. var readdir = require('readdir-enhanced');
  141. // Only return file names containing an underscore
  142. function myFilter(stats) {
  143. return stats.isFile() && stats.path.indexOf('_') >= 0;
  144. }
  145. readdir('my/directory', {filter: myFilter}, function(err, files) {
  146. console.log(files);
  147. // => __myFile.txt
  148. // => my_other_file.txt
  149. // => img_1.jpg
  150. // => node_modules
  151. });
  152. ```
  153. <a id="basepath"></a>
  154. ### Base Path
  155. 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.
  156. ```javascript
  157. var readdir = require('readdir-enhanced');
  158. var path = require('path');
  159. // Get absolute paths
  160. var absPath = path.resolve('my/dir');
  161. readdir('my/directory', {basePath: absPath}, function(err, files) {
  162. console.log(files);
  163. // => /absolute/path/to/my/directory/file1.txt
  164. // => /absolute/path/to/my/directory/file2.txt
  165. // => /absolute/path/to/my/directory/subdir
  166. });
  167. // Get paths relative to the working directory
  168. readdir('my/directory', {basePath: 'my/directory'}, function(err, files) {
  169. console.log(files);
  170. // => my/directory/file1.txt
  171. // => my/directory/file2.txt
  172. // => my/directory/subdir
  173. });
  174. ```
  175. <a id="sep"></a>
  176. ### Path Separator
  177. 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.
  178. ```javascript
  179. var readdir = require('readdir-enhanced');
  180. // Always use Windows path separators
  181. readdir('my/directory', {sep: '\\', deep: true}, function(err, files) {
  182. console.log(files);
  183. // => subdir1
  184. // => subdir1\file.txt
  185. // => subdir1\subdir2
  186. // => subdir1\subdir2\file.txt
  187. // => subdir1\subdir2\subdir3
  188. // => subdir1\subdir2\subdir3\file.txt
  189. });
  190. ```
  191. <a id="fs"></a>
  192. ### Custom FS methods
  193. 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`.
  194. ```javascript
  195. var readdir = require('readdir-enhanced');
  196. function myCustomReaddirMethod(dir, callback) {
  197. callback(null, ['__myFile.txt']);
  198. }
  199. var options = {
  200. fs: {
  201. readdir: myCustomReaddirMethod
  202. }
  203. };
  204. readdir('my/directory', options, function(err, files) {
  205. console.log(files);
  206. // => __myFile.txt
  207. });
  208. ```
  209. <a id="stats"></a>
  210. Get `fs.Stats` objects instead of strings
  211. ------------------------
  212. 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.
  213. > **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)).
  214. 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.
  215. ```javascript
  216. var readdir = require('readdir-enhanced');
  217. // Synchronous API
  218. var stats = readdir.sync.stat('my/directory');
  219. var stats = readdir.readdirSyncStat('my/directory');
  220. // Async API
  221. readdir.async.stat('my/directory', function(err, stats) { ... });
  222. readdir.readdirAsyncStat('my/directory', function(err, stats) { ... });
  223. // Streaming API
  224. readdir.stream.stat('my/directory')
  225. .on('data', function(stat) { ... })
  226. .on('file', function(stat) { ... })
  227. .on('directory', function(stat) { ... })
  228. .on('symlink', function(stat) { ... });
  229. readdir.readdirStreamStat('my/directory')
  230. .on('data', function(stat) { ... })
  231. .on('file', function(stat) { ... })
  232. .on('directory', function(stat) { ... })
  233. .on('symlink', function(stat) { ... });
  234. ```
  235. <a id="backward-compatible"></a>
  236. Backward Compatible
  237. --------------------
  238. `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.
  239. ```javascript
  240. var readdir = require('readdir-enhanced');
  241. var readdirSync = readdir.sync;
  242. // Use it just like Node's built-in fs.readdir function
  243. readdir('my/directory', function(err, files) { ... });
  244. // Use it just like Node's built-in fs.readdirSync function
  245. var files = readdirSync('my/directory');
  246. ```
  247. Contributing
  248. --------------------------
  249. 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).
  250. #### Building
  251. To build the project locally on your computer:
  252. 1. __Clone this repo__<br>
  253. `git clone https://github.com/bigstickcarpet/readdir-enhanced.git`
  254. 2. __Install dependencies__<br>
  255. `npm install`
  256. 3. __Run the tests__<br>
  257. `npm test`
  258. License
  259. --------------------------
  260. `readdir-enhanced` is 100% free and open-source, under the [MIT license](LICENSE). Use it however you want.