如何解决如何在后台运行 chokidar (persistent: true)?
我正在编写一个需要监视文件夹中(添加/删除/更新)文件的 node.js 程序。我正在使用 chokidar 来观看文件。这是代码
async function updateIndexFile() {
console.log({ path });
const pathArray = []
// Chokidar Code
const options = {
ignored: /(^|[\/\\])\../,// ignore dotfiles
persistent: true
}
// Initialize watcher.
const watcher = chokidar.watch(path,options);
watcher
.on('add',async (path) => {
console.log(`File ${path} has been added`)
let answer = await prompt({
type: 'list',name: 'adventure',// * Key
message: 'Choose your own adventure',choices: ['Indexify','Undo'] // * Add Feature Names Here
})
console.log(answer);
}) // Run indexify again on these files
.on('change',path => console.log(`File ${path} has been changed`)) // Run indexify again on these files
.on('unlink',path => console.log(`File ${path} has been removed`)); // Run indexify again on these files
但是,我无法使用终端,因为 chokidar 正在查看文件。所以,我想让 chokidar 在后台观看文件。所以,我仍然可以提示用户提出更多问题并做我的事情。
解决方法
我知道我迟到了 3 个月,您现在可能已经找到了答案,但这是为以后访问的任何人准备的。 您可以使用调用的节点模块使用 spawn 子进程。 简单地将您的代码放在一个文件中并使用以下代码执行该文件
const {spawn}=require(child_prcoess);
let detached_process = spawn(process.argv[0],["program_name","arg1","arg2","arg3"],{ detached: true}); //launch program_name and detach it from program_name with args provided
detached_process.unref(); //tell main program to do not wait for the completion of program_name
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。