Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion args.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const parseArgs = require('./lib/parse-args')
const { requireModule } = require('./util')
const { loadEnvQuitely } = require('./env-loader')

const DEFAULT_IGNORE = 'node_modules build dist .git bower_components logs .swp .nyc_output'
const { DEFAULT_IGNORE } = require('./lib/watch/constants')

const DEFAULT_ARGUMENTS = {
logLevel: 'fatal',
Expand Down
1 change: 1 addition & 0 deletions lib/watch/constants.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

module.exports = {
DEFAULT_IGNORE: 'node_modules build dist .git bower_components logs .swp .nyc_output',
GRACEFUL_SHUT: 'GRACEFUL SHUTDOWN',
READY: 'ready',
TIMEOUT: 5000
Expand Down
7 changes: 7 additions & 0 deletions lib/watch/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@

const chalk = require('chalk').default
const path = require('node:path')
const { DEFAULT_IGNORE } = require('./constants')
const ignoredDirectories = DEFAULT_IGNORE.split(' ').filter((entry) => entry !== '.swp')
const pathSeparator = '[/\\\\]'

const arrayToRegExp = (arr) => {
const reg = arr.map((file) => {
if (ignoredDirectories.includes(file)) {
const literal = file.replace('.', '\\.')
return `(?:^|${pathSeparator})${literal}(?=${pathSeparator}|$)`
}
if (/^\./.test(file)) { return `\\${file}` }
return file
}).join('|')
Expand Down
24 changes: 18 additions & 6 deletions test/watch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,23 @@ test('followWatch parameter should set watchDir correctly', async (t) => {
emitter2.stop()
})

test('should equal expect RegExp', t => {
t.plan(1)

const expectRegExp = /(node_modules|build|dist|\.git|bower_components|logs)/
const regExp = arrayToRegExp(['node_modules', 'build', 'dist', '.git', 'bower_components', 'logs'])
test('default watch ignores match whole path segments', () => {
const dirs = ['node_modules', 'build', 'dist', '.git', 'bower_components', 'logs', '.nyc_output']
const regExp = arrayToRegExp(dirs)
for (const separator of ['/', '\\']) {
const project = ['workspace', 'any-builder'].join(separator)
assert.strictEqual(regExp.test(project), false)
assert.strictEqual(regExp.test([project, 'src', 'distribution.js'].join(separator)), false)
for (const dir of dirs) {
assert.strictEqual(regExp.test(dir), true)
assert.strictEqual(regExp.test([project, dir, 'app.js'].join(separator)), true)
}
}
})

t.assert.deepStrictEqual(regExp, expectRegExp)
test('custom watch ignores still support regular expressions', () => {
const regExp = arrayToRegExp(['build', '.ts$', 'custom-[0-9]+'])
assert.strictEqual(regExp.test('src/app.ts'), true)
assert.strictEqual(regExp.test('src/app.ts.js'), false)
assert.strictEqual(regExp.test('src/custom-12/file.js'), true)
})
Loading