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

Tre*_*vor 5 javascript filesystems node.js

我希望使用nodejs Chokidar观看一个文件夹我只想监视添加,删除xml文件.我是Chokidar的新手,无法弄明白.我尝试将Chokidar忽略设置为匹配所有以.xml结尾的字符串,但看起来像Chokidar忽略接受负正则表达式

即使是下面的例子也行不通

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

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

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);
});
Run Code Online (Sandbox Code Playgroud)

Flo*_*ent 16

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

chokidar.watch("some/directory/**/*.xml", config)
Run Code Online (Sandbox Code Playgroud)