Browse Source

Remove walk() & walkSync()

Resolves #338
ci/travis-osximage
RyanZim 8 years ago
parent
commit
1f83cb0cdf
  1. 61
      README.md
  2. 2
      lib/index.js
  3. 0
      lib/walk-sync/__tests__/fixtures/dir1/file1_2
  4. 0
      lib/walk-sync/__tests__/fixtures/dir2/dir2_1/file2_1_1
  5. 0
      lib/walk-sync/__tests__/fixtures/file1
  6. 55
      lib/walk-sync/__tests__/walkSync.test.js
  7. 22
      lib/walk-sync/index.js
  8. 7
      lib/walk/__tests__/fixtures.json
  9. 73
      lib/walk/__tests__/walk.test.js
  10. 5
      lib/walk/index.js
  11. 4
      test.js

61
README.md

@ -403,65 +403,6 @@ fs.remove('/tmp/myfile', function (err) {
fs.removeSync('/home/jprichardson') //I just deleted my entire HOME directory.
```
### walk()
**walk(dir, [streamOptions])**
The function `walk()` from the module [`klaw`](https://github.com/jprichardson/node-klaw).
Returns a [Readable stream](https://nodejs.org/api/stream.html#stream_class_stream_readable) that iterates
through every file and directory starting with `dir` as the root. Every `read()` or `data` event
returns an object with two properties: `path` and `stats`. `path` is the full path of the file and
`stats` is an instance of [fs.Stats](https://nodejs.org/api/fs.html#fs_class_fs_stats).
Streams 1 (push) example:
```js
var fs = require('fs-extra')
var items = [] // files, directories, symlinks, etc
fs.walk(TEST_DIR)
.on('data', function (item) {
items.push(item.path)
})
.on('end', function () {
console.dir(items) // => [ ... array of files]
})
```
Streams 2 & 3 (pull) example:
```js
var items = [] // files, directories, symlinks, etc
var fs = require('fs-extra')
fs.walk(TEST_DIR)
.on('readable', function () {
var item
while ((item = this.read())) {
items.push(item.path)
}
})
.on('end', function () {
console.dir(items) // => [ ... array of files]
})
```
If you're not sure of the differences on Node.js streams 1, 2, 3 then I'd
recommend this resource as a good starting point: https://strongloop.com/strongblog/whats-new-io-js-beta-streams3/.
**See [`klaw` documentation](https://github.com/jprichardson/node-klaw) for more detailed usage.**
### walkSync(dir)
Lists all files inside a directory recursively
Examples:
```js
var fs = require('fs-extra')
var files = fs.walkSync('/home/jprichardson')
// files = ['/home/jprichardson/file1', '/home/jprichardson/dir1/file2']
```
### writeJson(file, object, [options], callback)
@ -527,8 +468,6 @@ What's needed?
- First, take a look at existing issues. Those are probably going to be where the priority lies.
- More tests for edge cases. Specifically on different platforms. There can never be enough tests.
- Improve test coverage. See coveralls output for more info.
- After the directory walker is integrated, any function that needs to traverse directories like
`copy`, `remove`, or `mkdirs` should be built on top of it.
Note: If you make any big changes, **you should definitely file an issue for discussion first.**

2
lib/index.js

@ -19,8 +19,6 @@ assign(fs, require('./move'))
assign(fs, require('./empty'))
assign(fs, require('./ensure'))
assign(fs, require('./output'))
assign(fs, require('./walk'))
assign(fs, require('./walk-sync'))
module.exports = fs

0
lib/walk-sync/__tests__/fixtures/dir1/file1_2

0
lib/walk-sync/__tests__/fixtures/dir2/dir2_1/file2_1_1

0
lib/walk-sync/__tests__/fixtures/file1

55
lib/walk-sync/__tests__/walkSync.test.js

@ -1,55 +0,0 @@
var assert = require('assert')
var path = require('path')
var fse = require('../../')
/* global describe, it */
var fixturesDir = path.join(__dirname, 'fixtures')
describe('walk-sync', function () {
it('should return an error if the source dir does not exist', function (done) {
try {
fse.walkSync('dirDoesNotExist/')
} catch (err) {
assert.equal(err.code, 'ENOENT')
} finally {
done()
}
})
it('should return an error if the source is not a dir', function (done) {
try {
fse.walkSync(path.join(fixturesDir, 'dir1/file1_2'))
} catch (err) {
assert.equal(err.code, 'ENOTDIR')
} finally {
done()
}
})
it('should return all items of a dir containing path and stats data successfully', function (done) {
var items = fse.walkSync(fixturesDir)
var files = ['dir1/file1_2', 'dir2/dir2_1/file2_1_1', 'file1']
files = files.map(function (item) {
return path.join(fixturesDir, item)
})
var dirs = ['dir1', 'dir2', 'dir2/dir2_1']
dirs = dirs.map(function (item) {
return path.join(fixturesDir, item)
})
var expectedItems = [
{path: dirs[0], stats: fse.lstatSync(dirs[0])},
{path: files[0], stats: fse.lstatSync(files[0])},
{path: dirs[1], stats: fse.lstatSync(dirs[1])},
{path: dirs[2], stats: fse.lstatSync(dirs[2])},
{path: files[1], stats: fse.lstatSync(files[1])},
{path: files[2], stats: fse.lstatSync(files[2])}
]
items.forEach(function (elem, i) {
assert.deepEqual(elem, expectedItems[i])
assert.strictEqual(elem.path, expectedItems[i].path)
assert.deepEqual(elem.stats, expectedItems[i].stats)
})
done()
})
})

22
lib/walk-sync/index.js

@ -1,22 +0,0 @@
var fs = require('graceful-fs')
var path = require('path')
var walkSync = function (dir, list) {
var files = fs.readdirSync(path.resolve(dir))
list = list || []
files.forEach(function (file) {
var nestedPath = path.join(dir, file)
var stat = fs.lstatSync(nestedPath)
if (stat.isDirectory()) {
list.push({path: nestedPath, stats: stat})
list = walkSync(nestedPath, list)
} else {
list.push({path: nestedPath, stats: stat})
}
})
return list
}
module.exports = {
walkSync: walkSync
}

7
lib/walk/__tests__/fixtures.json

@ -1,7 +0,0 @@
[
"a/b/c/d.txt",
"a/e.jpg",
"h/i/j/k.txt",
"h/i/l.txt",
"h/i/m.jpg"
]

73
lib/walk/__tests__/walk.test.js

@ -1,73 +0,0 @@
var assert = require('assert')
var path = require('path')
var os = require('os')
var fse = require('../../')
var fixtures = require('./fixtures')
/* global afterEach, beforeEach, describe, it */
// trinity: mocha
describe('walk()', function () {
var TEST_DIR
beforeEach(function (done) {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'walk')
fse.emptyDir(TEST_DIR, function (err) {
if (err) return done(err)
fixtures.forEach(function (f) {
f = path.join(TEST_DIR, f)
fse.outputFileSync(f, path.basename(f, path.extname(f)))
})
done()
})
})
afterEach(function (done) {
fse.remove(TEST_DIR, done)
})
it('should work w/ streams 1', function (done) {
var items = []
fse.walk(TEST_DIR)
.on('data', function (item) {
assert.strictEqual(typeof item.stats, 'object') // verify that property is there
items.push(item.path)
})
.on('end', function () {
items.sort()
var expected = ['a', 'a/b', 'a/b/c', 'a/b/c/d.txt', 'a/e.jpg', 'h', 'h/i', 'h/i/j',
'h/i/j/k.txt', 'h/i/l.txt', 'h/i/m.jpg']
expected = expected.map(function (item) {
return path.join(path.join(TEST_DIR, item))
})
expected.unshift(TEST_DIR)
assert.deepEqual(items, expected)
done()
})
})
it('should work w/ streams 2/3', function (done) {
var items = []
fse.walk(TEST_DIR)
.on('readable', function () {
var item
while ((item = this.read())) {
assert.strictEqual(typeof item.stats, 'object') // verify that property is there
items.push(item.path)
}
})
.on('end', function () {
items.sort()
var expected = ['a', 'a/b', 'a/b/c', 'a/b/c/d.txt', 'a/e.jpg', 'h', 'h/i', 'h/i/j',
'h/i/j/k.txt', 'h/i/l.txt', 'h/i/m.jpg']
expected = expected.map(function (item) {
return path.join(path.join(TEST_DIR, item))
})
expected.unshift(TEST_DIR)
assert.deepEqual(items, expected)
done()
})
})
})

5
lib/walk/index.js

@ -1,5 +0,0 @@
var klaw = require('klaw')
module.exports = {
walk: klaw
}

4
test.js

@ -2,7 +2,7 @@ var os = require('os')
var path = require('path')
var Mocha = require('mocha')
var assign = require('./lib/util/assign')
var fs = require('./')
var klaw = require('klaw')
var argv = require('minimist')(process.argv.slice(2))
@ -14,7 +14,7 @@ var mochaOpts = assign({
var mocha = new Mocha(mochaOpts)
fs.walk('./lib').on('readable', function () {
klaw('./lib').on('readable', function () {
var item
while ((item = this.read())) {
if (!item.stats.isFile()) return

Loading…
Cancel
Save