Skip to content

Commit 9561dd5

Browse files
4unitGennady Bakunovichtaylorotwell
authored
Chokidar v4 wildcards support (#1003)
* Chokidar v4 wildcards support * Update file-watcher.cjs --------- Co-authored-by: Gennady Bakunovich <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent dcd0e57 commit 9561dd5

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

bin/file-watcher.cjs

+39-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,47 @@ const chokidar = require('chokidar');
33
const paths = JSON.parse(process.argv[2]);
44
const poll = process.argv[3] ? true : false;
55

6-
const watcher = chokidar.watch(paths, {
6+
// Chokidar removed support for glob in version 4...
7+
const chokidarPackagePath = require.resolve('chokidar').replace('index.js', 'package.json');
8+
const chokidarVersion4 = require(chokidarPackagePath).version.startsWith('4.');
9+
10+
const extractWildcardExtension = (path) => {
11+
// Match patterns like *.php, **/*.blade.php
12+
const match = path.match(/\/\*\.((\w|\.)+)$/);
13+
14+
return match ? match[1] : null;
15+
};
16+
17+
const getPathBeforeWildcard = (path) => {
18+
const match = path.match(/^(.*?)(\*|$)/);
19+
20+
return match ? match[1] : path;
21+
};
22+
23+
const extractedPaths = paths.map(path => {
24+
return { path: getPathBeforeWildcard(path), extension: extractWildcardExtension(path) };
25+
});
26+
27+
const watcherPaths = chokidarVersion4 ? extractedPaths.map(ep => ep.path) : paths;
28+
29+
const watcherIgnored = chokidarVersion4 ? (path, stats) => {
30+
if (! stats?.isFile()) {
31+
return false;
32+
}
33+
34+
const matchedPattern = extractedPaths.find(ep => path.startsWith(ep.path));
35+
36+
if (! matchedPattern) {
37+
return true;
38+
}
39+
40+
return matchedPattern.extension ? ! path.endsWith(`.${matchedPattern.extension}`) : false;
41+
} : undefined;
42+
43+
const watcher = chokidar.watch(watcherPaths, {
744
ignoreInitial: true,
845
usePolling: poll,
46+
ignored: watcherIgnored,
947
});
1048

1149
watcher

0 commit comments

Comments
 (0)