0

I'm using load-grunt-config and grunt-contrib-copy, and I'm trying to get the copy task to replace some template tags with the 'process' option.

I know that replacing template tags is possible (from the grunt-contrib-copy documentation), but I'm not able to get it to work. What I'm trying to do is replace the string <%= init.project.name %> in "style.css" with the template variable of the same name (entered by the user with grunt-prompt).

On copy I want grunt to replace the template variable in the style.css file, with the value that it has in memory. But it doesn't do this when I use the code I have below. Does anyone know what I'm doing wrong?

Grunt copy task

// -------------------------------------
// Grunt copy
// -------------------------------------

module.exports = {

  // ----- Copy files for initialization (selected with grunt prompt) ----- //

  init: {
    files: [{
      cwd: 'init/php/templates',
      src: '<%= init.php.templates %>',
      dest: 'src/php/templates',
      expand: true
    }, {
      cwd: 'init/php/includes',
      src: '<%= init.php.includes %>',
      dest: 'src/php/includes',
      expand: true
    }, {
      cwd: 'init/js',
      src: '<%= init.scripts %>',
      dest: 'src/js',
      expand: true
    }, {
      cwd: 'init/css',
      src: 'style.css',
      dest: 'src/css',
      expand: true,
      options: {
        process: function (content, srcpath) {
          return grunt.template.process(content);
        }
      }
    }]
  }
};

Css file (wordpress)

/*
Theme Name: <%= init.project.name %>
Theme URI: 
Description: Description
Version: 1.0
Author: Name
Author URI: uri
Tags: Tags
*/

I've tried this answer, but processContent has been replaced by process, and this answer does not seem to work any longer (even when changing processContent to process).

Community
  • 1
  • 1

1 Answers1

5

You put options in wrong place, move it out and it should work.

module.exports = function (grunt) {
  return {
      init: {
        files: [{
          cwd: 'init/php/templates',
          src: '<%= init.php.templates %>',
          dest: 'src/php/templates',
          expand: true
        }, {
          cwd: 'init/php/includes',
          src: '<%= init.php.includes %>',
          dest: 'src/php/includes',
          expand: true
        }, {
          cwd: 'init/js',
          src: '<%= init.scripts %>',
          dest: 'src/js',
          expand: true
        }, {
          cwd: 'init/css',
          src: 'style.css',
          dest: 'src/css',
          expand: true
        }],
        options: {
          process: function (content, srcpath) {
            return grunt.template.process(content);
          }
        }
      }
  }
};

Updated to suit to load-grunt-config

emn178
  • 1,474
  • 8
  • 12
  • I've tried this, but it gives me the following error: "Warning: Error while processing ... file". Where the ... file is one of the files being copied (but not the file containing template tags). Do all files that are copied when this option is turned on need to contain template tags? Do you know what we can do about this? –  Apr 29 '14 at 21:09
  • All files will be copied. The error seems that is another problem. What are the variables init.php.templates, includes and scripts...? – emn178 Apr 30 '14 at 02:24
  • It does seem like that, but when I remove the options block, everything works as expected. When I include the options block, I get an error with the first file that the copy task encounters (which is a file that does not contain template tags, only the style.css file does). The variables come from [another grunt task](http://stackoverflow.com/a/23353473/1694077), which works as expected. –  Apr 30 '14 at 07:35
  • Maybe `grunt` is undefined and causes error. According document load-grunt-config writes, you could pass grunt object by this way. module.exports = function (grunt) { return { init: {...} } }; – emn178 Apr 30 '14 at 08:26
  • Hey @Sam did you test your hypothesis about files that don't contain template tags? See if it works with the options block included, and only files present that contain template tags. – Charney Kaye May 01 '14 at 03:20
  • Ah, that works. Since I have multiple copy tasks I had to wrap them all in the one `return {}` block, but once I did that it worked. Thanks for helping me with this! –  May 01 '14 at 08:09