function main()

in sse_uploader/src/cli-upload.js [33:68]


function main() {
  const config = loadConfigOrExit(program.config);
  FacebookAdsApi.init(config.access_token);

  const pixel = new AdsPixel(config.pixel_id);

  const testId = program.testMode ? config.test_id : null;

  let batches = [], batch = [];
  fs.createReadStream(program.input)
      .pipe(csv())
      .on('data', data => {
        if (!isEmptyObject(data)) {
          batch.push(getEventData(data));
          if (batch.length == config.batch_size) {
            batches.push(batch);
            batch = [];
          }
        }
      })
      .on('end', async () => {
        if (batch.length > 0)
          batches.push(batch);

        // Send all batches
        const sendPromises = [];
        for (let b of batches)
          sendPromises.push(sendEventsBatch(b, pixel, testId));

        try {
          await Promise.all(sendPromises);
        } catch(err) {
          console.error(err);
        }
      });
}