目标
检查两个文件夹(源文件夹和目标文件夹)中是否存在同名文件
尝试
尝试使用 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 文件夹中是否有任何文件。
最佳答案
您始终可以使用fs.existsSync()
:http://nodejs.org/api/fs.html#fs_fs_exists_path_callback 。一定要使用同步函数,Grunt 不喜欢异步函数。
还有;确保返回一个 bool 值。
在 Gruntfile.js
的顶部:
var fs = require('fs');
在您的任务设置中;
copy: {
unit: {
expand: true,
cwd: 'src',
src: '**',
dest: 'specs/unit',
filter: function(filepath){
return !fs.existsSync(filepath);
}
}
},
关于javascript - 检查目标文件是否已存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19466909/