Repository: jprichardson/node-fs-extra
Pull request #312 (2016-11-14 01:15:50)

Enhance walkSync() to return items with path and stats
Hi,

This PR is regarding #310. I modified `walkSync` and its test to return an array of items `{path:,stats:}` to be consistent with `walk`. Please let me know what you think. I appreciate it in advance. Thanks a lot. 

coveralls: 
[![Coverage Status](https://coveralls.io/builds/8803468/badge)](https://coveralls.io/builds/8803468)

Coverage increased (+0.05%) to 85.473% when pulling **fca1d7be39166f68007db2b3cf8ba3f72fffd415 on mawni:enhance-walkSync** into **b96e003adaa49ae56d83aaa72d77f62748787a7a on jprichardson:master**.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/8804778/badge)](https://coveralls.io/builds/8804778)

Coverage increased (+0.03%) to 85.457% when pulling **9df7ca072157fc97037897d15888e87de800b47e on mawni:enhance-walkSync** into **b96e003adaa49ae56d83aaa72d77f62748787a7a on jprichardson:master**.

RyanZim: @jprichardson Can you manually restart the appveyor build?
jprichardson: > @jprichardson Can you manually restart the appveyor build?

I wonder why it's failing here... I think a push has to trigger it...
RyanZim: @jprichardson I think it migh be related to https://github.com/isaacs/node-graceful-fs/issues/98.
RyanZim: @mawni Can you rebase onto `master`. The issue with windows tests should be fixed.
coveralls: 
[![Coverage Status](https://coveralls.io/builds/8838542/badge)](https://coveralls.io/builds/8838542)

Coverage increased (+0.02%) to 85.44% when pulling **501e02d8fc14a38f5f8988c066278ac955dd0ac7 on mawni:enhance-walkSync** into **b96e003adaa49ae56d83aaa72d77f62748787a7a on jprichardson:master**.

RyanZim: @jprichardson Are the changes introduced here merge-ready? If so, I will cherry-pick this to `master`.
jprichardson: @mawni will you squash these?

Patch:
diff --git a/lib/walk-sync/index.js b/lib/walk-sync/index.js
index 0ebe56e..e9b27d9 100644
--- a/lib/walk-sync/index.js
+++ b/lib/walk-sync/index.js
@@ -1,18 +1,20 @@
 var fs = require('graceful-fs')
 var path = require('path')
 
-var walkSync = function (dir, filelist) {
-  var files = fs.readdirSync(dir)
-  filelist = filelist || []
+var walkSync = function (dir, list) {
+  var files = fs.readdirSync(path.resolve(dir))
+  list = list || []
   files.forEach(function (file) {
     var nestedPath = path.join(dir, file)
-    if (fs.lstatSync(nestedPath).isDirectory()) {
-      filelist = walkSync(nestedPath, filelist)
+    var stat = fs.lstatSync(nestedPath)
+    if (stat.isDirectory()) {
+      list.push({path: nestedPath, stats: stat})
+      list = walkSync(nestedPath, list)
     } else {
-      filelist.push(nestedPath)
+      list.push({path: nestedPath, stats: stat})
     }
   })
-  return filelist
+  return list
 }
 
 module.exports = {


Test patch:
diff --git a/lib/walk-sync/__tests__/walkSync.test.js b/lib/walk-sync/__tests__/walkSync.test.js
index ee3b867..ad51189 100644
--- a/lib/walk-sync/__tests__/walkSync.test.js
+++ b/lib/walk-sync/__tests__/walkSync.test.js
@@ -27,14 +27,28 @@ describe('walk-sync', function () {
     }
   })
 
-  it('should return all files successfully for a dir', function (done) {
-    var files = fse.walkSync(fixturesDir)
-    var expectedFiles = ['dir1/file1_2', 'dir2/dir2_1/file2_1_1', 'file1']
-    expectedFiles = expectedFiles.map(function (item) {
+  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)
     })
-    files.forEach(function (elem, i) {
-      assert.equal(elem, expectedFiles[i])
+    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()
   })