Skip to content

Commit

Permalink
Merge pull request #8095 from newrelic/liz/delete-unused-images
Browse files Browse the repository at this point in the history
Add unused image deleting script
  • Loading branch information
LizBaker committed Jun 22, 2022
2 parents f382f3a + d91e50d commit 447eadc
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@
"db:clean": "./scripts/actions/translation_workflow/testing/cleanup.sh",
"i18n-utility": "npx ts-node ./scripts/i18n_utility/cli.ts",
"image-codemod": "./codemods/convertMDXImages/runImageWorkflow.sh",
"generate-nav-titles": " node scripts/getNavforTranslation.js"
"generate-nav-titles": " node scripts/getNavforTranslation.js",
"remove-unused-images": " node scripts/removeUnusedImages.js"
},
"husky": {
"hooks": {
Expand Down
60 changes: 60 additions & 0 deletions scripts/removeUnusedImages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const fs = require('fs');
const path = require('path');
const { IMAGE_ALLOWLIST } = require('./utils/constants');

const isFile = (filepath) => fs.statSync(filepath).isFile();
const isDirectory = (filepath) => fs.statSync(filepath).isDirectory();
const pathJoin = (base) => (filepath) => path.join(base, filepath);

const getDirectories = (filepath) =>
fs.readdirSync(filepath).map(pathJoin(filepath)).filter(isDirectory);

const getFiles = (filepath) =>
fs.readdirSync(filepath).map(pathJoin(filepath)).filter(isFile);

const getFilesRecursively = (filepath) =>
getDirectories(filepath)
.flatMap(getFilesRecursively)
.reduce((acc, file) => [...acc, file], getFiles(filepath));

const getAllImageImports = (path) => {
const files = getFilesRecursively(path);
const regex = new RegExp(`^import.+("|'|';|";)[ ]*$`, 'gm');
// imported images begin with 'import' and can end in ' or " with or without ; and any number of spaces after
const importStatements = files
.flatMap((file) => {
const textfile = fs.readFileSync(file, 'utf-8');
return textfile.match(regex);
})
.filter(Boolean);
const imageTitles = importStatements.map((importStatement) => {
return importStatement.split(`'`)[1].split(`/`)[1].replace(/\\/g, '');
// using replace here as the matched strings have escaped underscores
// and we need to remove the backslashes
});
return imageTitles;
};

const getAllImages = () => {
const imagesPaths = getFiles('src/images');
return imagesPaths.map((imagePath) => imagePath.split(`/`)[2]);
};

const deleteUnusedImages = () => {
const englishImportedImages = getAllImageImports('src/content/docs');
const i18nImportedImages = getAllImageImports('src/i18n/content');

const importedImages = englishImportedImages.concat(i18nImportedImages);
const inRepoImages = getAllImages();

const imagesNotBeingUsed = inRepoImages.filter(
(image) =>
!importedImages.includes(image) && !IMAGE_ALLOWLIST.includes(image)
);
console.log(
`Deleting ${imagesNotBeingUsed.length} image/s not being imported in any file`
);
imagesNotBeingUsed.forEach((image) => fs.unlinkSync(`src/images/${image}`));
};

deleteUnusedImages();
8 changes: 8 additions & 0 deletions scripts/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ module.exports = {
REORDER: 'REORDER',
},

IMAGE_ALLOWLIST: [
'quickstartTiles.svg',
'quickstartArrow.svg',
'favicon.png',
'bannerBackground.svg',
'bannerBackgroundDark.svg',
],

TYPES: {
BASIC_PAGE: 'page',
LANDING_PAGE: 'landing_page',
Expand Down

0 comments on commit 447eadc

Please sign in to comment.