diff --git a/spec/stdlib/fs-spec.coffee b/spec/stdlib/fs-spec.coffee index dd48935c8..b6f96fb57 100644 --- a/spec/stdlib/fs-spec.coffee +++ b/spec/stdlib/fs-spec.coffee @@ -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) - diff --git a/src/stdlib/fs.coffee b/src/stdlib/fs.coffee index 9cb912311..85b26a6d6 100644 --- a/src/stdlib/fs.coffee +++ b/src/stdlib/fs.coffee @@ -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 \ No newline at end of file