2

Goodmornig,

I'm using grunt-replace (https://github.com/outaTiME/grunt-replace) inside my gruntfile to replace some string in a html file by loading a json object from a json file.

I want to add some flexibility to this approach and i customized another task called 'setopts' that simply add some properties to the grunt.option that i use i the 'replace' task in the following way:

replace: {
    common: {
      options: {
        patterns: [
          {
            json: '<%=grunt.option("locales")%>'
          }
        ]
      },
      files: [
        {expand: true, flatten: true, src: ['public/sites/<%=grunt.option("domain")%>/index.html'], dest: 'public/sites/<%=grunt.option("domain")%>/'},
      ]
    }
}    

Here my 'setopts' task :

grunt.registerTask('setopts', function (domain) {

  locales = grunt.file.readJSON('src/locales/domain/myfile.json');
  grunt.option('locales', locales);

  grunt.option('domain', domain);

}  

I run the following task :

grunt.registerTask('maintask',    [ 'setopts:mydomain', 'replace:common']);

After some attempts i found that the 'files' property in the 'replace' task works fine but i get an error in the 'patterns' property :

Processing source...ERROR Warning: Error while processing "public/sites/xxxxx/index.html" file. Use --force to continue.

What's going wrong with this?

Thanks for any comment!

Giggioz
  • 457
  • 7
  • 21

1 Answers1

0

I know I'm 1.5 years late, but maybe some other people might need the answer to this.

The way I made it work was by not using grunt.option. Instead, I used grunt.config.set.

replace: {
    common: {
      options: {
        patterns: [
          {
            json: '<%= locales %>'
          }
        ]
      },
      files: [
        {expand: true, flatten: true, src: ['public/sites/<%= domain %>/index.html'], dest: 'public/sites/<%= domain %>/'},
      ]
    }
}   

Notice the way the locales variable is used as a value to the json property.

This is the setopts task:

grunt.registerTask('setopts', function (domain) {

  locales = grunt.file.readJSON('src/locales/domain/myfile.json');
  grunt.config.set('locales', locales);

  grunt.config.set('domain', domain);

}  

Hopefully it helps somebody :)

This question helped me find the answer Programmatically pass arguments to grunt task?

Adrian Marinica
  • 2,191
  • 5
  • 29
  • 53