#!/usr/bin/env node

// Imports
const {existsSync, readFileSync, statSync} = require('fs');
const {basename, join, resolve} = require('path');

// Constants
const MAX_IMAGE_SIZE = 30 * 1024;  // 30kb
const CONTENT_DIR = resolve(__dirname, '../../content');
const IMAGES_DIR = join(CONTENT_DIR, 'images/bios');
const CONTRIBUTORS_PATH = join(CONTENT_DIR, 'marketing/contributors.json');
const EXISTING_GROUPS = new Set(['Angular', 'GDE', 'Collaborators']);

// The list of profile images that exceed specified `MAX_IMAGE_SIZE` limit.
// These images were added before the size check was introduced. Exclude these images
// from size check for now, but still check other images (more importantly run the check
// for new PRs where profile images are added).
const EXCLUDE_FROM_SIZE_CHECK = new Set([
  'alainchautard.png', 'ahsanayaz.jpg', 'alan-agius4.jpg', 'andrew-kushnir.jpg',
  'brian-love.jpg', 'cexbrayat.jpg', 'christianliebel.jpg', 'patovargas.png', 'gerardsans.jpg',
  'jessicajaniuk.jpg', 'JiaLiPassion.jpg', 'juristr.jpg', 'katerina.jpg', 'kimmaida.jpg',
  'kyliau.jpg', 'lacolaco.jpg', 'leonardo.jpg', 'nirkaufman.jpg', 'sajee.jpg', 'sonukapoor.jpg',
  'tracylee.jpg', 'twerske.jpg', 'wesgrimes.jpg'
]);

// Run
_main();

// Functions - Definitions
function _main() {
  const contributors = JSON.parse(readFileSync(CONTRIBUTORS_PATH, 'utf8'));

  // Check that there are no missing images.
  const expectedImages = Object.keys(contributors)
      .filter(key => !!contributors[key].picture)
      .map(key => join(IMAGES_DIR, contributors[key].picture));
  const missingImages = expectedImages.filter(path => !existsSync(path));

  if (missingImages.length > 0) {
    throw new Error(
        'The following pictures are referenced in \'contributors.json\' but do not exist:' +
        missingImages.map(path => `\n  - ${path}`).join(''));
  }

  // Check that there are no images that exceed the size limit.
  const tooLargeImages = expectedImages
       .filter(path => !EXCLUDE_FROM_SIZE_CHECK.has(basename(path)))
       .filter(path => statSync(path).size > MAX_IMAGE_SIZE);
  if (tooLargeImages.length > 0) {
    throw new Error(
        `The following pictures exceed maximum size limit of ${MAX_IMAGE_SIZE / 1024}kb:` +
        tooLargeImages.map(path => `\n  - ${path}`).join(''));
  }

  // Verify that all keys are sorted alphabetically
  const keys = Object.keys(contributors);
  for (let i = 1; i < keys.length; i++) {
    if (keys[i - 1].toLowerCase() > keys[i].toLowerCase()) {
      throw new Error(
        `The following keys in 'contributors.json' are not in alphabetical order: '${keys[i - 1]}' and '${keys[i]}'.`
      );
    }
  }

  Object.entries(contributors).forEach(([key, entry]) => {
    // Make sure `lead` and `mentor` fields refer to existing entries
    if (entry.lead && !contributors[entry.lead]) {
      throw new Error(`The '${key}' entry contains 'lead' field, but it refers to non-existing entry ('${entry.lead}').`);
    }
    if (entry.mentor && !contributors[entry.mentor]) {
      throw new Error(`The '${key}' entry contains 'mentor' field, but it refers to non-existing entry ('${entry.mentor}').`);
    }

    // Verify that `groups` field is always present and contains existing groups
    if (!entry.groups || !Array.isArray(entry.groups) || entry.groups.length === 0) {
      throw new Error(`The 'groups' field should be defined as a non-empty array (entry: '${key}').`);
    }
    if (entry.groups.some(group => !EXISTING_GROUPS.has(group))) {
      throw new Error(`The '${key}' entry contains 'groups' field with unknown values ` +
        `(groups: ${JSON.stringify(entry.groups)}, known values: ${JSON.stringify(Array.from(EXISTING_GROUPS))}).`);
    }
  });
}

@@ -96,18 +96,8 @@
 st %7B
-basename, 
 join
@@ -126,16 +126,16 @@
 path');%0A
+
 %0A// Cons
@@ -444,767 +444,8 @@
 );%0A%0A
-// The list of profile images that exceed specified %60MAX_IMAGE_SIZE%60 limit.%0A// These images were added before the size check was introduced. Exclude these images%0A// from size check for now, but still check other images (more importantly run the check%0A// for new PRs where profile images are added).%0Aconst EXCLUDE_FROM_SIZE_CHECK = new Set(%5B%0A  'alainchautard.png', 'ahsanayaz.jpg', 'alan-agius4.jpg', 'andrew-kushnir.jpg',%0A  'brian-love.jpg', 'cexbrayat.jpg', 'christianliebel.jpg', 'patovargas.png', 'gerardsans.jpg',%0A  'jessicajaniuk.jpg', 'JiaLiPassion.jpg', 'juristr.jpg', 'katerina.jpg', 'kimmaida.jpg',%0A  'kyliau.jpg', 'lacolaco.jpg', 'leonardo.jpg', 'nirkaufman.jpg', 'sajee.jpg', 'sonukapoor.jpg',%0A  'tracylee.jpg', 'twerske.jpg', 'wesgrimes.jpg'%0A%5D);%0A%0A
 // R
@@ -1144,16 +1144,16 @@
  limit.%0A
+
   const 
@@ -1187,85 +1187,8 @@
 ages
-%0A       .filter(path =%3E !EXCLUDE_FROM_SIZE_CHECK.has(basename(path)))%0A       
 .fil

remove image size exclusion list (#41292)