1

I would like to write a Grunt task that, during build, will copy all .html files that I have and make an .asp version of it in /dist as well.

I have been trying to use grunt-contrib-copy to accomplish this, and here's what I have:

copy: {
  //some other tasks that work...

  //copy an .asp version of all .html files
  asp: {
    files: [{
      expand: true,
      dot: true,
      cwd: '<%= config.app %>',
      src: ['{,*/}*.html'],
      dest: '<%= config.dist %>',
      option: {
        process: function (content, srcpath) {
          return srcpath.replace(".asp");
        }
      }
    }]
  } //end asp task
},

I know that the process function is not actually correct... I've tried a few different regex to make it work to no avail. When I run the asp task, the Grunt CLI says that my it has copied 2 files, but they're nowhere to be found. Any help is appreciated.

GloryOfThe80s
  • 161
  • 1
  • 1
  • 8

1 Answers1

7

You can do that using rename function.

For example:

copy: {
  //some other tasks that work...

  //copy an .asp version of all .html files
  asp: {
    files: [{
      expand: true,
      dot: true,
      cwd: '<%= config.app %>',
      src: ['{,*/}*.html'],
      dest: '<%= config.dist %>',
      rename: function(dest, src) {
         return dest + src.replace(/\.html$/, ".asp");
      }
    }]
  } //end asp task
},

This should works.

Mario Araque
  • 4,562
  • 3
  • 15
  • 25
  • 5
    Indeed the `rename` method works, though by looking at the Grunt docs closer, I discovered you can also accomplish a simple file extension change using the `ext` property like so: `ext: '.asp'`. – GloryOfThe80s Apr 02 '15 at 13:35
  • Note that you might want to join the dest and src parameters using `path.sep()`, because now you join the file and folder without the slashes `somefoldersomefile.asp` instead of `somefolder/somefile.asp`. – Justus Romijn Sep 05 '16 at 11:19