Skip to content

Commit

Permalink
Merge pull request #286 from agnivade/walkSync
Browse files Browse the repository at this point in the history
Added walkSync function
  • Loading branch information
jprichardson committed Nov 1, 2016
2 parents 87dd3c8 + 075f402 commit d722ae9
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Empty file.
Empty file.
Empty file.
41 changes: 41 additions & 0 deletions lib/walk-sync/__tests__/walkSync.test.js
Original file line number Diff line number Diff line change
@@ -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()
})
})
20 changes: 20 additions & 0 deletions lib/walk-sync/index.js
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit d722ae9

Please sign in to comment.