我想复制一个文件夹并使用grunt重命名该文件夹的文件。
基本结构:
mainfolder
-project
-project.js
-project.html
我想要复制项目文件夹,并希望将project
替换为newname
,所以它看起来像:
mainfolder
-project
-project.js
-project.html
-newname
-newname.js
-newname.html
我发现重命名文件名很困难(内容被newname替换)。 我已按照以下步骤操作:
我尝试合并所有操作,但它没有用。
module.exports = function(grunt) {
grunt.initConfig({
copy: {
main: {
expand: true,
src: 'project/**',
dest: 'newname1',
},
},
replace: {
example: {
src: ['newname1/*'],
dest: 'newname',
replacements: [{
from: 'project',
to: 'newname'
},
{
from: 'Project',
to: 'Newname'
}
],
}
},
clean: {
folder: ['newname1']
},
rename: {
main: {
files: [{
src: ['newname/**]'],
dest: 'newname/**]'
}, ]
}
}
});
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-text-replace');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.registerTask('default', ['copy', 'replace', 'clean']);
};
答案 0 :(得分:1)
您可以使用自己的功能在grunt copy
内部重命名,如下所示:
我删除了newname1
文件夹和clean
任务,因为它们似乎是多余的。
module.exports = function(grunt) {
grunt.initConfig({
copy: {
main: {
expand: true,
cwd:"project",
src: '**',
dest: 'newname/',
rename: function(dest, src) {
if(src.indexOf('.')>0){
return dest+"newname"+src.substring(src.indexOf('.'),src.length);
}else{
return dest
}
},
},
},
replace: {
example: {
src: ['newname/*'],
dest: 'newname/',
replacements: [{
from: 'project',
to: 'newname'
},
{
from: 'Project',
to: 'Newname'
}
],
}
},
});
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-text-replace');
grunt.registerTask('default', ['copy', 'replace']);
};