diff --git a/package.json b/package.json index 9cb70adbd..fbf9670fe 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "diff": "git://github.com/benogle/jsdiff.git", "emissary": "0.19.0", "first-mate": "0.11.0", - "fs-plus": "0.13.0", + "fs-plus": "0.14.0", "fstream": "0.1.24", "fuzzaldrin": "0.1.0", "git-utils": "0.29.0", diff --git a/spec/directory-spec.coffee b/spec/directory-spec.coffee index ba8a24045..0910f02fe 100644 --- a/spec/directory-spec.coffee +++ b/spec/directory-spec.coffee @@ -76,6 +76,20 @@ describe "Directory", -> else expect(entry.symlink).toBeFalsy() + callback = jasmine.createSpy('getEntries') + directory.getEntries(callback) + + waitsFor -> callback.callCount is 1 + + runs -> + entries = callback.mostRecentCall.args[0] + for entry in entries + name = entry.getBaseName() + if name is 'symlink-to-dir' or name is 'symlink-to-file' + expect(entry.symlink).toBeTruthy() + else + expect(entry.symlink).toBeFalsy() + describe ".relativize(path)", -> describe "on #darwin or #linux", -> it "returns a relative path based on the directory's path", -> diff --git a/src/directory.coffee b/src/directory.coffee index 9886a2edb..3711a7d80 100644 --- a/src/directory.coffee +++ b/src/directory.coffee @@ -1,8 +1,11 @@ path = require 'path' + +async = require 'async' +{Emitter} = require 'emissary' fs = require 'fs-plus' pathWatcher = require 'pathwatcher' + File = require './file' -{Emitter} = require 'emissary' # Public: Represents a directory using {File}s module.exports = @@ -81,8 +84,6 @@ class Directory # Public: Reads file entries in this directory from disk synchronously. # - # Note: It follows symlinks. - # # Returns an Array of {File} and {Directory} objects. getEntriesSync: -> directories = [] @@ -99,6 +100,30 @@ class Directory directories.concat(files) + # Public: Reads file entries in this directory from disk asynchronously. + # + # * callback: A function to call with an Error as the first argument and + # an Array of {File} and {Directory} objects as the second argument. + getEntries: (callback) -> + fs.list @path, (error, entries) -> + return callback(error) if error? + + directories = [] + files = [] + statEntry = (entryPath, callback) -> + fs.stat entryPath, (error, stat) -> + return callback() if error? + + fs.isSymbolicLink entryPath, (symlink) -> + if stat.isDirectory() + directories.push(new Directory(entryPath, symlink)) + else if stat.isFile() + files.push(new File(entryPath, symlink)) + callback() + + async.eachLimit entries, 1, statEntry, -> + callback(directories.concat(files)) + # Private: subscribeToNativeChangeEvents: -> unless @watchSubscription?