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

时间:2016-11-07 15:18:42

标签: javascript node.js filesystems

我希望使用nodejs Chokidar观看一个文件夹 我只想监视添加,删除xml文件。 我是Chokidar的新手,无法弄明白。 我尝试将Chokidar忽略设置为匹配以.xml结尾的所有字符串,但看起来像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);
});

1 个答案:

答案 0 :(得分:15)

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

chokidar.watch("some/directory/**/*.xml", config)
相关问题