patz
patz

Reputation: 1316

appending text to multiple files using grunt-file-append

How to append text to multiple files using grunt-file-append

https://www.npmjs.com/package/grunt-file-append

grunt.initConfig({
  file_append: {
    default_options: {
      files: [
        {
          append: "text to append",
          prepend: "text to prepend",
          input: '/path/to/input/file'
          output: 'path/to/output/file'
        }
      ]
    }
  }
})

if I write the function in this way, for appending to multiple files it cases an error.

grunt.initConfig({
  file_append: {
    default_options: {
      files: [
        {
          append: "text to append",
          prepend: "text to prepend",
          input: './path/to/input/*.html'
          output: 'path/to/output/*.html'
        }
      ]
    }
  }
})

I get the following error :

Running "file_append:default_option" (file_append) task
>> Source file "./path/to/output/*.html" not found.
Warning: Task "file_append:default_option" failed. Use --force to continue.

Aborted due to warnings.

appending just to a single file works but not for multiple files, any thing i am doing wrong here.

Upvotes: 3

Views: 2292

Answers (3)

user1714053
user1714053

Reputation: 31

As @jmartins mentioned the code just isn't set up to deal with 'something/*.html', I think the only way to append multiple files (other than by amending the source code) is to have multiple objects in the array:

file_append: {
        default_options: {
            files: [{
                prepend: 'something',
                input: '<%= config.dist %>/<%= config.distScripts %>/script1.js',
                output: '<%= config.dist %>/<%= config.distScripts %>/script1.js'
            }, {
                prepend: 'something',
                input: '<%= config.dist %>/<%= config.distScripts %>/script2.js',
                output: '<%= config.dist %>/<%= config.distScripts %>/script2.js'
            }, {
                prepend: 'something',
                input: '<%= config.dist %>/<%= config.distScripts %>/script3.js',
                output: '<%= config.dist %>/<%= config.distScripts %>/script3.js'
            }]
        }
    }

This isn't great when you have a lot of files to update however so it would most likely be easier to simply update the source to do what you need if there's going to be a lot of files, or if there isn't a finite list and as such you really don't want to be updating the grunt file constantly.

Upvotes: 3

patz
patz

Reputation: 1316

This is the way I added the script tags and Id's dynamically

Replace all the text with specified replacement using grunt replace

Upvotes: 0

jmartins
jmartins

Reputation: 991

I don't think it is supposed to work. As you can see in the github code for grunt-file-append:

prepend = file.prepend || ""
append  = file.append  || ""
fileContent = grunt.file.read filepath
value = "#{ prepend }#{ fileContent }#{ append }"
grunt.file.write filepath, value

It only reads one file and appends/prepends on it.

Have you tried grunt-contrib-concat?

Upvotes: 3

Related Questions