Skip to content

Commit d722ae9

Browse files
authored
Merge pull request #286 from agnivade/walkSync
Added walkSync function
2 parents 87dd3c8 + 075f402 commit d722ae9

File tree

7 files changed

+75
-0
lines changed

7 files changed

+75
-0
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ Methods
111111
- [remove](#removedir-callback)
112112
- [removeSync](#removedir-callback)
113113
- [walk](#walk)
114+
- [walkSync](#walkSyncDir)
114115
- [writeJson](#writejsonfile-object-options-callback)
115116
- [writeJsonSync](#writejsonfile-object-options-callback)
116117

@@ -449,6 +450,18 @@ recommend this resource as a good starting point: https://strongloop.com/strongb
449450

450451
**See [`klaw` documentation](https://github.com/jprichardson/node-klaw) for more detailed usage.**
451452

453+
### walkSync(dir)
454+
455+
Lists all files inside a directory recursively
456+
457+
Examples:
458+
459+
```js
460+
var fs = require('fs-extra')
461+
462+
var files = fs.walkSync('/home/jprichardson')
463+
// files = ['/home/jprichardson/file1', '/home/jprichardson/dir1/file2']
464+
```
452465

453466
### writeJson(file, object, [options], callback)
454467

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ assign(fs, require('./empty'))
2020
assign(fs, require('./ensure'))
2121
assign(fs, require('./output'))
2222
assign(fs, require('./walk'))
23+
assign(fs, require('./walk-sync'))
2324

2425
module.exports = fs
2526

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

Whitespace-only changes.

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

Whitespace-only changes.

lib/walk-sync/__tests__/fixtures/file1

Whitespace-only changes.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
var assert = require('assert')
2+
var path = require('path')
3+
4+
var fse = require('../../')
5+
6+
/* global describe, it */
7+
var fixturesDir = path.join(__dirname, 'fixtures')
8+
9+
describe('walk-sync', function () {
10+
it('should return an error if the source dir does not exist', function (done) {
11+
try {
12+
fse.walkSync('dirDoesNotExist/')
13+
} catch (err) {
14+
assert.equal(err.code, 'ENOENT')
15+
} finally {
16+
done()
17+
}
18+
})
19+
20+
it('should return an error if the source is not a dir', function (done) {
21+
try {
22+
fse.walkSync(path.join(fixturesDir, 'dir1/file1_2'))
23+
} catch (err) {
24+
assert.equal(err.code, 'ENOTDIR')
25+
} finally {
26+
done()
27+
}
28+
})
29+
30+
it('should return all files successfully for a dir', function (done) {
31+
var files = fse.walkSync(fixturesDir)
32+
var expectedFiles = ['dir1/file1_2', 'dir2/dir2_1/file2_1_1', 'file1']
33+
expectedFiles = expectedFiles.map(function (item) {
34+
return path.join(fixturesDir, item)
35+
})
36+
files.forEach(function (elem, i) {
37+
assert.equal(elem, expectedFiles[i])
38+
})
39+
done()
40+
})
41+
})

lib/walk-sync/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var fs = require('graceful-fs')
2+
var path = require('path')
3+
4+
var walkSync = function (dir, filelist) {
5+
var files = fs.readdirSync(dir)
6+
filelist = filelist || []
7+
files.forEach(function (file) {
8+
var nestedPath = path.join(dir, file)
9+
if (fs.lstatSync(nestedPath).isDirectory()) {
10+
filelist = walkSync(nestedPath, filelist)
11+
} else {
12+
filelist.push(nestedPath)
13+
}
14+
})
15+
return filelist
16+
}
17+
18+
module.exports = {
19+
walkSync: walkSync
20+
}

0 commit comments

Comments
 (0)