From 51007b1cf2103101629092fd4f5093eb240f56ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Usarz?= Date: Fri, 28 Dec 2018 18:11:37 +0100 Subject: [PATCH] refactor(watcher): move baseDirFromPattern into PatternUtils.getBaseDir (#3241) --- lib/utils/pattern-utils.js | 14 ++++++++++++ lib/watcher.js | 10 ++------ test/unit/utils/pattern-utils.spec.js | 33 +++++++++++++++++++++++++++ test/unit/watcher.spec.js | 31 ------------------------- 4 files changed, 49 insertions(+), 39 deletions(-) create mode 100644 lib/utils/pattern-utils.js create mode 100644 test/unit/utils/pattern-utils.spec.js diff --git a/lib/utils/pattern-utils.js b/lib/utils/pattern-utils.js new file mode 100644 index 000000000..5921473e9 --- /dev/null +++ b/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 diff --git a/lib/watcher.js b/lib/watcher.js index 5a374c766..98d8f2371 100644 --- a/lib/watcher.js +++ b/lib/watcher.js @@ -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 diff --git a/test/unit/utils/pattern-utils.spec.js b/test/unit/utils/pattern-utils.spec.js new file mode 100644 index 000000000..77baf5b94 --- /dev/null +++ b/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') + }) +}) diff --git a/test/unit/watcher.spec.js b/test/unit/watcher.spec.js index 9a9dc5e6c..c71d87a9e 100644 --- a/test/unit/watcher.spec.js +++ b/test/unit/watcher.spec.js @@ -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