Skip to content

Commit

Permalink
refactor(watcher): refactor watchPatterns method
Browse files Browse the repository at this point in the history
  • Loading branch information
lusarz committed Jan 1, 2019
1 parent b05a54e commit 1a57029
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions lib/watcher.js
Expand Up @@ -11,20 +11,15 @@ const log = require('./logger').create('watcher')
const DIR_SEP = require('path').sep

function watchPatterns (patterns, watcher) {
let pathsToWatch = new Set()

// expand ['a/{b,c}'] to ['a/b', 'a/c']
expandBraces(patterns)
.forEach((pattern) => pathsToWatch.add(PatternUtils.getBaseDir(pattern)))

pathsToWatch = Array.from(pathsToWatch)
// watch only common parents, no sub paths
pathsToWatch.forEach((path) => {
if (!pathsToWatch.some((p) => p !== path && path.startsWith(p + DIR_SEP))) {
watcher.add(path)
log.debug(`Watching "${path}"`)
}
})
expandBraces(patterns) // expand ['a/{b,c}'] to ['a/b', 'a/c']
.map(PatternUtils.getBaseDir)
.filter((path, index, paths) => paths.indexOf(path) === index) // filter unique values
.forEach((path, index, paths) => {
if (!paths.some((p) => path.startsWith(p + DIR_SEP))) {
watcher.add(path)
log.debug(`Watching "${path}"`)
}
})
}

function checkAnyPathMatch (patterns, path) {
Expand Down

0 comments on commit 1a57029

Please sign in to comment.