var captchagen = require('captchagen');
var fs = require('fs');

var dataFilePath = './output/data.csv';
var imageOutputDir = './output/images/';
var imageUrlPrefix = 'http://localhost:3000/';
var times = +process.argv[2];

// generate images
var captchas = [];
for (var i = 0; i < times; ++i) {
  var captcha = captchagen.create();
  var filePath = imageOutputDir + i.toString() + '.png';
  console.log(captcha.text(), filePath);

  captcha.generate();

  captcha.buffer((e, buffer) => {
    fs.writeFile(filePath, buffer);
  });

  captchas.push(captcha);
}

// generate csv
var header = ['image', 'answer'].join(', ');
var body = captchas.map((captcha, i) => [
  imageUrlPrefix + i.toString() + '.png',
  captcha.text()
].join(', '));
var data = header + '\n' + body.join('\n');
fs.writeFile(dataFilePath, data);

@@ -224,74 +224,211 @@
 %0A// 
-generate images%0Avar captchas = %5B%5D;%0Afor (var i = 0; i %3C times; ++i)
+remove old images%0Afs.readdirSync(imageOutputDir).forEach((file) =%3E %7B%0A  if (file.match(/%5C.png$/)) %7B%0A    fs.unlinkSync(imageOutputDir + file);%0A  %7D%0A%7D)%0A%0A// generate images%0Avar generateCaptcha = (filename) =%3E
  %7B%0A 
@@ -501,70 +501,16 @@
 r + 
-i.toString() + '.png';%0A  console.log(captcha.text(), filePath)
+filename
 ;%0A%0A 
@@ -614,29 +614,227 @@
 %0A%0A  
-captchas.push(captcha
+return captcha;%0A%7D;%0Avar captchas = %5B%5D;%0Afor (var i = 0; i %3C times; ++i) %7B%0A  var filename = i.toString() + '.png';%0A  var captcha = generateCaptcha(filename);%0A  captchas.push(captcha);%0A  console.log(captcha.text(), filename
 );%0A%7D
@@ -891,17 +891,16 @@
 .join(',
- 
 ');%0Avar 
@@ -1005,17 +1005,16 @@
 .join(',
- 
 '));%0Avar

remove old images