【问题标题】:Copy folder and change file names using grunt使用 grunt 复制文件夹和更改文件名
【发布时间】:2017-08-11 13:57:50
【问题描述】:

我想复制一个文件夹并使用 grunt 重命名该文件夹的文件。

基本结构:

mainfolder
   -project
       -project.js
       -project.html

我想复制项目文件夹并想用newname 替换project,所以它看起来像:

mainfolder
   -project
       -project.js
       -project.html
    -newname
       -newname.js
       -newname.html

我发现重命名文件名很困难(内容被替换为新名称)。 我已按照以下步骤操作:

  • 将项目文件夹复制到 newname1 文件夹
  • 替换了 newname 文件夹中的内容
  • 已删除 newname1 文件夹

我尝试合并所有操作,但没有成功。


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']);
};

【问题讨论】:

    标签: angularjs gruntjs grunt-contrib-copy


    【解决方案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']);
    };
    

    【讨论】:

    • 嗨,在我尝试在副本中重命名函数之前,它不起作用。您的解决方案工作正常。我对重命名功能进行了一些更改,因为项目文件夹“viewproject.js”中还有一个文件,它只将一个文件“newname.js”更改为return dest + src.replace('project','newname');
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-23
    • 1970-01-01
    相关资源
    最近更新 更多