3

I am trying to run a task in grunt, it goes to a URL and saves the response in a file, but I want it to go to different URLs and make different files accordingly. So I am running a loop and at every iteration changing the data in config. But the task runs are appended at the end of the loop, so when loop is done changing all the values in config it runs the task 30 times with the latest changed values in the config, finally creating only 1 file 30 times again and again. Here is my code

module.exports = function(grunt){
 grunt.initConfig({
  id:0,
  http:{
   devel:{
   options: {
     url: 'http://127.0.0.1:8000/foo/<%= id %>/'
   },
   dest: 'www/foos/foo<%= id %>.json'
   }
  }
 });
 grunt.loadNpmTasks('grunt-http');   
 grunt.registerTask("default", function(){
    for (var i = 30; i > 1; i--) {
      grunt.config.set("id", i);
      var d = grunt.config.get("id");
      grunt.log.writeln("id = "+d);
      grunt.task.run("http");
    }
  });
};
  • Maybe this link will be of help: http://stackoverflow.com/a/37716046/3397771 – 76484 Jun 10 '16 at 21:56
  • This wasn't helpful, as i want to pass some argument to the task itself, in a sense.(The argument to be passed will be the iteration index of the loop) – Ishu Bansal Jun 13 '16 at 06:19

1 Answers1

0

I'm not 100% sure what it is you're trying to do, but this code should work. You need to create a unique task for each http task you want to run.

module.exports = function(grunt){
    grunt.initConfig({
        http:{
        }
    });
    grunt.loadNpmTasks('grunt-http');

    var http = {};

    for (var i = 5; i > 1; i--) {
        grunt.log.writeln("id = " + i);

        http['devel' + i] = {
            options: {
              url: 'http://127.0.0.1:8000/foo/' + i
            },
            dest: 'www/foos/foo' + i + '.json'
        };
    }

    grunt.config.set("http", http);

    grunt.registerTask("default", "http");
};
Mike Mellor
  • 1,316
  • 17
  • 22