Skip to content

Commit

Permalink
refactor(watcher): move baseDirFromPattern into PatternUtils.getBaseD…
Browse files Browse the repository at this point in the history
…ir (#3241)
  • Loading branch information
lusarz authored and johnjbarton committed Dec 28, 2018
1 parent 02f071d commit 51007b1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 39 deletions.
14 changes: 14 additions & 0 deletions lib/utils/pattern-utils.js
@@ -0,0 +1,14 @@
'use strict'

const path = require('path')

const PatternUtils = {
getBaseDir (pattern) {
return pattern
.replace(/[/\\][^/\\]*\*.*$/, '') // remove parts with *
.replace(/[/\\][^/\\]*[!+]\(.*$/, '') // remove parts with !(...) and +(...)
.replace(/[/\\][^/\\]*\)\?.*$/, '') || path.sep // remove parts with (...)?
}
}

module.exports = PatternUtils
10 changes: 2 additions & 8 deletions lib/watcher.js
Expand Up @@ -3,25 +3,19 @@
const chokidar = require('chokidar')
const mm = require('minimatch')
const expandBraces = require('expand-braces')
const PatternUtils = require('./utils/pattern-utils')

const helper = require('./helper')
const log = require('./logger').create('watcher')

const DIR_SEP = require('path').sep

function baseDirFromPattern (pattern) {
return pattern
.replace(/[/\\][^/\\]*\*.*$/, '') // remove parts with *
.replace(/[/\\][^/\\]*[!+]\(.*$/, '') // remove parts with !(...) and +(...)
.replace(/[/\\][^/\\]*\)\?.*$/, '') || DIR_SEP // remove parts with (...)?
}

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

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

pathsToWatch = Array.from(pathsToWatch)
// watch only common parents, no sub paths
Expand Down
33 changes: 33 additions & 0 deletions test/unit/utils/pattern-utils.spec.js
@@ -0,0 +1,33 @@
'use strict'
const PatternUtils = require('../../../lib/utils/pattern-utils')

describe('PatternUtils.getBaseDir', () => {
it('return parent directory without start', () => {
expect(PatternUtils.getBaseDir('/some/path/**/more.js')).to.equal('/some/path')
expect(PatternUtils.getBaseDir('/some/p*/file.js')).to.equal('/some')
})

it('remove part with !(x)', () => {
expect(PatternUtils.getBaseDir('/some/p/!(a|b).js')).to.equal('/some/p')
expect(PatternUtils.getBaseDir('/some/p!(c|b)*.js')).to.equal('/some')
})

it('remove part with +(x)', () => {
expect(PatternUtils.getBaseDir('/some/p/+(a|b).js')).to.equal('/some/p')
expect(PatternUtils.getBaseDir('/some/p+(c|bb).js')).to.equal('/some')
})

it('remove part with (x)?', () => {
expect(PatternUtils.getBaseDir('/some/p/(a|b)?.js')).to.equal('/some/p')
expect(PatternUtils.getBaseDir('/some/p(c|b)?.js')).to.equal('/some')
})

it('allow paths with parentheses', () => {
expect(PatternUtils.getBaseDir('/some/x (a|b)/a.js')).to.equal('/some/x (a|b)/a.js')
expect(PatternUtils.getBaseDir('/some/p(c|b)/*.js')).to.equal('/some/p(c|b)')
})

it('ignore exact files', () => {
expect(PatternUtils.getBaseDir('/usr/local/bin.js')).to.equal('/usr/local/bin.js')
})
})
31 changes: 0 additions & 31 deletions test/unit/watcher.spec.js
Expand Up @@ -11,37 +11,6 @@ describe('watcher', () => {
m = mocks.loadFile(path.join(__dirname, '/../../lib/watcher.js'), mocks_)
})

describe('baseDirFromPattern', () => {
it('should return parent directory without start', () => {
expect(m.baseDirFromPattern('/some/path/**/more.js')).to.equal('/some/path')
expect(m.baseDirFromPattern('/some/p*/file.js')).to.equal('/some')
})

it('should remove part with !(x)', () => {
expect(m.baseDirFromPattern('/some/p/!(a|b).js')).to.equal('/some/p')
expect(m.baseDirFromPattern('/some/p!(c|b)*.js')).to.equal('/some')
})

it('should remove part with +(x)', () => {
expect(m.baseDirFromPattern('/some/p/+(a|b).js')).to.equal('/some/p')
expect(m.baseDirFromPattern('/some/p+(c|bb).js')).to.equal('/some')
})

it('should remove part with (x)?', () => {
expect(m.baseDirFromPattern('/some/p/(a|b)?.js')).to.equal('/some/p')
expect(m.baseDirFromPattern('/some/p(c|b)?.js')).to.equal('/some')
})

it('should allow paths with parentheses', () => {
expect(m.baseDirFromPattern('/some/x (a|b)/a.js')).to.equal('/some/x (a|b)/a.js')
expect(m.baseDirFromPattern('/some/p(c|b)/*.js')).to.equal('/some/p(c|b)')
})

it('should ignore exact files', () => {
expect(m.baseDirFromPattern('/usr/local/bin.js')).to.equal('/usr/local/bin.js')
})
})

describe('watchPatterns', () => {
let chokidarWatcher = null

Expand Down

0 comments on commit 51007b1

Please sign in to comment.