【问题标题】:Checking if destination file already exists检查目标文件是否已经存在
【发布时间】:2013-10-28 07:34:04
【问题描述】:

目标

检查两个文件夹(源文件夹和目标文件夹)中是否存在同名文件

尝试 尝试使用grunt-contrib-copy 遍历路径,但显然这不起作用。

copy: {
  unit: {
    expand: true,
    cwd: 'src',
    src: '**',
    dest: 'specs/unit',
    filter: function(filepath){
      if(grunt.file.exists(filepath)){
        return grunt.file.write(filepath,'');
      }
    }
  }
},

我的理论

grunt.file.exists 似乎指向 src 文件夹而不是目标文件夹。所以它从不检查 src 文件夹中的文件是否真的存在于目标文件夹中,它只是检查 src 文件夹中是否有任何文件。

【问题讨论】:

    标签: javascript node.js gruntjs grunt-contrib-copy


    【解决方案1】:

    您始终可以使用fs.existsSync()http://nodejs.org/api/fs.html#fs_fs_exists_path_callback。一定要使用同步函数,Grunt 不喜欢异步函数。

    还有;确保返回一个布尔值。

    在您的Gruntfile.js 顶部:

    var fs = require('fs');
    

    在您的任务设置中;

    copy: {
      unit: {
        expand: true,
        cwd: 'src',
        src: '**',
        dest: 'specs/unit',
        filter: function(filepath){
          return !fs.existsSync(filepath);
        }
      }
    },
    

    【讨论】:

    • 这实际上不是检查 destination 文件夹是否存在文件或“src”文件夹吗?它似乎检查了“src”文件夹。
    • 似乎在检查 src,而不是 dest
    【解决方案2】:

    grunt.file.setBase('./destination')

    //检查文件

    grunt.file.setBase('./src')

    //检查文件

    【讨论】:

      猜你喜欢
      • 2021-11-28
      • 1970-01-01
      • 2023-01-25
      • 1970-01-01
      • 2014-10-06
      • 2012-05-13
      • 1970-01-01
      相关资源
      最近更新 更多