2016-11-07 119 views
4

我正在使用nodejs Chokidar观看文件夹 我只想监视添加,删除xml文件。 我是Chokidar的新手,无法弄清楚。 我尝试设置Chokidar忽略以匹配以.xml结尾的所有字符串,但它看起来像Chokidar忽略接受负的正则表达式的使用Chokidar观察特定的文件扩展名

即使下面的例子不工作

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); 
}); 

回答

9

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

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

也就是说,这真的很笨,我没有尝试过。 – Trevor

+0

嘿家伙,请看看我的问题:https://stackoverflow.com/questions/44530477/use-chokidar-for-watching-over-files-with-specific-extentions –

相关问题