{"action":"opened","issue":{"url":"https://api.github.com/repos/prakhar1989/grunt-newman/issues/3","labels_url":"https://api.github.com/repos/prakhar1989/grunt-newman/issues/3/labels{/name}","comments_url":"https://api.github.com/repos/prakhar1989/grunt-newman/issues/3/comments","events_url":"https://api.github.com/repos/prakhar1989/grunt-newman/issues/3/events","html_url":"https://github.com/prakhar1989/grunt-newman/issues/3","id":105578279,"number":3,"title":"Grunt-newman task in loop","user":{"login":"sudazzle","id":6572155,"avatar_url":"https://avatars.githubusercontent.com/u/6572155?v=3","gravatar_id":"","url":"https://api.github.com/users/sudazzle","html_url":"https://github.com/sudazzle","followers_url":"https://api.github.com/users/sudazzle/followers","following_url":"https://api.github.com/users/sudazzle/following{/other_user}","gists_url":"https://api.github.com/users/sudazzle/gists{/gist_id}","starred_url":"https://api.github.com/users/sudazzle/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sudazzle/subscriptions","organizations_url":"https://api.github.com/users/sudazzle/orgs","repos_url":"https://api.github.com/users/sudazzle/repos","events_url":"https://api.github.com/users/sudazzle/events{/privacy}","received_events_url":"https://api.github.com/users/sudazzle/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-09-09T11:31:07Z","updated_at":"2015-09-09T11:31:07Z","closed_at":null,"body":"My idea is to run a test. Read the output file and create an array of data and create a loop that creates the request filling the data from array and run it. But the issue is, as illustrated in the screen shot. It is working fine but at the end of the iteration the information is printed multiple times. More the loop runs the information gets printed more like gets doubled in every loop. I tried to work out but couldn't solve it. I need help. I have posted my codes as well:\r\n\r\n\r\n    (function() {\r\n    'use strict';\r\n\tvar hjson = require('hjson'),\r\n\t\tfs = require('fs'),\r\n\t\t_ = require('underscore'),\r\n\t\tconfig = require('./config'),\r\n\t\tgrunt = require('grunt');\r\n\t\r\n\t\tfunction replaceValue(i,o,key,value) {\r\n\t\t\tvar remaining = '';\r\n\t\t\ti.on('data', function(data) {\t\t\t\t\r\n\t\t\t\tremaining += data;\r\n\t\t\t\tremaining = remaining.trim();\r\n\t\t\t\t//console.log('\"data\": \"{\\\\\"' + key + '\\\\\": \\\\\"');\r\n\t\t\t\tvar index = remaining.indexOf('\\n');\t\t\t\t\r\n\t\t\t\t//console.log(index);\r\n\t\t\t\twhile(index > -1)\r\n\t\t\t\t{\r\n\t\t\t\t\tvar line = remaining.substring(0, index);\r\n\t\t\t\t\tvar replace_value_index = line.indexOf('\"data\": \"{\\\\\"' + key + '\\\\\": \\\\\"');\t\r\n\t\t\t\t\tif (replace_value_index > -1)\t\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tline = '\"data\": \"{\\\\\"' + key + '\\\\\": \\\\\"' + value + '\\\\\"}\",';\r\n\t\t\t\t\t}\t\t\r\n\t\t\t\t\to.write(line + '\\n');\r\n\t\t\t\t\tremaining = remaining.substring(index + 1);\t\t\t\t\t\r\n\t\t\t\t\tindex = remaining.indexOf('\\n');\t\t\t\t\t\r\n\t\t\t\t}\t\t\t\t\r\n\t\t\t});\r\n\t\t\ti.on('end', function() {\t\t\t\t\r\n\t\t\t\to.end();\r\n\t\t\t\to.on('finish', function() {\t\t\t\r\n\t\t\t\t\trunner(config.folder_multipletimerun, config.request_multipletimerun,runMultipleTest);\r\n\t\t\t\t\tcounter = counter + 1;\t\t\t\t\t\t\r\n\t\t\t\t});\r\n\t\t\t});\t\r\n\t\t}\t\r\n\r\n\t\tvar loopingData = []; \r\n\t\tvar counter = 0;\t\t\r\n\r\n\t\tfunction createMultipleTests() {\t\t\t\t\r\n\t\t\tconfig.generateMultipleTests = 'no';\t\t\t\r\n\t\t\tvar inputObj = fs.createReadStream('./outfile.json', {flags: 'r', mode: '0666', bufferSize: 4 * 1024});\t\t\t\r\n\t\t\tinputObj.on('data', function(data) {\t\t\t\t\r\n\t\t\t\tdata = JSON.parse(data);\t\t\t\t\r\n\t\t\t\tvar variable_values = _.find(data.environment.values, {'key': 'productcodes'});\t\t\t\t\r\n\t\t\t\tloopingData = String(variable_values.value).split(\",\");\t\t\t\t\r\n\t\t\t});\t\r\n\r\n\t\t\tinputObj.on('end', function(data) {\t\r\n\t\t\t\trunMultipleTest(loopingData[counter]);\t\t\t\t\r\n\t\t\t});\r\n\t\t}\r\n\r\n\t\tfunction runMultipleTest(replacingData) {\t\t\t\t\t\t\t\t\r\n\t\t\tvar inputObj2 = fs.createReadStream(config.request_multipletimerun_prerun + 'createorgentitlement_getpaymenttoken.hjson', {flags: 'r', mode: '0666', bufferSize: 4 * 1024});\t\r\n\t\t\tvar output2 = fs.createWriteStream(config.request_multipletimerun + 'runthismultipletime.hjson');\r\n\t\t\tvar replacing = replaceValue(inputObj2, output2, 'product_code', replacingData);\t\t\t\t\t\t\t\t\t\r\n\t\t}\t\r\n\r\n\t\tfunction runNewmanTask(createMultipleTests) {\t\r\n\t\t\t\r\n\t\t\tgrunt.tasks('newman', {}, function(){\t\r\n\t\t\t\tconsole.log('hello');\t\t\t\r\n\t\t\t\tif (config.generateMultipleTests === 'yes')\r\n\t\t\t\tcreateMultipleTests();\r\n\t\t\t});\t\t\t\r\n\t\t}\r\n\r\n\t\tfunction readDirectory(directoryname, output, isPrerun){\t\t\t\r\n\t\t\tvar counter=1;\t\t\r\n\t\t\tfs.readdir(directoryname, function(err, files){\r\n\t\t\t\tif (err) throw err;\t\t    \r\n\t\t\t    var total_no_of_files = files.length;\r\n\t\t\t    files.forEach(function(file){\t\t    \t\r\n\t\t\t\t\tvar input = fs.createReadStream(directoryname + file,{flags: 'r',mode: '0666', bufferSize: 4 * 1024});\r\n\t\t\t    \tvar completeWrite = readwrite(input,output,directoryname, counter, total_no_of_files, isPrerun);\t\t\t    \t\t\t    \t\t        \r\n\t\t\t        counter++;\r\n\t\t\t    });\t\t\t\t    \t\t\t    \t   \r\n\t\t\t});\t\t\r\n\t\t}\r\n\r\n\t\t//this function reads and writes a file. Reads a readable mutiline test codes and convets \r\n\t\t// into one line as required by grunt-newman. The basic idea is to look for the string \"tests\"\r\n\t\t// If \"tests\" is found set istest to true and start combining all the test code into a single line\"\r\n\t\t// Secondly look for the string \"time\", if found it will write the all the tests\r\n\t\t// and go to the normal readwrite mode.\r\n\t\tfunction readwrite(input, output, directoryname, counter, total_no_of_files, isPrerun)\r\n\t\t{\r\n\t\t\tvar jsontxt = '';\r\n\t\t\tvar remaining = '';\r\n\t\t\tvar tests = '';\r\n\t\t\tvar istest = false;\r\n\t\t\tvar istime = false;\t\t\t\r\n\r\n\t\t\tif(directoryname.indexOf('/folders/') > -1)\r\n\t\t\t{\r\n\t\t\t\tinput.on('data', function(data){\r\n\t\t\t\t\tremaining += data;\r\n\t\t\t\t\tremaining = remaining.trim();\r\n\t\t\t\t\tvar index = remaining.indexOf('\\n');\r\n\t\t\t\t\twhile(index > -1)\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tvar line = remaining.substring(0, index);\t\t\t\t\t\r\n\t\t\t\t\t\toutput.write(line + '\\n');\r\n\t\t\t\t\t\tremaining = remaining.substring(index + 1);\t\t\t\t\t\r\n\t\t\t\t\t\tindex = remaining.indexOf('\\n');\t\t\t\t\t\r\n\t\t\t\t\t}\r\n\t\t\t\t\t// if(counter == 1)\r\n\t\t\t\t\t// {\r\n\t\t\t\t\t// \toutput.write('}, \\n');\r\n\t\t\t\t\t// }\r\n\t\t\t\t\tif (counter < total_no_of_files)\t\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\toutput.write('}, \\n');\t\t\t\t\t\r\n\t\t\t\t\t}\r\n\t\t\t\t\tif(counter == total_no_of_files)\r\n\t\t\t\t    {\t\t    \t\r\n\t\t\t\t    \toutput.write('\\n } \\n], \\n');\r\n\t\t\t\t    \toutput.write('\\n \"timestamp\": 1409223830965, \\n \"synced\": false, \\n' );\r\n\t\t\t\t    \toutput.write('\\n \"requests\": [ \\n');\r\n\t\t\t\t    \tinput.on('end', function() {\t\t\t\t\r\n\t\t\t\t\t\t  output.end();\t\t\t\r\n\t\t\t\t\t\t});\t\r\n\t\t\t\t    }\r\n\t\t\t\t});\r\n\t\t\t}\r\n\r\n\t\t\tif(directoryname.indexOf('/requests/') > -1)\r\n\t\t\t{\r\n\t\t\t\t//console.log('got here');\r\n\t\t\t\tinput.on('data', function(data){\r\n\t\t\t\t\tremaining += data;\r\n\t\t\t\t\tremaining = remaining.trim();\r\n\t\t\t\t\tvar index = remaining.indexOf('\\n');\r\n\t\t\t\t\twhile(index > -1)\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tvar line = remaining.substring(0, index);\r\n\t\t\t\t\t\tremaining = remaining.substring(index + 1);\t\t\t\t\t\r\n\t\t\t\t\t\tindex = remaining.indexOf('\\n');\r\n\r\n\t\t\t\t\t\tvar indexfortests = line.indexOf('\\\"tests\\\":');\r\n\t\t\t\t\t\tvar indexfortime = line.indexOf('\\\"time\\\":');\t\t\t\t\r\n\t\t\t\t\t\r\n\t\t\t\t\t\tif (indexfortests > 0 && indexfortime < 0)\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\tistest = true;\r\n\t\t\t\t\t\t\toutput.write(jsontxt);\r\n\t\t\t\t\t\t\tjsontxt = '';\r\n\t\t\t\t\t\t}\r\n\r\n\t\t\t\t\t\tif (indexfortests < 0 && indexfortime > 0)\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\tistime = true;\r\n\t\t\t\t\t\t\tistest = false;\t\t\t\t\t\r\n\t\t\t\t\t\t}\r\n\r\n\t\t\t\t\t\tif (istest === true)\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\ttests = tests + line + ' \\\\n ';\r\n\t\t\t\t\t\t}\r\n\r\n\t\t\t\t\t\tif(istime === true && istest === false)\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\ttests = tests.substr(0,tests.length - 3);\t\t\t\t\t\r\n\t\t\t\t\t\t\toutput.write(tests + '\\n');\t\r\n\t\t\t\t\t\t\ttests = '';\t\t\t\t\r\n\t\t\t\t\t\t\tistime = false;\r\n\t\t\t\t\t\t}\r\n\r\n\t\t\t\t\t\tif (istest === false)\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\tjsontxt = jsontxt + line + '\\n';\r\n\t\t\t\t\t\t}\r\n\r\n\t\t\t\t\t}\r\n\t\t\t\t\toutput.write(jsontxt);\r\n\t\t\t\t\tjsontxt = '';\r\n\t\t\t\t\tif (istest === false && istime === false)\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\t\r\n\t\t\t\t\t\tif(counter < total_no_of_files)\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\toutput.write('}, \\n');\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t\t// if(counter > 1 )\r\n\t\t\t\t\t\t// {\r\n\t\t\t\t\t\t// \toutput.write(', \\n');\r\n\t\t\t\t\t\t// }\t\t\t\t\r\n\t\t\t\t\t}\r\n\t\t\t\t\t\t\t    \r\n\t\t\t\t    if(counter === total_no_of_files && isPrerun !== true)\r\n\t\t\t\t    {\t\t    \t\r\n\t\t\t\t    \t output.write('\\n } \\n ] \\n }');\r\n\t\t\t\t    \t //console.log(\"The file has been written\");\t\t\t\t    \t\t    \t \t    \t\t\t    \t \r\n\t\t\t\t    }\r\n\t\t\t\t    else\r\n\t\t\t\t    {\r\n\t\t\t\t    \toutput.write('}');\r\n\t\t\t\t    \tinput.on('end', function() {\t\t\t\t\r\n\t\t\t\t\t\t  output.end();\t\t\t\r\n\t\t\t\t\t\t});\t\r\n\t\t\t\t    }\t \r\n\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t\t \r\n\t\t}\t\r\n\t\t\r\n\t\tfunction runner(dir_folder, dir_request, runmultipletext) {\t\t\t\r\n\t\t\tvar mainRunnerfile = fs.createReadStream('./tests/main_runner.json', {flags: 'r',mode: '0666', bufferSize: 20 * 1024});\r\n\t\t\tvar outputfile = fs.createWriteStream('postmanCollection.json');\r\n\t\t\toutputfile.on('open', function() {\r\n\t\t\t\tmainRunnerfile.on('data', function(data) {\r\n\t\t\t\t\tdata = data.toString();\r\n\t\t\t\t\tdata = data.trim();\r\n\t\t\t\t\toutputfile.write(data);\t\r\n\t\t\t\t});\t\t\t\t\t\t\t\r\n\t\t\t\treadDirectory(dir_folder, outputfile);\t\t\t\t\r\n\t\t\t\treadDirectory(dir_request, outputfile);\t\r\n\t\t\t});\t\r\n\r\n\t\t\toutputfile.on('finish', function() {\t\t\t\t\r\n\t\t\t\tif(config.generateMultipleTests === 'yes')\r\n\t\t\t\t{\t\r\n\t\t\t\t\trunNewmanTask(createMultipleTests);\t\t\t\t\t\t\r\n\t\t\t\t}\r\n\t\t\t\telse\r\n\t\t\t\t{\r\n\t\t\t\t\trunNewmanTask();\t\t\t\t\t\r\n\t\t\t\t}\t\t\t\t\t\t\t\r\n\t\t\t});\t\r\n\r\n\t\t\tif(typeof(runmultipletext) === 'function')\r\n\t\t\t{\t\t\t\t\r\n\t\t\t\tif (counter < loopingData.length)\r\n\t\t\t\t{\t\t\r\n\t\t\t\t\t//runmultipletext(loopingData[counter]);\t\t\t\t\t\t\t\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\trunner(config.folder_directoryname, config.request_directoryname);\r\n    })();\r\n\r\n![screenshot from 2015-09-09 16 52 04](https://cloud.githubusercontent.com/assets/6572155/9760285/6f6aa7c8-5713-11e5-90eb-c2e76540bccb.png)"}}