检查目标文件是否已经存在
目标
要检查两个文件夹(src和目标文件夹)中是否存在具有相同名称的文件,
尝试尝试使用grunt-contrib-copy
来遍历path,但显然这是行不通的。
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文件夹中是否有任何文件。
您始终可以使用fs.existsSync()
: http : fs.existsSync()
。 一定要使用同步之一,Grunt不喜欢asynchronousfunction。
也; 确保你返回一个布尔值。
在你的Gruntfile.js
的顶部:
var fs = require('fs');
在你的任务设置;
copy: { unit: { expand: true, cwd: 'src', src: '**', dest: 'specs/unit', filter: function(filepath){ return !fs.existsSync(filepath); } } },