Skip to content

Commit

Permalink
Add second check for ignored items (after stating the fs)
Browse files Browse the repository at this point in the history
When deciding if an item should be ignored, it is helpful to know whether it is a file or a directory. Delaying the check after we have the stats information would mean unnecessary stat calls for those items that can be ignored based on the path.

If `options.ignored` is a function with two arguments, it will be called twice for each new item - once without the stats object, then again once we have the stats information.

If `options.ignored` is a regular expression or a function with a single argument, it will be called once.

Closes paulmillr#51
  • Loading branch information
vojtajina committed Oct 21, 2013
1 parent 7412fab commit 299c2bc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ watcher.close();
* `chokidar.watch(paths, options)`: takes paths to be watched and options:
* `options.ignored` (regexp or function) files to be ignored.
This function or regexp is tested against the **whole path**,
not just filename. Example:
`chokidar.watch('file', {ignored: /^\./})`.
not just filename. If it is a function with two arguments, it gets called
twice per path - once with a single argument (the path), second time with
two arguments (the path and the [`fs.Stats`](http://nodejs.org/api/fs.html#fs_class_fs_stats)
object of that path).
* `options.persistent` (default: `false`). Indicates whether the process
should continue to run as long as files are being watched.
* `options.ignorePermissionErrors` (default: `false`). Indicates
Expand Down
2 changes: 2 additions & 0 deletions src/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ exports.FSWatcher = class FSWatcher extends EventEmitter
if @options.ignorePermissionErrors and (not @_hasReadPermissions stats)
return

return if @_ignored.length is 2 and @_ignored item, stats

@_handleFile item, stats, initialAdd if stats.isFile()
@_handleDir item, initialAdd if stats.isDirectory()

Expand Down
27 changes: 27 additions & 0 deletions test/chokidar-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,15 @@ describe 'chokidar', ->

before (done) ->
try fs.unlinkSync getFixturePath('subdir/add.txt')
try fs.unlinkSync getFixturePath('subdir/dir/ignored.txt')
try fs.rmdirSync getFixturePath('subdir/dir')
try fs.rmdirSync getFixturePath('subdir')
done()

after (done) ->
try fs.unlinkSync getFixturePath('subdir/add.txt')
try fs.unlinkSync getFixturePath('subdir/dir/ignored.txt')
try fs.rmdirSync getFixturePath('subdir/dir')
try fs.rmdirSync getFixturePath('subdir')
done()

Expand Down Expand Up @@ -157,6 +161,29 @@ describe 'chokidar', ->
spy.should.have.been.calledWith testPath
done()

it 'should check ignore after stating', (done) ->
testDir = getFixturePath 'subdir'
spy = sinon.spy()

ignoredFn = (path, stats) ->
return no if path is testDir or not stats
# ignore directories
return stats.isDirectory()

watcher = chokidar.watch testDir, {ignored: ignoredFn}
watcher.on 'add', spy

fs.mkdirSync testDir, 0o755
fs.writeFileSync testDir + '/add.txt', '' # this file should be added
fs.mkdirSync testDir + '/dir', 0o755 # this dir will be ignored
fs.writeFileSync testDir + '/dir/ignored.txt', '' # so this file should be ignored

delay ->
spy.should.have.been.calledOnce
spy.should.have.been.calledWith testDir + '/add.txt'
done()


describe 'is-binary', ->
it 'should be a function', ->
isBinary.should.be.a 'function'
Expand Down

0 comments on commit 299c2bc

Please sign in to comment.