Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(deps): update chokidar to v4 #5256

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
50 changes: 46 additions & 4 deletions lib/cli/watch-run.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const path = require('path');
const chokidar = require('chokidar');
const Context = require('../context');
const collectFiles = require('./collect-files');
const glob = require('glob');

/**
* Exports the `watchRun` function that runs mocha in "watch" mode.
Expand Down Expand Up @@ -136,6 +137,40 @@ exports.watchRun = (mocha, {watchFiles, watchIgnore}, fileCollectParams) => {
});
};

class GlobFilesTracker {
constructor(watchFiles, watchIgnore) {
this.watchFilesSet = new Set();
this.watchFiles = watchFiles;
this.watchIgnore = watchIgnore;
}

regenerate() {
let watchIgnoreFlat = [];
for (const pattern of this.watchIgnore) {
watchIgnoreFlat = watchIgnoreFlat.concat(glob.sync(pattern, { dot: true }));
}

this.watchFilesSet.clear();
for (const pattern of this.watchFiles) {
glob.sync(pattern, { dot: true }).forEach(pathToCheck => {
for (const watchIgnore of watchIgnoreFlat) {
if (pathToCheck === watchIgnore) {
return;
}
if (pathToCheck.startsWith(watchIgnore + path.sep)) {
return;
}
}
this.watchFilesSet.add(pathToCheck);
});
}
}

has(filePath) {
return this.watchFilesSet.has(filePath)
}
}

/**
* Bootstraps a chokidar watcher. Handles keyboard input & signals
* @param {Mocha} mocha - Mocha instance
Expand Down Expand Up @@ -167,8 +202,10 @@ const createWatcher = (
// we handle global fixtures manually
mocha.enableGlobalSetup(false).enableGlobalTeardown(false);

const watcher = chokidar.watch(watchFiles, {
ignored: watchIgnore,
const tracker = new GlobFilesTracker(watchFiles, watchIgnore);
tracker.regenerate();

const watcher = chokidar.watch('.', {
ignoreInitial: true
});

Expand All @@ -184,8 +221,13 @@ const createWatcher = (
rerunner.run();
});

watcher.on('all', () => {
rerunner.scheduleRun();
watcher.on('all', (event, filePath) => {
if (event === 'add') {
tracker.regenerate();
}
if (tracker.has(filePath)) {
rerunner.scheduleRun();
}
});

hideCursor();
Expand Down
Loading