-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect-unused-images.js
More file actions
111 lines (91 loc) · 2.73 KB
/
Copy pathdetect-unused-images.js
File metadata and controls
111 lines (91 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* We want to check all images in the project and see if they are used in the project.
* // we need to traverse the src folder
* // we need to get all the images from src/images
* // we need to check .jsx, .js, .scss, .css files
*/
const fs = require("fs");
const path = require("path");
// Configuration
const SRC_DIR = "src";
const IMAGES_DIR = path.join(SRC_DIR, "images");
const VALID_EXTENSIONS = [".js", ".jsx", ".scss", ".css"];
const IMAGE_EXTENSIONS = [".jpg", ".jpeg", ".png", ".gif", ".svg", ".webp"];
// Get all image files from the images directory
function getImageFiles(dir) {
const images = new Set();
function traverse(currentDir) {
const files = fs.readdirSync(currentDir);
for (const file of files) {
const fullPath = path.join(currentDir, file);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
traverse(fullPath);
} else if (IMAGE_EXTENSIONS.includes(path.extname(file).toLowerCase())) {
images.add(fullPath);
}
}
}
traverse(dir);
return images;
}
// Get all code files from src directory
function getCodeFiles(dir, excludeDir) {
const codeFiles = [];
function traverse(currentDir) {
const files = fs.readdirSync(currentDir);
for (const file of files) {
const fullPath = path.join(currentDir, file);
const stat = fs.statSync(fullPath);
if (stat.isDirectory() && fullPath !== excludeDir) {
traverse(fullPath);
} else if (VALID_EXTENSIONS.includes(path.extname(file).toLowerCase())) {
codeFiles.push(fullPath);
}
}
}
traverse(dir);
return codeFiles;
}
// Check if an image is referenced in the code
function isImageReferenced(imagePath, codeFiles) {
const imageName = path.basename(imagePath);
for (const codeFile of codeFiles) {
const content = fs.readFileSync(codeFile, "utf-8");
if (content.includes(imageName)) {
return true;
}
}
return false;
}
// Main function
function main() {
try {
// Get all images and code files
const images = getImageFiles(IMAGES_DIR);
const codeFiles = getCodeFiles(SRC_DIR, IMAGES_DIR);
// Find unused images
const unusedImages = new Set();
for (const imagePath of images) {
if (!isImageReferenced(imagePath, codeFiles)) {
unusedImages.add(imagePath);
}
}
// Report results
if (unusedImages.size > 0) {
console.log("\nUnused images found:");
for (const imagePath of unusedImages) {
console.log(imagePath);
}
process.exit(1);
} else {
console.log("All images are being used in the codebase.");
process.exit(0);
}
} catch (error) {
console.error("Error:", error.message);
process.exit(1);
}
}
// Run the script
main();