我正在编写一个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在后台看文件。因此,我仍然可以提示用户提出更多问题,并做我自己的事情。
发布于 2021-06-29 02:54:38
我知道我迟到了3个月,你现在可能已经找到了答案,但这是为后来访问的人准备的。您可以使用名为的节点模块来使用派生子进程。简单地将您代码放在一个文件中,并使用以下代码执行该文件
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_namehttps://stackoverflow.com/questions/66656978
复制相似问题