我使用 fs.watch 来监控一些文件。我有多个针对不同文件类型的监视对象。是否可以在一个元素中定义多个文件类型结尾?
代码:过滤器之一:
filter = /\.scss$/;
try {
watch(watchPath, { recursive: true, filter: filter }, (evt, name) => {
if (!getFilename(name).startsWith("_") && evt === "update") {
log("Changed", `${project.name} => ${getFilename(".//" + name)}`);
let tmpFiles = getFolder(type, type === ResourceTypes.STYLE ? project.style : project.script);
let tmp = `.//${name}`.replace(/\\/gm, "//");
tmp = tmp.substring(type === ResourceTypes.STYLE ? project.style.length : project.script.length + 2);
if (tmp.startsWith("//")) {
tmp = tmp.substring(2);
}
tmp = tmp.replace(/(\/\/)/gm, ".");
if (type === ResourceTypes.STYLE) {
tmp = tmp.replace(".scss", ".css");
}
let file = tmpFiles.find((item) => {
return item.outFilename === tmp;
});
if (file) {
handleFile(type, project, file);
if (type === ResourceTypes.SCRIPT && project.bundleScript) {
handleBundle(type, project);;
}
if (type === ResourceTypes.STYLE && project.bundleStyle) {
handleBundle(type, project);
}
finished(getFilename(file.source));
}
else {
logError("Not found", new Error(`File ${getFilename(`.//${getFilename(name)} not found`)}`));
}
}
});
} catch (error) {
logError("Watching", error);
}