filegruntjsrenamegrunt-contrib-copy

gunrt-contrib-copy renaming file extensions


i have a directory and a number of sub directories so when i look for my files i use a globbing pattern like

'src/**/*.js'

the trouble is i want to rename extension to .old.js

the snag is i need the .old.js file to be renamed and stay in the directory it was found but it is not clear to me how this is achieved.

I have tried a variety of things and so far the only thing that has worked is to look at each directory seperately in a seperate copy task which is not really what i want to do.

Anyone have any other approach?


Solution

  • Initial directory structure

    Consider the following fictitious directory structure:

    src
    ├── 1.js
    ├── 2.js
    └── a
        ├── 3.js
        ├── 4.js
        └── b
            ├── 5.js
            ├── 6.js
            └── c
                ├── 7.js
                └── 8.js
    

    Example One

    Gruntfile.js

    You can configure your grunt-contrib-copy Task as follows:

    module.exports = function(grunt) {
    
        grunt.initConfig({
    
            copy: {
                js: {
                    files: [{
                        expand: true,
                        dot: true,
                        cwd: 'src',
                        dest: 'src/',
                        src: [
                            '**/*.js'
                        ],
                        rename: function(dest, src) {
                            return dest + src.replace('.js', '.old.js');
                        }
                    }]
                }
            }
    
        });
    
        grunt.loadNpmTasks('grunt-contrib-copy');
    
        grunt.registerTask('default', [
            'copy:js'
        ]);
    
    };
    

    Resultant directory structure (Example One)

    After running $ grunt via the CLI using the Gruntfile.js configured as shown above will result in the following:

    (Note: each original .js file has been duplicated to the same folder location and the .old.js extension added):

    src
    ├── 1.js
    ├── 1.old.js
    ├── 2.js
    ├── 2.old.js
    └── a
        ├── 3.js
        ├── 3.old.js
        ├── 4.js
        ├── 4.old.js
        └── b
            ├── 5.js
            ├── 5.old.js
            ├── 6.js
            ├── 6.old.js
            └── c
                ├── 7.js
                ├── 7.old.js
                ├── 8.js
                └── 8.old.js
    

    Example 2

    If you don't want to keep the original .js file then you'll also need to use grunt-contrib-clean to delete the originals.

    Gruntfile.js

    module.exports = function(grunt) {
    
        grunt.initConfig({
    
            copy: {
                js: {
                    files: [{
                        expand: true,
                        dot: true,
                        cwd: 'src',
                        dest: 'src/',
                        src: [
                            '**/*.js'
                        ],
                        rename: function(dest, src) {
                            return dest + src.replace('.js', '.old.js');
                        }
                    }]
                }
            },
    
            clean: {
                originaljs: [
                    'src/**/*.js',
                    '!src/**/*.old.js'
                ]
            }
    
        });
    
        grunt.loadNpmTasks('grunt-contrib-copy');
        grunt.loadNpmTasks('grunt-contrib-clean');
    
        grunt.registerTask('default', [
            'copy:js',
            'clean:originaljs'
        ]);
    
    };
    

    Resultant directory structure (Example Two)

    This time after running $ grunt using the revised Gruntfile.js will result in the following:

    (Note: each original .js has been deleted and only the .old.js extension exist.)

    src
    ├── 1.old.js
    ├── 2.old.js
    └── a
        ├── 3.old.js
        ├── 4.old.js
        └── b
            ├── 5.old.js
            ├── 6.old.js
            └── c
                ├── 7.old.js
                └── 8.old.js