首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在后台运行chokidar (persistent: true)?

如何在后台运行chokidar (persistent: true)?
EN

Stack Overflow用户
提问于 2021-03-16 22:12:52
回答 1查看 228关注 0票数 0

我正在编写一个node.js程序,需要监视(添加/删除/更新)文件夹中的文件。我正在使用chokidar来查看文件。下面是代码

代码语言:javascript
复制
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在后台看文件。因此,我仍然可以提示用户提出更多问题,并做我自己的事情。

EN

回答 1

Stack Overflow用户

发布于 2021-06-29 02:54:38

我知道我迟到了3个月,你现在可能已经找到了答案,但这是为后来访问的人准备的。您可以使用名为的节点模块来使用派生子进程。简单地将您代码放在一个文件中,并使用以下代码执行该文件

代码语言:javascript
复制
 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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66656978

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档