Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何监视文件并执行查找替换,但不捕获作为监视文件中事件的查找替换_Javascript_Node.js_Watch - Fatal编程技术网

Javascript 如何监视文件并执行查找替换,但不捕获作为监视文件中事件的查找替换

Javascript 如何监视文件并执行查找替换,但不捕获作为监视文件中事件的查找替换,javascript,node.js,watch,Javascript,Node.js,Watch,我真的不明白为什么在将say'fizbuzz'写入test.txt文件时,它当前会输出两行代码——我知道它正在进行查找和替换,但是有没有办法让查找和替换完成,这样它就不会被watch函数捕获 const watch = require('node-watch'); const replacer = require('replace-in-file'); const files_to_watch = [ '/users/gigatexal/code

我真的不明白为什么在将say'fizbuzz'写入test.txt文件时,它当前会输出两行代码——我知道它正在进行查找和替换,但是有没有办法让查找和替换完成,这样它就不会被watch函数捕获

const watch = require('node-watch');
const replacer = require('replace-in-file');

const files_to_watch = [
                        '/users/gigatexal/code/nodejs/test.txt',
                        '/users/gigatexal/code/nodejs/test2.txt',
                        '/users/gigatexal/code/nodejs/test3.txt'
                     ] 
安装完成,继续解决问题

files_to_watch.forEach(f =>
    {
        watch(f, 
            {resursive: false}, 
                function(evt, name){
                    replacer(
                        {
                         files: f, 
                         from: 'fizbuzz', 
                         to: 'foobar'
                        })
                         .then(change=>{console.log('the file ',f,' was fixed.')})
                         .catch(error=>(console.log('oops caught an error', error)));        
                }
            )
    }
);
运行上述程序后:

echo "fizbuzz" >> /users/gigatexal/code/nodejs/test2.txt
产出:

the file  /users/gigatexal/code/nodejs/test.txt  was fixed.
the file  /users/gigatexal/code/nodejs/test.txt  was fixed.

免责声明:我充其量只是一个中级/初级python开发人员,所以这还不是惯用的NodeJS代码。我想学习如何做到这一点。

因为你没有提供太多关于你的操作系统和程序包版本的信息,而且我也没有足够的声誉发表评论。我将尝试给出一些建议,但不确定它们是否有帮助

  • 现在请确保使用最新版本的
    节点监视
    ,v0.5.5

  • 创建一个新函数(比如
    handle\u file
    )来处理单个文件,这样就易于调试

    function handle_file(f) {
      watch(f, function(evt, name) {
        replacer({
          files: f,
          from: 'fizbuzz',
          to: 'foobar'
        })
       .then(change => {
          console.log('the file %s was fixed.', f)
        })
       .catch(error => {
          console.log('oops caught an error', error)
        });
      })
    }
    
    // now only need to do this
    files_to_watch.forEach(handle_file)
    
  • 用一些日志替换
    replacer
    功能,以查看是否检测到更改以及是否存在双重更改:

    function handle_file(f) {
      watch(f, function(evt, name) {
        console.log('%s changes.', name);
      })
    }
    
  • 然后也许你能告诉我问题出在哪里


  • 因为你没有提供太多关于你的操作系统和程序包版本的信息,我也没有足够的声誉来评论。我将尝试给出一些建议,但不确定它们是否有帮助

  • 现在请确保使用最新版本的
    节点监视
    ,v0.5.5

  • 创建一个新函数(比如
    handle\u file
    )来处理单个文件,这样就易于调试

    function handle_file(f) {
      watch(f, function(evt, name) {
        replacer({
          files: f,
          from: 'fizbuzz',
          to: 'foobar'
        })
       .then(change => {
          console.log('the file %s was fixed.', f)
        })
       .catch(error => {
          console.log('oops caught an error', error)
        });
      })
    }
    
    // now only need to do this
    files_to_watch.forEach(handle_file)
    
  • 用一些日志替换
    replacer
    功能,以查看是否检测到更改以及是否存在双重更改:

    function handle_file(f) {
      watch(f, function(evt, name) {
        console.log('%s changes.', name);
      })
    }
    
  • 然后也许你能告诉我问题出在哪里

  • console.log(“文件名已修复”)
    console.log(“文件名已修复”)