├── .github └── workflows │ └── ci.yml ├── .gitignore ├── .npmrc ├── CHANGELOG.md ├── LICENSE ├── README.md ├── package.json ├── src └── index.js └── tests ├── _test.js ├── fixtures.json ├── fixtures_links.json ├── fixtures_path-sorter.json ├── walk.test.js ├── walk_depth-limit.test.js ├── walk_filter.test.js ├── walk_links.test.js ├── walk_path-sorter.test.js └── walk_read-dir-error.test.js /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: Node.js CI 2 | on: 3 | push: 4 | branches: master 5 | pull_request: 6 | 7 | jobs: 8 | test: 9 | strategy: 10 | matrix: 11 | node: [14.x, 16.x, 18.x, latest] 12 | os: [ubuntu-latest, windows-latest] 13 | runs-on: ${{ matrix.os }} 14 | steps: 15 | - uses: actions/checkout@v3 16 | - name: Use Node.js ${{ matrix.node }} 17 | uses: actions/setup-node@v3 18 | with: 19 | node-version: ${{ matrix.node }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | 4.1.0 / 2022-12-30 2 | ------------------ 3 | 4 | - Add support for passing `URL` file paths ([#46](https://github.com/jprichardson/node-klaw/issues/46), [#47](https://github.com/jprichardson/node-klaw/pull/47)) 5 | 6 | 4.0.1 / 2021-09-18 7 | ------------------ 8 | 9 | - Don't publish unnecessary files 10 | 11 | 4.0.0 / 2021-09-18 12 | ------------------ 13 | 14 | - **BREAKING:** Require Node 14.14.0+ ([#43](https://github.com/jprichardson/node-klaw/pull/43)) 15 | - **BREAKING:** Remove graceful-fs dependency; install it manually and pass it as `fs` option if needed ([#43](https://github.com/jprichardson/node-klaw/pull/43)) 16 | - Additional documentation examples ([#34](https://github.com/jprichardson/node-klaw/pull/34)) 17 | 18 | 3.0.0 / 2018-08-01 19 | ------------------ 20 | 21 | - **BREAKING:** Follow symlinks by default (use the new `preserveSymlinks` option to get the old behavior) [#29](https://github.com/jprichardson/node-klaw/pull/29) 22 | - **BREAKING:** Drop Node v4 support 23 | 24 | 2.1.1 / 2017-11-18 25 | ------------------ 26 | 27 | - Performance optimization [#27](https://github.com/jprichardson/node-klaw/pull/27) 28 | 29 | 2.1.0 / 2017-08-10 30 | ------------------ 31 | 32 | ### Added 33 | 34 | - Added `depthLimit` option to limit how deep to recurse into folders. [#25](https://github.com/jprichardson/node-klaw/pull/25) 35 | 36 | 2.0.0 / 2017-06-23 37 | ------------------ 38 | 39 | ### Changed 40 | 41 | - `graceful-fs` is now a regular dependency, and is always loaded. This should speed up `require` time 42 | - Dropped support for Node 0.10 & 0.12 and io.js 43 | 44 | 1.3.1 / 2016-10-25 45 | ------------------ 46 | ### Added 47 | - `graceful-fs` added as an `optionalDependencies`. Thanks [ryanzim]! 48 | 49 | 1.3.0 / 2016-06-09 50 | ------------------ 51 | ### Added 52 | - `filter` option to pre-filter and not walk directories. 53 | 54 | 1.2.0 / 2016-04-16 55 | ------------------ 56 | - added support for custom `fs` implementation. Useful for https://github.com/tschaub/mock-fs 57 | 58 | 1.1.3 / 2015-12-23 59 | ------------------ 60 | - bugfix: if `readdir` error, got hung up. See: https://github.com/jprichardson/node-klaw/issues/1 61 | 62 | 1.1.2 / 2015-11-12 63 | ------------------ 64 | - assert that param `dir` is a `string` 65 | 66 | 1.1.1 / 2015-10-25 67 | ------------------ 68 | - bug fix, options not being passed 69 | 70 | 1.1.0 / 2015-10-25 71 | ------------------ 72 | - added `queueMethod` and `pathSorter` to `options` to affect searching strategy. 73 | 74 | 1.0.0 / 2015-10-25 75 | ------------------ 76 | - removed unused `filter` param 77 | - bugfix: always set `streamOptions` to `objectMode` 78 | - simplified, converted from push mode (streams 1) to proper pull mode (streams 3) 79 | 80 | 0.1.0 / 2015-10-25 81 | ------------------ 82 | - initial release 83 | 84 | 85 | [ryanzim]: https://github.com/ryanzim 86 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | (The MIT License) 2 | 3 | Copyright (c) 2015-2016 JP Richardson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files 6 | (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, 7 | merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 11 | 12 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 13 | WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS 14 | OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 15 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Node.js - klaw 2 | ============== 3 | 4 | JavaScript Standard Style 5 | 6 | A Node.js file system walker extracted from [fs-extra](https://github.com/jprichardson/node-fs-extra). 7 | 8 | [![npm Package](https://img.shields.io/npm/v/klaw.svg?style=flat-square)](https://www.npmjs.org/package/klaw) 9 | [![build status](https://api.travis-ci.org/jprichardson/node-klaw.svg)](http://travis-ci.org/jprichardson/node-klaw) 10 | [![windows build status](https://ci.appveyor.com/api/projects/status/github/jprichardson/node-klaw?branch=master&svg=true)](https://ci.appveyor.com/project/jprichardson/node-klaw/branch/master) 11 | 12 | Install 13 | ------- 14 | 15 | npm i --save klaw 16 | 17 | If you're using Typescript, we've got [types](https://github.com/DefinitelyTyped/DefinitelyTyped/pull/11492/files): 18 | 19 | npm i --save-dev @types/klaw 20 | 21 | 22 | Name 23 | ---- 24 | 25 | `klaw` is `walk` backwards :p 26 | 27 | 28 | Sync 29 | ---- 30 | 31 | If you need the same functionality but synchronous, you can use [klaw-sync](https://github.com/manidlou/node-klaw-sync). 32 | 33 | 34 | Usage 35 | ----- 36 | 37 | ### klaw(directory, [options]) 38 | 39 | Returns a [Readable stream](https://nodejs.org/api/stream.html#stream_class_stream_readable) that iterates 40 | through every file and directory starting with `dir` as the root. Every `read()` or `data` event 41 | returns an object with two properties: `path` and `stats`. `path` is the full path of the file and 42 | `stats` is an instance of [fs.Stats](https://nodejs.org/api/fs.html#fs_class_fs_stats). 43 | 44 | - `directory`: The directory to recursively walk. Type `string` or file `URL`. 45 | - `options`: [Readable stream options](https://nodejs.org/api/stream.html#stream_new_stream_readable_options) and 46 | the following: 47 | - `queueMethod` (`string`, default: `'shift'`): Either `'shift'` or `'pop'`. On `readdir()` array, call either `shift()` or `pop()`. 48 | - `pathSorter` (`function`, default: `undefined`): Sorting [function for Arrays](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort). 49 | - `fs` (`object`, default: [`graceful-fs`](https://github.com/isaacs/node-graceful-fs)): Use this to hook into the `fs` methods or to use [`mock-fs`](https://github.com/tschaub/mock-fs) 50 | - `filter` (`function`, default: `undefined`): Filtering [function for Arrays](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) 51 | - `depthLimit` (`number`, default: `undefined`): The number of times to recurse before stopping. -1 for unlimited. 52 | - `preserveSymlinks` (`boolean`, default: `false`): Whether symlinks should be followed or treated as items themselves. If true, symlinks will be returned as items in their own right. If false, the linked item will be returned and potentially recursed into, in its stead. 53 | 54 | **Streams 1 (push) example:** 55 | 56 | ```js 57 | const klaw = require('klaw') 58 | 59 | const items = [] // files, directories, symlinks, etc 60 | klaw('/some/dir') 61 | .on('data', item => items.push(item.path)) 62 | .on('end', () => console.dir(items)) // => [ ... array of files] 63 | ``` 64 | 65 | **Streams 2 & 3 (pull) example:** 66 | 67 | ```js 68 | const klaw = require('klaw') 69 | 70 | const items = [] // files, directories, symlinks, etc 71 | klaw('/some/dir') 72 | .on('readable', function () { 73 | let item 74 | while ((item = this.read())) { 75 | items.push(item.path) 76 | } 77 | }) 78 | .on('end', () => console.dir(items)) // => [ ... array of files] 79 | ``` 80 | 81 | **```for-await-of``` example:** 82 | 83 | ```js 84 | for await (const file of klaw('/some/dir')) { 85 | console.log(file) 86 | } 87 | ``` 88 | 89 | ### Error Handling 90 | 91 | Listen for the `error` event. 92 | 93 | Example: 94 | 95 | ```js 96 | const klaw = require('klaw') 97 | 98 | klaw('/some/dir') 99 | .on('readable', function () { 100 | let item 101 | while ((item = this.read())) { 102 | // do something with the file 103 | } 104 | }) 105 | .on('error', (err, item) => { 106 | console.log(err.message) 107 | console.log(item.path) // the file the error occurred on 108 | }) 109 | .on('end', () => console.dir(items)) // => [ ... array of files] 110 | ``` 111 | 112 | 113 | ### Aggregation / Filtering / Executing Actions (Through Streams) 114 | 115 | On many occasions you may want to filter files based upon size, extension, etc. 116 | Or you may want to aggregate stats on certain file types. Or maybe you want to 117 | perform an action on certain file types. 118 | 119 | You should use the module [`through2`](https://www.npmjs.com/package/through2) to easily 120 | accomplish this. 121 | 122 | Install `through2`: 123 | 124 | npm i --save through2 125 | 126 | 127 | **Example (skipping directories):** 128 | 129 | ```js 130 | const klaw = require('klaw') 131 | const through2 = require('through2') 132 | 133 | const excludeDirFilter = through2.obj(function (item, enc, next) { 134 | if (!item.stats.isDirectory()) this.push(item) 135 | next() 136 | }) 137 | 138 | const items = [] // files, directories, symlinks, etc 139 | klaw('/some/dir') 140 | .pipe(excludeDirFilter) 141 | .on('data', item => items.push(item.path)) 142 | .on('end', () => console.dir(items)) // => [ ... array of files without directories] 143 | ``` 144 | 145 | **Example (ignore hidden directories):** 146 | 147 | ```js 148 | const klaw = require('klaw') 149 | const path = require('path') 150 | 151 | const filterFunc = item => { 152 | const basename = path.basename(item) 153 | return basename === '.' || basename[0] !== '.' 154 | } 155 | 156 | klaw('/some/dir', { filter: filterFunc }) 157 | .on('data', item => { 158 | // only items of none hidden folders will reach here 159 | }) 160 | ``` 161 | 162 | **Example (totaling size of PNG files):** 163 | 164 | ```js 165 | const klaw = require('klaw') 166 | const path = require('path') 167 | const through2 = require('through2') 168 | 169 | let totalPngsInBytes = 0 170 | const aggregatePngSize = through2.obj(function (item, enc, next) { 171 | if (path.extname(item.path) === '.png') { 172 | totalPngsInBytes += item.stats.size 173 | } 174 | this.push(item) 175 | next() 176 | }) 177 | 178 | klaw('/some/dir') 179 | .pipe(aggregatePngSize) 180 | .on('data', item => items.push(item.path)) 181 | .on('end', () => console.dir(totalPngsInBytes)) // => total of all pngs (bytes) 182 | ``` 183 | 184 | 185 | **Example (deleting all .tmp files):** 186 | 187 | ```js 188 | const fs = require('fs') 189 | const klaw = require('klaw') 190 | const through2 = require('through2') 191 | 192 | const deleteAction = through2.obj(function (item, enc, next) { 193 | this.push(item) 194 | 195 | if (path.extname(item.path) === '.tmp') { 196 | item.deleted = true 197 | fs.unlink(item.path, next) 198 | } else { 199 | item.deleted = false 200 | next() 201 | } 202 | }) 203 | 204 | const deletedFiles = [] 205 | klaw('/some/dir') 206 | .pipe(deleteAction) 207 | .on('data', item => { 208 | if (!item.deleted) return 209 | deletedFiles.push(item.path) 210 | }) 211 | .on('end', () => console.dir(deletedFiles)) // => all deleted files 212 | ``` 213 | 214 | You can even chain a bunch of these filters and aggregators together. By using 215 | multiple pipes. 216 | 217 | **Example (using multiple filters / aggregators):** 218 | 219 | ```js 220 | klaw('/some/dir') 221 | .pipe(filterCertainFiles) 222 | .pipe(deleteSomeOtherFiles) 223 | .on('end', () => console.log('all done!')) 224 | ``` 225 | 226 | **Example passing (piping) through errors:** 227 | 228 | Node.js does not `pipe()` errors. This means that the error on one stream, like 229 | `klaw` will not pipe through to the next. If you want to do this, do the following: 230 | 231 | ```js 232 | const klaw = require('klaw') 233 | const through2 = require('through2') 234 | 235 | const excludeDirFilter = through2.obj(function (item, enc, next) { 236 | if (!item.stats.isDirectory()) this.push(item) 237 | next() 238 | }) 239 | 240 | const items = [] // files, directories, symlinks, etc 241 | klaw('/some/dir') 242 | .on('error', err => excludeDirFilter.emit('error', err)) // forward the error on 243 | .pipe(excludeDirFilter) 244 | .on('data', item => items.push(item.path)) 245 | .on('end', () => console.dir(items)) // => [ ... array of files without directories] 246 | ``` 247 | 248 | 249 | ### Searching Strategy 250 | 251 | Pass in options for `queueMethod`, `pathSorter`, and `depthLimit` to affect how the file system 252 | is recursively iterated. See the code for more details, it's less than 50 lines :) 253 | 254 | 255 | 256 | License 257 | ------- 258 | 259 | MIT 260 | 261 | Copyright (c) 2015 [JP Richardson](https://github.com/jprichardson) 262 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "klaw", 3 | "version": "4.1.0", 4 | "description": "File system walker with Readable stream interface.", 5 | "main": "./src/index.js", 6 | "scripts": { 7 | "lint": "standard", 8 | "test": "npm run lint && npm run unit", 9 | "unit": "tape tests/**/*.js | tap-spec" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "git+https://github.com/jprichardson/node-klaw.git" 14 | }, 15 | "keywords": [ 16 | "walk", 17 | "walker", 18 | "fs", 19 | "readable", 20 | "streams" 21 | ], 22 | "engines": { 23 | "node": ">=14.14.0" 24 | }, 25 | "author": "JP Richardson", 26 | "license": "MIT", 27 | "files": [ 28 | "src/" 29 | ], 30 | "bugs": { 31 | "url": "https://github.com/jprichardson/node-klaw/issues" 32 | }, 33 | "homepage": "https://github.com/jprichardson/node-klaw#readme", 34 | "devDependencies": { 35 | "standard": "^16.0.3", 36 | "tap-spec": "^5.0.0", 37 | "tape": "^5.3.1" 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | const { strictEqual } = require('assert') 2 | const path = require('path') 3 | const fs = require('fs') 4 | const { Readable } = require('stream') 5 | const { fileURLToPath } = require('url') 6 | 7 | class Walker extends Readable { 8 | /** 9 | * @param {string} dir 10 | * @param {Object} options 11 | */ 12 | constructor (dir, options) { 13 | if (dir instanceof URL) { 14 | dir = fileURLToPath(dir) 15 | } 16 | strictEqual(typeof dir, 'string', '`dir` parameter should be of type string or file URL. Got type: ' + typeof dir) 17 | options = { 18 | queueMethod: 'shift', 19 | pathSorter: undefined, 20 | filter: undefined, 21 | depthLimit: undefined, 22 | preserveSymlinks: false, 23 | ...options, 24 | objectMode: true 25 | } 26 | 27 | super(options) 28 | this.root = path.resolve(dir) 29 | this.paths = [this.root] 30 | this.options = options 31 | if (options.depthLimit > -1) { this.rootDepth = this.root.split(path.sep).length + 1 } 32 | this.fs = options.fs || fs 33 | } 34 | 35 | _read () { 36 | if (this.paths.length === 0) { return this.push(null) } 37 | const pathItem = this.paths[this.options.queueMethod]() 38 | 39 | const statFunction = this.options.preserveSymlinks ? this.fs.lstat : this.fs.stat 40 | 41 | statFunction(pathItem, (err, stats) => { 42 | const item = { path: pathItem, stats: stats } 43 | if (err) { return this.emit('error', err, item) } 44 | 45 | if (!stats.isDirectory() || (this.rootDepth && 46 | pathItem.split(path.sep).length - this.rootDepth >= this.options.depthLimit)) { 47 | return this.push(item) 48 | } 49 | 50 | this.fs.readdir(pathItem, (err, pathItems) => { 51 | if (err) { 52 | this.push(item) 53 | return this.emit('error', err, item) 54 | } 55 | 56 | pathItems = pathItems.map(function (part) { return path.join(pathItem, part) }) 57 | if (this.options.filter) { pathItems = pathItems.filter(this.options.filter) } 58 | if (this.options.pathSorter) { pathItems.sort(this.options.pathSorter) } 59 | // faster way to do do incremental batch array pushes 60 | this.paths.push.apply(this.paths, pathItems) 61 | 62 | this.push(item) 63 | }) 64 | }) 65 | } 66 | } 67 | 68 | /** 69 | * @param {string} root 70 | * @param {Object} [options] 71 | */ 72 | function walk (root, options) { 73 | return new Walker(root, options) 74 | } 75 | 76 | module.exports = walk 77 | -------------------------------------------------------------------------------- /tests/_test.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs') 2 | const os = require('os') 3 | const path = require('path') 4 | const tape = require('tape') 5 | 6 | // for all practical purposes, this is a beforeEach and afterEach 7 | function test (desc, testFn) { 8 | tape(desc, function (t) { 9 | const testDir = path.join(os.tmpdir(), 'klaw-tests') 10 | fs.rm(testDir, { recursive: true, force: true }, function (err) { 11 | if (err) return t.end(err) 12 | fs.mkdir(testDir, function (err) { 13 | if (err) return t.end(err) 14 | 15 | const oldEnd = t.end 16 | t.end = function () { 17 | fs.rm(testDir, { recursive: true, force: true }, function (err) { 18 | err ? oldEnd.apply(t, [err]) : oldEnd.apply(t, arguments) 19 | }) 20 | } 21 | 22 | testFn(t, testDir) 23 | }) 24 | }) 25 | }) 26 | } 27 | 28 | module.exports = test 29 | -------------------------------------------------------------------------------- /tests/fixtures.json: -------------------------------------------------------------------------------- 1 | [ 2 | "a/b/c/d.txt", 3 | "a/e.jpg", 4 | "h/i/j/k.txt", 5 | "h/i/l.txt", 6 | "h/i/m.jpg" 7 | ] 8 | -------------------------------------------------------------------------------- /tests/fixtures_links.json: -------------------------------------------------------------------------------- 1 | { 2 | "a/b.txt": { }, 3 | "b": { "type": "file", "target": "./a/b.txt" }, 4 | "c": { "type": "dir", "target": "./a" }, 5 | "d": { "type": "file", "target": "./broken" } 6 | } -------------------------------------------------------------------------------- /tests/fixtures_path-sorter.json: -------------------------------------------------------------------------------- 1 | [ 2 | "a", 3 | "b", 4 | "c" 5 | ] 6 | -------------------------------------------------------------------------------- /tests/walk.test.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs') 2 | const path = require('path') 3 | const { pathToFileURL } = require('url') 4 | const test = require('./_test') 5 | const klaw = require('../') 6 | const fixtures = require('./fixtures') 7 | 8 | test('should work w/ streams 1', function (t, testDir) { 9 | fixtures.forEach(function (f) { 10 | f = path.join(testDir, f) 11 | const dir = path.dirname(f) 12 | fs.mkdirSync(dir, { recursive: true }) 13 | fs.writeFileSync(f, path.basename(f, path.extname(f))) 14 | }) 15 | 16 | const items = [] 17 | klaw(testDir) 18 | .on('data', function (item) { 19 | items.push(item.path) 20 | }) 21 | .on('error', t.end) 22 | .on('end', function () { 23 | items.sort() 24 | let expected = ['a', 'a/b', 'a/b/c', 'a/b/c/d.txt', 'a/e.jpg', 'h', 'h/i', 'h/i/j', 25 | 'h/i/j/k.txt', 'h/i/l.txt', 'h/i/m.jpg'] 26 | expected = expected.map(function (item) { 27 | return path.join(path.join(testDir, item)) 28 | }) 29 | expected.unshift(testDir) 30 | 31 | t.same(items, expected) 32 | t.end() 33 | }) 34 | }) 35 | 36 | test('should work w/ streams 2/3', function (t, testDir) { 37 | fixtures.forEach(function (f) { 38 | f = path.join(testDir, f) 39 | const dir = path.dirname(f) 40 | fs.mkdirSync(dir, { recursive: true }) 41 | fs.writeFileSync(f, path.basename(f, path.extname(f))) 42 | }) 43 | 44 | const items = [] 45 | klaw(testDir) 46 | .on('readable', function () { 47 | let item 48 | while ((item = this.read())) { 49 | items.push(item.path) 50 | } 51 | }) 52 | .on('error', t.end) 53 | .on('end', function () { 54 | items.sort() 55 | let expected = ['a', 'a/b', 'a/b/c', 'a/b/c/d.txt', 'a/e.jpg', 'h', 'h/i', 'h/i/j', 56 | 'h/i/j/k.txt', 'h/i/l.txt', 'h/i/m.jpg'] 57 | expected = expected.map(function (item) { 58 | return path.join(path.join(testDir, item)) 59 | }) 60 | expected.unshift(testDir) 61 | 62 | t.same(items, expected) 63 | t.end() 64 | }) 65 | }) 66 | 67 | test('should work w/ file URLs', function (t, testDir) { 68 | fixtures.forEach(function (f) { 69 | f = path.join(testDir, f) 70 | const dir = path.dirname(f) 71 | fs.mkdirSync(dir, { recursive: true }) 72 | fs.writeFileSync(f, path.basename(f, path.extname(f))) 73 | }) 74 | 75 | const items = [] 76 | klaw(pathToFileURL(testDir)) 77 | .on('data', function (item) { 78 | items.push(item.path) 79 | }) 80 | .on('error', t.end) 81 | .on('end', function () { 82 | items.sort() 83 | let expected = ['a', 'a/b', 'a/b/c', 'a/b/c/d.txt', 'a/e.jpg', 'h', 'h/i', 'h/i/j', 84 | 'h/i/j/k.txt', 'h/i/l.txt', 'h/i/m.jpg'] 85 | expected = expected.map(function (item) { 86 | return path.join(path.join(testDir, item)) 87 | }) 88 | expected.unshift(testDir) 89 | 90 | t.same(items, expected) 91 | t.end() 92 | }) 93 | }) 94 | -------------------------------------------------------------------------------- /tests/walk_depth-limit.test.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs') 2 | const path = require('path') 3 | const test = require('./_test') 4 | const klaw = require('../') 5 | const fixtures = require('./fixtures.json') 6 | 7 | test('should honor depthLimit option -1', function (t, testDir) { 8 | const expected = ['a', 'a/b', 'a/b/c', 'a/b/c/d.txt', 'a/e.jpg', 'h', 'h/i', 9 | 'h/i/j', 'h/i/j/k.txt', 'h/i/l.txt', 'h/i/m.jpg'] 10 | run(t, testDir, -1, expected) 11 | }) 12 | 13 | test('should honor depthLimit option 0', function (t, testDir) { 14 | const expected = ['a', 'h'] 15 | run(t, testDir, 0, expected) 16 | }) 17 | 18 | test('should honor depthLimit option 1', function (t, testDir) { 19 | const expected = ['a', 'a/b', 'a/e.jpg', 'h', 'h/i'] 20 | run(t, testDir, 1, expected) 21 | }) 22 | 23 | test('should honor depthLimit option 2', function (t, testDir) { 24 | const expected = ['a', 'a/b', 'a/b/c', 'a/e.jpg', 'h', 'h/i', 'h/i/j', 25 | 'h/i/l.txt', 'h/i/m.jpg'] 26 | run(t, testDir, 2, expected) 27 | }) 28 | 29 | test('should honor depthLimit option 3', function (t, testDir) { 30 | const expected = ['a', 'a/b', 'a/b/c', 'a/b/c/d.txt', 'a/e.jpg', 'h', 'h/i', 31 | 'h/i/j', 'h/i/j/k.txt', 'h/i/l.txt', 'h/i/m.jpg'] 32 | run(t, testDir, 3, expected) 33 | }) 34 | 35 | function run (t, testDir, depthLimit, expected) { 36 | fixtures.forEach(function (f) { 37 | f = path.join(testDir, f) 38 | const dir = path.dirname(f) 39 | fs.mkdirSync(dir, { recursive: true }) 40 | fs.writeFileSync(f, path.basename(f, path.extname(f))) 41 | }) 42 | 43 | const items = [] 44 | klaw(testDir, { depthLimit: depthLimit }) 45 | .on('data', function (item) { 46 | items.push(item.path) 47 | }) 48 | .on('error', t.end) 49 | .on('end', function () { 50 | items.sort() 51 | expected = expected.map(function (item) { 52 | return path.join(path.join(testDir, item)) 53 | }) 54 | expected.unshift(testDir) 55 | 56 | t.same(items, expected) 57 | t.end() 58 | }) 59 | } 60 | -------------------------------------------------------------------------------- /tests/walk_filter.test.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs') 2 | const path = require('path') 3 | const test = require('./_test') 4 | const klaw = require('../') 5 | const fixtures = require('./fixtures') 6 | 7 | test('should not fire event on filtered items', function (t, testDir) { 8 | fixtures.forEach(function (f) { 9 | f = path.join(testDir, f) 10 | const dir = path.dirname(f) 11 | fs.mkdirSync(dir, { recursive: true }) 12 | fs.writeFileSync(f, path.basename(f, path.extname(f))) 13 | }) 14 | 15 | const items = [] 16 | const filter = function (filepath) { 17 | return path.basename(filepath) !== 'a' 18 | } 19 | 20 | klaw(testDir, { filter: filter }) 21 | .on('data', function (item) { 22 | if (fs.lstatSync(item.path).isFile()) items.push(item.path) 23 | }) 24 | .on('error', t.end) 25 | .on('end', function () { 26 | let expected = ['c', 'b', 'a'] 27 | expected = expected.map(function (item) { 28 | return path.join(testDir, item) 29 | }) 30 | expected.unshift(testDir) 31 | 32 | t.ok(items.length < fixtures.length, 'we should see less items due to filter') 33 | t.end() 34 | }) 35 | }) 36 | -------------------------------------------------------------------------------- /tests/walk_links.test.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs') 2 | const path = require('path') 3 | const test = require('./_test') 4 | const klaw = require('../') 5 | const fixtures = require('./fixtures_links.json') 6 | 7 | function loadLinkFixtures (testDir) { 8 | Object.keys(fixtures).forEach(function (f) { 9 | const link = fixtures[f] 10 | f = path.join(testDir, f) 11 | 12 | const dir = path.dirname(f) 13 | fs.mkdirSync(dir, { recursive: true }) 14 | 15 | if (link.target) { 16 | const realTarget = path.resolve(testDir, link.target) 17 | let missing 18 | if (!fs.existsSync(realTarget)) { 19 | missing = true 20 | fs.writeFileSync(realTarget, '') 21 | } 22 | fs.symlinkSync(link.target, f, link.type) 23 | if (missing) { 24 | fs.unlinkSync(realTarget) 25 | } 26 | } else { 27 | fs.writeFileSync(f, path.basename(f, path.extname(f))) 28 | } 29 | }) 30 | } 31 | 32 | test('should follow links by default', function (t, testDir) { 33 | loadLinkFixtures(testDir) 34 | 35 | const items = [] 36 | klaw(testDir) 37 | .on('data', function (item) { 38 | items.push(item.path) 39 | }) 40 | .on('error', t.end) 41 | .on('end', function () { 42 | items.sort() 43 | let expected = ['a', 'a/b.txt', 'b', 'c', 'c/b.txt'] 44 | expected = expected.map(function (item) { 45 | return path.join(path.join(testDir, item)) 46 | }) 47 | expected.unshift(testDir) 48 | 49 | t.same(items, expected) 50 | t.end() 51 | }) 52 | }) 53 | 54 | test('should not follow links if requested', function (t, testDir) { 55 | loadLinkFixtures(testDir) 56 | 57 | const items = [] 58 | klaw(testDir, { preserveSymlinks: true }) 59 | .on('data', function (item) { 60 | items.push(item.path) 61 | }) 62 | .on('error', t.end) 63 | .on('end', function () { 64 | items.sort() 65 | let expected = ['a', 'a/b.txt', 'b', 'c', 'd'] 66 | expected = expected.map(function (item) { 67 | return path.join(path.join(testDir, item)) 68 | }) 69 | expected.unshift(testDir) 70 | 71 | t.same(items, expected) 72 | t.end() 73 | }) 74 | }) 75 | -------------------------------------------------------------------------------- /tests/walk_path-sorter.test.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs') 2 | const path = require('path') 3 | const test = require('./_test') 4 | const klaw = require('../') 5 | const fixtures = require('./fixtures_path-sorter.json') 6 | 7 | const stringCompare = function (a, b) { 8 | if (a < b) return -1 9 | else if (a > b) return 1 10 | else return 0 11 | } 12 | 13 | test('should sort in reverse order [z -> a]', function (t, testDir) { 14 | fixtures.forEach(function (f) { 15 | f = path.join(testDir, f) 16 | const dir = path.dirname(f) 17 | fs.mkdirSync(dir, { recursive: true }) 18 | fs.writeFileSync(f, path.basename(f, path.extname(f))) 19 | }) 20 | 21 | const items = [] 22 | const pathSorter = function (a, b) { return stringCompare(b, a) } 23 | klaw(testDir, { pathSorter: pathSorter }) 24 | .on('data', function (item) { 25 | items.push(item.path) 26 | }) 27 | .on('error', t.end) 28 | .on('end', function () { 29 | let expected = ['c', 'b', 'a'] 30 | expected = expected.map(function (item) { 31 | return path.join(testDir, item) 32 | }) 33 | expected.unshift(testDir) 34 | 35 | t.same(items, expected) 36 | t.end() 37 | }) 38 | }) 39 | 40 | test('should sort in order [a -> z]', function (t, testDir) { 41 | fixtures.forEach(function (f) { 42 | f = path.join(testDir, f) 43 | const dir = path.dirname(f) 44 | fs.mkdirSync(dir, { recursive: true }) 45 | fs.writeFileSync(f, path.basename(f, path.extname(f))) 46 | }) 47 | 48 | const items = [] 49 | const pathSorter = function (a, b) { return stringCompare(a, b) } 50 | klaw(testDir, { pathSorter: pathSorter }) 51 | .on('data', function (item) { 52 | items.push(item.path) 53 | }) 54 | .on('error', t.end) 55 | .on('end', function () { 56 | let expected = ['a', 'b', 'c'] 57 | expected = expected.map(function (item) { 58 | return path.join(testDir, item) 59 | }) 60 | expected.unshift(testDir) 61 | 62 | t.same(items, expected) 63 | t.end() 64 | }) 65 | }) 66 | -------------------------------------------------------------------------------- /tests/walk_read-dir-error.test.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs') 2 | const path = require('path') 3 | const test = require('./_test') 4 | const klaw = require('../') 5 | 6 | test('walk directory, if error on readdir, at least end', function (t, testDir) { 7 | // simulate directory issue 8 | const unreadableDir = path.join(testDir, 'unreadable-dir') 9 | 10 | fs.mkdirSync(unreadableDir, { recursive: true }) 11 | fs.chmodSync(unreadableDir, '0222') 12 | 13 | // not able to simulate on windows 14 | if (process.platform === 'win32') return t.end() 15 | 16 | t.plan(2) 17 | const items = [] 18 | klaw(testDir) 19 | .on('data', function (item) { 20 | items.push(item.path) 21 | }) 22 | .on('error', function (err) { 23 | t.true(err, 'caught error') 24 | }) 25 | .on('end', function () { 26 | t.true(true, 'be sure we end') 27 | t.end() 28 | }) 29 | }) 30 | --------------------------------------------------------------------------------