Skip to content

Commit ceb5835

Browse files
committed
⚙️ Create check-data script to detect unused SVGs
1 parent bd6ce41 commit ceb5835

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

utils/check-data.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { readdir, unlink } from "fs/promises";
2+
import { join } from "path";
3+
import { readFileSync } from "fs";
4+
5+
// ⚙️ Settings:
6+
const svgDir = join(process.cwd(), "static", "library");
7+
const dataFile = join(process.cwd(), "src", "data", "svgs.ts");
8+
const DELETE_UNUSED = false;
9+
10+
async function checkUnusedSVGs(): Promise<void> {
11+
try {
12+
const files = await readdir(svgDir);
13+
const svgFiles = files.filter((file) => file.endsWith(".svg"));
14+
const dataContent = readFileSync(dataFile, "utf-8");
15+
const unusedFiles: { filename: string; path: string }[] = [];
16+
17+
for (const file of svgFiles) {
18+
if (!dataContent.includes(file)) {
19+
unusedFiles.push({
20+
filename: file,
21+
path: `/library/${file}`,
22+
});
23+
}
24+
}
25+
26+
if (unusedFiles.length === 0) {
27+
console.log("✅ All SVG files are being used in data/svgs.ts");
28+
} else {
29+
console.log(
30+
`⚠️ Found ${unusedFiles.length}/${svgFiles.length} unused SVG file(s) in /static/library:`,
31+
);
32+
console.table(unusedFiles);
33+
34+
if (DELETE_UNUSED) {
35+
console.log("\n🗑️ Deleting unused files...");
36+
let deletedCount = 0;
37+
38+
for (const file of unusedFiles) {
39+
try {
40+
const filePath = join(svgDir, file.filename);
41+
await unlink(filePath);
42+
deletedCount++;
43+
console.log(`- Deleted: ${file.filename}`);
44+
} catch (error) {
45+
console.error(`- Error: failed to delete ${file.filename}:`, error);
46+
}
47+
}
48+
49+
console.log(`\n✅ Successfully deleted ${deletedCount} file(s).`);
50+
} else {
51+
console.log(
52+
"\n💡 To delete these files, set DELETE_UNUSED = true in the script.",
53+
);
54+
throw new Error(
55+
`❌ Error: Found ${unusedFiles.length} unused SVG file(s). Please check the list above.`,
56+
);
57+
}
58+
}
59+
} catch (error) {
60+
console.error("❌ Error:", error);
61+
throw error;
62+
}
63+
}
64+
65+
checkUnusedSVGs();

0 commit comments

Comments
 (0)