#!/usr/bin/env node
/* eslint-disable global-require, no-console, no-shadow */

require('@babel/register');

const fs = require('fs');
const path = require('path');

const QRCode = require('qrcode');
const config = require('config');

const fetch = (...args) =>
  import('node-fetch').then(({ default: fetch }) => fetch(...args));

const rootDir = path.join(__dirname, '..');
const distDir = path.join(rootDir, 'dist');
const destDir = path.join(distDir, 'qrcodes');
const lang = 'en-US';
if (!fs.existsSync(destDir)) {
  fs.mkdirSync(destDir);
}

(async () => {
  const res = await fetch(
    `https://addons.mozilla.org/api/v5/addons/search/?app=android&promoted=recommended&type=extension&lang=${lang}`,
  );
  const { results: addons } = await res.json();

  await Promise.all(
    addons.map(({ id, url }) => {
      const filePath = path.join(destDir, `${id}.png`);
      let content = url.replace(`/${lang}`, '');
      content = [
        `${content}?utm_campaign=addon-qr-code`,
        `utm_content=${id}`,
        `utm_medium=referral&utm_source=addons.mozilla.org`,
      ].join('&');
      return QRCode.toFile(filePath, content, { margin: 1 });
    }),
  );

  console.log(addons.length, ' QR codes created');

  const knownIds = config.get('addonIdsWithQRCodes');
  const addonIdsFromAPI = [...new Set(addons.map(({ id }) => id))];
  const differences = [
    ...addonIdsFromAPI.filter((x) => !knownIds.includes(x)),
    ...knownIds.filter((x) => !addonIdsFromAPI.includes(x)),
  ];

  if (differences.length > 0) {
    console.log(`'addonIdsWithQRCodes' might be outdated:`);
    console.log(`  addonIdsWithQRCodes: ${[...knownIds].sort()}`);
    console.log(`  IDs from API       : ${addonIdsFromAPI.sort()}`);
    process.exit(1);
  }
})();

@@ -155,24 +155,58 @@
 e('path');%0A%0A
+const config = require('config');%0A
 const QRCode
@@ -229,30 +229,30 @@
 de');%0Aconst 
-config
+rimraf
  = require('
@@ -247,30 +247,30 @@
  = require('
-config
+rimraf
 ');%0A%0Aconst f
@@ -410,17 +410,17 @@
 %0Aconst d
-i
+e
 stDir = 
@@ -443,100 +443,159 @@
 r, '
-dist');%0Aconst destDir = path.join(distDir, 'qrcodes');%0Aconst lang = 'en-US';%0Aif (!fs.existsS
+src', 'amo', 'components', 'AddonQRCode', 'img');%0Aconst lang = 'en-US';%0A%0A// Remove, then recreate, the img folder to clear pre-existing files.%0Arimraf.s
 ync(
@@ -602,22 +602,18 @@
 destDir)
-) %7B%0A  
+;%0A
 fs.mkdir
@@ -626,18 +626,16 @@
 estDir);
-%0A%7D
 %0A%0A(async
@@ -1249,24 +1249,77 @@
   %7D),%0A  );%0A%0A
+  const fileCount = fs.readdirSync(destDir).length;%0A%0A
   console.lo
@@ -1336,17 +1336,16 @@
 ength, '
- 
 QR codes
@@ -1349,18 +1349,279 @@
 des 
-created');
+requested');%0A  console.log(fileCount, 'files written');%0A%0A  if (fileCount !== addons.length) %7B%0A    console.log('Invalid number of files written:');%0A    console.log(%60$%7Baddons.length%7D files requested%60);%0A    console.log(%60$%7BfileCount%7D files written%60);%0A    process.exit(1);%0A  %7D
 %0A%0A  

Write images to component folder, clear folder first, and check for presence of files after (#10938)