1

I'm trying to use grunt-contrib-copy (version 0.4.1) to rewrite some strings inside a file during the build process. As a test I can copy the file, so it's not a permissions or location problem.

These are the options for the fileProcessor task (in Coffeescript syntax) to copy the file:

fileProcessor:
    files: [
        expand: true
        cwd: "target/js/"
        dest: "target/js/"
        src: ["**/test-*.js"]
        rename: (dest, src) ->
          grunt.verbose.writeln src, " => ", dest + src.substring(0, src.lastIndexOf("/") + 1) + "foo.js"
          dest + src.substring(0, src.lastIndexOf("/") + 1) + "foo.js"
    ]

I get the following output, everything worked as expected:

Running "copy:fileProcessor" (copy) task
Verifying property copy.fileProcessor exists in config...OK
test-25fd6a1c3a890933.js  =>  target/js/foo.js
Files: target/js/test-25fd6a1c3a890933.js -> target/js/foo.js
Options: processContent=false, processContentExclude=[]
Options: processContent=false, processContentExclude=[]
Copying target/js/test-25fd6a1c3a890933.js -> target/js/foo.js
Reading target/js/test-25fd6a1c3a890933.js...OK
Writing target/js/foo.js...OK
Copied 1 files

So far so good. Clearly grunt-contrib-copy has no problem with the file itself. So I replace the the rename task with a process task, and use a simple regex to see if it works:

fileProcessor:
    files: [
        expand: true
        cwd: "target/js/"
        dest: "target/js/"
        src: ["**/test-*.js"]
        processContent: (content, src) ->
            re = new RegExp("(angular)+")
            content.replace(re, "foo")
    ]

Which produces the following output:

Running "copy:fileProcessor" (copy) task
Verifying property copy.fileProcessor exists in config...OK
Files: target/working/shared/scripts/bootstrap-25fd6a1c3a890933.js -> target/working/shared/scripts/bootstrap-25fd6a1c3a890933.js
Options: processContent=false, processContentExclude=[]
Options: processContent=false, processContentExclude=[]
Copying target/working/shared/scripts/bootstrap-25fd6a1c3a890933.js -> target/working/shared/scripts/bootstrap-25fd6a1c3a890933.js
Reading target/working/shared/scripts/bootstrap-25fd6a1c3a890933.js...OK
Writing target/working/shared/scripts/bootstrap-25fd6a1c3a890933.js...OK
Copied 1 files

So grunt-contrib-copy finds the file I need to process, but the processing never happens. processContent remains set to the default false.

Looking in copy.js, when grunt.file.copy(src, dest, copyOptions) is called, copyOptions is an object with only two properties:

  • process, which is false, and
  • noProcess, which is an empty array.

So my process options are never passed along. Any ideas on why this could be?

Smandoli
  • 6,919
  • 3
  • 49
  • 83
Fugue
  • 416
  • 6
  • 20

1 Answers1

4

your processContent is in the wrong place, it needs to be wrapped into options and moved one level higher. Also note that processContent is depreciated and replaced by process

fileProcessor:
    files: [
        expand: true
        cwd: "target/js/"
        dest: "target/js/"
        src: ["**/test-*.js"]
    ]
    options: 
        process: (content, src) ->
            re = new RegExp("(angular)+")
            content.replace(re, "foo")
Andreas
  • 489
  • 6
  • 14