Mansi Parekh
Mansi Parekh

Reputation: 519

Copy folder and change file names using grunt

I want to copy one folder and rename files of that folder using grunt.

Basic Structure:

mainfolder
   -project
       -project.js
       -project.html

I want to copy project folder and want to replace project with newname so it will look like:

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

I am finding it difficult in renaming files name (content is replaced with newname). I have followed this steps:

I tried merging all operation but it didn't work.


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

Upvotes: 2

Views: 551

Answers (1)

Revive
Revive

Reputation: 2248

You could do the renaming inside of grunt copy with your own function like below: I removed the newname1 folder and clean task as they appeared redundant.

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

Upvotes: 1

Related Questions