Skip to content

Commit

Permalink
feat(watcher): Allow using braces in watcher
Browse files Browse the repository at this point in the history
Expands braces in watcher, so you can watch: ['a/{b,c}'] which would
expand to ['a/b', 'a/c']

Closes #1249
  • Loading branch information
JetFault authored and dignifiedquire committed May 20, 2015
1 parent 3bf13ec commit e046379
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/watcher.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var chokidar = require('chokidar');
var mm = require('minimatch');
var expandBraces = require('expand-braces');

var helper = require('./helper');
var log = require('./logger').create('watcher');
Expand All @@ -19,6 +20,9 @@ var watchPatterns = function(patterns, watcher) {
var uniqueMap = {};
var path;

// expand ['a/{b,c}'] to ['a/b', 'a/c']
patterns = expandBraces(patterns);

patterns.forEach(function(pattern) {
path = baseDirFromPattern(pattern);
if (!uniqueMap[path]) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@
"chokidar": ">=0.8.2",
"glob": "~3.2.7",
"minimatch": "~0.2",
"expand-braces": "~0.1.1",
"http-proxy": "~0.10",
"optimist": "~0.6.0",
"rimraf": "~2.2.5",
Expand Down
15 changes: 15 additions & 0 deletions test/unit/watcher.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,21 @@ describe 'watcher', ->
expect(chokidarWatcher.watchedPaths_).to.deep.equal ['/some', '/a']


it 'should expand braces and watch all the patterns', ->
m.watchPatterns ['/some/{a,b}/*.js', '/a/*'], chokidarWatcher
expect(chokidarWatcher.watchedPaths_).to.deep.equal ['/some/a', '/some/b', '/a']


it 'should not watch the same path twice', ->
m.watchPatterns ['/some/a*.js', '/some/*.txt'], chokidarWatcher
expect(chokidarWatcher.watchedPaths_).to.deep.equal ['/some']


it 'should not watch the same path twice when using braces', ->
m.watchPatterns ['/some/*.{js,txt}'], chokidarWatcher
expect(chokidarWatcher.watchedPaths_).to.deep.equal ['/some']


it 'should not watch subpaths that are already watched', ->
m.watchPatterns ['/some/sub/*.js', '/some/a*.*'].map(path.normalize), chokidarWatcher
expect(chokidarWatcher.watchedPaths_).to.deep.equal [path.normalize('/some')]
Expand All @@ -75,6 +85,11 @@ describe 'watcher', ->
expect(chokidarWatcher.watchedPaths_).to.deep.equal ['/some/test-file.js', '/some/test']


it 'should watch files matching a subpath directory with braces', ->
m.watchPatterns ['/some/{a,b}/test.js'], chokidarWatcher
expect(chokidarWatcher.watchedPaths_).to.deep.equal ['/some/a/test.js', '/some/b/test.js']


describe 'getWatchedPatterns', ->

it 'should return list of watched patterns (strings)', ->
Expand Down

0 comments on commit e046379

Please sign in to comment.