Add fs.traverseTree

This commit is contained in:
Corey Johnson & Nathan Sobo
2012-05-08 17:45:29 -07:00
parent d9d78ada51
commit 289193787e
2 changed files with 35 additions and 3 deletions

View File

@@ -40,6 +40,31 @@ describe "fs", ->
it "returns an empty string for paths without an extension", ->
expect(fs.extension("a/b.not-extension/a-dir")).toBe ''
describe ".traverseTree(path, fn)", ->
fixturesDir = null
beforeEach ->
fixturesDir = require.resolve('fixtures')
it "calls fn for every path in the tree at the given path", ->
paths = []
fs.traverseTree fixturesDir, (path) -> paths.push(path)
expect(paths).toEqual fs.listTree(fixturesDir)
it "does not recurse into a directory if it is pruned", ->
paths = []
fs.traverseTree fixturesDir, (path, prune) ->
console.log path
if path.match(/\/dir$/)
prune()
else
paths.push(path)
expect(paths.length).toBeGreaterThan 0
for path in paths
expect(path).not.toMatch /dir/
describe ".async", ->
directoryPath = null
beforeEach ->
@@ -56,4 +81,3 @@ describe "fs", ->
waitsForPromise ->
fs.async.list(directoryPath).done (result) ->
expect(result).toEqual fs.list(directoryPath)

View File

@@ -80,6 +80,15 @@ module.exports =
makeDirectory: (path) ->
$native.makeDirectory(path)
traverseTree: (rootPath, fn) ->
recurse = null
prune = -> recurse = false
for path in @list(rootPath)
recurse = true
fn(path, prune)
@traverseTree(path, fn) if @isDirectory(path) and recurse
async:
list: (path) ->
deferred = $.Deferred()
@@ -91,5 +100,4 @@ module.exports =
deferred = $.Deferred()
$native.asyncList path, true, (subpaths) ->
deferred.resolve subpaths
deferred
deferred