Codebase list node-klaw / 4f2c10f
Update upstream source from tag 'upstream/4.0.1' Update to upstream version '4.0.1' with Debian dir bf83700765e5d5a81c320920552f5249778f4a42 Yadd 2 years ago
17 changed file(s) with 188 addition(s) and 193 deletion(s). Raw diff Collapse all Expand all
+0
-1
.gitignore less more
0 node_modules/
+0
-3
.npmignore less more
0 tests/
1 appveyor.yml
2 .travis.yml
0 package-lock=false
+0
-13
.travis.yml less more
0 sudo: false
1 language: node_js
2 node_js:
3 - "6"
4 - "8"
5 - "10"
6 matrix:
7 include:
8 - node_js: "10"
9 env: TEST_SUITE=lint
10 env:
11 - TEST_SUITE=unit
12 script: npm run-script $TEST_SUITE
0 4.0.1 / 2021-09-18
1 ------------------
2
3 - Don't publish unnecessary files
4
5 4.0.0 / 2021-09-18
6 ------------------
7
8 - **BREAKING:** Require Node 14.14.0+ ([#43](https://github.com/jprichardson/node-klaw/pull/43))
9 - **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))
10 - Additional documentation examples ([#34](https://github.com/jprichardson/node-klaw/pull/34))
11
012 3.0.0 / 2018-08-01
113 ------------------
214
7777 .on('end', () => console.dir(items)) // => [ ... array of files]
7878 ```
7979
80 **```for-await-of``` example:**
81
82 ```js
83 for await (const file of klaw('/some/dir')) {
84 console.log(file)
85 }
86 ```
87
8088 ### Error Handling
8189
8290 Listen for the `error` event.
+0
-26
appveyor.yml less more
0 # Test against this version of Node.js
1 environment:
2 matrix:
3 # node.js
4 - nodejs_version: "6"
5 - nodejs_version: "8"
6 - nodejs_version: "10"
7
8 # Install scripts. (runs after repo cloning)
9 install:
10 # Get the latest stable version of Node.js or io.js
11 - ps: Install-Product node $env:nodejs_version
12 # install modules
13 - npm config set loglevel warn
14 - npm install --silent
15
16 # Post-install test scripts.
17 test_script:
18 # Output useful info for debugging.
19 - node --version
20 - npm --version
21 # run tests
22 - npm run unit
23
24 # Don't actually build.
25 build: off
00 {
11 "name": "klaw",
2 "version": "3.0.0",
2 "version": "4.0.1",
33 "description": "File system walker with Readable stream interface.",
44 "main": "./src/index.js",
55 "scripts": {
6 "lint": "standard && standard-markdown",
6 "lint": "standard",
77 "test": "npm run lint && npm run unit",
88 "unit": "tape tests/**/*.js | tap-spec"
99 },
1515 "walk",
1616 "walker",
1717 "fs",
18 "fs-extra",
1918 "readable",
2019 "streams"
2120 ],
21 "engines": {
22 "node": ">=14.14.0"
23 },
2224 "author": "JP Richardson",
2325 "license": "MIT",
26 "files": [
27 "src/"
28 ],
2429 "bugs": {
2530 "url": "https://github.com/jprichardson/node-klaw/issues"
2631 },
2732 "homepage": "https://github.com/jprichardson/node-klaw#readme",
28 "dependencies": {
29 "graceful-fs": "^4.1.9"
30 },
3133 "devDependencies": {
32 "mkdirp": "^0.5.1",
33 "rimraf": "^2.4.3",
34 "standard": "^11.0.1",
35 "standard-markdown": "^4.0.1",
34 "standard": "^16.0.3",
3635 "tap-spec": "^5.0.0",
37 "tape": "^4.2.2"
36 "tape": "^5.3.1"
3837 }
3938 }
0 var assert = require('assert')
1 var path = require('path')
2 var Readable = require('stream').Readable
3 var util = require('util')
0 const { strictEqual } = require('assert')
1 const path = require('path')
2 const fs = require('fs')
3 const { Readable } = require('stream')
44
5 function Walker (dir, options) {
6 assert.strictEqual(typeof dir, 'string', '`dir` parameter should be of type string. Got type: ' + typeof dir)
7 var defaultStreamOptions = { objectMode: true }
8 var defaultOpts = {
9 queueMethod: 'shift',
10 pathSorter: undefined,
11 filter: undefined,
12 depthLimit: undefined,
13 preserveSymlinks: false
14 }
15 options = Object.assign(defaultOpts, options, defaultStreamOptions)
16
17 Readable.call(this, options)
18 this.root = path.resolve(dir)
19 this.paths = [this.root]
20 this.options = options
21 if (options.depthLimit > -1) this.rootDepth = this.root.split(path.sep).length + 1
22 this.fs = options.fs || require('graceful-fs')
23 }
24 util.inherits(Walker, Readable)
25
26 Walker.prototype._read = function () {
27 if (this.paths.length === 0) return this.push(null)
28 var self = this
29 var pathItem = this.paths[this.options.queueMethod]()
30
31 var statFunction = this.options.preserveSymlinks ? self.fs.lstat : self.fs.stat
32
33 statFunction(pathItem, function (err, stats) {
34 var item = { path: pathItem, stats: stats }
35 if (err) return self.emit('error', err, item)
36
37 if (!stats.isDirectory() || (self.rootDepth &&
38 pathItem.split(path.sep).length - self.rootDepth >= self.options.depthLimit)) {
39 return self.push(item)
5 class Walker extends Readable {
6 /**
7 * @param {string} dir
8 * @param {Object} options
9 */
10 constructor (dir, options) {
11 strictEqual(typeof dir, 'string', '`dir` parameter should be of type string. Got type: ' + typeof dir)
12 options = {
13 queueMethod: 'shift',
14 pathSorter: undefined,
15 filter: undefined,
16 depthLimit: undefined,
17 preserveSymlinks: false,
18 ...options,
19 objectMode: true
4020 }
4121
42 self.fs.readdir(pathItem, function (err, pathItems) {
43 if (err) {
44 self.push(item)
45 return self.emit('error', err, item)
22 super(options)
23 this.root = path.resolve(dir)
24 this.paths = [this.root]
25 this.options = options
26 if (options.depthLimit > -1) { this.rootDepth = this.root.split(path.sep).length + 1 }
27 this.fs = options.fs || fs
28 }
29
30 _read () {
31 if (this.paths.length === 0) { return this.push(null) }
32 const pathItem = this.paths[this.options.queueMethod]()
33
34 const statFunction = this.options.preserveSymlinks ? this.fs.lstat : this.fs.stat
35
36 statFunction(pathItem, (err, stats) => {
37 const item = { path: pathItem, stats: stats }
38 if (err) { return this.emit('error', err, item) }
39
40 if (!stats.isDirectory() || (this.rootDepth &&
41 pathItem.split(path.sep).length - this.rootDepth >= this.options.depthLimit)) {
42 return this.push(item)
4643 }
4744
48 pathItems = pathItems.map(function (part) { return path.join(pathItem, part) })
49 if (self.options.filter) pathItems = pathItems.filter(self.options.filter)
50 if (self.options.pathSorter) pathItems.sort(self.options.pathSorter)
51 // faster way to do do incremental batch array pushes
52 self.paths.push.apply(self.paths, pathItems)
45 this.fs.readdir(pathItem, (err, pathItems) => {
46 if (err) {
47 this.push(item)
48 return this.emit('error', err, item)
49 }
5350
54 self.push(item)
51 pathItems = pathItems.map(function (part) { return path.join(pathItem, part) })
52 if (this.options.filter) { pathItems = pathItems.filter(this.options.filter) }
53 if (this.options.pathSorter) { pathItems.sort(this.options.pathSorter) }
54 // faster way to do do incremental batch array pushes
55 this.paths.push.apply(this.paths, pathItems)
56
57 this.push(item)
58 })
5559 })
56 })
60 }
5761 }
5862
63 /**
64 * @param {string} root
65 * @param {Object} [options]
66 */
5967 function walk (root, options) {
6068 return new Walker(root, options)
6169 }
0 var mkdirp = require('mkdirp')
1 var os = require('os')
2 var path = require('path')
3 var rimraf = require('rimraf')
4 var tape = require('tape')
0 const fs = require('fs')
1 const os = require('os')
2 const path = require('path')
3 const tape = require('tape')
54
65 // for all practical purposes, this is a beforeEach and afterEach
76 function test (desc, testFn) {
87 tape(desc, function (t) {
9 var testDir = path.join(os.tmpdir(), 'klaw-tests')
10 rimraf(testDir, function (err) {
8 const testDir = path.join(os.tmpdir(), 'klaw-tests')
9 fs.rm(testDir, { recursive: true, force: true }, function (err) {
1110 if (err) return t.end(err)
12 mkdirp(testDir, function (err) {
11 fs.mkdir(testDir, function (err) {
1312 if (err) return t.end(err)
1413
15 var oldEnd = t.end
14 const oldEnd = t.end
1615 t.end = function () {
17 rimraf(testDir, function (err) {
16 fs.rm(testDir, { recursive: true, force: true }, function (err) {
1817 err ? oldEnd.apply(t, [err]) : oldEnd.apply(t, arguments)
1918 })
2019 }
00 {
11 "a/b.txt": { },
22 "b": { "type": "file", "target": "./a/b.txt" },
3 "c": { "type": "dir", "target": "./a" }
3 "c": { "type": "dir", "target": "./a" },
4 "d": { "type": "file", "target": "./broken" }
45 }
0 var fs = require('fs')
1 var mkdirp = require('mkdirp')
2 var path = require('path')
3 var test = require('./_test')
4 var klaw = require('../')
5 var fixtures = require('./fixtures')
0 const fs = require('fs')
1 const path = require('path')
2 const test = require('./_test')
3 const klaw = require('../')
4 const fixtures = require('./fixtures')
65
76 test('should work w/ streams 1', function (t, testDir) {
87 fixtures.forEach(function (f) {
98 f = path.join(testDir, f)
10 var dir = path.dirname(f)
11 mkdirp.sync(dir)
9 const dir = path.dirname(f)
10 fs.mkdirSync(dir, { recursive: true })
1211 fs.writeFileSync(f, path.basename(f, path.extname(f)))
1312 })
1413
15 var items = []
14 const items = []
1615 klaw(testDir)
1716 .on('data', function (item) {
1817 items.push(item.path)
2019 .on('error', t.end)
2120 .on('end', function () {
2221 items.sort()
23 var expected = ['a', 'a/b', 'a/b/c', 'a/b/c/d.txt', 'a/e.jpg', 'h', 'h/i', 'h/i/j',
22 let expected = ['a', 'a/b', 'a/b/c', 'a/b/c/d.txt', 'a/e.jpg', 'h', 'h/i', 'h/i/j',
2423 'h/i/j/k.txt', 'h/i/l.txt', 'h/i/m.jpg']
2524 expected = expected.map(function (item) {
2625 return path.join(path.join(testDir, item))
3534 test('should work w/ streams 2/3', function (t, testDir) {
3635 fixtures.forEach(function (f) {
3736 f = path.join(testDir, f)
38 var dir = path.dirname(f)
39 mkdirp.sync(dir)
37 const dir = path.dirname(f)
38 fs.mkdirSync(dir, { recursive: true })
4039 fs.writeFileSync(f, path.basename(f, path.extname(f)))
4140 })
4241
43 var items = []
42 const items = []
4443 klaw(testDir)
4544 .on('readable', function () {
46 var item
45 let item
4746 while ((item = this.read())) {
4847 items.push(item.path)
4948 }
5150 .on('error', t.end)
5251 .on('end', function () {
5352 items.sort()
54 var expected = ['a', 'a/b', 'a/b/c', 'a/b/c/d.txt', 'a/e.jpg', 'h', 'h/i', 'h/i/j',
53 let expected = ['a', 'a/b', 'a/b/c', 'a/b/c/d.txt', 'a/e.jpg', 'h', 'h/i', 'h/i/j',
5554 'h/i/j/k.txt', 'h/i/l.txt', 'h/i/m.jpg']
5655 expected = expected.map(function (item) {
5756 return path.join(path.join(testDir, item))
0 var fs = require('fs')
1 var mkdirp = require('mkdirp')
2 var path = require('path')
3 var test = require('./_test')
4 var klaw = require('../')
5 var fixtures = require('./fixtures')
0 const fs = require('fs')
1 const path = require('path')
2 const test = require('./_test')
3 const klaw = require('../')
4 const fixtures = require('./fixtures.json')
65
76 test('should honor depthLimit option -1', function (t, testDir) {
8 var expected = ['a', 'a/b', 'a/b/c', 'a/b/c/d.txt', 'a/e.jpg', 'h', 'h/i',
7 const expected = ['a', 'a/b', 'a/b/c', 'a/b/c/d.txt', 'a/e.jpg', 'h', 'h/i',
98 'h/i/j', 'h/i/j/k.txt', 'h/i/l.txt', 'h/i/m.jpg']
109 run(t, testDir, -1, expected)
1110 })
1211
1312 test('should honor depthLimit option 0', function (t, testDir) {
14 var expected = ['a', 'h']
13 const expected = ['a', 'h']
1514 run(t, testDir, 0, expected)
1615 })
1716
1817 test('should honor depthLimit option 1', function (t, testDir) {
19 var expected = ['a', 'a/b', 'a/e.jpg', 'h', 'h/i']
18 const expected = ['a', 'a/b', 'a/e.jpg', 'h', 'h/i']
2019 run(t, testDir, 1, expected)
2120 })
2221
2322 test('should honor depthLimit option 2', function (t, testDir) {
24 var expected = ['a', 'a/b', 'a/b/c', 'a/e.jpg', 'h', 'h/i', 'h/i/j',
23 const expected = ['a', 'a/b', 'a/b/c', 'a/e.jpg', 'h', 'h/i', 'h/i/j',
2524 'h/i/l.txt', 'h/i/m.jpg']
2625 run(t, testDir, 2, expected)
2726 })
2827
2928 test('should honor depthLimit option 3', function (t, testDir) {
30 var expected = ['a', 'a/b', 'a/b/c', 'a/b/c/d.txt', 'a/e.jpg', 'h', 'h/i',
29 const expected = ['a', 'a/b', 'a/b/c', 'a/b/c/d.txt', 'a/e.jpg', 'h', 'h/i',
3130 'h/i/j', 'h/i/j/k.txt', 'h/i/l.txt', 'h/i/m.jpg']
3231 run(t, testDir, 3, expected)
3332 })
3534 function run (t, testDir, depthLimit, expected) {
3635 fixtures.forEach(function (f) {
3736 f = path.join(testDir, f)
38 var dir = path.dirname(f)
39 mkdirp.sync(dir)
37 const dir = path.dirname(f)
38 fs.mkdirSync(dir, { recursive: true })
4039 fs.writeFileSync(f, path.basename(f, path.extname(f)))
4140 })
4241
43 var items = []
42 const items = []
4443 klaw(testDir, { depthLimit: depthLimit })
4544 .on('data', function (item) {
4645 items.push(item.path)
0 var fs = require('fs')
1 var mkdirp = require('mkdirp')
2 var path = require('path')
3 var test = require('./_test')
4 var klaw = require('../')
5 var fixtures = require('./fixtures')
0 const fs = require('fs')
1 const path = require('path')
2 const test = require('./_test')
3 const klaw = require('../')
4 const fixtures = require('./fixtures')
65
76 test('should not fire event on filtered items', function (t, testDir) {
87 fixtures.forEach(function (f) {
98 f = path.join(testDir, f)
10 var dir = path.dirname(f)
11 mkdirp.sync(dir)
9 const dir = path.dirname(f)
10 fs.mkdirSync(dir, { recursive: true })
1211 fs.writeFileSync(f, path.basename(f, path.extname(f)))
1312 })
1413
15 var items = []
16 var filter = function (filepath) {
14 const items = []
15 const filter = function (filepath) {
1716 return path.basename(filepath) !== 'a'
1817 }
1918
20 klaw(testDir, {filter: filter})
19 klaw(testDir, { filter: filter })
2120 .on('data', function (item) {
2221 if (fs.lstatSync(item.path).isFile()) items.push(item.path)
2322 })
2423 .on('error', t.end)
2524 .on('end', function () {
26 var expected = ['c', 'b', 'a']
25 let expected = ['c', 'b', 'a']
2726 expected = expected.map(function (item) {
2827 return path.join(testDir, item)
2928 })
0 var fs = require('fs')
1 var mkdirp = require('mkdirp')
2 var path = require('path')
3 var test = require('./_test')
4 var klaw = require('../')
5 var fixtures = require('./fixtures_links.json')
0 const fs = require('fs')
1 const path = require('path')
2 const test = require('./_test')
3 const klaw = require('../')
4 const fixtures = require('./fixtures_links.json')
65
76 function loadLinkFixtures (testDir) {
87 Object.keys(fixtures).forEach(function (f) {
9 var link = fixtures[f]
8 const link = fixtures[f]
109 f = path.join(testDir, f)
1110
12 var dir = path.dirname(f)
13 mkdirp.sync(dir)
11 const dir = path.dirname(f)
12 fs.mkdirSync(dir, { recursive: true })
1413
1514 if (link.target) {
15 const realTarget = path.resolve(testDir, link.target)
16 let missing
17 if (!fs.existsSync(realTarget)) {
18 missing = true
19 fs.writeFileSync(realTarget, '')
20 }
1621 fs.symlinkSync(link.target, f, link.type)
22 if (missing) {
23 fs.unlinkSync(realTarget)
24 }
1725 } else {
1826 fs.writeFileSync(f, path.basename(f, path.extname(f)))
1927 }
2331 test('should follow links by default', function (t, testDir) {
2432 loadLinkFixtures(testDir)
2533
26 var items = []
34 const items = []
2735 klaw(testDir)
2836 .on('data', function (item) {
2937 items.push(item.path)
3139 .on('error', t.end)
3240 .on('end', function () {
3341 items.sort()
34 var expected = ['a', 'a/b.txt', 'b', 'c', 'c/b.txt']
42 let expected = ['a', 'a/b.txt', 'b', 'c', 'c/b.txt']
3543 expected = expected.map(function (item) {
3644 return path.join(path.join(testDir, item))
3745 })
4553 test('should not follow links if requested', function (t, testDir) {
4654 loadLinkFixtures(testDir)
4755
48 var items = []
56 const items = []
4957 klaw(testDir, { preserveSymlinks: true })
5058 .on('data', function (item) {
5159 items.push(item.path)
5361 .on('error', t.end)
5462 .on('end', function () {
5563 items.sort()
56 var expected = ['a', 'a/b.txt', 'b', 'c']
64 let expected = ['a', 'a/b.txt', 'b', 'c', 'd']
5765 expected = expected.map(function (item) {
5866 return path.join(path.join(testDir, item))
5967 })
0 var fs = require('fs')
1 var mkdirp = require('mkdirp')
2 var path = require('path')
3 var test = require('./_test')
4 var klaw = require('../')
5 var fixtures = require('./fixtures_path-sorter')
0 const fs = require('fs')
1 const path = require('path')
2 const test = require('./_test')
3 const klaw = require('../')
4 const fixtures = require('./fixtures_path-sorter.json')
5
6 const stringCompare = function (a, b) {
7 if (a < b) return -1
8 else if (a > b) return 1
9 else return 0
10 }
611
712 test('should sort in reverse order [z -> a]', function (t, testDir) {
813 fixtures.forEach(function (f) {
914 f = path.join(testDir, f)
10 var dir = path.dirname(f)
11 mkdirp.sync(dir)
15 const dir = path.dirname(f)
16 fs.mkdirSync(dir, { recursive: true })
1217 fs.writeFileSync(f, path.basename(f, path.extname(f)))
1318 })
1419
15 var items = []
16 var pathSorter = function (a, b) { return b > a }
20 const items = []
21 const pathSorter = function (a, b) { return stringCompare(b, a) }
1722 klaw(testDir, { pathSorter: pathSorter })
1823 .on('data', function (item) {
1924 items.push(item.path)
2025 })
2126 .on('error', t.end)
2227 .on('end', function () {
23 var expected = ['c', 'b', 'a']
28 let expected = ['c', 'b', 'a']
2429 expected = expected.map(function (item) {
2530 return path.join(testDir, item)
2631 })
3439 test('should sort in order [a -> z]', function (t, testDir) {
3540 fixtures.forEach(function (f) {
3641 f = path.join(testDir, f)
37 var dir = path.dirname(f)
38 mkdirp.sync(dir)
42 const dir = path.dirname(f)
43 fs.mkdirSync(dir, { recursive: true })
3944 fs.writeFileSync(f, path.basename(f, path.extname(f)))
4045 })
4146
42 var items = []
43 var pathSorter = function (a, b) { return a > b }
47 const items = []
48 const pathSorter = function (a, b) { return stringCompare(a, b) }
4449 klaw(testDir, { pathSorter: pathSorter })
4550 .on('data', function (item) {
4651 items.push(item.path)
4752 })
4853 .on('error', t.end)
4954 .on('end', function () {
50 var expected = ['a', 'b', 'c']
55 let expected = ['a', 'b', 'c']
5156 expected = expected.map(function (item) {
5257 return path.join(testDir, item)
5358 })
0 var fs = require('fs')
1 var mkdirp = require('mkdirp')
2 var path = require('path')
3 var test = require('./_test')
4 var klaw = require('../')
0 const fs = require('fs')
1 const path = require('path')
2 const test = require('./_test')
3 const klaw = require('../')
54
65 test('walk directory, if error on readdir, at least end', function (t, testDir) {
76 // simulate directory issue
8 var unreadableDir = path.join(testDir, 'unreadable-dir')
9 mkdirp.sync(unreadableDir)
7 const unreadableDir = path.join(testDir, 'unreadable-dir')
8
9 fs.mkdirSync(unreadableDir, { recursive: true })
1010 fs.chmodSync(unreadableDir, '0222')
1111
1212 // not able to simulate on windows
1313 if (process.platform === 'win32') return t.end()
1414
1515 t.plan(2)
16 var items = []
16 const items = []
1717 klaw(testDir)
1818 .on('data', function (item) {
1919 items.push(item.path)