Add fs.listAsync

This commit is contained in:
Nathan Sobo 2013-03-20 18:43:13 -06:00
parent 93bef861df
commit a5dcd71548
2 changed files with 35 additions and 0 deletions

View File

@ -127,3 +127,25 @@ describe "fs", ->
it "returns the paths with the specified extensions", -> it "returns the paths with the specified extensions", ->
path = require.resolve('fixtures/css.css') path = require.resolve('fixtures/css.css')
expect(fs.list(fs.resolveOnLoadPath('fixtures'), ['.css'])).toEqual [path] 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

View File

@ -93,6 +93,19 @@ module.exports =
@traverseTreeSync(rootPath, onPath, onPath) @traverseTreeSync(rootPath, onPath, onPath)
paths 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) -> listTree: (rootPath) ->
paths = [] paths = []
onPath = (path) => onPath = (path) =>