[d24f17c] | 1 | node-klaw-sync
|
---|
| 2 | ==============
|
---|
| 3 |
|
---|
| 4 | [](https://www.npmjs.com/package/klaw-sync)
|
---|
| 5 | [](https://travis-ci.org/manidlou/node-klaw-sync)
|
---|
| 6 | [](https://ci.appveyor.com/project/manidlou/node-klaw-sync/branch/master)
|
---|
| 7 | [](https://standardjs.com)
|
---|
| 8 | [](https://snyk.io/test/npm/klaw-sync)
|
---|
| 9 |
|
---|
| 10 | `klaw-sync` is a Node.js recursive and fast file system walker, which is the synchronous counterpart of [klaw](https://github.com/jprichardson/node-klaw). It lists all files and directories inside a directory recursively and returns an array of objects that each object has two properties: `path` and `stats`. `path` is the full path of the file or directory and `stats` is an instance of [fs.Stats](https://nodejs.org/api/fs.html#fs_class_fs_stats).
|
---|
| 11 |
|
---|
| 12 | Install
|
---|
| 13 | -------
|
---|
| 14 |
|
---|
| 15 | npm i klaw-sync
|
---|
| 16 |
|
---|
| 17 | Usage
|
---|
| 18 | -----
|
---|
| 19 |
|
---|
| 20 | ### klawSync(directory[, options])
|
---|
| 21 |
|
---|
| 22 | - `directory` `<String>`
|
---|
| 23 | - `options` `<Object>` (optional) _all options are `false` by default_
|
---|
| 24 | - `nodir` `<Boolean>`
|
---|
| 25 | - return only files (ignore directories).
|
---|
| 26 | - `nofile` `<Boolean>`
|
---|
| 27 | - return only directories (ignore files).
|
---|
| 28 | - `depthLimit`: `<Number>`
|
---|
| 29 | - the number of times to recurse before stopping. `-1` for unlimited.
|
---|
| 30 | - `fs`: `<Object>`
|
---|
| 31 | - custom `fs`, useful when mocking `fs` object.
|
---|
| 32 | - `filter` `<Function>`
|
---|
| 33 | - function that gets one argument `fn({path: '', stats: {}})` and returns true to include or false to exclude the item.
|
---|
| 34 | - `traverseAll` `<Boolean>`
|
---|
| 35 | - traverse all subdirectories, regardless of `filter` option. (When set to `true`, `traverseAll` produces similar behavior to the default behavior prior to v4.0.0. The current default of `traverseAll: false` is equivalent to the old `noRecurseOnFailedFilter: true`).
|
---|
| 36 | - **Return:** `<Array<Object>>` `[{path: '', stats: {}}]`
|
---|
| 37 |
|
---|
| 38 | Examples
|
---|
| 39 | --------
|
---|
| 40 |
|
---|
| 41 | ```js
|
---|
| 42 | const klawSync = require('klaw-sync')
|
---|
| 43 |
|
---|
| 44 | const paths = klawSync('/some/dir')
|
---|
| 45 | // paths = [{path: '/some/dir/dir1', stats: {}}, {path: '/some/dir/file1', stats: {}}]
|
---|
| 46 | ```
|
---|
| 47 |
|
---|
| 48 | _**catch error**_
|
---|
| 49 |
|
---|
| 50 | ```js
|
---|
| 51 | const klawSync = require('klaw-sync')
|
---|
| 52 |
|
---|
| 53 | let paths
|
---|
| 54 | try {
|
---|
| 55 | paths = klawSync('/some/dir')
|
---|
| 56 | } catch (er) {
|
---|
| 57 | console.error(er)
|
---|
| 58 | }
|
---|
| 59 | console.dir(paths)
|
---|
| 60 | ```
|
---|
| 61 |
|
---|
| 62 | _**files only**_
|
---|
| 63 |
|
---|
| 64 | ```js
|
---|
| 65 | const klawSync = require('klaw-sync')
|
---|
| 66 |
|
---|
| 67 | const files = klawSync('/some/dir', {nodir: true})
|
---|
| 68 | // files = [{path: '/some/dir/file1', stats: {}}, {path: '/some/dir/file2', stats: {}}]
|
---|
| 69 | ```
|
---|
| 70 |
|
---|
| 71 | _**directories only**_
|
---|
| 72 |
|
---|
| 73 | ```js
|
---|
| 74 | const klawSync = require('klaw-sync')
|
---|
| 75 |
|
---|
| 76 | const dirs = klawSync('/some/dir', {nofile: true})
|
---|
| 77 | // dirs = [{path: '/some/dir/dir1', stats: {}}, {path: '/some/dir/dir2', stats: {}}]
|
---|
| 78 | ```
|
---|
| 79 |
|
---|
| 80 | _**ignore hidden directories**_
|
---|
| 81 |
|
---|
| 82 |
|
---|
| 83 | ```js
|
---|
| 84 | const path = require('path')
|
---|
| 85 | const klawSync = require('klaw-sync')
|
---|
| 86 |
|
---|
| 87 | const filterFn = item => {
|
---|
| 88 | const basename = path.basename(item.path)
|
---|
| 89 | return basename === '.' || basename[0] !== '.'
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | const paths = klawSync('/some/dir', { filter: filterFn})
|
---|
| 93 | ```
|
---|
| 94 |
|
---|
| 95 | _**filter based on stats**_
|
---|
| 96 |
|
---|
| 97 | Here `traverseAll` option is required since we still want to read all directories even if they don't pass the `filter` function, to see if their contents do pass the `filter` function.
|
---|
| 98 |
|
---|
| 99 | ```js
|
---|
| 100 | const klawSync = require('klaw-sync')
|
---|
| 101 |
|
---|
| 102 | const refTime = new Date(2017, 3, 24).getTime()
|
---|
| 103 | const filterFn = item => item.stats.mtime.getTime() > refTime
|
---|
| 104 |
|
---|
| 105 | const paths = klawSync('/some/dir', { filter: filterFn })
|
---|
| 106 | ```
|
---|
| 107 |
|
---|
| 108 | Run tests
|
---|
| 109 | ---------
|
---|
| 110 |
|
---|
| 111 | lint: `npm run lint`
|
---|
| 112 |
|
---|
| 113 | unit test: `npm run unit`
|
---|
| 114 |
|
---|
| 115 | lint & unit: `npm test`
|
---|
| 116 |
|
---|
| 117 | benchmark: `npm run benchmark`
|
---|
| 118 |
|
---|
| 119 | Performance compare to other similar modules
|
---|
| 120 | -----------------------------------------------
|
---|
| 121 |
|
---|
| 122 | Running some [benchmark](https://github.com/bestiejs/benchmark.js) tests on these modules:
|
---|
| 123 |
|
---|
| 124 | - `klaw-sync`
|
---|
| 125 | - [walk-sync](https://github.com/joliss/node-walk-sync)
|
---|
| 126 |
|
---|
| 127 | (as of Jan 25, 2017) `klaw-sync` is the fastest module!
|
---|
| 128 |
|
---|
| 129 | ##### results (tested on Ubuntu 18.04, Intel(R) Core(TM) i7-2630QM CPU @ 2.00GHz, 8 CPUs, 8g RAM, node v10.9.0)
|
---|
| 130 |
|
---|
| 131 | ```bash
|
---|
| 132 | Running benchmark tests..
|
---|
| 133 |
|
---|
| 134 | root dir length: 1110
|
---|
| 135 | walk-sync x 139 ops/sec ±2.48% (76 runs sampled)
|
---|
| 136 | klaw-sync x 163 ops/sec ±1.20% (80 runs sampled)
|
---|
| 137 | Fastest is klaw-sync
|
---|
| 138 |
|
---|
| 139 | root dir length: 11110
|
---|
| 140 | walk-sync x 13.23 ops/sec ±1.10% (37 runs sampled)
|
---|
| 141 | klaw-sync x 15.10 ops/sec ±1.06% (41 runs sampled)
|
---|
| 142 | Fastest is klaw-sync
|
---|
| 143 |
|
---|
| 144 | root dir length: 111110
|
---|
| 145 | walk-sync x 1.17 ops/sec ±2.06% (7 runs sampled)
|
---|
| 146 | klaw-sync x 1.25 ops/sec ±2.10% (8 runs sampled)
|
---|
| 147 | Fastest is klaw-sync
|
---|
| 148 | ```
|
---|
| 149 |
|
---|
| 150 | Credit
|
---|
| 151 | ------
|
---|
| 152 |
|
---|
| 153 | Special thanks to:
|
---|
| 154 |
|
---|
| 155 | - [agnivade](https://github.com/agnivade)
|
---|
| 156 | - [jprichardson](https://github.com/jprichardson)
|
---|
| 157 | - [RyanZim](https://github.com/RyanZim)
|
---|
| 158 |
|
---|
| 159 | for their contribution and support.
|
---|
| 160 |
|
---|
| 161 | License
|
---|
| 162 | -------
|
---|
| 163 |
|
---|
| 164 | Licensed under MIT
|
---|