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

标签 javascript node.js filesystems

我正在寻找使用 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)

关于javascript - 使用 Chokidar 监视特定的文件扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40468608/

相关文章:

javascript - 在解析期间检测内容中的正则表达式

node.js - Azure Functions 未记录 App Insights 中的所有跟踪

javascript - ReactJS 和 Express with Axios 返回 404 错误消息

java - 获取文件元数据的开销(上次修改日期)

javascript - 使用 grunt js 在没有 *.con.js 文件的情况下进行缩小和连接

javascript - jquery点击确认后不起作用

javascript - superagent 和 nock 如何协同工作?

linux - 在 Linux 中挂载和更改文件时使用的系统调用序列

c++ - 在执行过程中如何用C++代码在计算机中找到Iostream文件

javascript - 如何在 onload 事件之前运行 JavaScript 片段(Google Chrome 扩展)