使用 Chokidar 监视特定的文件扩展名

Use Chokidar to watch for specific file extension

我正在寻找使用 nodejs Chokidar 的文件夹 我只想监视 xml 文件的添加、删除。 我是 Chokidar 的新手,无法弄清楚。 我尝试设置 Chokidar ignore 以匹配所有以 .xml 结尾的字符串,但看起来 Chokidar ignore 接受负正则表达式的

下面的例子也行不通

watcher = chokidar.watch(watcherFolder, { 
    ignored: /[^l]$,
    persistent: true,
    ignoreInitial: true,
    alwaysState: true}
);

有没有办法做到这一点,或者我是否必须将过滤器添加到回调函数?

watcher.on('add', function(path) {
    if (!/\.xml$/i.test(path)) { return; }
    console.log('chokidar: add: ' + path);
});
watcher.on('unlink', function(path) {
    if (!/\.xml$/i.test(path)) { return; }
    console.log('chokidar: unlink: ' + path);
});

watcher.on('change', function(path) {
    if (!/\.xml$/i.test(path)) { return; }
    console.log('chokidar: change: ' + path);
});

chokidar 接受 glob 模式作为第一个参数。
您可以使用它来匹配您的 XML 个文件。

chokidar.watch("some/directory/**/*.xml", config)