使用Chokidar来观察特定的文件扩展名 [英] Use Chokidar to watch for specific file extension

查看:608
本文介绍了使用Chokidar来观察特定的文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am looking to watch a folder using the nodejs Chokidar I only want to monitor for additions, deletes of xml files. I am new to Chokidar and can't figure it out. I tried setting the Chokidar ignore to match all strings that end in .xml but it looks like Chokidar ignore accepts negative regex's

即使下面的例子不工作

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

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

Is there a way to do this or do I have to add the filter to the callback function?

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 accepts a glob pattern as first argument.
You can use it match your XML files.

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

这篇关于使用Chokidar来观察特定的文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆