ci: check that there are no unused contributor images (#41290)

When a contributor was removed from `contributors.json`, the
corresponding image should also be removed from
`aio/content/images/bios/`. However, this was often overlooked,
resulting in unused images remaining in `aio/content/images/bios/`.

This commit adds a check to ensure that all images in
`aio/content/images/bios/` are referenced in `contributors.json`.

PR Close #41290
This commit is contained in:
George Kalpakas 2021-03-22 19:00:20 +02:00 committed by Joey Perrott
parent ac66b0199e
commit 80f11a9b86
1 changed files with 14 additions and 4 deletions

View File

@ -1,7 +1,7 @@
#!/usr/bin/env node
// Imports
const {existsSync, readFileSync, statSync} = require('fs');
const {readdirSync, readFileSync, statSync} = require('fs');
const {join, resolve} = require('path');
// Constants
@ -17,19 +17,29 @@ _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));
const existingImages = readdirSync(IMAGES_DIR)
.filter(name => name !== '_no-one.jpg')
.map(name => join(IMAGES_DIR, name));
// Check that there are no missing images.
const missingImages = expectedImages.filter(path => !existingImages.includes(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 unused images.
const unusedImages = existingImages.filter(path => !expectedImages.includes(path));
if (unusedImages.length > 0) {
throw new Error(
'The following pictures are not referenced in \'contributors.json\' and should be deleted:' +
unusedImages.map(path => `\n - ${path}`).join(''));
}
// Check that there are no images that exceed the size limit.
const tooLargeImages = expectedImages.filter(path => statSync(path).size > MAX_IMAGE_SIZE);
if (tooLargeImages.length > 0) {