3

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.

patz
  • 1,306
  • 4
  • 25
  • 42

3 Answers3

3

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?

jmartins
  • 991
  • 4
  • 16
  • I have used grunt contrib concat, but I an new to grunt, dont know how to merge these two plugins into each other. – patz Apr 15 '15 at 14:48
  • Is there a way I can use grunt contrib contact and append or prepend tags using that. – patz Apr 15 '15 at 14:48
  • yes, take a look on `banner` and `footer` option: https://github.com/gruntjs/grunt-contrib-concat#banner – jmartins Apr 15 '15 at 15:38
  • 1
    If grunt-contrib-concat does not fit your requirement, try adding strings to replace in the beginning and end of your files and replacing it using [grunt-string-replace](https://github.com/erickrdch/grunt-string-replace) – jmartins Apr 15 '15 at 15:46
  • You main question have been answered. Please mark as the answer if it fits what you were looking for. Thanks. – jmartins Apr 15 '15 at 15:59
  • So the banner and footer work for me, the problem I have that my string is a script tag " – patz Apr 15 '15 at 16:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/75315/discussion-between-patz-and-jmartins). – patz Apr 15 '15 at 16:52
3

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.

0

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

Replace all the text with specified replacement using grunt replace

Community
  • 1
  • 1
patz
  • 1,306
  • 4
  • 25
  • 42