diff --git a/spec/stdlib/fs-utils-spec.coffee b/spec/stdlib/fs-utils-spec.coffee index cc82a3e66..ff85ca850 100644 --- a/spec/stdlib/fs-utils-spec.coffee +++ b/spec/stdlib/fs-utils-spec.coffee @@ -127,3 +127,25 @@ describe "fs", -> it "returns the paths with the specified extensions", -> path = require.resolve('fixtures/css.css') expect(fs.list(fs.resolveOnLoadPath('fixtures'), ['.css'])).toEqual [path] + describe ".listAsync(path, [extensions,] callback)", -> + paths = null + + it "calls the callback with the absolute paths of entries within the given directory", -> + waitsFor (done) -> + fs.listAsync project.getPath(), (err, result) -> + paths = result + done() + runs -> + expect(paths).toContain project.resolve('css.css') + expect(paths).toContain project.resolve('coffee.coffee') + expect(paths).toContain project.resolve('two-hundred.txt') + + it "can filter the paths by an optional array of file extensions", -> + waitsFor (done) -> + fs.listAsync project.getPath(), ['css', '.coffee'], (err, result) -> + paths = result + done() + runs -> + expect(paths).toContain project.resolve('css.css') + expect(paths).toContain project.resolve('coffee.coffee') + expect(path).toMatch /(css|coffee)$/ for path in paths diff --git a/src/stdlib/fs-utils.coffee b/src/stdlib/fs-utils.coffee index 0d59af00c..b85a896db 100644 --- a/src/stdlib/fs-utils.coffee +++ b/src/stdlib/fs-utils.coffee @@ -93,6 +93,19 @@ module.exports = @traverseTreeSync(rootPath, onPath, onPath) paths + listAsync: (rootPath, rest...) -> + extensions = rest.shift() if rest.length > 1 + done = rest.shift() + fs.readdir rootPath, (err, paths) => + return done(err) if err + paths = @filterExtensions(paths, extensions) if extensions + paths = paths.map (path) => @join(rootPath, path) + done(null, paths) + + filterExtensions: (paths, extensions) -> + extensions = extensions.map (ext) -> '.' + ext.replace(/^\./, '') + paths.filter (path) => _.include(extensions, @extension(path)) + listTree: (rootPath) -> paths = [] onPath = (path) =>