-
Notifications
You must be signed in to change notification settings - Fork 774
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #286 from agnivade/walkSync
Added walkSync function
- Loading branch information
Showing
7 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |