source: node_modules/klaw-sync/README.md

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 4.9 KB
RevLine 
[d24f17c]1node-klaw-sync
2==============
3
4[![npm Package](https://img.shields.io/npm/v/klaw-sync.svg?style=flat-square)](https://www.npmjs.com/package/klaw-sync)
5[![Build Status](https://travis-ci.org/manidlou/node-klaw-sync.svg?branch=master)](https://travis-ci.org/manidlou/node-klaw-sync)
6[![windows Build status](https://ci.appveyor.com/api/projects/status/braios34k6qw4h5p/branch/master?svg=true)](https://ci.appveyor.com/project/manidlou/node-klaw-sync/branch/master)
7[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg?style=flat-square)](https://standardjs.com)
8[![Known Vulnerabilities](https://snyk.io/test/npm/klaw-sync/badge.svg?style=flat-square)](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
12Install
13-------
14
15 npm i klaw-sync
16
17Usage
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
38Examples
39--------
40
41```js
42const klawSync = require('klaw-sync')
43
44const paths = klawSync('/some/dir')
45// paths = [{path: '/some/dir/dir1', stats: {}}, {path: '/some/dir/file1', stats: {}}]
46```
47
48_**catch error**_
49
50```js
51const klawSync = require('klaw-sync')
52
53let paths
54try {
55 paths = klawSync('/some/dir')
56} catch (er) {
57 console.error(er)
58}
59console.dir(paths)
60```
61
62_**files only**_
63
64```js
65const klawSync = require('klaw-sync')
66
67const 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
74const klawSync = require('klaw-sync')
75
76const 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
84const path = require('path')
85const klawSync = require('klaw-sync')
86
87const filterFn = item => {
88 const basename = path.basename(item.path)
89 return basename === '.' || basename[0] !== '.'
90}
91
92const paths = klawSync('/some/dir', { filter: filterFn})
93```
94
95_**filter based on stats**_
96
97Here `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
100const klawSync = require('klaw-sync')
101
102const refTime = new Date(2017, 3, 24).getTime()
103const filterFn = item => item.stats.mtime.getTime() > refTime
104
105const paths = klawSync('/some/dir', { filter: filterFn })
106```
107
108Run tests
109---------
110
111lint: `npm run lint`
112
113unit test: `npm run unit`
114
115lint & unit: `npm test`
116
117benchmark: `npm run benchmark`
118
119Performance compare to other similar modules
120-----------------------------------------------
121
122Running 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
132Running benchmark tests..
133
134root dir length: 1110
135walk-sync x 139 ops/sec ±2.48% (76 runs sampled)
136klaw-sync x 163 ops/sec ±1.20% (80 runs sampled)
137Fastest is klaw-sync
138
139root dir length: 11110
140walk-sync x 13.23 ops/sec ±1.10% (37 runs sampled)
141klaw-sync x 15.10 ops/sec ±1.06% (41 runs sampled)
142Fastest is klaw-sync
143
144root dir length: 111110
145walk-sync x 1.17 ops/sec ±2.06% (7 runs sampled)
146klaw-sync x 1.25 ops/sec ±2.10% (8 runs sampled)
147Fastest is klaw-sync
148```
149
150Credit
151------
152
153Special thanks to:
154
155- [agnivade](https://github.com/agnivade)
156- [jprichardson](https://github.com/jprichardson)
157- [RyanZim](https://github.com/RyanZim)
158
159for their contribution and support.
160
161License
162-------
163
164Licensed under MIT
Note: See TracBrowser for help on using the repository browser.