Repository: jprichardson/node-fs-extra
Pull request #286 (2016-10-14 09:10:04)

Added walkSync function


Patch:
diff --git a/README.md b/README.md
index 06648b8..acc1249 100644
--- a/README.md
+++ b/README.md
@@ -111,6 +111,7 @@ Methods
 - [remove](#removedir-callback)
 - [removeSync](#removedir-callback)
 - [walk](#walk)
+- [walkSync](#walkSyncDir)
 - [writeJson](#writejsonfile-object-options-callback)
 - [writeJsonSync](#writejsonfile-object-options-callback)
 
@@ -449,6 +450,18 @@ recommend this resource as a good starting point: https://strongloop.com/strongb
 
 **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)
 
diff --git a/lib/index.js b/lib/index.js
index a56bb0c..7b874d5 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -20,6 +20,7 @@ assign(fs, require('./empty'))
 assign(fs, require('./ensure'))
 assign(fs, require('./output'))
 assign(fs, require('./walk'))
+assign(fs, require('./walk-sync'))
 
 module.exports = fs
 
diff --git a/lib/walk-sync/index.js b/lib/walk-sync/index.js
new file mode 100644
index 0000000..0ebe56e
--- /dev/null
+++ b/lib/walk-sync/index.js
@@ -0,0 +1,20 @@
+var fs = require('graceful-fs')
+var path = require('path')
+
+var walkSync = function (dir, filelist) {
+  var files = fs.readdirSync(dir)
+  filelist = filelist || []
+  files.forEach(function (file) {
+    var nestedPath = path.join(dir, file)
+    if (fs.lstatSync(nestedPath).isDirectory()) {
+      filelist = walkSync(nestedPath, filelist)
+    } else {
+      filelist.push(nestedPath)
+    }
+  })
+  return filelist
+}
+
+module.exports = {
+  walkSync: walkSync
+}


Test patch:
diff --git a/lib/walk-sync/__tests__/fixtures/dir1/file1_2 b/lib/walk-sync/__tests__/fixtures/dir1/file1_2
new file mode 100644
index 0000000..e69de29
diff --git a/lib/walk-sync/__tests__/fixtures/dir2/dir2_1/file2_1_1 b/lib/walk-sync/__tests__/fixtures/dir2/dir2_1/file2_1_1
new file mode 100644
index 0000000..e69de29
diff --git a/lib/walk-sync/__tests__/fixtures/file1 b/lib/walk-sync/__tests__/fixtures/file1
new file mode 100644
index 0000000..e69de29
diff --git a/lib/walk-sync/__tests__/walkSync.test.js b/lib/walk-sync/__tests__/walkSync.test.js
new file mode 100644
index 0000000..ee3b867
--- /dev/null
+++ b/lib/walk-sync/__tests__/walkSync.test.js
@@ -0,0 +1,41 @@
+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 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) {
+      return path.join(fixturesDir, item)
+    })
+    files.forEach(function (elem, i) {
+      assert.equal(elem, expectedFiles[i])
+    })
+    done()
+  })
+})