Skip to content

Commit

Permalink
Chokidar v4 wildcards support (#1003)
Browse files Browse the repository at this point in the history
* Chokidar v4 wildcards support

* Update file-watcher.cjs

---------

Co-authored-by: Gennady Bakunovich <[email protected]>
Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
3 people authored Mar 6, 2025
1 parent dcd0e57 commit 9561dd5
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion bin/file-watcher.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,47 @@ const chokidar = require('chokidar');
const paths = JSON.parse(process.argv[2]);
const poll = process.argv[3] ? true : false;

const watcher = chokidar.watch(paths, {
// Chokidar removed support for glob in version 4...
const chokidarPackagePath = require.resolve('chokidar').replace('index.js', 'package.json');
const chokidarVersion4 = require(chokidarPackagePath).version.startsWith('4.');

const extractWildcardExtension = (path) => {
// Match patterns like *.php, **/*.blade.php
const match = path.match(/\/\*\.((\w|\.)+)$/);

return match ? match[1] : null;
};

const getPathBeforeWildcard = (path) => {
const match = path.match(/^(.*?)(\*|$)/);

return match ? match[1] : path;
};

const extractedPaths = paths.map(path => {
return { path: getPathBeforeWildcard(path), extension: extractWildcardExtension(path) };
});

const watcherPaths = chokidarVersion4 ? extractedPaths.map(ep => ep.path) : paths;

const watcherIgnored = chokidarVersion4 ? (path, stats) => {
if (! stats?.isFile()) {
return false;
}

const matchedPattern = extractedPaths.find(ep => path.startsWith(ep.path));

if (! matchedPattern) {
return true;
}

return matchedPattern.extension ? ! path.endsWith(`.${matchedPattern.extension}`) : false;
} : undefined;

const watcher = chokidar.watch(watcherPaths, {
ignoreInitial: true,
usePolling: poll,
ignored: watcherIgnored,
});

watcher
Expand Down

0 comments on commit 9561dd5

Please sign in to comment.